From 47c360951b18469137d15ab5653b9cd61d2acbef Mon Sep 17 00:00:00 2001 From: Alden Date: Tue, 26 May 2026 16:45:28 +0800 Subject: [PATCH 1/2] fix: correct epub toc targets --- bin/epub | 6 +-- bin/preprocess-epub.py | 109 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 8 deletions(-) diff --git a/bin/epub b/bin/epub index 2ff80af..2dc5723 100755 --- a/bin/epub +++ b/bin/epub @@ -28,7 +28,7 @@ convert_to_epub() { pandoc -o "$OUTPUT_BOOK" --metadata-file="$meta_file" \ --toc-depth=2 \ --top-level-division=chapter \ - --file-scope=true \ + --split-level=1 \ --css="$css_file" \ --webtex \ --wrap=preserve \ @@ -39,14 +39,14 @@ convert_to_epub() { "${TEMP_DIR}"/ch2.md \ "${TEMP_DIR}"/ch3.md \ "${TEMP_DIR}"/ch4.md \ - "${TEMP_DIR}"/part-ii.md \ "${TEMP_DIR}"/ch5.md \ + "${TEMP_DIR}"/part-ii.md \ "${TEMP_DIR}"/ch6.md \ "${TEMP_DIR}"/ch7.md \ "${TEMP_DIR}"/ch8.md \ "${TEMP_DIR}"/ch9.md \ - "${TEMP_DIR}"/part-iii.md \ "${TEMP_DIR}"/ch10.md \ + "${TEMP_DIR}"/part-iii.md \ "${TEMP_DIR}"/ch11.md \ "${TEMP_DIR}"/ch12.md \ "${TEMP_DIR}"/ch13.md \ diff --git a/bin/preprocess-epub.py b/bin/preprocess-epub.py index 758f41a..abeabaf 100755 --- a/bin/preprocess-epub.py +++ b/bin/preprocess-epub.py @@ -15,6 +15,15 @@ from pathlib import Path FIGURE_SHORTCODE_RE = re.compile(r"\{\{<\s*figure\b(.*?)>\}\}", re.DOTALL) ATTR_RE = re.compile(r'([\w-]+)="([^"]*)"') ABS_IMAGE_RE = re.compile(r'!\[([^\]]*)\]\(/(?!static/)([^)]+)\)') +FRONT_MATTER_RE = re.compile(r"\A---\s*\n(.*?)\n---\s*\n?", re.DOTALL) +TITLE_RE = re.compile(r'^title:\s*(?:"([^"]*)"|\'([^\']*)\'|(.+?))\s*$', re.MULTILINE) +LINK_HEADING_RE = re.compile(r"^(#{2,6})\s+(\[[^\]]+\]\([^)]+\))\s*$", re.MULTILINE) +HEADER_ID_RE = re.compile(r"\{#([A-Za-z0-9_:-]+)\}") +RAW_ID_RE = re.compile(r'(]*\bid=")([^"]+)(")', re.IGNORECASE) +RAW_HREF_RE = re.compile(r'(]*\bhref=")(/[^"#?)]*)(#[^"]*)?(")', re.IGNORECASE) +MD_HREF_RE = re.compile(r"(? Date: Tue, 26 May 2026 17:00:41 +0800 Subject: [PATCH 2/2] fix: render epub callouts --- bin/preprocess-epub.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bin/preprocess-epub.py b/bin/preprocess-epub.py index abeabaf..b52c112 100755 --- a/bin/preprocess-epub.py +++ b/bin/preprocess-epub.py @@ -13,6 +13,7 @@ import sys from pathlib import Path FIGURE_SHORTCODE_RE = re.compile(r"\{\{<\s*figure\b(.*?)>\}\}", re.DOTALL) +CALLOUT_SHORTCODE_RE = re.compile(r"\{\{<\s*callout\b(.*?)>\}\}(.*?)\{\{<\s*/callout\s*>\}\}", re.DOTALL) ATTR_RE = re.compile(r'([\w-]+)="([^"]*)"') ABS_IMAGE_RE = re.compile(r'!\[([^\]]*)\]\(/(?!static/)([^)]+)\)') FRONT_MATTER_RE = re.compile(r"\A---\s*\n(.*?)\n---\s*\n?", re.DOTALL) @@ -135,6 +136,14 @@ def convert_markdown(text, slug, known_pages=None): alt = _escape_alt_text(attrs.get("caption") or attrs.get("title") or "") return f'![{alt}]({src})' + def replace_callout_shortcode(match): + body = match.group(2).strip() + if not body: + return "" + quoted = "\n".join(f"> {line}" if line else ">" for line in body.splitlines()) + return f"> **注意**\n>\n{quoted}" + + text = CALLOUT_SHORTCODE_RE.sub(replace_callout_shortcode, text) text = FIGURE_SHORTCODE_RE.sub(replace_figure_shortcode, text) # 把 Markdown 里的绝对路径图片 ![](/map/ch01.png) 转为 static/map/ch01.png