Skip to content

Commit 9840cec

Browse files
committed
docs: Prefix url replacements with site-url in replace-urls.py preprocessor
1 parent 3f94944 commit 9840cec

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

docs/preprocessors/replace-urls.py

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
__doc__ = """Replace urls across the entire book"""
22

3-
from copy import copy
43
import glob
54
import json
65
from pathlib import Path
76
import sys
87

9-
from typing import cast
8+
from typing import List, cast
109
from urllib.parse import urlparse
1110

12-
from libs.utils import Utils, debug as _debug
11+
from libs.utils import debug as _debug
1312
from libs.types import MdBook
1413

14+
PREPROCESSOR_NAME = "replace-urls"
15+
1516

1617
def debug(*obj):
1718
return _debug("REPLACE-URLS:", *obj)
@@ -35,25 +36,40 @@ def is_url(url) -> bool:
3536
return res
3637

3738

38-
if __name__ == "__main__":
39+
def main():
3940
if len(sys.argv) > 1:
4041
if sys.argv[1] == "supports":
4142
sys.exit(0)
4243
context, book = json.load(sys.stdin)
4344

4445
book = MdBook(book)
4546

46-
config = Utils.get_config_from_ctx("replace-urls", context)
47+
config = context["config"]["preprocessor"][PREPROCESSOR_NAME]
4748
if not config:
4849
print(json.dumps(book._data))
4950
exit(0)
5051
elif not isinstance(config, dict):
5152
print(json.dumps(book._data))
5253
exit(0)
5354

55+
book_src = cast(str, context["config"]["book"]["src"])
56+
57+
# Prefix to append to replaced urls if output.html.site-url is set and the replacement starts with `/`
58+
site_url_prefix = ""
59+
_output_html_site_url = None
60+
try:
61+
_aux = context["config"]["output"]["html"]["site-url"]
62+
_output_html_site_url = _aux
63+
except Exception as _:
64+
pass
65+
site_url_prefix = (
66+
_output_html_site_url.rstrip("/").lstrip("/") if _output_html_site_url else ""
67+
)
68+
del _output_html_site_url
69+
5470
ignore_paths_list_globs = cast(list[str], list(config.get("ignore") or []))
55-
ignore_paths: list[str] = []
56-
root_dir = Path(context["root"], context["config"]["book"]["src"])
71+
ignore_paths: List[str] = list()
72+
root_dir = Path(context["root"], book_src)
5773
for p in ignore_paths_list_globs:
5874
ignore_paths += glob.glob(p, root_dir=root_dir)
5975

@@ -62,11 +78,20 @@ def is_url(url) -> bool:
6278
config_mappings: dict = config["mappings"]
6379

6480
# Get the url mappings
81+
# If replacement starts with `/`, prepend
6582
url_mappings: list[tuple[str, str]] = [
6683
(k, v)
6784
for k, v in config_mappings.items()
6885
if k not in _IGNORE_STRINGS and is_url(k)
6986
]
87+
url_mappings = list(
88+
map(
89+
lambda m: (
90+
(m[0], site_url_prefix + m[1]) if cast(str, m[1]).startswith("/") else m
91+
),
92+
url_mappings,
93+
)
94+
)
7095

7196
# Replace the urls
7297
# book_s = json.dumps(book)
@@ -84,7 +109,10 @@ def is_url(url) -> bool:
84109
continue
85110

86111
for old_url, new_url in url_mappings:
87-
old = copy(section.chapter.content)
88112
section.chapter.content = section.chapter.content.replace(old_url, new_url)
89113

90114
print(json.dumps(book._data))
115+
116+
117+
if __name__ == "__main__":
118+
main()

0 commit comments

Comments
 (0)