From 09516f0df9fb63559de2c0828e216e912c01a443 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Fri, 16 Apr 2021 13:23:53 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Redirect=20of=20filenames?= =?UTF-8?q?=20containing=20dots=20(#45)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sphinxext/rediraffe.py | 32 +++++++++++++------ tests/roots/ext/test-dot_in_filename/conf.py | 8 +++++ .../ext/test-dot_in_filename/docs/a.b.c.rst | 1 + .../roots/ext/test-dot_in_filename/index.rst | 1 + .../ext/test-dot_in_filename/redirects.txt | 1 + tests/test_ext.py | 12 +++++++ 6 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 tests/roots/ext/test-dot_in_filename/conf.py create mode 100644 tests/roots/ext/test-dot_in_filename/docs/a.b.c.rst create mode 100644 tests/roots/ext/test-dot_in_filename/index.rst create mode 100644 tests/roots/ext/test-dot_in_filename/redirects.txt diff --git a/sphinxext/rediraffe.py b/sphinxext/rediraffe.py index 18a4876..708f5d7 100644 --- a/sphinxext/rediraffe.py +++ b/sphinxext/rediraffe.py @@ -121,6 +121,14 @@ def create_simple_redirects(graph_edges: dict) -> dict: return redirects +def remove_suffix(docname: str, suffixes: List[str]) -> str: + """Remove any known suffixes for a file path.""" + for suffix in suffixes: + if docname.endswith(suffix): + return docname[: -len(suffix)] + return docname + + def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None: """ Build amd write redirects @@ -206,18 +214,22 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None: src_redirect_from = Path(PureWindowsPath(src_redirect_from)) src_redirect_to = Path(PureWindowsPath(src_redirect_to)) - # relative paths from source dir (without ext) - redirect_from = src_redirect_from.with_suffix("") - redirect_to = src_redirect_to.with_suffix("") + # remove extensions + redirect_from_name = remove_suffix( + src_redirect_from.name, app.config.source_suffix + ) + redirect_to_name = remove_suffix(src_redirect_to.name, app.config.source_suffix) + + redirect_from = src_redirect_from.parent / f"{redirect_from_name}.html" + redirect_to = src_redirect_to.parent / f"{redirect_to_name}.html" if type(app.builder) == DirectoryHTMLBuilder: - if redirect_from.name != "index": - redirect_from = redirect_from / "index" - if redirect_to.name != "index": - redirect_to = redirect_to / "index" - - redirect_from = redirect_from.with_suffix(".html") - redirect_to = redirect_to.with_suffix(".html") + if redirect_from_name != "index": + redirect_from = ( + src_redirect_from.parent / redirect_from_name / "index.html" + ) + if redirect_to_name != "index": + redirect_to = src_redirect_to.parent / redirect_to_name / "index.html" # absolute paths into the build dir build_redirect_from = Path(app.outdir) / redirect_from diff --git a/tests/roots/ext/test-dot_in_filename/conf.py b/tests/roots/ext/test-dot_in_filename/conf.py new file mode 100644 index 0000000..8136c0e --- /dev/null +++ b/tests/roots/ext/test-dot_in_filename/conf.py @@ -0,0 +1,8 @@ +extensions = ["sphinxext.rediraffe"] + +master_doc = "index" +exclude_patterns = ["_build"] + +html_theme = "basic" + +rediraffe_redirects = "redirects.txt" diff --git a/tests/roots/ext/test-dot_in_filename/docs/a.b.c.rst b/tests/roots/ext/test-dot_in_filename/docs/a.b.c.rst new file mode 100644 index 0000000..9dd7ac9 --- /dev/null +++ b/tests/roots/ext/test-dot_in_filename/docs/a.b.c.rst @@ -0,0 +1 @@ +f1 \ No newline at end of file diff --git a/tests/roots/ext/test-dot_in_filename/index.rst b/tests/roots/ext/test-dot_in_filename/index.rst new file mode 100644 index 0000000..e5616f3 --- /dev/null +++ b/tests/roots/ext/test-dot_in_filename/index.rst @@ -0,0 +1 @@ +Index File \ No newline at end of file diff --git a/tests/roots/ext/test-dot_in_filename/redirects.txt b/tests/roots/ext/test-dot_in_filename/redirects.txt new file mode 100644 index 0000000..1675f59 --- /dev/null +++ b/tests/roots/ext/test-dot_in_filename/redirects.txt @@ -0,0 +1 @@ +docs/x.y.z.rst docs/a.b.c.rst diff --git a/tests/test_ext.py b/tests/test_ext.py index 08909dc..3aaa1ee 100644 --- a/tests/test_ext.py +++ b/tests/test_ext.py @@ -74,6 +74,12 @@ def test_backslashes(self, app: Sphinx, ensure_redirect): ensure_redirect("docs/folder2/toindex.html", "index.html") ensure_redirect("totoindex.html", "index.html") + @pytest.mark.sphinx("html", testroot="dot_in_filename") + def test_dot_in_filename(self, app: Sphinx, ensure_redirect): + app.build() + assert app.statuscode == 0 + ensure_redirect("docs/x.y.z.html", "docs/a.b.c.html") + @pytest.mark.sphinx("html", testroot="mixed_slashes") def test_mixed_slashes(self, app: Sphinx, ensure_redirect): app.build() @@ -326,6 +332,12 @@ def test_backslashes(self, app: Sphinx, ensure_redirect): ensure_redirect("docs/folder2/toindex/index.html", "index.html") ensure_redirect("totoindex/index.html", "index.html") + @pytest.mark.sphinx("dirhtml", testroot="dot_in_filename") + def test_dot_in_filename(self, app: Sphinx, ensure_redirect): + app.build() + assert app.statuscode == 0 + ensure_redirect("docs/x.y.z/index.html", "docs/a.b.c/index.html") + @pytest.mark.sphinx("dirhtml", testroot="mixed_slashes") def test_mixed_slashes(self, app: Sphinx, ensure_redirect): app.build()