Skip to content

Commit 961b327

Browse files
authored
🐛 FIX: File extensions in ToC (#44)
Ensure files are still matched, if they are provided with file extensions.
1 parent e54db50 commit 961b327

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

sphinx_external_toc/events.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ def parse_toc_to_env(app: Sphinx, config: Config) -> None:
7676
config.external_site_map = site_map
7777

7878
# Update the master_doc to the root doc of the site map
79-
if config["master_doc"] != site_map.root.docname:
80-
logger.info("[etoc] Changing master_doc to '%s'", site_map.root.docname)
81-
config["master_doc"] = site_map.root.docname
79+
root_doc = remove_suffix(site_map.root.docname, config.source_suffix)
80+
if config["master_doc"] != root_doc:
81+
logger.info("[etoc] Changing master_doc to '%s'", root_doc)
82+
config["master_doc"] = root_doc
8283

8384
if config["external_toc_exclude_missing"]:
8485
# add files not specified in ToC file to exclude list
@@ -181,6 +182,14 @@ def insert_toctrees(app: Sphinx, doctree: nodes.document) -> None:
181182
site_map: SiteMap = app.env.external_site_map
182183
doc_item: Optional[Document] = site_map.get(app.env.docname)
183184

185+
# check for matches with suffix
186+
# TODO check in sitemap, that we do not have multiple docs of the same name
187+
# (strip extensions on creation)
188+
for suffix in app.config.source_suffix:
189+
if doc_item is not None:
190+
break
191+
doc_item = site_map.get(app.env.docname + suffix)
192+
184193
if doc_item is None or not doc_item.subtrees:
185194
if toc_placeholders:
186195
create_warning(

tests/test_sphinx.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,36 @@ def test_absolute_path(tmp_path: Path, sphinx_build_factory):
112112
# run sphinx
113113
builder = sphinx_build_factory(src_dir)
114114
builder.build()
115+
116+
117+
def test_file_extensions(tmp_path: Path, sphinx_build_factory):
118+
"""Test for tocs containing docnames with file extensions."""
119+
src_dir = tmp_path / "srcdir"
120+
src_dir.mkdir(exist_ok=True)
121+
# write documents
122+
src_dir.joinpath("intro.rst").write_text("Head\n====\n", encoding="utf8")
123+
src_dir.joinpath("markdown.rst").write_text("Head\n====\n", encoding="utf8")
124+
src_dir.joinpath("notebooks.rst").write_text("Head\n====\n", encoding="utf8")
125+
# write toc
126+
toc_path = tmp_path / "toc.yml"
127+
toc_path.write_text(
128+
"""
129+
format: jb-book
130+
root: intro.rst
131+
chapters:
132+
- file: markdown.rst
133+
sections:
134+
- file: notebooks.rst
135+
""",
136+
encoding="utf8",
137+
)
138+
# write conf.py
139+
content = f"""
140+
extensions = ["sphinx_external_toc"]
141+
external_toc_path = {Path(os.path.abspath(toc_path)).as_posix()!r}
142+
143+
"""
144+
src_dir.joinpath("conf.py").write_text(content, encoding="utf8")
145+
# run sphinx
146+
builder = sphinx_build_factory(src_dir)
147+
builder.build()

0 commit comments

Comments
 (0)