feat(transit): 添加正文引用标记到书签超链接功能

- 新增 CITE_PATTERN 正则表达式匹配 [N] 引用格式
- 添加 base_dir 参数支持相对图片路径解析
- 实现书签创建和超链接替换功能
- 添加 link_body_citations 函数处理正文引用链接
- 在参考文献段落中添加书签标识
- 支持将 [N] 引用替换为指向参考文献的超链接
This commit is contained in:
zzy
2026-05-10 15:07:20 +08:00
parent 0b10e97e0c
commit 4e39a4f2ac
4 changed files with 153 additions and 7 deletions

View File

@@ -7,6 +7,7 @@
import struct
import re
from pathlib import Path
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
@@ -25,15 +26,26 @@ _FIG_CAPTION = re.compile(
)
def make_image_paragraph(block: str) -> dict | None:
"""若 *block* 包含 ``<img>`` 标签,返回图片段落字典;否则返回 ``None``。"""
def make_image_paragraph(block: str, base_dir: str | Path | None = None) -> dict | None:
"""若 *block* 包含 ``<img>`` 标签,返回图片段落字典;否则返回 ``None``。
Parameters
----------
block : str
文本块。
base_dir : str | Path | None
Markdown 文件所在目录,用于解析图片相对路径。
"""
m = _IMG_TAG.search(block)
if not m:
return None
attrs = dict(_ATTR.findall(block))
src = attrs.get("src", m.group(2))
if base_dir and not Path(src).is_absolute():
src = str(Path(base_dir) / src)
return {
"type": "image",
"src": attrs.get("src", m.group(2)),
"src": src,
"alt": attrs.get("alt", ""),
}