Skip to content

Commit 0af3f79

Browse files
committed
🐛 FIX: external_toc_path with absolute path (#25)
1 parent 547a6ac commit 0af3f79

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ external_toc_path = "_toc.yml" # optional, default: _toc.yml
2727
external_toc_exclude_missing = False # optional, default: False
2828
```
2929

30-
Note the `external_toc_path` is always read as a Unix path.
30+
Note the `external_toc_path` is always read as a Unix path, and can either be specified relative to the source directory (recommended) or as an absolute path.
3131

3232
### Basic Structure
3333

sphinx_external_toc/events.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,16 @@ def parse_toc_to_env(app: Sphinx, config: Config) -> None:
5959
6060
Also, change the ``master_doc`` and add to ``exclude_patterns`` if necessary.
6161
"""
62-
path = Path(app.srcdir) / PurePosixPath(app.config["external_toc_path"])
63-
if not (path.exists() and path.is_file()):
64-
raise ExtensionError(
65-
f"[etoc] `external_toc_path` is not an existing file: {path}"
66-
)
62+
# TODO this seems to work in the tests, but I still want to double check
63+
external_toc_path = PurePosixPath(app.config["external_toc_path"])
64+
if not external_toc_path.is_absolute():
65+
path = Path(app.srcdir) / str(external_toc_path)
66+
else:
67+
path = Path(str(external_toc_path))
68+
if not path.exists():
69+
raise ExtensionError(f"[etoc] `external_toc_path` does not exist: {path}")
70+
if not path.is_file():
71+
raise ExtensionError(f"[etoc] `external_toc_path` is not a file: {path}")
6772
try:
6873
site_map = parse_toc_yaml(path)
6974
except Exception as exc:

tests/test_sphinx.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,21 @@ def test_warning(path: Path, tmp_path: Path, sphinx_build_factory):
9494
builder = sphinx_build_factory(src_dir)
9595
builder.build(assert_pass=False)
9696
assert sitemap.meta["expected_warning"] in builder.warnings
97+
98+
99+
def test_absolute_path(tmp_path: Path, sphinx_build_factory):
100+
"""Test if `external_toc_path` is supplied as an absolute path."""
101+
src_dir = tmp_path / "srcdir"
102+
# write document files
103+
toc_path = Path(__file__).parent.joinpath("_toc_files", "basic.yml")
104+
create_site_from_toc(toc_path, root_path=src_dir, toc_name=None)
105+
# write conf.py
106+
content = f"""
107+
extensions = ["sphinx_external_toc"]
108+
external_toc_path = {Path(os.path.abspath(toc_path)).as_posix()!r}
109+
110+
"""
111+
src_dir.joinpath("conf.py").write_text(content, encoding="utf8")
112+
# run sphinx
113+
builder = sphinx_build_factory(src_dir)
114+
builder.build()

0 commit comments

Comments
 (0)