恢复Pandoc经典自定义编写器tba.lua时的模板配置问题
解决Pandoc经典自定义编写器
Template()函数返回值错误问题 问题出在经典自定义编写器与现代Pandoc Lua API的差异上:经典编写器要求Template()函数返回原始模板字符串,但你调用的pandoc.template.compile()返回的是Pandoc内部编译后的Template对象,不符合经典编写器的API约定,因此报错。
修正方案
直接读取默认模板的原始文本内容返回即可,不需要提前编译。修改后的Template()函数如下:
function Template () -- 获取默认模板的路径 local template_path = pandoc.template.default 'tba.lua' -- 打开并读取模板文件的全部内容 local template_file = io.open(template_path, 'r') if not template_file then error("无法找到tba.lua的默认模板文件") end local template_content = template_file:read('*a') template_file:close() -- 返回原始模板字符串 return template_content end
原因说明
Pandoc的经典自定义编写器(也就是你要恢复的tba.lua这类)是基于旧版API设计的,它会自行处理模板的编译逻辑,因此要求Template()函数返回未处理的模板文本。而pandoc.template.compile()是为现代Lua编写器设计的API,返回的是编译后的模板实例,和经典编写器的要求不兼容。
内容的提问来源于stack exchange,提问作者lodwicz
相关产品推荐
相关产品推荐

