Skip to content

Instantly share code, notes, and snippets.

@tarleb
Created May 22, 2020 19:14
Show Gist options
  • Select an option

  • Save tarleb/7df9d5663457c0ea82e8444c71f14064 to your computer and use it in GitHub Desktop.

Select an option

Save tarleb/7df9d5663457c0ea82e8444c71f14064 to your computer and use it in GitHub Desktop.
local yaml_template = "---\nDUMMY: %s\n---"
function evaluate (str)
local tag, value = str:match('^:(%a*):(.*)')
if tag == 'lua' then
return load(
'return ' .. value,
"evaluating " .. value
)()
elseif tag == 'json' then
return pandoc.read(yaml_template:format(value)).meta['DUMMY']
end
return str
end
function Meta (meta)
for key, value in pairs(meta) do
if type(value) == 'string' then
meta[key] = evaluate(value)
end
end
return meta
end
@tarleb
Copy link
Copy Markdown
Author

tarleb commented May 22, 2020

Lua filter which evaluates specially marked metadata that has been passed through the command line. It allows to use either JSON or Lua.

Example:

pandoc --lua-filter=eval-metadata.lua --to=markdown -s \
    --metadata lua-test=':lua:{one="Hello",two={pandoc.Strong"World!"}}' \
    --metadata json-test=':json:["*emphasized*", "`code`"]' \
    <<< test

Result:

---
json-test:
- '*emphasised*'
- '`code`'
lua-test:
  one: Hello
  two: '**World**'
---

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment