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

@@ -132,7 +132,7 @@ ERROR_TEMPLATE = """
app = None
change_queue = None
current_yaml_file = None
current_template_dir = None
current_template_file = None
class YAMLChangeHandler:
@@ -144,10 +144,10 @@ class YAMLChangeHandler:
change_queue.put('reload')
def generate_preview_html(yaml_file, template_dir):
def generate_preview_html(yaml_file, template_file):
"""生成完整的预览 HTML 页面"""
try:
pres = Presentation(yaml_file, template_dir)
pres = Presentation(yaml_file, template_file)
renderer = HtmlRenderer()
slides_html = ""
@@ -169,7 +169,7 @@ def create_flask_app():
def index():
"""主页面"""
try:
return generate_preview_html(current_yaml_file, current_template_dir)
return generate_preview_html(current_yaml_file, current_template_file)
except Exception as e:
return ERROR_TEMPLATE.replace('{{ error }}', f"生成预览失败: {str(e)}")
@@ -186,17 +186,17 @@ def create_flask_app():
return flask_app
def start_preview_server(yaml_file, template_dir, port, host='127.0.0.1', open_browser=True):
def start_preview_server(yaml_file, template_file, port, host='127.0.0.1', open_browser=True):
"""启动预览服务器
Args:
yaml_file: YAML 文件路径
template_dir: 模板目录路径
template_file: 模板库文件路径
port: 服务器端口
host: 主机地址默认127.0.0.1
open_browser: 是否自动打开浏览器默认True
"""
global app, change_queue, current_yaml_file, current_template_dir
global app, change_queue, current_yaml_file, current_template_file
if Flask is None:
log_error("预览功能需要 flask 和 watchdog 依赖")
@@ -204,7 +204,7 @@ def start_preview_server(yaml_file, template_dir, port, host='127.0.0.1', open_b
sys.exit(1)
current_yaml_file = yaml_file
current_template_dir = template_dir
current_template_file = template_file
change_queue = queue.Queue()
# 创建 Flask 应用