Skip to content

Commit 777d805

Browse files
committed
Revert "docs: Prefix url replacements with site-url in replace-urls.py preprocessor"
This reverts commit a685de4.
1 parent 3483730 commit 777d805

File tree

1 file changed

+8
-34
lines changed

1 file changed

+8
-34
lines changed

docs/preprocessors/replace-urls.py

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

3+
from copy import copy
34
import glob
45
import json
56
from pathlib import Path
67
import sys
78

8-
from typing import List, cast
9+
from typing import cast
910
from urllib.parse import urlparse
1011

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

14-
PREPROCESSOR_NAME = "replace-urls"
15-
1615

1716
def debug(*obj):
1817
return _debug("REPLACE-URLS:", *obj)
@@ -36,38 +35,25 @@ def is_url(url) -> bool:
3635
return res
3736

3837

39-
def main():
38+
if __name__ == "__main__":
4039
if len(sys.argv) > 1:
4140
if sys.argv[1] == "supports":
4241
sys.exit(0)
4342
context, book = json.load(sys.stdin)
4443

4544
book = MdBook(book)
4645

47-
config = context["config"]["preprocessor"][PREPROCESSOR_NAME]
46+
config = Utils.get_config_from_ctx("replace-urls", context)
4847
if not config:
4948
print(json.dumps(book._data))
5049
exit(0)
5150
elif not isinstance(config, dict):
5251
print(json.dumps(book._data))
5352
exit(0)
5453

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 = _output_html_site_url.rstrip("/") if _output_html_site_url else ""
66-
del _output_html_site_url
67-
6854
ignore_paths_list_globs = cast(list[str], list(config.get("ignore") or []))
69-
ignore_paths: List[str] = list()
70-
root_dir = Path(context["root"], book_src)
55+
ignore_paths: list[str] = []
56+
root_dir = Path(context["root"], context["config"]["book"]["src"])
7157
for p in ignore_paths_list_globs:
7258
ignore_paths += glob.glob(p, root_dir=root_dir)
7359

@@ -76,20 +62,11 @@ def main():
7662
config_mappings: dict = config["mappings"]
7763

7864
# Get the url mappings
79-
# If replacement starts with `/`, prepend
8065
url_mappings: list[tuple[str, str]] = [
8166
(k, v)
8267
for k, v in config_mappings.items()
8368
if k not in _IGNORE_STRINGS and is_url(k)
8469
]
85-
url_mappings = list(
86-
map(
87-
lambda m: (
88-
(m[0], site_url_prefix + m[1]) if cast(str, m[1]).startswith("/") else m
89-
),
90-
url_mappings,
91-
)
92-
)
9370

9471
# Replace the urls
9572
# book_s = json.dumps(book)
@@ -107,10 +84,7 @@ def main():
10784
continue
10885

10986
for old_url, new_url in url_mappings:
87+
old = copy(section.chapter.content)
11088
section.chapter.content = section.chapter.content.replace(old_url, new_url)
11189

11290
print(json.dumps(book._data))
113-
114-
115-
if __name__ == "__main__":
116-
main()

0 commit comments

Comments
 (0)