2 Commits
Author SHA1 Message Date
kaiandUser <human> 1394d98346 v0.7.1: \u4fee\u590d emoji \u65b9\u6846 + \u5206\u9875\u95ee\u9898 + \u5220\u9664\u6458\u8981\u91cd\u590d
\u7528\u6237\u53cd\u9988\u4e09\u4e2a P0 bug\uff1a

1. \u8868\u683c\u4e2d\u7684 \u2705/\u274c/\ud83d\udd36 \u6e32\u67d3\u4e3a\u65b9\u6846
   \u539f\u56e0\uff1a\u601d\u6e90\u5b57\u4f53\u5b50\u96c6\u4e0d\u542b\u5f69\u8272 emoji\u7684 glyph\u3002\u9664\u975e\u6362\u5b57\u4f53\uff08\u8d85\u51fa\u9879\u76ee\u8303\u56f4\uff09\uff0c
   \u5426\u5219\u53ea\u80fd\u66ff\u6362\u6210\u5b57\u4f53\u6709\u7684\u7b26\u53f7\u3002
   \u65b0\u589e _EMOJI_FALLBACK \u6620\u5c04\u8868\uff0c\u6db5\u76d6 20+ \u5e38\u7528 emoji\uff08\u5bf9\u5e94 CHECK/CROSS/DIAMOND/CIRCLE/STAR \u7b49\uff09\u3002
   \u7528 fontTools \u9a8c\u8bc1\u8fc7\u5b57\u6bcd\u6709\u65e0\uff1a\u2713/\u00d7/\u25c6/\u25c7/\u25cf/\u25cb/\u2605/\u203b \u90fd\u6709\u3001\u2717/\u2611/\u2610 \u6ca1\u6709\uff0c\u5df2\u8c03\u6574\u76f8\u5e94\u66ff\u6362\u503c\u3002

2. \u76ee\u5f55/\u672f\u8bed\u8868\u4e0e\u4e0b\u4e00\u7ae0\u540c\u9875 / \u4e2d\u95f4\u7a7a\u9875
   \u539f\u56e0\uff1abuild_toc \u672b\u5c3e\u52a0 PageBreak\uff0c\u7136\u540e\u4e0b\u4e00\u4e2a\u7ae0\u8282 H1 \u53c8 PageBreak\uff0c\u4e24\u4e2a PageBreak \u5939\u51fa\u7a7a\u9875\u3002
   \u4fee\u590d\uff1abuild_toc / \u672f\u8bed\u8868\u6e32\u67d3\u672b\u5c3e\u53bb\u6389 PageBreak\uff0c\u4ec5\u9760\u4e0b\u4e2a H1 \u7684 PageBreak\u3002
   \u5f00\u5934\u7684 PageBreak \u4fdd\u7559\uff0c\u4fdd\u8bc1\u201c\u76ee\u5f55 / \u672f\u8bed\u8868\u72ec\u7acb\u7b2c\u4e00\u9875\u5f00\u59cb\u201d\u3002

3. \u201c\u6267\u884c\u6458\u8981\u201d\u548c\u201c\u6458\u8981\u201d\u91cd\u590d
   \u6839\u636e\u7528\u6237\u9009\u62e9\uff1a\u4fdd\u7559\u6267\u884c\u6458\u8981\uff08Executive Summary\uff09\uff0c\u5220\u6389\u6458\u8981\uff08Abstract\uff09\u3002
   build_body \u65b0\u589e\u201c\u8df3\u8fc7\u6574\u4e2a\u7ae0\u8282\u201d\u903b\u8f91\uff1a\u770b\u5230 "## \u6458\u8981" / "# \u6458\u8981" / "## Abstract" \u5c31\u8df3\u5230\u4e0b\u4e00\u4e2a H1/H2\u3002

\u91cd\u6784\u6e05\u7406\uff1a
- \u63d0\u53d6 _render_generic_block \u5e2e\u52a9\u51fd\u6570\uff0c\u4ee3\u66ff build_body \u5185\u8054\u7684 p/quote/bullet/hr/image/table \u6e32\u67d3\u4ee3\u7801
- \u672f\u8bed\u8868\u90e8\u5206\u5faa\u73af\u590d\u7528\u8be5\u51fd\u6570

