Skip to content

Commit

Permalink
🐛 FIX: Redirect of filenames containing dots (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Apr 16, 2021
1 parent 81f08b3 commit 09516f0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
32 changes: 22 additions & 10 deletions sphinxext/rediraffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/roots/ext/test-dot_in_filename/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extensions = ["sphinxext.rediraffe"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

rediraffe_redirects = "redirects.txt"
1 change: 1 addition & 0 deletions tests/roots/ext/test-dot_in_filename/docs/a.b.c.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f1
1 change: 1 addition & 0 deletions tests/roots/ext/test-dot_in_filename/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index File
1 change: 1 addition & 0 deletions tests/roots/ext/test-dot_in_filename/redirects.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs/x.y.z.rst docs/a.b.c.rst
12 changes: 12 additions & 0 deletions tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 09516f0

Please sign in to comment.