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"