\u9a8c\u8bc1\u7ed3\u679c\uff1aPDF \u4ece 56 \u9875 \u2192 54 \u9875\uff08\u5220\u6389\u91cd\u590d\u7684\u6458\u8981 + \u6d88\u9664\u7a7a\u9875\uff09\u3002
\u5c55\u5f00\u7ae0\u8282\u8d77\u59cb\u9875\u987a\u5e8f\uff1a\u5c01\u9762(1) - \u514d\u8d23(2) - \u6267\u884c\u6458\u8981(2) - \u672f\u8bed\u8868(4) - \u76ee\u5f55(9) - \u6b63\u6587\u7b2c\u4e00\u7ae0(11) - \u7b2c\u5341\u7ae0(47) - \u53c2\u8003\u6587\u732e\u3002

remote \u914d\u7f6e\u4e3a http://192.168.50.45:3000/kai/deep_research.git\u3002

Co-authored-by: User <human>
2026-04-22 16:05:45 +08:00
kaiandUser <human> c88da4a20f v0.7: \u4fee\u590d PDF \u5f15\u6587\u7f16\u53f7\u4e0d\u5bf9\u5e94 + \u5c01\u9762\u91cd\u590d
\u4e24\u4e2a P0 bug \u4fee\u590d\uff1a

1. \u5f15\u6587\u7f16\u53f7\u5931\u914d
   \u5148\u524d\u7b56\u7565\uff1a\u53c2\u8003\u6587\u732e\u533a\u6309\u6b63\u6587\u51fa\u73b0\u987a\u5e8f\u91cd\u7f16\u53f7\u4e3a [1]/[2]/...\uff0c\u5bfc\u81f4\u6b63\u6587\u4e2d\u4e0a\u6807\u7684 [src_E43]
   \u4e0e\u53c2\u8003\u6587\u732e\u533a\u7684 [27] \u5b8c\u5168\u5bf9\u4e0d\u4e0a\u3002
   \u65b0\u7b56\u7565\uff1a\u53c2\u8003\u6587\u732e\u6761\u76ee\u76f4\u63a5\u7528\u539f\u59cb src_id \u4f5c\u7f16\u53f7\uff08\u5982 [src_E43] ...\uff09\uff0c
   \u6309\u5b57\u6bcd\u6570\u5b57\u6392\u5e8f\u5206\u7ec4\u5c55\u793a\u3002\u6b63\u6587\u548c\u53c2\u8003\u6587\u732e\u540c key\uff0c\u4e00\u773c\u5bf9\u5e94\u3002
   \u540c\u65f6\u628a\u7f3a\u5931\u7684 src_id\uff08sources.jsonl \u91cc\u6ca1\u7684\uff09\u5355\u72ec\u5217\u5728\u300c\u672a\u627e\u5230\u6765\u6e90\u300d
   \u7ae0\u8282\uff0c\u6a59\u8272\u8b66\u793a\uff0c\u8868\u660e\u662f\u539f\u59cb\u62a5\u544a\u7684\u8d28\u91cf\u95ee\u9898\u3001\u800c\u975e\u6e32\u67d3\u95ee\u9898\u3002
   \u9876\u90e8\u65b0\u589e\u300c\u5f15\u6587\u5065\u5eb7\u72b6\u6001\u300d\u5c0f\u7ed3\uff08\u6b63\u6587\u5f15\u7528X\u3001\u6536\u5f55Y\u3001\u7f3a\u5931Z\uff09\u3002

