1616
1717if TYPE_CHECKING :
1818 from sphinx .application import Sphinx
19+ from sphinx .config import Config
1920
2021# Monkeypatch sphinx.environment.default_settings as Sphinx doesn't allow custom settings or Readers
2122# These settings should go in docutils.conf, but are overridden here for now so as not to affect
@@ -37,12 +38,37 @@ def _depart_maths():
3738 pass # No-op callable for the type checker
3839
3940
40- def _update_config_for_builder (app : Sphinx ):
41+ def _check_external_builder (app : Sphinx , conf : Config ) -> None :
42+ if "internal_builder" not in app .tags : # internal_builder exists if Sphinx is run by build.py
43+ conf .html_context = {"external_builder" : True }
44+ else :
45+ conf .html_context = {"external_builder" : False }
46+ app .tags .remove ("internal_builder" )
47+
48+ if conf .html_context ["external_builder" ]:
49+ app .connect ("build-finished" , _post_build ) # Post-build tasks
50+
51+
52+ def _update_config_for_builder (app : Sphinx ) -> None :
53+ if app .config .html_context ["external_builder" ]:
54+ app .builder .copysource = False # Prevent unneeded source copying - we link direct to GitHub
55+ app .builder .search = False # Disable search
56+
4157 if app .builder .name == "dirhtml" :
4258 config .pep_url = f"../{ config .pep_stem } "
4359 app .env .settings ["pep_file_url_template" ] = "../pep-%04d"
4460
4561
62+ def _post_build (app : Sphinx , exception : Exception | None ) -> None :
63+ from pathlib import Path
64+
65+ from build import create_index_file
66+
67+ if exception is not None :
68+ return
69+ create_index_file (Path (app .outdir ), app .builder .name )
70+
71+
4672def setup (app : Sphinx ) -> dict [str , bool ]:
4773 """Initialize Sphinx extension."""
4874
@@ -51,8 +77,9 @@ def setup(app: Sphinx) -> dict[str, bool]:
5177 app .add_role ("pep" , pep_role .PEPRole (), override = True ) # Transform PEP references to links
5278 app .set_translator ("html" , pep_html_translator .PEPTranslator ) # Docutils Node Visitor overrides (html builder)
5379 app .set_translator ("dirhtml" , pep_html_translator .PEPTranslator ) # Docutils Node Visitor overrides (dirhtml builder)
54- app .connect ("env-before-read-docs " , create_pep_zero ) # PEP 0 hook
80+ app .connect ("config-inited " , _check_external_builder ) # Check if build is being orchestrated by an external process
5581 app .connect ("builder-inited" , _update_config_for_builder ) # Update configuration values for builder used
82+ app .connect ("env-before-read-docs" , create_pep_zero ) # PEP 0 hook
5683
5784 # Mathematics rendering
5885 inline_maths = HTMLTranslator .visit_math , _depart_maths
0 commit comments