英文:
How can you take Quarto YAML metadata and use it as a value in Lua filter?
问题
我目前正在尝试编写一个Quarto的扩展,要求使其可以通过YAML中的选项进行自定义,而不是让用户直接编辑Lua脚本。
以下是我迄今为止拥有的Lua过滤器,但我无论如何都无法获得可工作的代码,以替换颜色和样式的固定值的元数据输入。我尝试了在线找到的各种方法来包含元数据,但都不能正常工作。我确信这足够简单,但如果有人能指导我正确的方向,我将不胜感激。
function HorizontalRule (elem)
if FORMAT == 'docx' then
local color = "0000FF"
local style = "double"
local horizontallinerule = string.format([[
<w:p>
<w:pPr>
<w:pStyle w:val="HorizontalRule"/>
<w:ind w:firstLine="0"/>
<w:jc w:val="center"/>
<w:pBdr>
<w:bottom w:val="%s" w:color="%s"/>
</w:pBdr>
</w:pPr>
<w:r>
<w:t></w:t>
</w:r>
</w:p>]], style, color)
return pandoc.RawBlock('openxml', horizontallinerule)
end
end
如果需要完整的示例,请查看GitHub上项目的测试分支这里。
英文:
I am currently trying to write an extension for Quarto, and I've been asked to make it customizable using options in the YAML rather than having users edit the Lua script directly
Here is the lua filter I have so far, but I cannot for the life of me get working code to have metadata inputs to replace the fixed values for color and style. I've tried a variety of ways I found online of including meta, but cannot get any to work. I'm sure it's simple enough, but if someone could point me in the right direction it would be greatly appreciated.
function HorizontalRule (elem)
if FORMAT == 'docx' then
local color = "0000FF"
local style = "double"
local horizontallinerule = string.format([[<w:p>
<w:pPr>
<w:pStyle w:val="HorizontalRule"/>
<w:ind w:firstLine="0"/>
<w:jc w:val="center"/>
<w:pBdr>
<w:bottom w:val="%s" w:color="%s"/>
</w:pBdr>
</w:pPr>
<w:r>
<w:t></w:t>
</w:r>
</w:p>]], style, color)
return pandoc.RawBlock('openxml', horizontallinerule)
end
end
If a full repex is useful, see my test branch of the project on GitHub here
答案1
得分: 2
最简单的方法可能是过滤主 Pandoc
文档。该元素包含元数据和实际文档。这里是一个示例:
local function make_hr_handler (remove_hr)
return function (el)
if remove_hr then
return {}
end
end
end
function Pandoc (doc)
local remove_horizontal_rules = doc.meta['remove-horizontal-rules']
return doc:walk {
HorizontalRule = make_hr_handler(remove_horizontal_rules),
}
end
请注意,make_hr_handler
返回一个函数,因此过滤函数是动态生成的。doc:walk
方法接受一个过滤函数的表格;它基本上与定义全局(顶级)过滤函数的方式相同。
相同的效果可以通过两个函数共享的变量来实现,但我觉得上述方法更容易理解。
英文:
The easiest way is probably to filter the main Pandoc
document. That element contains both the metadata and the actual document. Here's an example:
local function make_hr_handler (remove_hr)
return function (el)
if remove_hr then
return {}
end
end
end
function Pandoc (doc)
local remove_horizontal_rules = doc.meta['remove-horizontal-rules']
return doc:walk {
HorizontalRule = make_hr_handler(remove_horizontal_rules),
}
end
Note that make_hr_handler
returns a function, so the filter function is generated dynamically. The doc:walk
method takes a table of filter functions; it basically works the same as defining global (top-level) filter functions.
The same effect could be achieved with variables shared by both functions, but I find the above easier.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论