Do you know that you can use Pandoc itself as a YAML parser?
Some things to look out for:
- Pandoc parses YAML scalar values (but not keys) as Pandoc markdown and returns a tree of objects. To get a "plain" data structure out of that you have to run the `meta` part of the Pandoc object through a function along the lines of
````lua
local function meta2data (x)
if 'table' == type(x) then
local t = x.t -- tag == element type
if not t or 'MetaMap' == t or 'MetaList' == t then
-- turn into a plain data table
local d = {}
for k,v in pairs(x) do
d[k] = meta2data(v) -- call recursively
end
return d
elseif t then
-- probably text, so stringify it
return pandoc.utils.stringify(x)
end
else
-- something else, probably a string or boolean
return x
end
end
local data = meta2data(doc.meta)
-- or less expensively:
local some_data = meta2data(doc.meta.some_field)
````
NOTE: I'm "quoting" the meta2data function from brain memory so some detail covering some edge case may be missing, but it's the general idea.
- The restringification of the MetaBlocks and MetaInlines elements (or rather their contents) removes everything which Pandoc considers formatting, so you, or your users, have to wrap any literal strings in the YAML where whitespace and/or punctuation is important in Pandoc code elements, and these need in turn to be wrapped in YAML quoted strings since YAML reserves the backtick for future use as a metacharacter:
````````yaml
foo: '`2**10`'
bar: |
````
No Markdown parsing here
````
````````
- If you read in YAML from a file you have to make sure that it starts and ends with appropriate YAML block delimiters so that Pandoc recognises it as "metadata"
````lua
if not yaml:match('^%-%-%-%f[^%-]') then
yaml = "---\n" .. yaml
end
if not yaml:match('\n([%.%-])%1%1%s*$') then
yaml = yaml .. "\n...\n"
end
````
This looks complicated but the first two points are generally what you have to do/observe anyway if you configure your filter through metadata, and having Pandoc parse the YAML using Haskell is probably still more efficient and robust than using a pure Lua YAML parser. (I've taken a stab at reimplementing [YAML::Tiny](
https://metacpan.org/pod/YAML::Tiny) in Lua and it isn't easy!)