Skip to content

Instantly share code, notes, and snippets.

@takada-at
Created June 8, 2015 15:17
Show Gist options
  • Save takada-at/e974fb7a23800cb4e448 to your computer and use it in GitHub Desktop.
Save takada-at/e974fb7a23800cb4e448 to your computer and use it in GitHub Desktop.
-- This is a sample custom writer for pandoc.
-- hatena
-- Character escaping
local function escape(s, in_attribute)
return s:gsub("[<>&\"']",
function(x)
if x == '<' then
return '&lt;'
elseif x == '>' then
return '&gt;'
elseif x == '&' then
return '&amp;'
elseif x == '"' then
return '&quot;'
elseif x == "'" then
return '&#39;'
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
-- 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.
-- This gives you a fragment. You could use the metadata table to
-- fill variables in a custom lua template. Or, pass `--template=...`
-- to pandoc, and pandoc will add do the template processing as
-- usual.
function Doc(body, metadata, variables)
local buffer = {}
local function add(s)
table.insert(buffer, s)
end
add(body)
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 '\n'
end
function Emph(s)
return '<span class="deco" style="font-style:italic;">' .. s .. "</span>"
end
function Strong(s)
return '<span class="deco" style="font-weight:bold;">' .. s .. "</span>"
end
function Subscript(s)
return "<sub>" .. s .. "</sub>"
end
function Superscript(s)
return "<sup>" .. s .. "</sup>"
end
function SmallCaps(s)
return '<span style="font-variant: small-caps;">' .. s .. '</span>'
end
function Strikeout(s)
return '<del>' .. s .. '</del>'
end
function Link(s, src, tit)
return "[" .. src .. ":title=" .. tit .. "]"
end
function Image(s, src, tit)
return "[" .. src .. ":image]"
end
function Code(s, attr)
return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
end
function InlineMath(s)
return "[tex:" .. escape(s) .. "]"
end
function DisplayMath(s)
return "[tex:" .. escape(s) .. "]"
end
function Note(s)
return "((" .. s .. "))"
end
function Span(s, attr)
return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
end
function Cite(s, cs)
local ids = {}
for _,cit in ipairs(cs) do
table.insert(ids, cit.citationId)
end
return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
"\">" .. s .. "</span>"
end
function Plain(s)
return s
end
function Para(s)
return s
end
-- lev is an integer, the header level.
function Header(lev, s, attr)
local buffer = {}
for i = 1,lev do
table.insert(buffer, "*")
end
local attrs = table.concat(attr, "")
return table.concat(buffer, "") .. attrs .. " " .. s
end
function BlockQuote(s)
return '>>\n' .. s .. '\n<<'
end
function HorizontalRule()
return "<hr/>"
end
function CodeBlock(s, attr)
return ">|" .. table.concat(attr, "")
.. "|\n" .. s .. "\n||<"
end
function BulletList(items)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "-" .. item)
end
return table.concat(buffer, "\n")
end
function OrderedList(items)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "+" .. item)
end
return table.concat(buffer, "\n")
end
-- Revisit association list STackValue instance.
function DefinitionList(items)
local buffer = {}
for _,item in pairs(items) do
for k, v in pairs(item) do
table.insert(buffer,
":" .. k .. ":" ..
table.concat(v, "")
.. "\n")
end
end
return table.concat(buffer, "\n")
end
-- Caption is a string, aligns is an array of strings,
-- widths is an array of floats, headers is an array of
-- strings, rows is an array of arrays of strings.
function Table(caption, aligns, widths, headers, rows)
local buffer = {}
local function add(s)
table.insert(buffer, s)
end
if caption ~= "" then
add(caption)
end
local header_row = {}
local empty_header = true
for i, h in pairs(headers) do
table.insert(header_row, h)
empty_header = empty_header and h == ""
end
if empty_header then
head = ""
else
head = "|*" .. table.concat(header_row, "|*") .. "|"
end
add(head)
for _, row in pairs(rows) do
local rows = {}
for i,c in pairs(row) do
table.insert(rows, c)
end
add("|" .. table.concat(rows, "|") .. "|")
end
return table.concat(buffer,'\n')
end
function Div(s, attr)
return s
end
-- The following code will produce runtime warnings when you haven't defined
-- all of the functions you need for the custom writer, so it's useful
-- to include when you're working on a writer.
local meta = {}
meta.__index =
function(_, key)
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
return function() return "" end
end
setmetatable(_G, meta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment