Skip to content

Instantly share code, notes, and snippets.

@tarleb
Created October 8, 2020 19:50
Show Gist options
  • Save tarleb/891492f21772a3c5eef63a9a7fcb35eb to your computer and use it in GitHub Desktop.
Save tarleb/891492f21772a3c5eef63a9a7fcb35eb to your computer and use it in GitHub Desktop.
local List = require 'pandoc.List'
local meta
function merge_meta(m1, m2)
local result = m1
for k, v in pairs(m2) do
if result[k] == nil then
result[k] = v
end
end
return result
end
--- Filter function for code blocks
function include (cb)
-- ignore code blocks which are not of class "include".
if not cb.classes:includes 'include' then
return
end
-- Markdown is used if this is nil.
local format = cb.attributes['format']
local blocks = List:new()
for line in cb.text:gmatch('[^\n]+') do
if line:sub(1,2) ~= '//' then
local fh = io.open(line)
if not fh then
io.stderr:write("Cannot open file " .. line .. " | Skipping includes\n")
else
local contents = pandoc.read(fh:read '*a', format)
blocks:extend(contents.blocks)
meta = merge_meta(meta, contents.meta)
fh:close()
end
end
end
return blocks
end
return {
{Meta = function (m) meta = m end},
{CodeBlock = include},
{Meta = function (_) return meta end},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment