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:
@@ -12,16 +12,18 @@ from loaders.yaml_loader import load_yaml_file, validate_template_yaml
|
||||
class ResourceValidator:
|
||||
"""资源验证器"""
|
||||
|
||||
def __init__(self, yaml_dir: Path, template_dir: Path = None):
|
||||
def __init__(self, yaml_dir: Path, template_dir: Path = None, yaml_data: dict = None):
|
||||
"""
|
||||
初始化资源验证器
|
||||
|
||||
Args:
|
||||
yaml_dir: YAML 文件所在目录
|
||||
template_dir: 模板文件目录(可选)
|
||||
yaml_data: YAML 数据(用于检查内联模板)
|
||||
"""
|
||||
self.yaml_dir = yaml_dir
|
||||
self.template_dir = template_dir
|
||||
self.yaml_data = yaml_data or {}
|
||||
|
||||
def validate_image(self, element, slide_index: int, elem_index: int) -> list:
|
||||
"""
|
||||
@@ -84,7 +86,13 @@ class ResourceValidator:
|
||||
if not template_name:
|
||||
return issues
|
||||
|
||||
# 检查是否提供了模板目录
|
||||
# 检查是否为内联模板
|
||||
inline_templates = self.yaml_data.get("templates", {})
|
||||
if template_name in inline_templates:
|
||||
# 内联模板已在 validate_presentation_yaml 中验证,这里不需要额外验证
|
||||
return issues
|
||||
|
||||
# 检查是否提供了模板目录(外部模板)
|
||||
if not self.template_dir:
|
||||
issues.append(
|
||||
ValidationIssue(
|
||||
@@ -150,21 +158,27 @@ class ResourceValidator:
|
||||
if not template_name:
|
||||
return issues
|
||||
|
||||
if not self.template_dir:
|
||||
return issues
|
||||
# 检查是否为内联模板
|
||||
inline_templates = self.yaml_data.get("templates", {})
|
||||
if template_name in inline_templates:
|
||||
template_data = inline_templates[template_name]
|
||||
else:
|
||||
# 外部模板
|
||||
if not self.template_dir:
|
||||
return issues
|
||||
|
||||
template_path = self.template_dir / template_name
|
||||
if not template_path.suffix:
|
||||
template_path = template_path.with_suffix(".yaml")
|
||||
template_path = self.template_dir / template_name
|
||||
if not template_path.suffix:
|
||||
template_path = template_path.with_suffix(".yaml")
|
||||
|
||||
if not template_path.exists():
|
||||
return issues
|
||||
if not template_path.exists():
|
||||
return issues
|
||||
|
||||
try:
|
||||
template_data = load_yaml_file(template_path)
|
||||
validate_template_yaml(template_data, str(template_path))
|
||||
except Exception:
|
||||
return issues
|
||||
try:
|
||||
template_data = load_yaml_file(template_path)
|
||||
validate_template_yaml(template_data, str(template_path))
|
||||
except Exception:
|
||||
return issues
|
||||
|
||||
template_vars = template_data.get("vars", [])
|
||||
required_vars = [v["name"] for v in template_vars if v.get("required", False)]
|
||||
|
||||
Reference in New Issue
Block a user