-- This is a sample custom writer for pandoc. It produces output
-- that is very similar to that of pandoc's HTML writer.
-- There is one new feature: code blocks marked with class 'dot'
-- are piped through graphviz and images are included in the HTML
-- output using 'data:' URLs.
--
-- Invoke with: pandoc -t sample.lua
--
-- Note: you need not have lua installed on your system to use this
-- custom writer. However, if you do have lua installed, you can
-- use it to test changes to the script. 'lua sample.lua' will
-- produce informative error messages if your code contains
-- syntax errors.
-- Character escaping
local function escape(s, in_attribute)
return s:gsub("[<>&\"']",
function(x)
if x == '<' then
return '<'
elseif x == '>' then
return '>'
elseif x == '&' then
return '&'
elseif x == '"' then
return '"'
elseif x == "'" then
return '''
else
return x
end
end)
end
-- Helper function to convert an attributes table into
-- a string that can be put into HTML tags.
local function attributes(attr)
local attr_table = {}
for x,y in pairs(attr) do
if y and y ~= "" then
table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
end
end
return table.concat(attr_table)
end
-- Run cmd on a temporary file containing inp and return result.
local function pipe(cmd, inp)
local tmp = os.tmpname()
local tmph = io.open(tmp, "w")
tmph:write(inp)
tmph:close()
local outh = io.popen(cmd .. " " .. tmp,"r")
local result = outh:read("*all")
outh:close()
os.remove(tmp)
return result
end
-- Table to store footnotes, so they can be included at the end.
local notes = {}
-- Blocksep is used to separate block elements.
function Blocksep()
return "\n\n"
end
-- This function is called once for the whole document. Parameters:
-- body is a string, metadata is a table, variables is a table.
-- One could use some kind of templating
-- system here; this just gives you a simple standalone HTML file.
function Doc(body, metadata, variables)
local buffer = {}
for k, v in pairs(metadata) do
if type(v) == "string" then
table.insert(buffer, string.upper(k) .. ': ' .. v)
else
table.insert(buffer, string.upper(k) .. ': ' .. table.concat(v))
end
end
return table.concat(buffer, "\n")
end
-- The functions that follow render corresponding pandoc elements.
-- s is always a string, attr is always a table of attributes, and
-- items is always an array of strings (the items in a list).
-- Comments indicate the types of other variables.
function Str(s)
return escape(s)
end
function Space()
return " "
end
function LineBreak()
return "
"
end
function Emph(s)
return "" .. s .. ""
end
function Strong(s)
return "" .. s .. ""
end
function Subscript(s)
return "" .. s .. ""
end
function Superscript(s)
return "" .. s .. ""
end
function SmallCaps(s)
return '' .. s .. ''
end
function Strikeout(s)
return '' .. s .. ''
end
function Link(s, src, tit)
return "" .. s .. ""
end
function Image(s, src, tit)
return ""
end
function Code(s, attr)
return "" .. escape(s) .. "
"
end
function InlineMath(s)
return "\\(" .. escape(s) .. "\\)"
end
function DisplayMath(s)
return "\\[" .. escape(s) .. "\\]"
end
function Note(s)
local num = #notes + 1
-- insert the back reference right before the final closing tag.
s = string.gsub(s,
'(.*)', '%1 ↩')
-- add a list item with the note to the note table.
table.insert(notes, '
" .. s .. "
" end -- lev is an integer, the header level. function Header(lev, s, attr) return "\n" .. s .. "\n" end function HorizontalRule() return "
" .. escape(s) ..
"
"
end
end
function BulletList(items)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "' .. h .. ' | ') empty_header = empty_header and h == "" end if empty_header then head = "" else add('
---|
' .. c .. ' | ') end add('