Skip to content

Commit 3890ab5

Browse files
authored
refactor: Move writing extra files to an earlier stage in the build
This step does not depend on anything that happens the HTML-rendering steps, only on the Markdown-rendering steps, so it's fine to move earlier. An example of why it's bad that the files do not exist during the HTML-rendering steps can be seen in a hypothetical commit oprypin/mkdocs@a677dd7 - basing the HTML content on the content of those files fails. PR #275: #275
1 parent 3838ba0 commit 3890ab5

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

src/mkdocstrings/plugin.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ def inventory_enabled(self) -> bool:
197197
inventory_enabled = any(handler.enable_inventory for handler in self.handlers.seen_handlers)
198198
return inventory_enabled
199199

200+
def on_env(self, env, config: Config, **kwargs):
201+
"""Write mkdocstrings' extra files into the site dir.
202+
203+
This is done in `on_env` because it's the only event that happens right after all Markdown rendering.
204+
"""
205+
if self._handlers:
206+
css_content = "\n".join(handler.renderer.extra_css for handler in self.handlers.seen_handlers)
207+
write_file(css_content.encode("utf-8"), os.path.join(config["site_dir"], self.css_filename))
208+
209+
if self.inventory_enabled:
210+
log.debug("Creating inventory file objects.inv")
211+
inv_contents = self.handlers.inventory.format_sphinx()
212+
write_file(inv_contents, os.path.join(config["site_dir"], "objects.inv"))
213+
200214
def on_post_build(self, config: Config, **kwargs) -> None: # noqa: W0613,R0201 (unused arguments, cannot be static)
201215
"""Teardown the handlers.
202216
@@ -213,15 +227,7 @@ def on_post_build(self, config: Config, **kwargs) -> None: # noqa: W0613,R0201
213227
config: The MkDocs config object.
214228
kwargs: Additional arguments passed by MkDocs.
215229
"""
216-
if self.handlers:
217-
css_content = "\n".join(handler.renderer.extra_css for handler in self.handlers.seen_handlers)
218-
write_file(css_content.encode("utf-8"), os.path.join(config["site_dir"], self.css_filename))
219-
220-
if self.inventory_enabled:
221-
log.debug("Creating inventory file objects.inv")
222-
inv_contents = self.handlers.inventory.format_sphinx()
223-
write_file(inv_contents, os.path.join(config["site_dir"], "objects.inv"))
224-
230+
if self._handlers:
225231
log.debug("Tearing handlers down")
226232
self.handlers.teardown()
227233

0 commit comments

Comments
 (0)