Skip to content

Commit

Permalink
fix: Config overwrite and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Jul 6, 2024
1 parent 4ba8d6c commit 37d93ad
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions src/sphinxnotes/fasthtml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
sphinxnotes.fasthtml
~~~~~~~~~~~~~~~~~~~~
Sphinx builder specialized for fast incremental HTML build
Sphinx builder specialized for fast incremental HTML build
:copyright: Copyright 2024 Shengyu Zhang
:license: BSD, see LICENSE for details.
TODO:
- [ ] skip 'checking consistency'
- [ ] why always [config changed ('gettext_auto_build')]?
- [ ] config-able.
"""
Expand All @@ -37,7 +36,7 @@ def __init__(self, app: Sphinx, env: BuildEnvironment) -> None:
# to ensure that the behavior with StandaloneHTMLBuilderi as same
# as possible.
#
# Otherwise, different builde name causes troubles:
# Otherwise, different builde name causes troubles:
#
# - Builder.tags will be different (see Builder.init()), which leads
# to BuildInfo changes, finally leads Builder.get_outdated_docs()
Expand All @@ -55,26 +54,54 @@ def gen_pages_from_extensions(self) -> None:
pass # skip gen


def _overwrite_config(self) -> None:
"""
Overwrite sphinx.config.Config to skip some operations that slow down
the build.
Should be called before builder.Builder.read().
"""
self._old_config = {}
def overwrite(name, val, optional=False):
if optional and not hasattr(self.config, name):
return
self._old_config[name] = getattr(self.config, name)
setattr(self.config, name, val)

overwrite('html_domain_indices', False)
# Do not build mo files.
overwrite('gettext_auto_build', False)
# Prevent intersphinx cache expiration.
overwrite('intersphinx_cache_limit', -1, optional=True)


def _restore_config(self) -> None:
"""
Restore sphinx.config.Config to keep sphinx.application.ENV_PICKLE_FILENAME
unchanged.
Must be called before pickle file is dumped to disk.
"""
for name, val in self._old_config.items():
setattr(self.config, name, val)
self._old_config = {}


def _on_builder_inited(app: Sphinx):
if not isinstance(app.builder, FastHTMLBuilder):
return

# Disable general index.
app.config.html_use_index = False # type: ignore
app.builder.use_index = False
# Disable domain-specific indices.
app.config.html_domain_indices = False # type: ignore
app.builder._overwrite_config()

# Don't use index.
app.builder.use_index = False
# Disable search.
app.builder.search = False

# Do not update toctree.
app.env.glob_toctrees = set()
app.env.reread_always = set() # marked by env.note_reread()

# Do not build mo files.
app.config.gettext_auto_build = False # type: ignore


def _on_env_get_outdated(app: Sphinx, env: BuildEnvironment, added: set[str],
changed: set[str], removed: set[str]) -> list[str]:
Expand All @@ -100,8 +127,17 @@ def clear_and_update(dst, src):

return []


def _on_env_updated(app: Sphinx, env: BuildEnvironment):
if not isinstance(app.builder, FastHTMLBuilder):
return []

app.builder._restore_config()


def setup(app: Sphinx):
app.connect('builder-inited', _on_builder_inited, priority=100)
app.connect('env-get-outdated', _on_env_get_outdated)
app.connect('env-updated', _on_env_updated)

app.add_builder(FastHTMLBuilder)

0 comments on commit 37d93ad

Please sign in to comment.