From c88da4a20f8d8b9b17ce4c74a4f5303d8ff3f9ca Mon Sep 17 00:00:00 2001 From: kai Date: Wed, 22 Apr 2026 15:41:56 +0800 Subject: [PATCH] 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 --- .opencode/templates/report-template.py | 144 +++++++++++++++---------- pyproject.toml | 1 + uv.lock | 14 +++ 3 files changed, 100 insertions(+), 59 deletions(-) diff --git a/.opencode/templates/report-template.py b/.opencode/templates/report-template.py index 9dd913e..279c938 100755 --- a/.opencode/templates/report-template.py +++ b/.opencode/templates/report-template.py @@ -812,6 +812,19 @@ def format_gb7714(rec: dict) -> str: 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( blocks: List[Block], sources_path: Optional[Path], @@ -819,7 +832,12 @@ def build_references( ) -> List: """生成参考文献段落。 - 引用顺序:按正文首次出现的先后排列(GB/T 7714 顺序编码制)。 + v0.7 改变:**不再按出现顺序重编号**(之前会导致正文中 `[src_E43]` 和参考文献 + 区的 `[27]` 对不上)。改为: + - 参考文献条目直接用原始 `src_id` 作为编号(如 `[src_E43] Alnylam..., 2025.`) + - 按 src_id 字母数字排序分组 + - 缺失的 src_id 单独一段列出,明显标注供人工核查 + - 顶部给一条"引文健康状态"小结 """ story: list = [] story.append(Paragraph("参考文献", styles["h1"])) @@ -835,31 +853,67 @@ def build_references( )) return story - if not sources: - # 至少列出所有被引用的 ID,供人工回填 + cited_set = set(cited_ids) + 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"正文引用 {len(cited_set)} 条独立标识符;" + f"sources.jsonl 收录 {len(sources)} 条," + f"{len(matched)} 条可对应," + f"{len(missing)} 条在 sources.jsonl 中未找到。" + ) + if unused: + health += f" 另有 {len(unused)} 条收录来源未在正文中引用,已省略展示。" + story.append(Paragraph( + f"引文健康状态:{health}", + styles["caption"], + )) + story.append(Spacer(1, 0.3 * cm)) + + # 主列表:按 src_id 字母数字排序 + if matched: story.append(Paragraph( - f"(未找到 sources.jsonl 或其内容为空。以下为正文出现的 {len(cited_ids)} 个引用标识符)", + "收录来源", + styles["h3"], + )) + for sid in sorted(matched, key=_sort_src_id): + rec = sources[sid] + text = format_gb7714(rec) + # 编号就是原始 sid,便于和正文中的 [src_E43] 上标对应 + entry = f"[{sid}] {text}" + story.append(Paragraph(entry, styles["footnote"])) + + # 缺失列表:明显标注 + if missing: + story.append(Spacer(1, 0.4 * cm)) + story.append(Paragraph( + f"未找到来源({len(missing)} 条)", + styles["h3"], + )) + story.append(Paragraph( + "" + "以下标识符在正文中出现但未在 sources.jsonl 中找到对应记录。" + "可能是编写阶段的占位符未回填,或原始研究员引用不规范,请核查后补充。" + "", styles["caption"], )) - for i, sid in enumerate(cited_ids, 1): - story.append(Paragraph(f"[{i}] {sid}", styles["footnote"])) - return story - - missing: list[str] = [] - for i, sid in enumerate(cited_ids, 1): - rec = sources.get(sid) - if not rec: - missing.append(sid) + # 按字母数字排序分组展示,一行三个,节省篇幅 + sorted_missing = sorted(missing, key=_sort_src_id) + # 每 4 个一行 + row_size = 4 + for k in range(0, len(sorted_missing), row_size): + chunk = sorted_missing[k : k + row_size] + row_text = "  ".join(f"[{sid}]" for sid in chunk) story.append(Paragraph( - f"[{i}] {sid}(来源记录缺失,请核查 sources.jsonl)", + f"{row_text}", styles["footnote"], )) - continue - text = format_gb7714(rec) - # 前面加序号,后面追加 [sid] 便于正文回溯 - entry = f"[{i}] {text} 【{sid}】" - story.append(Paragraph(entry, styles["footnote"])) + # 打印到 stderr if missing: print( f"WARNING: {len(missing)} cited src_ids not found in sources.jsonl: " @@ -955,52 +1009,24 @@ def build_body( - H1 triggers PageBreak;H2/H3 keepWithNext;表格 splitByRow """ story: list = [] - first_h1_seen = False # 是否已跳过正文首个 H1 - skipping_cover_meta = False # 是否在吞掉封面元信息段 - - # Summary 样式 in_summary = False - # 准备跳过标志:标题级别下一个 "目录""参考文献" 见到时替换掉它(包含其下紧跟的占位段) - # 采用简单索引遍历以便向前看。 - i = 0 + # 第一步:跳过"封面块"——从正文开头一直跳到第一个 H2/H3 前。 + # 封面块 = 首个 H1(主标题) + 副标题(加粗 p) + 元信息段(Confidentiality/Date/Version) + 分隔线(hr)。 + # 这些已由 build_cover 从 manifest 独立生成,正文里再出现就是重复。 + # 规则简单可靠:跳过所有 block 直到遇到第一个 H2/H3(如 "## 免责声明")。 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: block = blocks[i] - # --- 跳过正文首个 H1(封面标题)+ 紧跟的元信息/hr --- - if not first_h1_seen and block.kind == "h1": - first_h1_seen = True - skipping_cover_meta = True - i += 1 - continue - if skipping_cover_meta: - # 吞掉 p(元信息)、hr、quote(副标题可能被当成加粗段) - # 遇到 h1/h2/h3 就停止吞 - if block.kind in ("h1", "h2", "h3"): - skipping_cover_meta = False - # 不 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 - - # --- H1 处理(非首个)--- + # --- H1 处理(非首个;本循环内 first_section_idx 之后的 H1 都是真正的章节 H1)--- if block.kind == "h1": story.append(PageBreak()) content = block.content diff --git a/pyproject.toml b/pyproject.toml index ee073c8..55895c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ dependencies = [ "python-dateutil>=2.9.0", "PyYAML>=6.0.1", "rich>=13.7.0", + "pypdf>=6.10.2", ] [project.optional-dependencies] diff --git a/uv.lock b/uv.lock index b47670c..e63f2ef 100644 --- a/uv.lock +++ b/uv.lock @@ -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 = "3.0.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, { name = "pillow" }, + { name = "pypdf" }, { name = "python-dateutil" }, { name = "pyyaml" }, { name = "reportlab" }, @@ -397,6 +398,7 @@ requires-dist = [ { name = "numpy", specifier = ">=1.26.0" }, { name = "pandas", specifier = ">=2.1.0" }, { name = "pillow", specifier = ">=10.0.0" }, + { name = "pypdf", specifier = ">=6.10.2" }, { name = "pytest", marker = "extra == 'dev'", specifier = ">=7.4.0" }, { name = "python-dateutil", specifier = ">=2.9.0" }, { 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" }, ] +[[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]] name = "pytest" version = "9.0.3"