Skip to content

Commit 6467012

Browse files
authored
Merge pull request #814 from executablebooks/agoose77/fix-update-config
fix: use `config-inited` event to register config
2 parents 956553a + 41aebcf commit 6467012

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

Diff for: pyproject.toml

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ additional-compiled-static-assets = [
1313
testpaths = [
1414
"tests"
1515
]
16+
filterwarnings = [
17+
"error",
18+
'ignore:Jupyter is migrating its paths to use standard platformdirs:DeprecationWarning',
19+
'ignore:The frontend\.OptionParser class will be replaced:DeprecationWarning',
20+
'ignore:The frontend\.Option class will be removed:DeprecationWarning',
21+
'ignore:nodes\.Node\.traverse\(\) is obsoleted by Node\.findall\(\):PendingDeprecationWarning',
22+
# jupyter-client throws this
23+
'ignore:datetime\.datetime\.utcfromtimestamp\(\) is deprecated:DeprecationWarning',
24+
'ignore:datetime\.datetime\.utcnow\(\) is deprecated:DeprecationWarning',
25+
# Sphinx triggers this
26+
'''ignore:'imghdr' is deprecated and slated for removal in Python 3\.13:DeprecationWarning''',
27+
]
1628

1729
[project]
1830
name = "sphinx-book-theme"

Diff for: src/sphinx_book_theme/__init__.py

+12-30
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from pathlib import Path
55
from functools import lru_cache
66

7-
from docutils.parsers.rst.directives.body import Sidebar
87
from docutils import nodes as docutil_nodes
98
from sphinx.application import Sphinx
109
from sphinx.locale import get_translation
1110
from sphinx.util import logging
1211
from pydata_sphinx_theme.utils import get_theme_options_dict
1312

13+
from .directives import Margin
1414
from .nodes import SideNoteNode
1515
from .header_buttons import (
1616
prep_header_buttons,
@@ -179,32 +179,9 @@ def check_deprecation_keys(app):
179179
)
180180

181181

182-
class Margin(Sidebar):
183-
"""Goes in the margin to the right of the page."""
184-
185-
optional_arguments = 1
186-
required_arguments = 0
187-
188-
def run(self):
189-
"""Run the directive."""
190-
if not self.arguments:
191-
self.arguments = [""]
192-
nodes = super().run()
193-
nodes[0].attributes["classes"].append("margin")
194-
195-
# Remove the "title" node if it is empty
196-
if not self.arguments:
197-
nodes[0].children.pop(0)
198-
return nodes
199-
200-
201182
def update_general_config(app):
202183
theme_dir = get_html_theme_path()
203-
# Update templates for sidebar. Needed for jupyter-book builds as jb
204-
# uses an instance of Sphinx class from sphinx.application to build the app.
205-
# The __init__ function of which calls self.config.init_values() just
206-
# before emitting `config-inited` event. The init_values function overwrites
207-
# templates_path variable.
184+
208185
app.config.templates_path.append(os.path.join(theme_dir, "components"))
209186

210187

@@ -245,11 +222,20 @@ def setup(app: Sphinx):
245222
app.connect("builder-inited", check_deprecation_keys)
246223
app.connect("builder-inited", update_sourcename)
247224
app.connect("builder-inited", update_context_with_repository_info)
248-
app.connect("builder-inited", update_general_config)
249225
app.connect("html-page-context", add_metadata_to_page)
250226
app.connect("html-page-context", hash_html_assets)
251227
app.connect("html-page-context", update_templates)
252228

229+
# This extension has both theme-like and extension-like features.
230+
# Themes are initialised immediately before use, thus we cannot
231+
# rely on an event to set the config - the theme config must be
232+
# set in setup(app):
233+
update_general_config(app)
234+
# Meanwhile, extensions are initialised _first_, and any config
235+
# values set during setup() will be overwritten. We must therefore
236+
# register the `config-inited` event to set these config options
237+
app.connect("config-inited", update_general_config)
238+
253239
# Nodes
254240
SideNoteNode.add_node(app)
255241

@@ -266,10 +252,6 @@ def setup(app: Sphinx):
266252
# Post-transforms
267253
app.add_post_transform(HandleFootnoteTransform)
268254

269-
# Update templates for sidebar, for builds where config-inited is not called
270-
# (does not work in case of jupyter-book)
271-
app.config.templates_path.append(os.path.join(theme_dir, "components"))
272-
273255
return {
274256
"parallel_read_safe": True,
275257
"parallel_write_safe": True,

Diff for: src/sphinx_book_theme/directives.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from docutils.parsers.rst.directives.body import Sidebar
2+
3+
4+
class Margin(Sidebar):
5+
"""Goes in the margin to the right of the page."""
6+
7+
optional_arguments = 1
8+
required_arguments = 0
9+
10+
def run(self):
11+
"""Run the directive."""
12+
if not self.arguments:
13+
self.arguments = [""]
14+
nodes = super().run()
15+
nodes[0].attributes["classes"].append("margin")
16+
17+
# Remove the "title" node if it is empty
18+
if not self.arguments:
19+
nodes[0].children.pop(0)
20+
return nodes

Diff for: tests/test_build.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
from pathlib import Path
33
from shutil import copytree, rmtree
4-
from subprocess import check_call
54
from importlib.metadata import version
65
from packaging.version import parse
76

@@ -28,9 +27,10 @@ def __init__(self, app: SphinxTestApp, src: Path):
2827
f".sphinx{sphinx.version_info[0]}" # software version tracking for fixtures
2928
)
3029

31-
def build(self, assert_pass=True):
30+
def build(self, assert_pass=True, assert_no_warnings=True):
3231
self.app.build()
33-
assert self.warnings == "", self.status
32+
if assert_no_warnings:
33+
assert self.warnings == "", self.status
3434
return self
3535

3636
@property
@@ -62,14 +62,12 @@ def _func(src_folder, **kwargs):
6262
yield _func
6363

6464

65-
def test_parallel_build():
66-
# We cannot use the sphinx_build_factory because SpinxTestApp does
67-
# not have a way to pass parallel=2 to the Sphinx constructor
68-
# https://github.com/sphinx-doc/sphinx/blob/d8c006f1c0e612d0dc595ae463b8e4c3ebee5ca4/sphinx/testing/util.py#L101
69-
check_call(
70-
f"sphinx-build -j 2 -W -b html {path_tests}/sites/parallel-build build",
71-
shell=True,
72-
)
65+
def test_parallel_build(sphinx_build_factory):
66+
sphinx_build = sphinx_build_factory("parallel-build", parallel=2) # type: SphinxBuild
67+
sphinx_build.build(
68+
assert_pass=True, assert_no_warnings=False
69+
) # TODO: filter these warnings
70+
assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")
7371

7472

7573
def test_build_book(sphinx_build_factory, file_regression):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div class="bd-sidebar-secondary bd-toc">
2+
</div>

0 commit comments

Comments
 (0)