From d403115cf1f84b896c38d0acf48c0d6f3c6ab4ca Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Tue, 6 Oct 2020 17:12:49 -0700 Subject: [PATCH] fixing dirhtml builds --- sphinx_book_theme/__init__.py | 12 ++++++++---- tests/test_build.py | 8 ++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/sphinx_book_theme/__init__.py b/sphinx_book_theme/__init__.py index c4ccdc32..d5033022 100644 --- a/sphinx_book_theme/__init__.py +++ b/sphinx_book_theme/__init__.py @@ -86,12 +86,16 @@ def find_url_relative_to_root(pagename, relative_page, path_docs_source): # Convert everything to paths for use later path_rel = Path(relative_page).with_suffix("") - path_parent = Path(pagename) # pagename is relative to docs root + if relative_page.endswith(".html"): + # HTML file Sphinx builder + path_parent = Path(pagename).parent # pagename is .html relative to docs root + else: + # DirHTML Sphinx builder. + path_parent = Path(pagename) # pagename is the parent folder if dirhtml builder + source_dir = Path(path_docs_source) # This should be the path to `relative_page`, relative to `pagename` - path_rel_from_page_dir = source_dir.joinpath( - path_parent.parent.joinpath(path_rel.parent) - ) + path_rel_from_page_dir = source_dir.joinpath(path_parent.joinpath(path_rel.parent)) path_from_page_dir = Path(os.path.abspath(path_rel_from_page_dir)) page_rel_root = path_from_page_dir.relative_to(source_dir).joinpath(path_rel.name) return page_rel_root diff --git a/tests/test_build.py b/tests/test_build.py index aada4c89..3ad775d7 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -270,3 +270,11 @@ def test_missing_title(sphinx_build_factory): """Test building with a book w/ no title on the master page.""" with pytest.raises(ThemeError, match="Landing page missing a title: index"): sphinx_build_factory("notitle").build() + + +def test_docs_dirhtml(sphinx_build_factory): + """Test that builds with dirhtml pass without error.""" + sphinx_build = sphinx_build_factory("base", buildername="dirhtml").build( + assert_pass=True + ) + assert (sphinx_build.outdir / "index.html").exists(), sphinx_build.outdir.glob("*")