Skip to content

Commit 4026ec2

Browse files
Daltz333TheTripleV
andauthored
Implement Auto Redirect Builder (#10)
Co-authored-by: TheTripleV <[email protected]> Co-authored-by: TheTripleV <[email protected]>
1 parent 9a19dbf commit 4026ec2

File tree

25 files changed

+139
-12
lines changed

25 files changed

+139
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ To check that deleted/renamed files in your git repo are in your redirects,
3131
1. Make sure `rediraffe_branch` and `rediraffe_redirects` are set in conf.py.
3232
2. Run the `rediraffecheckdiff` builder.
3333

34+
### Auto Redirect builder
35+
For convenience, the auto redirect builder can be used to automatically add renamed files to your redirects file. Simply run the `rediraffewritediff` builder.
36+
37+
Note: The auto redirect builder only works with a configuration file.
38+
Note: Deleted files cannot be added to your redirects file automatically.
39+
3440
## Options
3541
These values are placed in the conf.py of your sphinx project.
3642

@@ -49,6 +55,8 @@ These values are placed in the conf.py of your sphinx project.
4955
* `to_url` - the path to to_url's built html file relative to the outdir.
5056
* `rel_url` - the relative path from from_url to to_url.
5157

58+
* `rediraffe_auto_redirect_perc`
59+
* Optional. The percentage as an integer representing the accuracy required before auto redirecting with the `rediraffewritediff` builder. The default is 100.
5260

5361
## Example Config
5462

sphinxext/rediraffe.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,17 @@ def init(self) -> None:
252252
src_path = Path(self.app.srcdir)
253253

254254
rediraffe_redirects = self.app.config.rediraffe_redirects
255+
redirects_path = None
255256
if isinstance(rediraffe_redirects, dict):
256257
pass
257258
elif isinstance(rediraffe_redirects, str):
258-
path = Path(src_path) / rediraffe_redirects
259-
if not path.is_file():
259+
redirects_path = Path(src_path) / rediraffe_redirects
260+
if not redirects_path.is_file():
260261
logger.error(red("rediraffe: rediraffe_redirects file does not exist."))
261262
self.app.statuscode = 1
262263
return
263264
try:
264-
rediraffe_redirects = create_graph(path)
265+
rediraffe_redirects = create_graph(redirects_path)
265266
except ExtensionError as e:
266267
self.app.statuscode = 1
267268
return
@@ -309,7 +310,6 @@ def abs_path_in_src_dir_w_src_suffix(filename: str) -> Union[Path, None]:
309310
continue
310311
if path_rename_to == None:
311312
continue
312-
313313
rename_hints[path_rename_from] = (path_rename_to, perc)
314314

315315
# run git diff
@@ -338,13 +338,29 @@ def abs_path_in_src_dir_w_src_suffix(filename: str) -> Union[Path, None]:
338338
logger.error(err_msg)
339339
self.app.statuscode = 1
340340

341-
for renamed_file in rename_hints:
342-
hint_to, perc = rename_hints[renamed_file]
343-
if renamed_file in absolute_redirects:
344-
logger.info(
345-
f"renamed file {renamed_file} redirects to {absolute_redirects[renamed_file]}."
346-
)
347-
else:
341+
with redirects_path.open("a") as redirects_file:
342+
343+
for renamed_file in rename_hints:
344+
hint_to, perc = rename_hints[renamed_file]
345+
346+
if renamed_file in absolute_redirects:
347+
logger.info(
348+
f"renamed file {renamed_file} redirects to {absolute_redirects[renamed_file]}."
349+
)
350+
continue
351+
352+
if self.name == "rediraffewritediff":
353+
if perc >= self.app.config.rediraffe_auto_redirect_perc:
354+
rel_rename_from = f'"{str(PurePosixPath(renamed_file.relative_to(src_path)))}"'
355+
rel_rename_to = (
356+
f'"{str(PurePosixPath(hint_to.relative_to(src_path)))}"'
357+
)
358+
redirects_file.write(f"{rel_rename_from} {rel_rename_to}\n")
359+
logger.info(
360+
f"{green('(okay)')} Renamed file {rel_rename_from} has been redirected to {rel_rename_to} in your redirects file!"
361+
)
362+
continue
363+
348364
err_msg = (
349365
f"{red('(broken)')} {renamed_file} was deleted but is not redirected!"
350366
f" Hint: This file was renamed to {hint_to} with a similarity of {perc}%."
@@ -368,12 +384,29 @@ def read(self):
368384
return []
369385

370386

387+
class WriteRedirectsDiffBuilder(CheckRedirectsDiffBuilder):
388+
name = "rediraffewritediff"
389+
390+
def init(self) -> None:
391+
rediraffe_redirects = self.app.config.rediraffe_redirects
392+
if not isinstance(rediraffe_redirects, str):
393+
logger.error(
394+
f"{red('(broken)')} Automatic redirects is only available with a redirects file."
395+
)
396+
self.app.statuscode = 1
397+
return
398+
399+
super().init()
400+
401+
371402
def setup(app: Sphinx) -> Dict[str, Any]:
372403
app.add_config_value("rediraffe_redirects", "", None)
373404
app.add_config_value("rediraffe_branch", "", None)
374405
app.add_config_value("rediraffe_template", None, None)
406+
app.add_config_value("rediraffe_auto_redirect_perc", 100, None)
375407

376408
app.add_builder(CheckRedirectsDiffBuilder)
409+
app.add_builder(WriteRedirectsDiffBuilder)
377410
app.connect("build-finished", build_redirects)
378411

379412
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
another file
2+
3+
but more!

0 commit comments

Comments
 (0)