mirror of
https://github.com/ublue-os/bazzite.git
synced 2025-01-01 03:21:41 +00:00
docs: Prefix url replacements with site-url in replace-urls.py preprocessor
This commit is contained in:
parent
3f94944b8f
commit
a685de4dce
@ -1,17 +1,18 @@
|
|||||||
__doc__ = """Replace urls across the entire book"""
|
__doc__ = """Replace urls across the entire book"""
|
||||||
|
|
||||||
from copy import copy
|
|
||||||
import glob
|
import glob
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from typing import cast
|
from typing import List, cast
|
||||||
from urllib.parse import urlparse
|
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
|
from libs.types import MdBook
|
||||||
|
|
||||||
|
PREPROCESSOR_NAME = "replace-urls"
|
||||||
|
|
||||||
|
|
||||||
def debug(*obj):
|
def debug(*obj):
|
||||||
return _debug("REPLACE-URLS:", *obj)
|
return _debug("REPLACE-URLS:", *obj)
|
||||||
@ -35,7 +36,7 @@ def is_url(url) -> bool:
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def main():
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if sys.argv[1] == "supports":
|
if sys.argv[1] == "supports":
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
@ -43,7 +44,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
book = MdBook(book)
|
book = MdBook(book)
|
||||||
|
|
||||||
config = Utils.get_config_from_ctx("replace-urls", context)
|
config = context["config"]["preprocessor"][PREPROCESSOR_NAME]
|
||||||
if not config:
|
if not config:
|
||||||
print(json.dumps(book._data))
|
print(json.dumps(book._data))
|
||||||
exit(0)
|
exit(0)
|
||||||
@ -51,9 +52,22 @@ if __name__ == "__main__":
|
|||||||
print(json.dumps(book._data))
|
print(json.dumps(book._data))
|
||||||
exit(0)
|
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_globs = cast(list[str], list(config.get("ignore") or []))
|
||||||
ignore_paths: list[str] = []
|
ignore_paths: List[str] = list()
|
||||||
root_dir = Path(context["root"], context["config"]["book"]["src"])
|
root_dir = Path(context["root"], book_src)
|
||||||
for p in ignore_paths_list_globs:
|
for p in ignore_paths_list_globs:
|
||||||
ignore_paths += glob.glob(p, root_dir=root_dir)
|
ignore_paths += glob.glob(p, root_dir=root_dir)
|
||||||
|
|
||||||
@ -62,11 +76,20 @@ if __name__ == "__main__":
|
|||||||
config_mappings: dict = config["mappings"]
|
config_mappings: dict = config["mappings"]
|
||||||
|
|
||||||
# Get the url mappings
|
# Get the url mappings
|
||||||
|
# If replacement starts with `/`, prepend
|
||||||
url_mappings: list[tuple[str, str]] = [
|
url_mappings: list[tuple[str, str]] = [
|
||||||
(k, v)
|
(k, v)
|
||||||
for k, v in config_mappings.items()
|
for k, v in config_mappings.items()
|
||||||
if k not in _IGNORE_STRINGS and is_url(k)
|
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
|
# Replace the urls
|
||||||
# book_s = json.dumps(book)
|
# book_s = json.dumps(book)
|
||||||
@ -84,7 +107,10 @@ if __name__ == "__main__":
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
for old_url, new_url in url_mappings:
|
for old_url, new_url in url_mappings:
|
||||||
old = copy(section.chapter.content)
|
|
||||||
section.chapter.content = section.chapter.content.replace(old_url, new_url)
|
section.chapter.content = section.chapter.content.replace(old_url, new_url)
|
||||||
|
|
||||||
print(json.dumps(book._data))
|
print(json.dumps(book._data))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user