Skip to content

Commit 8a49677

Browse files
authored
Update export_docs.py
1 parent 58e2eda commit 8a49677

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎scripts/export_docs.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
page id (e.g. "{#2-2-settings}" on page 2.2.-Settings), and cross-page links
1111
are rewritten to same-document anchors pointing at those ids.
1212
13+
Image paths are rewritten from site-root-absolute (/images/...) to a path
14+
relative to the print page URL (/<lang>/print/ -> ../../images/...).
15+
1316
Usage:
1417
python scripts/export_docs.py # -> print.md for every multi-page language
1518
python scripts/export_docs.py fr # -> docs/doc/fr/print.md only
@@ -34,6 +37,8 @@
3437
SITE_LINK_RE = re.compile(r"\[([^]]+)]\(/[^)]*\)")
3538
DOC_LINK_RE = re.compile(
3639
r"\[([^]]+)]\(([^)#\s]+?)(?:\.md)?(?:/)?(?:#([^)]*))?\)")
40+
# Root-absolute image paths (/images/...) become relative to /<lang>/print/.
41+
IMAGE_RE = re.compile(r"(\!\[[^]]*]\()(/?images/)")
3742

3843

3944
def load_config() -> dict:
@@ -192,9 +197,10 @@ def add_heading_ids(text: str, pid: str) -> str:
192197

193198

194199
def page_to_markdown(path: Path, anchors: dict[tuple[str, str | None], str]) -> str:
195-
"""Read one page: strip front-matter, rewrite links, add heading ids."""
200+
"""Read one page: strip front-matter, rewrite images and links, add ids."""
196201
text = path.read_text(encoding="utf-8")
197202
text = FRONT_MATTER_RE.sub("", text, count=1)
203+
text = IMAGE_RE.sub(r"\1../../images/", text)
198204
text = rewrite_links(text, anchors)
199205
text = add_heading_ids(text, page_id(path))
200206
return text.strip()

0 commit comments

Comments
 (0)