feat: 添加内联模板支持
支持在 YAML 源文件中直接定义模板,无需单独的模板文件。 简化单文档编写流程,降低模板系统使用门槛。 核心功能: - 在 YAML 顶层新增 templates 字段定义内联模板 - 支持变量替换、条件渲染、默认值等完整模板功能 - 内联模板优先于外部模板查找 - 同名冲突检测:禁止内联和外部模板同名 - 相互引用检测:禁止内联模板之间相互引用 - 完整的错误处理和验证机制 代码变更: - core/template.py: 新增 from_data() 类方法 - core/presentation.py: 支持内联模板查找和冲突检测 - loaders/yaml_loader.py: 新增 validate_templates_yaml() 验证 - validators/: 扩展验证器支持内联模板 测试: - 新增 9 个内联模板专项测试 - 修复 1 个变量验证测试 - 所有 333 个测试通过 文档: - README.md: 添加内联模板使用指南和最佳实践 - README_DEV.md: 说明实现细节和设计决策 完全向后兼容,不使用 templates 字段时行为不变。
This commit is contained in:
@@ -78,6 +78,8 @@ def validate_presentation_yaml(data, file_path=""):
|
||||
if not isinstance(data['slides'], list):
|
||||
raise YAMLError(f"{file_path}: 'slides' 必须是一个列表")
|
||||
|
||||
# 验证 templates 字段(内联模板)
|
||||
validate_templates_yaml(data, file_path)
|
||||
|
||||
def validate_template_yaml(data, file_path=""):
|
||||
"""
|
||||
@@ -111,3 +113,50 @@ def validate_template_yaml(data, file_path=""):
|
||||
|
||||
if not isinstance(data['elements'], list):
|
||||
raise YAMLError(f"{file_path}: 'elements' 必须是一个列表")
|
||||
|
||||
|
||||
def validate_templates_yaml(data, file_path=""):
|
||||
"""
|
||||
验证 templates 字段结构(内联模板)
|
||||
|
||||
Args:
|
||||
data: 解析后的 YAML 数据
|
||||
file_path: 文件路径(用于错误消息)
|
||||
|
||||
Raises:
|
||||
YAMLError: 结构验证失败
|
||||
"""
|
||||
# 验证 templates 字段是否为字典
|
||||
if 'templates' in data:
|
||||
if not isinstance(data['templates'], dict):
|
||||
raise YAMLError(f"{file_path}: 'templates' 必须是一个字典")
|
||||
|
||||
# 验证每个内联模板的结构
|
||||
for template_name, template_data in data['templates'].items():
|
||||
# 构建模板位置路径
|
||||
template_location = f"{file_path}.templates.{template_name}"
|
||||
|
||||
# 验证模板是字典
|
||||
if not isinstance(template_data, dict):
|
||||
raise YAMLError(f"{template_location}: 模板定义必须是字典")
|
||||
|
||||
# 验证必需的 elements 字段
|
||||
if 'elements' not in template_data:
|
||||
raise YAMLError(f"{template_location}: 缺少必需字段 'elements'")
|
||||
|
||||
if not isinstance(template_data['elements'], list):
|
||||
raise YAMLError(f"{template_location}: 'elements' 必须是一个列表")
|
||||
|
||||
# 验证可选的 vars 字段
|
||||
if 'vars' in template_data:
|
||||
if not isinstance(template_data['vars'], list):
|
||||
raise YAMLError(f"{template_location}: 'vars' 必须是一个列表")
|
||||
|
||||
# 验证每个变量定义
|
||||
for i, var_def in enumerate(template_data['vars']):
|
||||
if not isinstance(var_def, dict):
|
||||
raise YAMLError(f"{template_location}.vars[{i}]: 变量定义必须是字典")
|
||||
|
||||
# 验证必需的 name 字段
|
||||
if 'name' not in var_def:
|
||||
raise YAMLError(f"{template_location}.vars[{i}]: 缺少必需字段 'name'")
|
||||
|
||||
Reference in New Issue
Block a user