2. \u5c01\u9762\u91cd\u590d\uff08\u622a\u56fe\uff1a\u526f\u6807\u9898 + Confidentiality/Date/Version \u4ecd\u5728\u6b63\u6587\u9996\u9875\uff09
   \u539f\u56e0\uff1a\u539f\u8df3\u8fc7\u903b\u8f91\u662f\u201c\u8df3\u9996\u4e2a H1 \u2192 \u8df3\u5339\u914d is_cover_frontmatter \u7684 p\u201d\u3002
   \u526f\u6807\u9898\u662f\u52a0\u7c97\u6bb5\uff08**...**\uff09\uff0c\u4e0d\u542b "Confidentiality/Date" \u7b49\u5173\u952e\u8bcd\uff0c
   \u5339\u914d\u4e0d\u4e0a\u5c31\u89e6\u53d1\u300c\u5c01\u9762\u7ed3\u675f\u300d\u903b\u8f91\uff0c\u540e\u7eed\u5143\u4fe1\u606f\u6bb5\u4e5f\u6240\u4ee5\u5c31\u6f0f\u5305\u4e86\u3002
   \u65b0\u7b56\u7565\uff1a\u7b80\u5316\u4e3a\u300c\u6253\u8868\u4ece\u7b2c\u4e00\u4e2a H2/H3 \u5f00\u59cb\u8fed\u4ee3\u300d\uff0c\u524d\u9762\u7684 block \u5168\u90e8\u4e22\u6389\u3002
   \u7406\u7531\uff1a\u5c01\u9762\u5df2\u7531 build_cover \u4ece manifest \u72ec\u7acb\u751f\u6210\uff0c\u6b63\u6587\u5f00\u5934\u5728\u7b2c\u4e00\u4e2a H2
   \uff08\u201c## \u514d\u8d23\u58f0\u660e\u201d\uff09\u524d\u7684\u4efb\u4f55\u5185\u5bb9\u90fd\u662f\u5197\u4f59\u7684\u5c01\u9762\u5143\u4fe1\u606f\u3002

\u9a8c\u8bc1\uff1a\u91cd\u8dd1 PDF\uff0c\u7528 pypdf \u63d0\u53d6\u7b2c 1-2 \u9875\u548c\u53c2\u8003\u6587\u732e\u9875\u786e\u8ba4\u4e24\u4e2a bug \u90fd\u5df2\u6d88\u5931\u3002

\u9879\u76ee\u65b0\u589e pypdf \u4f9d\u8d56\uff08\u5de5\u5177\u7c7b\uff0c\u9a8c\u8bc1 PDF \u6587\u672c\u5185\u5bb9\u7528\uff09\u3002

