refactor(config): 重构配置类以支持动态元数据字段

配置类 ThesisConfig 现在使用 metadata 字典直接透传 TOML 配置,
无需为每个变量单独声明字段。新增模板变量只需修改 TOML 文件,
无需修改 Python 代码。

BREAKING CHANGE: 配置文件结构发生改变,从单独字段改为统一的
metadata 节点。
This commit is contained in:
zzy
2026-05-08 23:07:23 +08:00
parent 74d28ea2d8
commit fc6afdea9d
3 changed files with 24 additions and 43 deletions

View File

@@ -15,7 +15,8 @@ from .body import body_to_paragraphs, replace_placeholder
from .references import references_to_paragraphs
_TEXT_FIELDS = [
# 解析器可能产生的字段(用于填充报告)
_PARSER_FIELDS = [
"title",
"abstact_cn_context",
"abstract_cn_keywords",
@@ -24,13 +25,7 @@ _TEXT_FIELDS = [
"acknowledgement",
"reference",
"appendix",
"student_name",
"student_id",
"college",
"major",
"class",
"advisor",
"advisor_title",
"body_md",
]
@@ -81,12 +76,11 @@ def generate_thesis(
body_end_kw=config.body_end_keywords,
)
# 3. 合并配置 → 上下文(配置优先
# 3. 合并配置 → 上下文(配置填充解析器未产生的空白
for k, v in config.to_dict().items():
if k == "title" and config.title_from_md and context.get("title"):
continue # 以 markdown 标题为准
if v != "<None>":
context[k] = v
context.setdefault(k, v)
# 4. 用 defaultdict 兜底缺失键
ctx = defaultdict(lambda: "<None>", context)
@@ -129,17 +123,18 @@ def generate_thesis(
print(f"[完成] 论文生成完成: {output_path}")
# 10. 字段填充报告
# 10. 字段填充报告(动态收集所有模板与解析字段)
report_fields = list(dict.fromkeys([*config.metadata.keys(), *_PARSER_FIELDS]))
print("\n--- 字段填充情况 ---")
for key in _TEXT_FIELDS:
val = ctx[key]
for key in report_fields:
val = ctx.get(key, "<None>")
if val == "<None>":
print(f" [缺失] {key}")
else:
preview = str(val)[:60].replace("\n", " ")
print(f" [OK] {key}: {preview}...")
missing = [k for k in _TEXT_FIELDS if ctx[k] == "<None>"]
missing = [k for k in report_fields if ctx.get(k, "<None>") == "<None>"]
if missing:
print("\n[警告] 以下字段缺失,已填充 '<None>'")
for f in missing: