feat(transit): 添加参考文献解析功能并修复段落编号继承问题

新增了参考文献处理模块,支持按照 GB 7714 《文后参考文献著录规则》顺序编码制
解析和格式化参考文献。同时修复了段落替换过程中自动编号丢失的问题。

- 新增 transit/references.py 模块,提供参考文献解析和格式化功能
- 在 body.py 的 replace_placeholder 函数中实现段落编号属性的正确继承
- 修改 transit/__init__.py 导入新的参考文献处理函数
- 更新 transit/config.py 添加参考文献样式配置项
- 修改 transit/renderer.py 集成参考文献处理流程
This commit is contained in:
zzy
2026-05-08 22:14:51 +08:00
parent c29a3e6af0
commit 74d28ea2d8
5 changed files with 149 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ from docx import Document
from .config import load_config, ThesisConfig
from .parser import parse_markdown
from .body import body_to_paragraphs, replace_placeholder
from .references import references_to_paragraphs
_TEXT_FIELDS = [
@@ -97,8 +98,13 @@ def generate_thesis(
if body_md else []
)
# 6. 占位符
# 6. 解析参考文献为段落列表
ref_text = ctx.get("reference", "")
ref_paragraphs = references_to_paragraphs(ref_text, ref_style=config.reference_style)
# 7. 占位符(替代模板变量,后处理时替换)
ctx["body_placeholder"] = "__CONTEXT_PLACEHOLDER__"
ctx["reference"] = "__REFERENCE_PLACEHOLDER__"
# 7. 渲染模板
doc = DocxTemplate(str(template_path))
@@ -108,12 +114,16 @@ def generate_thesis(
temp_path = Path(output_path).with_suffix(".tmp")
doc.save(str(temp_path))
# 9. 正文注入
# 9. 正文注入+参考文献注入
final_doc = Document(str(temp_path))
replace_placeholder(
final_doc, "__CONTEXT_PLACEHOLDER__", body_paragraphs,
default_body_style=config.body_style,
)
replace_placeholder(
final_doc, "__REFERENCE_PLACEHOLDER__", ref_paragraphs,
default_body_style=config.reference_style,
)
final_doc.save(str(output_path))
temp_path.unlink(missing_ok=True)