1-- This is a sample custom writer for pandoc. It produces output
2-- that is very similar to that of pandoc's HTML writer.
3-- There is one new feature: code blocks marked with class 'dot'
4-- are piped through graphviz and images are included in the HTML
5-- output using 'data:' URLs.
7-- Invoke with: pandoc -t sample.lua
9-- Note: you need not have lua installed on your system to use this
10-- custom writer. However, if you do have lua installed, you can
11-- use it to test changes to the script. 'lua sample.lua' will
12-- produce informative error messages if your code contains
16local function escape(s, in_attribute)
17 return s:gsub("[<>&\"']",
35-- Helper function to convert an attributes table into
36-- a string that can be put into HTML tags.
37local function attributes(attr)
39 for x,y in pairs(attr) do
41 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
44 return table.concat(attr_table)
47-- Run cmd on a temporary file containing inp and return result.
48local function pipe(cmd, inp)
49 local tmp = os.tmpname()
50 local tmph = io.open(tmp, "w")
53 local outh = io.popen(cmd .. " " .. tmp,"r")
54 local result = outh:read("*all")
60-- Table to store footnotes, so they can be included at the end.
63-- Blocksep is used to separate block elements.
68-- This function is called once for the whole document. Parameters:
69-- body is a string, metadata is a table, variables is a table.
70-- This gives you a fragment. You could use the metadata table to
71-- fill variables in a custom lua template. Or, pass `--template=...`
72-- to pandoc, and pandoc will add do the template processing as
74function Doc(body, metadata, variables)
77 table.insert(buffer, s)
81 add('<ol class="footnotes">')
82 for _,note in pairs(notes) do
87 return table.concat(buffer,'\n') .. '\n'
90-- The functions that follow render corresponding pandoc elements.
91-- s is always a string, attr is always a table of attributes, and
92-- items is always an array of strings (the items in a list).
93-- Comments indicate the types of other variables.
108 return "<em>" .. s .. "</em>"
112 return "<strong>" .. s .. "</strong>"
116 return "<sub>" .. s .. "</sub>"
119function Superscript(s)
120 return "<sup>" .. s .. "</sup>"
124 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
128 return '<del>' .. s .. '</del>'
131function Link(s, src, tit)
132 return "<a href='" .. escape(src,true) .. "' title='" ..
133 escape(tit,true) .. "'>" .. s .. "</a>"
136function Image(s, src, tit)
137 return "<img src='" .. escape(src,true) .. "' title='" ..
138 escape(tit,true) .. "'/>"
141function Code(s, attr)
142 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
145function InlineMath(s)
146 return "\\(" .. escape(s) .. "\\)"
149function DisplayMath(s)
150 return "\\[" .. escape(s) .. "\\]"
154 local num = #notes + 1
155 -- insert the back reference right before the final closing tag.
157 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></')
158 -- add a list item with the note to the note table.
159 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
160 -- return the footnote reference, linked to the note.
161 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
162 '"><sup>' .. num .. '</sup></a>'
165function Span(s, attr)
166 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
171 for _,cit in ipairs(cs) do
172 table.insert(ids, cit.citationId)
174 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
175 "\">" .. s .. "</span>"
183 return "<p>" .. s .. "</p>"
186-- lev is an integer, the header level.
187function Header(lev, s, attr)
188 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
191function BlockQuote(s)
192 return "<blockquote>\n" .. s .. "\n</blockquote>"
195function HorizontalRule()
199function CodeBlock(s, attr)
200 -- If code block has class 'dot', pipe the contents through dot
201 -- and base64, and include the base64-encoded png as a data: URL.
202 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
203 local png = pipe("base64", pipe("dot -Tpng", s))
204 return '<img src="data:image/png;base64,' .. png .. '"/>'
205 -- otherwise treat as code (one could pipe through a highlighter)
207 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
212function BulletList(items)
214 for _, item in pairs(items) do
215 table.insert(buffer, "<li>" .. item .. "</li>")
217 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
220function OrderedList(items)
222 for _, item in pairs(items) do
223 table.insert(buffer, "<li>" .. item .. "</li>")
225 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
228-- Revisit association list STackValue instance.
229function DefinitionList(items)
231 for _,item in pairs(items) do
232 for k, v in pairs(item) do
233 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
234 table.concat(v,"</dd>\n<dd>") .. "</dd>")
237 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
240-- Convert pandoc alignment to something HTML can use.
241-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
242function html_align(align)
243 if align == 'AlignLeft' then
245 elseif align == 'AlignRight' then
247 elseif align == 'AlignCenter' then
254function CaptionedImage(src, tit, caption)
255 return '<div class="figure">\n<img src="' .. escape(src,true) ..
256 '" title="' .. escape(tit,true) .. '"/>\n' ..
257 '<p class="caption">' .. caption .. '</p>\n</div>'
260-- Caption is a string, aligns is an array of strings,
261-- widths is an array of floats, headers is an array of
262-- strings, rows is an array of arrays of strings.
263function Table(caption, aligns, widths, headers, rows)
265 local function add(s)
266 table.insert(buffer, s)
269 if caption ~= "" then
270 add("<caption>" .. caption .. "</caption>")
272 if widths and widths[1] ~= 0 then
273 for _, w in pairs(widths) do
274 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
277 local header_row = {}
278 local empty_header = true
279 for i, h in pairs(headers) do
280 local align = html_align(aligns[i])
281 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
282 empty_header = empty_header and h == ""
287 add('<tr class="header">')
288 for _,h in pairs(header_row) do
294 for _, row in pairs(rows) do
295 class = (class == "even" and "odd") or "even"
296 add('<tr class="' .. class .. '">')
297 for i,c in pairs(row) do
298 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
303 return table.concat(buffer,'\n')
307 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
310-- The following code will produce runtime warnings when you haven't defined
311-- all of the functions you need for the custom writer, so it's useful
312-- to include when you're working on a writer.
316 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
317 return function() return "" end
319setmetatable(_G, meta)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list