Co-authored-by: User <human>
2026-04-22 15:41:56 +08:00
3 changed files with 234 additions and 103 deletions
+219 -103
View File
@@ -528,6 +528,37 @@ _UNICODE_SUB = {
_SUPER_CHARS_RE = re.compile(f"([{''.join(_UNICODE_SUPER)}]+)") _SUPER_CHARS_RE = re.compile(f"([{''.join(_UNICODE_SUPER)}]+)")
_SUB_CHARS_RE = re.compile(f"([{''.join(_UNICODE_SUB)}]+)") _SUB_CHARS_RE = re.compile(f"([{''.join(_UNICODE_SUB)}]+)")
# 彩色 emoji / 特殊符号 → 文字替代。思源字体子集不含这些字形,直接放会渲染成方框。
# 替换为字体里**实际存在**的符号(经过 fontTools 验证)。
# 验证命令见 scripts/lib/verify_font_glyphs.py
_EMOJI_FALLBACK = {
"": "", # U+2705 → U+2713 CHECK MARK(思源有)
"": "×", # U+274C → U+00D7 MULTIPLICATION SIGN(思源有,✗ U+2717 思源没有)
"": "×",
"": "×",
"🔶": "", # U+1F536 → U+25C6 BLACK DIAMOND(思源有)
"🔷": "", # U+25C7 WHITE DIAMOND(思源有)
"🟢": "", # U+25CF BLACK CIRCLE(思源有)
"🔴": "",
"🟡": "", # U+25CB WHITE CIRCLE
"🟠": "",
"": "", # U+2605 BLACK STAR(思源有)
"": "",
"": "[✓]", # U+2611 思源没有,用方括号包围替代
"": "[×]",
"": "[ ]",
"": "",
"": "",
"⚠️": "", # U+203B REFERENCE MARK(思源有)
"": "",
"💡": "",
"📌": "",
"🔑": "",
"📊": "",
"📈": "",
"📉": "",
}
def _replace_unicode_superscripts(text: str) -> str: def _replace_unicode_superscripts(text: str) -> str:
"""把连续的 Unicode 上标字符替换为 ReportLab <super> 标签。 """把连续的 Unicode 上标字符替换为 ReportLab <super> 标签。
@@ -549,10 +580,20 @@ def _replace_unicode_superscripts(text: str) -> str:
return text return text
def _replace_emoji(text: str) -> str:
"""把字体里没有的 emoji 替换为字体里有的等价符号。"""
for emoji, fallback in _EMOJI_FALLBACK.items():
if emoji in text:
text = text.replace(emoji, fallback)
return text
def md_inline_to_rl(text: str, *, add_cjk_space: bool = True) -> str: def md_inline_to_rl(text: str, *, add_cjk_space: bool = True) -> str:
"""Markdown inline → ReportLab mini HTML.""" """Markdown inline → ReportLab mini HTML."""
# 先做 Unicode 上/下标归一(字体子集不含这些字形,否则渲染为方框) # 先做 Unicode 上/下标归一(字体子集不含这些字形,否则渲染为方框)
text = _replace_unicode_superscripts(text) text = _replace_unicode_superscripts(text)
# emoji 替换为字体里有的符号
text = _replace_emoji(text)
# 然后在中英交界处加空格 # 然后在中英交界处加空格
if add_cjk_space: if add_cjk_space:
text = _add_cjk_spaces(text) text = _add_cjk_spaces(text)
@@ -711,8 +752,8 @@ def collect_toc_entries(blocks: List[Block]) -> List[tuple[int, str]]:
def build_toc(blocks: List[Block], styles: StyleSheet1) -> List: def build_toc(blocks: List[Block], styles: StyleSheet1) -> List:
"""生成目录条目。 """生成目录条目。
目录末尾 PageBreak 让后续内容独立成页。开头不 PageBreak 不在本函数内部 PageBreak——前面由调用方(H1/H2 分支)插入 PageBreak
调用方(H1 分支)已经负责在 H1 前另起一页 后面靠下一个章节的 H1 PageBreak 自然起作用。避免"连续 PageBreak 产生空页"
""" """
story: list = [] story: list = []
story.append(Paragraph("目录", styles["h1"])) story.append(Paragraph("目录", styles["h1"]))
@@ -720,7 +761,6 @@ def build_toc(blocks: List[Block], styles: StyleSheet1) -> List:
for level, title in collect_toc_entries(blocks): for level, title in collect_toc_entries(blocks):
style_name = "toc-h1" if level == 1 else "toc-h2" style_name = "toc-h1" if level == 1 else "toc-h2"
story.append(Paragraph(md_inline_to_rl(title), styles[style_name])) story.append(Paragraph(md_inline_to_rl(title), styles[style_name]))
story.append(PageBreak())
return story return story
@@ -812,6 +852,19 @@ def format_gb7714(rec: dict) -> str:
return body return body
def _sort_src_id(sid: str) -> tuple:
"""为 src_id 生成排序键:按字母段分组(A/B/C/E/...),组内按数字升序。"""
m = re.match(r"src_([A-Za-z]+)?(\d+)?([A-Za-z0-9_\-]*)", sid)
if not m:
return ("~", 0, sid)
alpha, num, rest = m.group(1) or "", m.group(2) or "0", m.group(3) or ""
try:
num_int = int(num)
except ValueError:
num_int = 0
return (alpha, num_int, rest)
def build_references( def build_references(
blocks: List[Block], blocks: List[Block],
sources_path: Optional[Path], sources_path: Optional[Path],
@@ -819,7 +872,12 @@ def build_references(
) -> List: ) -> List:
"""生成参考文献段落。 """生成参考文献段落。
引用顺序:按正文首次出现的先后排列(GB/T 7714 顺序编码制)。 v0.7 改变:**不再按出现顺序重编号**(之前会导致正文中 `[src_E43]` 和参考文献
区的 `[27]` 对不上)。改为:
- 参考文献条目直接用原始 `src_id` 作为编号(如 `[src_E43] Alnylam..., 2025.`
- 按 src_id 字母数字排序分组
- 缺失的 src_id 单独一段列出,明显标注供人工核查
- 顶部给一条"引文健康状态"小结
""" """
story: list = [] story: list = []
story.append(Paragraph("参考文献", styles["h1"])) story.append(Paragraph("参考文献", styles["h1"]))
@@ -835,31 +893,67 @@ def build_references(
)) ))
return story return story
if not sources: cited_set = set(cited_ids)
# 至少列出所有被引用的 ID,供人工回填 matched = [sid for sid in cited_ids if sid in sources]
missing = [sid for sid in cited_ids if sid not in sources]
# sources.jsonl 里有但正文没引用的——列为"备选"不展示,只统计
unused = [sid for sid in sources if sid not in cited_set]
# 头部健康状态
health = (
f"正文引用 <b>{len(cited_set)}</b> 条独立标识符;"
f"sources.jsonl 收录 <b>{len(sources)}</b> 条,"
f"<b>{len(matched)}</b> 条可对应,"
f"<b>{len(missing)}</b> 条在 sources.jsonl 中未找到。"
)
if unused:
health += f" 另有 {len(unused)} 条收录来源未在正文中引用,已省略展示。"
story.append(Paragraph(
f"<font color='#6b7280' size=8>引文健康状态:{health}</font>",
styles["caption"],
))
story.append(Spacer(1, 0.3 * cm))
# 主列表:按 src_id 字母数字排序
if matched:
story.append(Paragraph( story.append(Paragraph(
f"(未找到 sources.jsonl 或其内容为空。以下为正文出现的 {len(cited_ids)} 个引用标识符)", "<b>收录来源</b>",
styles["h3"],
))
for sid in sorted(matched, key=_sort_src_id):
rec = sources[sid]
text = format_gb7714(rec)
# 编号就是原始 sid,便于和正文中的 [src_E43] 上标对应
entry = f"<b>[{sid}]</b> {text}"
story.append(Paragraph(entry, styles["footnote"]))
# 缺失列表:明显标注
if missing:
story.append(Spacer(1, 0.4 * cm))
story.append(Paragraph(
f"<b>未找到来源({len(missing)} 条)</b>",
styles["h3"],
))
story.append(Paragraph(
"<font color='#b45309' size=8>"
"以下标识符在正文中出现但未在 <code>sources.jsonl</code> 中找到对应记录。"
"可能是编写阶段的占位符未回填,或原始研究员引用不规范,请核查后补充。"
"</font>",
styles["caption"], styles["caption"],
)) ))
for i, sid in enumerate(cited_ids, 1): # 按字母数字排序分组展示,一行三个,节省篇幅
story.append(Paragraph(f"[{i}] {sid}", styles["footnote"])) sorted_missing = sorted(missing, key=_sort_src_id)
return story # 每 4 个一行
row_size = 4
missing: list[str] = [] for k in range(0, len(sorted_missing), row_size):
for i, sid in enumerate(cited_ids, 1): chunk = sorted_missing[k : k + row_size]
rec = sources.get(sid) row_text = "  ".join(f"[{sid}]" for sid in chunk)
if not rec:
missing.append(sid)
story.append(Paragraph( story.append(Paragraph(
f"[{i}] {sid}(来源记录缺失,请核查 sources.jsonl", f"<font color='#b45309'>{row_text}</font>",
styles["footnote"], styles["footnote"],
)) ))
continue
text = format_gb7714(rec)
# 前面加序号,后面追加 [sid] 便于正文回溯
entry = f"[{i}] {text} <font color='#6b7280' size=7>【{sid}】</font>"
story.append(Paragraph(entry, styles["footnote"]))
# 打印到 stderr
if missing: if missing:
print( print(
f"WARNING: {len(missing)} cited src_ids not found in sources.jsonl: " f"WARNING: {len(missing)} cited src_ids not found in sources.jsonl: "
@@ -939,6 +1033,49 @@ def render_table(md_table: str, styles: StyleSheet1) -> Table:
return table return table
def _render_generic_block(block: Block, story: list, base_dir: Path, styles: StyleSheet1, *, in_summary: bool) -> None:
"""渲染一个非 H1/H2 的 blockp/quote/bullet/hr/image/table/h3)。
提取出来的帮助函数,给术语表内部循环和主循环复用。
"""
if block.kind == "h3":
story.append(Paragraph(md_inline_to_rl(block.content), styles["h3"]))
elif block.kind == "p":
if _TOC_PLACEHOLDER_RE.search(block.content) or _REF_PLACEHOLDER_RE.search(block.content):
return
style = styles["summary"] if in_summary else styles["body"]
story.append(Paragraph(md_inline_to_rl(block.content), style))
elif block.kind == "quote":
story.append(Paragraph(md_inline_to_rl(block.content), styles["quote"]))
elif block.kind == "bullet":
story.append(Paragraph("" + md_inline_to_rl(block.content), styles["bullet"]))
elif block.kind == "hr":
story.append(Spacer(1, 0.3 * cm))
elif block.kind == "image":
img_path = base_dir / block.content
if img_path.exists():
try:
img = Image(str(img_path), width=15 * cm, height=10 * cm, kind="proportional")
story.append(img)
if block.meta and block.meta.get("caption"):
story.append(Paragraph(block.meta["caption"], styles["caption"]))
except Exception as e:
story.append(Paragraph(
f"[图片加载失败:{block.content}{e}]",
styles["caption"],
))
else:
story.append(Paragraph(
f"[图片未找到:{block.content}]",
styles["caption"],
))
elif block.kind == "table":
try:
story.append(render_table(block.content, styles))
except Exception as e:
story.append(Paragraph(f"[表格渲染失败: {e}]", styles["caption"]))
def build_body( def build_body(
blocks: List[Block], blocks: List[Block],
base_dir: Path, base_dir: Path,
@@ -955,62 +1092,45 @@ def build_body(
- H1 triggers PageBreakH2/H3 keepWithNext;表格 splitByRow - H1 triggers PageBreakH2/H3 keepWithNext;表格 splitByRow
""" """
story: list = [] story: list = []
first_h1_seen = False # 是否已跳过正文首个 H1
skipping_cover_meta = False # 是否在吞掉封面元信息段
# Summary 样式
in_summary = False in_summary = False
# 准备跳过标志:标题级别下一个 "目录""参考文献" 见到时替换掉它(包含其下紧跟的占位段) # 第一步:跳过"封面块"——从正文开头一直跳到第一个 H2/H3 前。
# 采用简单索引遍历以便向前看 # 封面块 = 首个 H1(主标题) + 副标题(加粗 p) + 元信息段(Confidentiality/Date/Version + 分隔线(hr
i = 0 # 这些已由 build_cover 从 manifest 独立生成,正文里再出现就是重复。
# 规则简单可靠:跳过所有 block 直到遇到第一个 H2/H3(如 "## 免责声明")。
n = len(blocks) n = len(blocks)
first_section_idx = n
for k, b in enumerate(blocks):
if b.kind in ("h2", "h3"):
first_section_idx = k
break
i = first_section_idx # 从第一个 section 开始处理
while i < n: while i < n:
block = blocks[i] block = blocks[i]
# --- 跳过正文首个 H1(封面标题)+ 紧跟的元信息/hr --- # --- H1 处理(非首个;本循环内 first_section_idx 之后的 H1 都是真正的章节 H1)---
if not first_h1_seen and block.kind == "h1": if block.kind == "h1":
first_h1_seen = True content = block.content
skipping_cover_meta = True title_low = content.strip().lower()
i += 1
continue # 跳过整章:Abstract(保留 Executive Summary
if skipping_cover_meta: if title_low in ("摘要", "abstract"):
# 吞掉 p(元信息)、hr、quote(副标题可能被当成加粗段) j = i + 1
# 遇到 h1/h2/h3 就停止吞 while j < n and blocks[j].kind != "h1":
if block.kind in ("h1", "h2", "h3"): j += 1
skipping_cover_meta = False i = j
# 不 continue,让当前 block 正常处理
elif block.kind == "p" and is_cover_frontmatter(block.content):
i += 1
continue
elif block.kind in ("hr", "quote", "p", "bullet"):
# 第一个 hr 标记封面结束
if block.kind == "hr":
skipping_cover_meta = False
i += 1
continue
# 普通段落:如果不是封面元信息,就认为封面已结束
if block.kind == "p":
skipping_cover_meta = False
# fall through to normal handling
else:
i += 1
continue
else:
i += 1
continue continue
# --- H1 处理(非首个)--- # 所有非被替换的 H1 都 PageBreak
if block.kind == "h1":
story.append(PageBreak()) story.append(PageBreak())
content = block.content
if any(k in content for k in ("执行摘要", "Executive Summary", "管理层摘要")): if any(k in content for k in ("执行摘要", "Executive Summary", "管理层摘要")):
in_summary = True in_summary = True
else: else:
in_summary = False in_summary = False
# 目录 / 参考文献:替换为自动生成的内容 # 目录:已经 PageBreak 了,build_toc 内部再 PageBreak(独立页)
title_low = content.strip().lower()
if any(s in title_low for s in ("目录", "table of contents")): if any(s in title_low for s in ("目录", "table of contents")):
story.extend(build_toc(blocks, styles)) story.extend(build_toc(blocks, styles))
# 跳过紧随其后的占位段 # 跳过紧随其后的占位段
@@ -1019,6 +1139,15 @@ def build_body(
j += 1 j += 1
i = j i = j
continue continue
# 术语表:H1 本身已 PageBreak,末尾靠下一个 H1 自然换页
if any(s in title_low for s in ("术语表", "glossary")):
story.append(Paragraph(md_inline_to_rl(content), styles["h1"]))
i += 1
while i < n and blocks[i].kind != "h1":
sub = blocks[i]
_render_generic_block(sub, story, base_dir, styles, in_summary=False)
i += 1
continue
if any(s in title_low for s in ("参考文献", "references")): if any(s in title_low for s in ("参考文献", "references")):
story.extend(build_references(blocks, sources_path, styles)) story.extend(build_references(blocks, sources_path, styles))
j = i + 1 j = i + 1
@@ -1034,13 +1163,37 @@ def build_body(
# --- H2 同样检测占位符 --- # --- H2 同样检测占位符 ---
if block.kind == "h2": if block.kind == "h2":
title_low = block.content.strip().lower() title_low = block.content.strip().lower()
# 跳过整段:Abstract(与 Executive Summary 重复,根据用户偏好保留 Executive Summary
if title_low in ("摘要", "abstract"):
# 跳到下一个 H1/H2
j = i + 1
while j < n and blocks[j].kind not in ("h1", "h2"):
j += 1
i = j
continue
# 目录:前后 PageBreak(独立成页)
if any(s in title_low for s in ("目录", "table of contents")): if any(s in title_low for s in ("目录", "table of contents")):
story.append(PageBreak())
story.extend(build_toc(blocks, styles)) story.extend(build_toc(blocks, styles))
j = i + 1 j = i + 1
while j < n and blocks[j].kind == "p" and _TOC_PLACEHOLDER_RE.search(blocks[j].content): while j < n and blocks[j].kind == "p" and _TOC_PLACEHOLDER_RE.search(blocks[j].content):
j += 1 j += 1
i = j i = j
continue continue
# 术语表:前 PageBreak,后靠下一个 H1/H2 自然换页
if any(s in title_low for s in ("术语表", "glossary")):
story.append(PageBreak())
story.append(Paragraph(md_inline_to_rl(block.content), styles["h1"]))
i += 1
while i < n and blocks[i].kind not in ("h1", "h2"):
sub = blocks[i]
_render_generic_block(sub, story, base_dir, styles, in_summary=False)
i += 1
continue
if any(s in title_low for s in ("参考文献", "references")): if any(s in title_low for s in ("参考文献", "references")):
story.extend(build_references(blocks, sources_path, styles)) story.extend(build_references(blocks, sources_path, styles))
j = i + 1 j = i + 1
@@ -1053,45 +1206,8 @@ def build_body(
i += 1 i += 1
continue continue
if block.kind == "h3": # 非 H1/H2 的 block:统一走 _render_generic_block
story.append(Paragraph(md_inline_to_rl(block.content), styles["h3"])) _render_generic_block(block, story, base_dir, styles, in_summary=in_summary)
elif block.kind == "p":
# 跳过已识别但没有标题的孤立占位符(防御性)
if _TOC_PLACEHOLDER_RE.search(block.content) or _REF_PLACEHOLDER_RE.search(block.content):
i += 1
continue
style = styles["summary"] if in_summary else styles["body"]
story.append(Paragraph(md_inline_to_rl(block.content), style))
elif block.kind == "quote":
story.append(Paragraph(md_inline_to_rl(block.content), styles["quote"]))
elif block.kind == "bullet":
story.append(Paragraph("" + md_inline_to_rl(block.content), styles["bullet"]))
elif block.kind == "hr":
story.append(Spacer(1, 0.3 * cm))
elif block.kind == "image":
img_path = base_dir / block.content
if img_path.exists():
try:
img = Image(str(img_path), width=15 * cm, height=10 * cm, kind="proportional")
story.append(img)
if block.meta and block.meta.get("caption"):
story.append(Paragraph(block.meta["caption"], styles["caption"]))
except Exception as e:
story.append(Paragraph(
f"[图片加载失败:{block.content}{e}]",
styles["caption"],
))
else:
story.append(Paragraph(
f"[图片未找到:{block.content}]",
styles["caption"],
))
elif block.kind == "table":
try:
story.append(render_table(block.content, styles))
except Exception as e:
story.append(Paragraph(f"[表格渲染失败: {e}]", styles["caption"]))
i += 1 i += 1
return story return story
+1
View File
@@ -19,6 +19,7 @@ dependencies = [
"python-dateutil>=2.9.0", "python-dateutil>=2.9.0",
"PyYAML>=6.0.1", "PyYAML>=6.0.1",
"rich>=13.7.0", "rich>=13.7.0",
"pypdf>=6.10.2",
] ]
[project.optional-dependencies] [project.optional-dependencies]
Generated
+14
View File
@@ -374,6 +374,7 @@ dependencies = [
{ name = "pandas", version = "2.3.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, { name = "pandas", version = "2.3.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
{ name = "pandas", version = "3.0.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, { name = "pandas", version = "3.0.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" },
{ name = "pillow" }, { name = "pillow" },
{ name = "pypdf" },
{ name = "python-dateutil" }, { name = "python-dateutil" },
{ name = "pyyaml" }, { name = "pyyaml" },
{ name = "reportlab" }, { name = "reportlab" },
@@ -397,6 +398,7 @@ requires-dist = [
{ name = "numpy", specifier = ">=1.26.0" }, { name = "numpy", specifier = ">=1.26.0" },
{ name = "pandas", specifier = ">=2.1.0" }, { name = "pandas", specifier = ">=2.1.0" },
{ name = "pillow", specifier = ">=10.0.0" }, { name = "pillow", specifier = ">=10.0.0" },
{ name = "pypdf", specifier = ">=6.10.2" },
{ name = "pytest", marker = "extra == 'dev'", specifier = ">=7.4.0" }, { name = "pytest", marker = "extra == 'dev'", specifier = ">=7.4.0" },
{ name = "python-dateutil", specifier = ">=2.9.0" }, { name = "python-dateutil", specifier = ">=2.9.0" },
{ name = "pyyaml", specifier = ">=6.0.1" }, { name = "pyyaml", specifier = ">=6.0.1" },
@@ -1292,6 +1294,18 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/10/bd/c038d7cc38edc1aa5bf91ab8068b63d4308c66c4c8bb3cbba7dfbc049f9c/pyparsing-3.3.2-py3-none-any.whl", hash = "sha256:850ba148bd908d7e2411587e247a1e4f0327839c40e2e5e6d05a007ecc69911d", size = 122781, upload-time = "2026-01-21T03:57:55.912Z" }, { url = "https://files.pythonhosted.org/packages/10/bd/c038d7cc38edc1aa5bf91ab8068b63d4308c66c4c8bb3cbba7dfbc049f9c/pyparsing-3.3.2-py3-none-any.whl", hash = "sha256:850ba148bd908d7e2411587e247a1e4f0327839c40e2e5e6d05a007ecc69911d", size = 122781, upload-time = "2026-01-21T03:57:55.912Z" },
] ]
[[package]]
name = "pypdf"
version = "6.10.2"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "typing-extensions", marker = "python_full_version < '3.11'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/7b/3f/9f2167401c2e94833ca3b69535bad89e533b5de75fefe4197a2c224baec2/pypdf-6.10.2.tar.gz", hash = "sha256:7d09ce108eff6bf67465d461b6ef352dcb8d84f7a91befc02f904455c6eea11d", size = 5315679, upload-time = "2026-04-15T16:37:36.978Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/0c/d6/1d5c60cc17bbdf37c1552d9c03862fc6d32c5836732a0415b2d637edc2d0/pypdf-6.10.2-py3-none-any.whl", hash = "sha256:aa53be9826655b51c96741e5d7983ca224d898ac0a77896e64636810517624aa", size = 336308, upload-time = "2026-04-15T16:37:34.851Z" },
]
[[package]] [[package]]
name = "pytest" name = "pytest"
version = "9.0.3" version = "9.0.3"