Reputation: 18487
My goal is to input different text in the template based on a single variable in the yaml.
Below is a minimal attempt, but I can't get it to work.
I'm looking for a Lua filter that set the variable $selected$
based on the value of $switch$
.
In practice, I'll set several template variables based on that variable.
The idea is to have one more generic template instead of many templates with relative few differences.
pandoc index.md --to html --from markdown --output index.html --template template.html --lua-filter=filter.lua
file 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>
file 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
}
}
desired output
<html>
<title>test</title>
<body>
<h1>first choise</h1>
<h2>a</h2>
<p>Some text</p>
</body>
</html>
the result I get
<html>
<title>alternative</title>
<body>
<h1></h1>
<h2>a</h2>
<p>Some text</p>
</body>
</html>
Upvotes: 4
Views: 650
Reputation: 22609
The issue here is that metadata values look like strings, but can be of some other type. Here, they are Inlines, as can be checked with this filter:
function Meta (meta)
print(pandoc.utils.type(meta.switch))
end
The easiest solution is to convert the value to a string with pandoc.utils.stringify
:
Meta = function(meta)
meta.selected = choose(pandoc.utils.stringify(meta.switch))
return meta
end
The filter should work as expected now.
Upvotes: 3