1
0

refactor: 重构外部模板系统,改为单文件模板库模式

主要变更:
- 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件
- 添加模板库 YAML 验证功能
- 为模板添加 base_dir 支持,正确解析相对路径资源
- 内联模板与外部模板同名时改为警告(内联优先)
- 移除模板缓存机制,直接使用模板库字典
- 更新所有相关测试以适配新的模板加载方式

此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
2026-03-05 13:26:29 +08:00
parent bd12fce14b
commit f1aae96a04
27 changed files with 2141 additions and 1988 deletions

View File

@@ -95,14 +95,38 @@ elements:
align: center
"""
TEMPLATE_LIBRARY_YAML = """templates:
title-slide:
vars:
- name: title
required: true
- name: subtitle
required: false
default: ""
elements:
- type: text
box: [1, 2, 8, 1]
content: "{title}"
font:
size: 44
bold: true
align: center
- type: text
box: [1, 3.5, 8, 0.5]
content: "{subtitle}"
visible: "{subtitle != ''}"
font:
size: 24
align: center
"""
@pytest.fixture
def sample_template(temp_dir):
"""创建测试模板目录和文件"""
template_dir = temp_dir / "templates"
template_dir.mkdir()
(template_dir / "title-slide.yaml").write_text(TEMPLATE_YAML, encoding="utf-8")
return template_dir
"""创建测试模板文件"""
template_file = temp_dir / "templates.yaml"
template_file.write_text(TEMPLATE_LIBRARY_YAML, encoding="utf-8")
return template_file
@pytest.fixture
@@ -156,65 +180,61 @@ def slide_size(request):
def complex_template(temp_dir):
"""创建复杂模板(包含多个变量和条件)"""
template_content = """
vars:
- name: title
required: true
- name: subtitle
required: false
default: ""
- name: author
required: false
default: ""
- name: date
required: false
default: ""
elements:
- type: shape
box: [0, 0, 10, 5.625]
shape: rectangle
fill: "#2c3e50"
- type: text
box: [1, 1.5, 8, 1]
content: "{title}"
font:
size: 44
bold: true
color: "#ffffff"
align: center
- type: text
box: [1, 2.8, 8, 0.6]
content: "{subtitle}"
visible: "{subtitle != ''}"
font:
size: 24
color: "#ecf0f1"
align: center
- type: text
box: [1, 4, 8, 0.5]
content: "{author}"
visible: "{author != ''}"
font:
size: 18
color: "#bdc3c7"
align: center
- type: text
box: [1, 4.8, 8, 0.4]
content: "{date}"
visible: "{date != ''}"
font:
size: 14
color: "#95a5a6"
align: center
templates:
complex-slide:
vars:
- name: title
required: true
- name: subtitle
required: false
default: ""
- name: author
required: false
default: ""
- name: date
required: false
default: ""
elements:
- type: shape
box: [0, 0, 10, 5.625]
shape: rectangle
fill: "#2c3e50"
- type: text
box: [1, 1.5, 8, 1]
content: "{title}"
font:
size: 44
bold: true
color: "#ffffff"
align: center
- type: text
box: [1, 2.8, 8, 0.6]
content: "{subtitle}"
visible: "{subtitle != ''}"
font:
size: 24
color: "#ecf0f1"
align: center
- type: text
box: [1, 4, 8, 0.5]
content: "{author}"
visible: "{author != ''}"
font:
size: 18
color: "#bdc3c7"
align: center
- type: text
box: [1, 4.8, 8, 0.4]
content: "{date}"
visible: "{date != ''}"
font:
size: 14
color: "#95a5a6"
align: center
"""
template_dir = temp_dir / "templates"
template_dir.mkdir()
(template_dir / "complex-slide.yaml").write_text(template_content)
return template_dir
template_file = temp_dir / "complex_templates.yaml"
template_file.write_text(template_content)
return template_file
@pytest.fixture