refactor: 重构外部模板系统,改为单文件模板库模式
主要变更: - 将 templates_dir 参数改为 template_file,支持单个模板库 YAML 文件 - 添加模板库 YAML 验证功能 - 为模板添加 base_dir 支持,正确解析相对路径资源 - 内联模板与外部模板同名时改为警告(内联优先) - 移除模板缓存机制,直接使用模板库字典 - 更新所有相关测试以适配新的模板加载方式 此重构简化了模板管理,使模板资源的路径解析更加清晰明确。
This commit is contained in:
28
yaml2pptx.py
28
yaml2pptx.py
@@ -39,20 +39,20 @@ def parse_args():
|
||||
# check 子命令
|
||||
check_parser = subparsers.add_parser('check', help='验证 YAML 文件')
|
||||
check_parser.add_argument("input", type=str, help="输入的 YAML 文件路径")
|
||||
check_parser.add_argument("--template-dir", type=str, default=None, help="模板文件目录路径")
|
||||
check_parser.add_argument("--template", type=str, default=None, help="模板库文件路径")
|
||||
|
||||
# convert 子命令
|
||||
convert_parser = subparsers.add_parser('convert', help='转换 YAML 为 PPTX')
|
||||
convert_parser.add_argument("input", type=str, help="输入的 YAML 文件路径")
|
||||
convert_parser.add_argument("output", type=str, nargs="?", help="输出的 PPTX 文件路径(可选,默认为输入文件名.pptx)")
|
||||
convert_parser.add_argument("--template-dir", type=str, default=None, help="模板文件目录路径")
|
||||
convert_parser.add_argument("--template", type=str, default=None, help="模板库文件路径")
|
||||
convert_parser.add_argument("--skip-validation", action="store_true", help="跳过自动验证")
|
||||
convert_parser.add_argument("-f", "--force", action="store_true", help="强制覆盖已存在文件")
|
||||
|
||||
# preview 子命令
|
||||
preview_parser = subparsers.add_parser('preview', help='启动预览服务器')
|
||||
preview_parser.add_argument("input", type=str, help="输入的 YAML 文件路径")
|
||||
preview_parser.add_argument("--template-dir", type=str, default=None, help="模板文件目录路径")
|
||||
preview_parser.add_argument("--template", type=str, default=None, help="模板库文件路径")
|
||||
preview_parser.add_argument("--port", type=int, default=None, help="服务器端口(默认:随机端口 30000-40000)")
|
||||
preview_parser.add_argument("--host", type=str, default="127.0.0.1", help="主机地址(默认:127.0.0.1)")
|
||||
preview_parser.add_argument("--no-browser", action="store_true", help="不自动打开浏览器")
|
||||
@@ -65,14 +65,14 @@ def handle_check(args):
|
||||
try:
|
||||
input_path = Path(args.input)
|
||||
|
||||
# 处理模板目录
|
||||
template_dir = None
|
||||
if args.template_dir:
|
||||
template_dir = Path(args.template_dir)
|
||||
# 处理模板库文件
|
||||
template_file = None
|
||||
if args.template:
|
||||
template_file = Path(args.template)
|
||||
|
||||
# 执行验证
|
||||
validator = Validator()
|
||||
result = validator.validate(input_path, template_dir)
|
||||
result = validator.validate(input_path, template_file)
|
||||
|
||||
# 输出验证结果
|
||||
print(result.format_output())
|
||||
@@ -103,13 +103,13 @@ def handle_preview(args):
|
||||
if port is None:
|
||||
port = random.randint(30000, 40000)
|
||||
|
||||
# 处理模板目录
|
||||
template_dir = Path(args.template_dir) if args.template_dir else None
|
||||
# 处理模板库文件
|
||||
template_file = Path(args.template) if args.template else None
|
||||
|
||||
# 启动预览服务器
|
||||
start_preview_server(
|
||||
yaml_file=input_path,
|
||||
template_dir=template_dir,
|
||||
template_file=template_file,
|
||||
port=port,
|
||||
host=args.host,
|
||||
open_browser=not args.no_browser
|
||||
@@ -173,9 +173,9 @@ def handle_convert(args):
|
||||
# 自动验证(除非使用 --skip-validation)
|
||||
if not args.skip_validation:
|
||||
log_info("验证 YAML 文件...")
|
||||
template_dir = Path(args.template_dir) if args.template_dir else None
|
||||
template_file = Path(args.template) if args.template else None
|
||||
validator = Validator()
|
||||
result = validator.validate(input_path, template_dir)
|
||||
result = validator.validate(input_path, template_file)
|
||||
|
||||
# 如果有错误,输出并终止
|
||||
if result.has_errors():
|
||||
@@ -190,7 +190,7 @@ def handle_convert(args):
|
||||
|
||||
# 1. 加载演示文稿
|
||||
log_info("加载演示文稿...")
|
||||
pres = Presentation(input_path, templates_dir=args.template_dir)
|
||||
pres = Presentation(input_path, template_file=args.template)
|
||||
|
||||
# 2. 创建 PPTX 生成器
|
||||
log_info(f"创建演示文稿 ({pres.size})...")
|
||||
|
||||
Reference in New Issue
Block a user