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:
@@ -6,7 +6,9 @@ Markdown 正文 → Word 段落转换。
|
||||
"""
|
||||
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from docx import Document
|
||||
from docx.oxml.ns import qn
|
||||
|
||||
_PAT_HEADING = re.compile(r"^(#{1,6})\s+(.+)$", re.MULTILINE)
|
||||
|
||||
@@ -84,6 +86,11 @@ def replace_placeholder(
|
||||
placeholder_style = para.style.name if para.style else None
|
||||
parent = para._element.getparent()
|
||||
idx = list(parent).index(para._element)
|
||||
|
||||
# 保存原段落的编号属性(numPr),用于继承自动编号
|
||||
orig_pPr = para._element.find(qn("w:pPr"))
|
||||
numPr = orig_pPr.find(qn("w:numPr")) if orig_pPr is not None else None
|
||||
|
||||
parent.remove(para._element)
|
||||
|
||||
for pd_data in reversed(paragraphs):
|
||||
@@ -93,15 +100,24 @@ def replace_placeholder(
|
||||
# 尝试应用样式,逐步降级
|
||||
applied = _apply_style(new_p, doc, style_name)
|
||||
if not applied and style_name.startswith("Heading"):
|
||||
# 标题样式找不到
|
||||
new_p.style = doc.styles["Normal"]
|
||||
elif not applied:
|
||||
# 正文样式找不到 → 尝试占位符自身的样式
|
||||
if placeholder_style:
|
||||
_apply_style(new_p, doc, placeholder_style)
|
||||
if new_p.style.name == "Normal" and placeholder_style:
|
||||
new_p.style = doc.styles[placeholder_style]
|
||||
|
||||
# 继承原段落的编号属性(自动编号)
|
||||
if numPr is not None:
|
||||
new_pPr = new_p._element.find(qn("w:pPr"))
|
||||
if new_pPr is None:
|
||||
new_pPr = new_p._element.makeelement(qn("w:pPr"), {})
|
||||
new_p._element.insert(0, new_pPr)
|
||||
existing = new_pPr.find(qn("w:numPr"))
|
||||
if existing is not None:
|
||||
new_pPr.remove(existing)
|
||||
new_pPr.append(deepcopy(numPr))
|
||||
|
||||
parent.insert(idx, new_p._element)
|
||||
break
|
||||
|
||||
|
||||
Reference in New Issue
Block a user