Skip to content

Commit 09516f0

Browse files
authored
🐛 FIX: Redirect of filenames containing dots (#45)
1 parent 81f08b3 commit 09516f0

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

sphinxext/rediraffe.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ def create_simple_redirects(graph_edges: dict) -> dict:
121121
return redirects
122122

123123

124+
def remove_suffix(docname: str, suffixes: List[str]) -> str:
125+
"""Remove any known suffixes for a file path."""
126+
for suffix in suffixes:
127+
if docname.endswith(suffix):
128+
return docname[: -len(suffix)]
129+
return docname
130+
131+
124132
def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
125133
"""
126134
Build amd write redirects
@@ -206,18 +214,22 @@ def build_redirects(app: Sphinx, exception: Union[Exception, None]) -> None:
206214
src_redirect_from = Path(PureWindowsPath(src_redirect_from))
207215
src_redirect_to = Path(PureWindowsPath(src_redirect_to))
208216

209-
# relative paths from source dir (without ext)
210-
redirect_from = src_redirect_from.with_suffix("")
211-
redirect_to = src_redirect_to.with_suffix("")
217+
# remove extensions
218+
redirect_from_name = remove_suffix(
219+
src_redirect_from.name, app.config.source_suffix
220+
)
221+
redirect_to_name = remove_suffix(src_redirect_to.name, app.config.source_suffix)
222+
223+
redirect_from = src_redirect_from.parent / f"{redirect_from_name}.html"
224+
redirect_to = src_redirect_to.parent / f"{redirect_to_name}.html"
212225

213226
if type(app.builder) == DirectoryHTMLBuilder:
214-
if redirect_from.name != "index":
215-
redirect_from = redirect_from / "index"
216-
if redirect_to.name != "index":
217-
redirect_to = redirect_to / "index"
218-
219-
redirect_from = redirect_from.with_suffix(".html")
220-
redirect_to = redirect_to.with_suffix(".html")
227+
if redirect_from_name != "index":
228+
redirect_from = (
229+
src_redirect_from.parent / redirect_from_name / "index.html"
230+
)
231+
if redirect_to_name != "index":
232+
redirect_to = src_redirect_to.parent / redirect_to_name / "index.html"
221233

222234
# absolute paths into the build dir
223235
build_redirect_from = Path(app.outdir) / redirect_from
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extensions = ["sphinxext.rediraffe"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
rediraffe_redirects = "redirects.txt"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Index File
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docs/x.y.z.rst docs/a.b.c.rst

tests/test_ext.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def test_backslashes(self, app: Sphinx, ensure_redirect):
7474
ensure_redirect("docs/folder2/toindex.html", "index.html")
7575
ensure_redirect("totoindex.html", "index.html")
7676

77+
@pytest.mark.sphinx("html", testroot="dot_in_filename")
78+
def test_dot_in_filename(self, app: Sphinx, ensure_redirect):
79+
app.build()
80+
assert app.statuscode == 0
81+
ensure_redirect("docs/x.y.z.html", "docs/a.b.c.html")
82+
7783
@pytest.mark.sphinx("html", testroot="mixed_slashes")
7884
def test_mixed_slashes(self, app: Sphinx, ensure_redirect):
7985
app.build()
@@ -326,6 +332,12 @@ def test_backslashes(self, app: Sphinx, ensure_redirect):
326332
ensure_redirect("docs/folder2/toindex/index.html", "index.html")
327333
ensure_redirect("totoindex/index.html", "index.html")
328334

335+
@pytest.mark.sphinx("dirhtml", testroot="dot_in_filename")
336+
def test_dot_in_filename(self, app: Sphinx, ensure_redirect):
337+
app.build()
338+
assert app.statuscode == 0
339+
ensure_redirect("docs/x.y.z/index.html", "docs/a.b.c/index.html")
340+
329341
@pytest.mark.sphinx("dirhtml", testroot="mixed_slashes")
330342
def test_mixed_slashes(self, app: Sphinx, ensure_redirect):
331343
app.build()

0 commit comments

Comments
 (0)