Skip to content

Commit 0682c3c

Browse files
committed
Implement auto redirect
1 parent eb95d44 commit 0682c3c

10 files changed

Lines changed: 72 additions & 7 deletions

File tree

sphinxext/rediraffe.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ def abs_path_in_src_dir_w_src_suffix(filename: str) -> Union[Path, None]:
291291
continue
292292
if path_rename_to == None:
293293
continue
294-
295294
rename_hints[path_rename_from] = (path_rename_to, perc)
296295

297296
# run git diff
@@ -320,19 +319,57 @@ def abs_path_in_src_dir_w_src_suffix(filename: str) -> Union[Path, None]:
320319
logger.error(err_msg)
321320
self.app.statuscode = 1
322321

322+
file = open(path, "a")
323+
323324
for renamed_file in rename_hints:
324325
hint_to, perc = rename_hints[renamed_file]
325326
if renamed_file in absolute_redirects:
326327
logger.info(
327328
f"renamed file {renamed_file} redirects to {absolute_redirects[renamed_file]}."
328329
)
329330
else:
330-
err_msg = (
331-
f"{red('(broken)')} {renamed_file} was deleted but is not redirected!"
332-
f" Hint: This file was renamed to {hint_to} with a similarity of {perc}%."
333-
)
334-
logger.error(err_msg)
335-
self.app.statuscode = 1
331+
if self.app.config.rediraffe_auto_redirect and not path.is_file():
332+
logger.warning(
333+
f"{red('(broken)')} Automatic redirects is only available with a redirects file."
334+
)
335+
elif (
336+
self.app.config.rediraffe_auto_redirect
337+
and "rediraffewritediff" in self.name
338+
):
339+
if path_rename_from not in absolute_redirects:
340+
if perc >= self.app.config.rediraffe_auto_redirect_perc:
341+
from_fancy_path = str(renamed_file)[
342+
len(self.app.srcdir) + 1 :
343+
]
344+
345+
from_fancy_path = "\"" + from_fancy_path.replace("\\", "/") + "\""
346+
347+
to_fancy_path = str(hint_to)[
348+
len(self.app.srcdir) + 1 :
349+
]
350+
351+
to_fancy_path = "\"" + to_fancy_path.replace("\\", "/") + "\""
352+
353+
file.write(from_fancy_path + " " + to_fancy_path + "\n")
354+
logger.info(
355+
f"{green('(okay)')} Renamed file {renamed_file} has been redirected to {hint_to} in your redirects file!"
356+
)
357+
else:
358+
err_msg = (
359+
f"{red('(broken)')} {renamed_file} was deleted but is not redirected!"
360+
f" Hint: This file was renamed to {hint_to} with a similarity of {perc}%."
361+
)
362+
logger.error(err_msg)
363+
self.app.statuscode = 1
364+
else:
365+
err_msg = (
366+
f"{red('(broken)')} {renamed_file} was deleted but is not redirected!"
367+
f" Hint: This file was renamed to {hint_to} with a similarity of {perc}%."
368+
)
369+
logger.error(err_msg)
370+
self.app.statuscode = 1
371+
372+
file.close()
336373

337374
def get_outdated_docs(self):
338375
return []
@@ -347,12 +384,22 @@ def get_target_uri(self, docname=None, typ=None):
347384
return ""
348385

349386

387+
class WriteRedirectsDiffBuilder(CheckRedirectsDiffBuilder):
388+
name = "rediraffewritediff"
389+
390+
def init(self) -> None:
391+
super().init()
392+
393+
350394
def setup(app: Sphinx) -> Dict[str, Any]:
351395
app.add_config_value("rediraffe_redirects", "", None)
352396
app.add_config_value("rediraffe_branch", "", None)
353397
app.add_config_value("rediraffe_template", None, None)
398+
app.add_config_value("rediraffe_auto_redirect", False, None)
399+
app.add_config_value("rediraffe_auto_redirect_perc", 100, None)
354400

355401
app.add_builder(CheckRedirectsDiffBuilder)
402+
app.add_builder(WriteRedirectsDiffBuilder)
356403
app.connect("build-finished", build_redirects)
357404

358405
return {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
another file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index file

tests/roots/builder/test-renamed_write_file_not_redirected/HEAD/redirects.txt

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
another file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index file

tests/roots/builder/test-renamed_write_file_not_redirected/HEAD~1/redirects.txt

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extensions = ["sphinxext.rediraffe"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
rediraffe_branch = "HEAD~1"
9+
rediraffe_redirects = "redirects.txt"
10+
rediraffe_auto_redirect = True

tests/roots/builder/test-renamed_write_file_not_redirected/redirects.txt

Whitespace-only changes.

tests/test_builder.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def test_builder_renamed_file_not_redirected(app_init_repo):
3636
app_init_repo.build()
3737
assert app_init_repo.statuscode == 1
3838

39+
@pytest.mark.sphinx("rediraffewritediff", testroot="renamed_file_not_redirected")
40+
def test_builder_renamed_file_write_not_redirected(app_init_repo):
41+
app_init_repo.build()
42+
assert app_init_repo.statuscode == 0
3943

4044
@pytest.mark.sphinx("rediraffecheckdiff", testroot="link_redirected_to_chain")
4145
def test_builder_link_redirected_to_chain(app_init_repo):

0 commit comments

Comments
 (0)