Skip to content

Commit 2e1df43

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

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

docs/preprocessors/replace-urls.py

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

3-
from copy import copy
3+
from copy import copy, deepcopy
44
import glob
55
import json
66
from pathlib import Path
77
import sys
88

9-
from typing import cast
9+
from typing import Any, Callable, List, Optional, cast
1010
from urllib.parse import urlparse
1111

12-
from libs.utils import Utils, debug as _debug
12+
from libs.utils import debug as _debug
1313
from libs.types import MdBook
1414

15+
PREPROCESSOR_NAME = "replace-urls"
16+
1517

1618
def debug(*obj):
1719
return _debug("REPLACE-URLS:", *obj)
@@ -35,25 +37,38 @@ def is_url(url) -> bool:
3537
return res
3638

3739

38-
if __name__ == "__main__":
40+
def main():
3941
if len(sys.argv) > 1:
4042
if sys.argv[1] == "supports":
4143
sys.exit(0)
4244
context, book = json.load(sys.stdin)
4345

4446
book = MdBook(book)
4547

46-
config = Utils.get_config_from_ctx("replace-urls", context)
48+
config = context["config"]["preprocessor"][PREPROCESSOR_NAME]
4749
if not config:
4850
print(json.dumps(book._data))
4951
exit(0)
5052
elif not isinstance(config, dict):
5153
print(json.dumps(book._data))
5254
exit(0)
5355

56+
book_src = cast(str, context["config"]["book"]["src"])
57+
58+
# Prefix to append to replaced urls if output.html.site-url is set and the replacement starts with `/`
59+
site_url_prefix = ""
60+
_output_html_site_url = None
61+
try:
62+
_aux = context["config"]["output"]["html"]["site-url"]
63+
_output_html_site_url = _aux
64+
except Exception as _:
65+
pass
66+
site_url_prefix = _output_html_site_url.rstrip("/") if _output_html_site_url else ""
67+
del _output_html_site_url
68+
5469
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"])
70+
ignore_paths: List[str] = list()
71+
root_dir = Path(context["root"], book_src)
5772
for p in ignore_paths_list_globs:
5873
ignore_paths += glob.glob(p, root_dir=root_dir)
5974

@@ -62,11 +77,20 @@ def is_url(url) -> bool:
6277
config_mappings: dict = config["mappings"]
6378

6479
# Get the url mappings
80+
# If replacement starts with `/`, prepend
6581
url_mappings: list[tuple[str, str]] = [
6682
(k, v)
6783
for k, v in config_mappings.items()
6884
if k not in _IGNORE_STRINGS and is_url(k)
6985
]
86+
url_mappings = list(
87+
map(
88+
lambda m: (
89+
(m[0], site_url_prefix + m[1]) if cast(str, m[1]).startswith("/") else m
90+
),
91+
url_mappings,
92+
)
93+
)
7094

7195
# Replace the urls
7296
# book_s = json.dumps(book)
@@ -84,7 +108,10 @@ def is_url(url) -> bool:
84108
continue
85109

86110
for old_url, new_url in url_mappings:
87-
old = copy(section.chapter.content)
88111
section.chapter.content = section.chapter.content.replace(old_url, new_url)
89112

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

0 commit comments

Comments
 (0)