我的目标是根据yaml文件中的单个变量,在模板中输入不同的文本。
以下是我最初的尝试,但我无法使其工作。
我正在寻找一个Lua过滤器,它可以根据
我得到的结果
$switch$
的值设置变量$selected$
。
实际上,我将基于该变量设置几个模板变量。
这个想法是拥有一个更通用的模板,而不是许多只有相对少量差异的模板。
代码如下:pandoc index.md --to html --from markdown --output index.html --template template.html --lua-filter=filter.lua
文件名为:index.md
---
title: "test"
switch: "a"
---
Some text
file template.html
<html>
<title>$title$</title>
<body>
<h1>$selected$</h1>
<h2>$switch$</h2>
$body$
</body>
</html>
文件 filter.lua
local function choose(info)
local result
if (info == "a")
then
result = "first choise"
else
result = "alternative"
end
return result
end
return {
{
Meta = function(meta)
meta.title, meta.selected = choose(meta.switch)
return meta
end
}
}
期望的输出结果
<html>
<title>test</title>
<body>
<h1>first choise</h1>
<h2>a</h2>
<p>Some text</p>
</body>
</html>
我得到的结果
<html>
<title>alternative</title>
<body>
<h1></h1>
<h2>a</h2>
<p>Some text</p>
</body>
</html>