This document provides an overview of the PEP rendering system, as a companion to PEP 676.
Configuration is stored in three files:
The configuration:
build.py manages the rendering process. Usage is covered in Building PEPs Locally.
The Sphinx extension and theme are contained in the pep_sphinx_extensions directory. The following is a brief overview of the stages of the PEP rendering process, and how the extension functions at each point.
The extension registers several objects:
The extension also patches default behaviour:
After the Sphinx builder object is created and initialised, we ensure the configuration is correct for the builder chosen.
Currently this involves updating the relative link template. See _update_config_for_builder in pep_sphinx_extensions/__init__.py.
The create_pep_zero hook is called. See 5. PEP 0.
Parsing the document is handled by PEPParser (pep_sphinx_extensions.pep_processor.parsing.pep_parser.PEPParser), a lightweight wrapper over sphinx.parsers.RSTParser.
PEPParser reads the document with leading RFC 2822 headers and registers the transforms we want to apply. These are:
Transforms are then applied in priority order.
This overrides the built-in :pep: role to return the correct URL.
PEPs start with a set of RFC 2822 headers, per PEP 1. This transform validates that the required headers are present and of the correct data type, and removes headers not for display. It must run before the PEPTitle transform.
We generate the title node from the parsed title in the PEP headers, and make all nodes in the document children of the new title node. This transform must also handle parsing reStructuredText markup within PEP titles, such as PEP 604.
The automatic table of contents (TOC) is inserted in this transform in a two-part process.
First, the transform inserts a placeholder for the TOC and a horizontal rule after the document title and PEP headers. A callback transform then recursively walks the document to create the TOC, starting from after the placeholder node. Whilst walking the document, all reference nodes in the titles are removed, and titles are given a self-link.
This first builds a map of file modification times from a single git call, as a speed-up. This will return incorrect results on a shallow checkout of the repository, as is the default on continuous integration systems.
We then attempt to remove any empty references sections, and append metadata in the footer (source link and last modified timestamp).
pep_html_builder.FileBuilder.prepare_writing initialises the bare minimum of the Docutils writer and the settings for writing documents. This provides a significant speed-up over the base Sphinx implementation, as most of the data automatically initialised was unused.
PEPTranslator overrides paragraph and reference logic to replicate processing from the previous docutils.writers.pep-based system. Paragraphs are made compact where possible by omitting <p> tags, and footnote references are be enclosed in square brackets.
Finally in pep_html_builder, we gather all the parts to be passed to the Jinja template. This is also where we create the sidebar table of contents.
The HTML files are then written out to the build directory.
The theme is comprised of the HTML template in pep_sphinx_extensions/pep_theme/templates/page.html and the stylesheets in pep_sphinx_extensions/pep_theme/static.
The template is entirely self-contained, not relying on any default behaviour from Sphinx. It specifies the CSS files to include, the favicon, and basic semantic information for the document structure.
The styles are defined in two parts:
The generation of the index, PEP 0, happens in three phases. The reStructuredText source file is generated, it is then added to Sphinx, and finally the data is post processed.
pep-0000.rst is created during a callback, before documents are loaded by Sphinx.
We first parse the individual PEP files to get the RFC 2822 header, and then parse and validate that metadata.
After collecting and validating all the PEP data, the index itself is created in three steps:
We then add the newly created PEP 0 file to two Sphinx variables so that it will be processed as a normal source document.
The PEPHeaders transform schedules the PEP 0 post-processing code. This serves two functions: masking email addresses and linking numeric PEP references to the actual documents.
The RSS feed is created by extracting the header metadata and abstract from the ten most recent PEPs.