16
16
17
17
if TYPE_CHECKING :
18
18
from sphinx .application import Sphinx
19
+ from sphinx .config import Config
19
20
20
21
# Monkeypatch sphinx.environment.default_settings as Sphinx doesn't allow custom settings or Readers
21
22
# 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():
37
38
pass # No-op callable for the type checker
38
39
39
40
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
+
41
57
if app .builder .name == "dirhtml" :
42
58
config .pep_url = f"../{ config .pep_stem } "
43
59
app .env .settings ["pep_file_url_template" ] = "../pep-%04d"
44
60
45
61
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
+
46
72
def setup (app : Sphinx ) -> dict [str , bool ]:
47
73
"""Initialize Sphinx extension."""
48
74
@@ -51,8 +77,9 @@ def setup(app: Sphinx) -> dict[str, bool]:
51
77
app .add_role ("pep" , pep_role .PEPRole (), override = True ) # Transform PEP references to links
52
78
app .set_translator ("html" , pep_html_translator .PEPTranslator ) # Docutils Node Visitor overrides (html builder)
53
79
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
55
81
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
56
83
57
84
# Mathematics rendering
58
85
inline_maths = HTMLTranslator .visit_math , _depart_maths
0 commit comments