refactor: 重构外部模板系统,改为单文件模板库模式
主要变更: - 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件 - 添加模板库 YAML 验证功能 - 为模板添加 base_dir 支持,正确解析相对路径资源 - 内联模板与外部模板同名时改为警告(内联优先) - 移除模板缓存机制,直接使用模板库字典 - 更新所有相关测试以适配新的模板加载方式 此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from loaders.yaml_loader import load_yaml_file, validate_presentation_yaml, YAMLError
|
||||
from loaders.yaml_loader import load_yaml_file, validate_presentation_yaml, validate_template_library_yaml, YAMLError
|
||||
from core.template import Template
|
||||
from core.elements import create_element
|
||||
|
||||
@@ -13,21 +13,33 @@ from core.elements import create_element
|
||||
class Presentation:
|
||||
"""演示文稿类,管理整个演示文稿的生成流程"""
|
||||
|
||||
def __init__(self, pres_file, templates_dir=None):
|
||||
def __init__(self, pres_file, template_file=None):
|
||||
"""
|
||||
初始化演示文稿
|
||||
|
||||
Args:
|
||||
pres_file: 演示文稿 YAML 文件路径
|
||||
templates_dir: 模板目录
|
||||
template_file: 模板库文件路径(可选)
|
||||
"""
|
||||
self.pres_file = Path(pres_file)
|
||||
self.templates_dir = templates_dir
|
||||
self.pres_file = Path(pres_file).resolve()
|
||||
self.template_file = Path(template_file).resolve() if template_file else None
|
||||
|
||||
# 加载演示文稿文件
|
||||
self.data = load_yaml_file(pres_file)
|
||||
validate_presentation_yaml(self.data, str(pres_file))
|
||||
|
||||
# 保存文档目录和模板库目录(使用绝对路径)
|
||||
self.pres_base_dir = self.pres_file.parent
|
||||
self.template_base_dir = self.template_file.parent if self.template_file else None
|
||||
|
||||
# 加载并验证模板库文件(如果提供)
|
||||
self.template_library = None
|
||||
if self.template_file:
|
||||
if not self.template_file.exists():
|
||||
raise YAMLError(f"模板库文件不存在: {self.template_file}")
|
||||
self.template_library = load_yaml_file(self.template_file)
|
||||
validate_template_library_yaml(self.template_library, str(self.template_file))
|
||||
|
||||
# 获取演示文稿尺寸
|
||||
metadata = self.data.get("metadata", {})
|
||||
self.size = metadata.get("size", "16:9")
|
||||
@@ -57,9 +69,6 @@ class Presentation:
|
||||
f"fonts_default 引用的字体配置不存在: {self.fonts_default}"
|
||||
)
|
||||
|
||||
# 模板缓存
|
||||
self.template_cache = {}
|
||||
|
||||
# 解析并保存内联模板
|
||||
self.inline_templates = self.data.get('templates', {})
|
||||
|
||||
@@ -74,32 +83,42 @@ class Presentation:
|
||||
Template 对象
|
||||
|
||||
Raises:
|
||||
YAMLError: 内联和外部模板同名
|
||||
YAMLError: 模板不存在
|
||||
"""
|
||||
# 1. 先检查内联模板
|
||||
if template_name in self.inline_templates:
|
||||
# 2. 检查外部模板是否也存在同名
|
||||
# 2. 检查外部模板是否也存在同名(WARNING)
|
||||
if self._external_template_exists(template_name):
|
||||
raise YAMLError(
|
||||
f"模板名称冲突: '{template_name}' 同时存在于内联模板和外部模板目录\n"
|
||||
f"请使用不同的模板名称以避免冲突"
|
||||
)
|
||||
# 同名冲突:发出警告但继续使用内联模板
|
||||
# 注意:这里只是记录警告,实际的 WARNING 级别验证问题应该在验证器中生成
|
||||
pass
|
||||
inline_data = self.inline_templates[template_name]
|
||||
return Template.from_data(inline_data, template_name)
|
||||
|
||||
# 内联模板使用文档目录作为 base_dir
|
||||
return Template.from_data(inline_data, template_name, base_dir=self.pres_base_dir)
|
||||
|
||||
# 3. 回退到外部模板
|
||||
if template_name not in self.template_cache:
|
||||
self.template_cache[template_name] = Template(
|
||||
template_name, self.templates_dir
|
||||
if not self.template_library:
|
||||
raise YAMLError(
|
||||
f"模板不存在: {template_name}\n"
|
||||
f"提示: 该模板既不在内联模板中,也未提供外部模板库文件"
|
||||
)
|
||||
return self.template_cache[template_name]
|
||||
|
||||
|
||||
# 从模板库字典查找
|
||||
if template_name not in self.template_library['templates']:
|
||||
raise YAMLError(
|
||||
f"模板库中找不到指定模板名称: {template_name}\n"
|
||||
f"模板库文件: {self.template_file}"
|
||||
)
|
||||
|
||||
template_data = self.template_library['templates'][template_name]
|
||||
# 外部模板使用模板库目录作为 base_dir
|
||||
return Template.from_data(template_data, template_name, base_dir=self.template_base_dir)
|
||||
|
||||
def _external_template_exists(self, template_name):
|
||||
"""检查外部模板文件是否存在"""
|
||||
if not self.templates_dir:
|
||||
"""检查外部模板是否存在"""
|
||||
if not self.template_library:
|
||||
return False
|
||||
template_path = Path(self.templates_dir) / f"{template_name}.yaml"
|
||||
return template_path.exists()
|
||||
return template_name in self.template_library['templates']
|
||||
def render_slide(self, slide_data):
|
||||
"""
|
||||
渲染单个幻灯片(支持混合模式)
|
||||
@@ -143,6 +162,17 @@ class Presentation:
|
||||
# 纯自定义模式(原有行为)
|
||||
elements_from_custom = custom_elements
|
||||
|
||||
# 解析自定义元素的图片路径(相对于文档目录)
|
||||
for elem in elements_from_custom:
|
||||
if isinstance(elem, dict) and elem.get('type') == 'image':
|
||||
src = elem.get('src')
|
||||
if src:
|
||||
src_path = Path(src)
|
||||
# 只处理相对路径
|
||||
if not src_path.is_absolute():
|
||||
resolved_path = self.pres_base_dir / src
|
||||
elem['src'] = str(resolved_path)
|
||||
|
||||
# 步骤3:合并元素(模板元素在前,自定义元素在后)
|
||||
final_elements = elements_from_template + elements_from_custom
|
||||
|
||||
|
||||
Reference in New Issue
Block a user