55from typing import TYPE_CHECKING
66
77from docutils .writers .html5_polyglot import HTMLTranslator
8+ from jinja2 .environment import Template
89from sphinx import environment
910
1011from pep_sphinx_extensions .pep_processor .html import pep_html_builder
@@ -22,17 +23,32 @@ def _depart_maths():
2223 pass # No-op callable for the type checker
2324
2425
25- def _update_config_for_builder (app : Sphinx ):
26+ def _update_config_for_builder (app : Sphinx ) -> None :
2627 app .env .document_ids = {} # For PEPReferenceRoleTitleText
2728 if app .builder .name == "dirhtml" :
2829 app .env .settings ["pep_url" ] = "../pep-{:0>4}"
2930
31+ # internal_builder exists if Sphinx is run by build.py
32+ if "internal_builder" not in app .tags :
33+ app .connect ("build-finished" , _post_build ) # Post-build tasks
34+
35+
36+ def _post_build (app : Sphinx , exception : Exception | None ) -> None :
37+ from pathlib import Path
38+
39+ from build import create_index_file
40+
41+ if exception is not None :
42+ return
43+ create_index_file (Path (app .outdir ), app .builder .name )
44+
3045
3146def setup (app : Sphinx ) -> dict [str , bool ]:
3247 """Initialize Sphinx extension."""
3348
3449 environment .default_settings ["pep_url" ] = "pep-{:0>4}.html"
3550 environment .default_settings ["halt_level" ] = 2 # Fail on Docutils warning
51+ Template .render ._patched = True # don't modify rendered HTML
3652
3753 # Register plugin logic
3854 app .add_builder (pep_html_builder .FileBuilder , override = True )
0 commit comments