docs: Prefix url replacements with site-url in replace-urls.py preprocessor

This commit is contained in:
Zeglius 2024-08-17 15:04:00 +02:00
parent 3f94944b8f
commit a685de4dce

View File

@ -1,17 +1,18 @@
__doc__ = """Replace urls across the entire book"""
from copy import copy
import glob
import json
from pathlib import Path
import sys
from typing import cast
from typing import List, cast
from urllib.parse import urlparse
from libs.utils import Utils, debug as _debug
from libs.utils import debug as _debug
from libs.types import MdBook
PREPROCESSOR_NAME = "replace-urls"
def debug(*obj):
return _debug("REPLACE-URLS:", *obj)
@ -35,7 +36,7 @@ def is_url(url) -> bool:
return res
if __name__ == "__main__":
def main():
if len(sys.argv) > 1:
if sys.argv[1] == "supports":
sys.exit(0)
@ -43,7 +44,7 @@ if __name__ == "__main__":
book = MdBook(book)
config = Utils.get_config_from_ctx("replace-urls", context)
config = context["config"]["preprocessor"][PREPROCESSOR_NAME]
if not config:
print(json.dumps(book._data))
exit(0)
@ -51,9 +52,22 @@ if __name__ == "__main__":
print(json.dumps(book._data))
exit(0)
book_src = cast(str, context["config"]["book"]["src"])
# Prefix to append to replaced urls if output.html.site-url is set and the replacement starts with `/`
site_url_prefix = ""
_output_html_site_url = None
try:
_aux = context["config"]["output"]["html"]["site-url"]
_output_html_site_url = _aux
except Exception as _:
pass
site_url_prefix = _output_html_site_url.rstrip("/") if _output_html_site_url else ""
del _output_html_site_url
ignore_paths_list_globs = cast(list[str], list(config.get("ignore") or []))
ignore_paths: list[str] = []
root_dir = Path(context["root"], context["config"]["book"]["src"])
ignore_paths: List[str] = list()
root_dir = Path(context["root"], book_src)
for p in ignore_paths_list_globs:
ignore_paths += glob.glob(p, root_dir=root_dir)
@ -62,11 +76,20 @@ if __name__ == "__main__":
config_mappings: dict = config["mappings"]
# Get the url mappings
# If replacement starts with `/`, prepend
url_mappings: list[tuple[str, str]] = [
(k, v)
for k, v in config_mappings.items()
if k not in _IGNORE_STRINGS and is_url(k)
]
url_mappings = list(
map(
lambda m: (
(m[0], site_url_prefix + m[1]) if cast(str, m[1]).startswith("/") else m
),
url_mappings,
)
)
# Replace the urls
# book_s = json.dumps(book)
@ -84,7 +107,10 @@ if __name__ == "__main__":
continue
for old_url, new_url in url_mappings:
old = copy(section.chapter.content)
section.chapter.content = section.chapter.content.replace(old_url, new_url)
print(json.dumps(book._data))
if __name__ == "__main__":
main()