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. The image format can be controlled
6-- via the `image_format` metadata field.
8-- Invoke with: pandoc -t sample.lua
10-- Note: you need not have lua installed on your system to use this
11-- custom writer. However, if you do have lua installed, you can
12-- use it to test changes to the script. 'lua sample.lua' will
13-- produce informative error messages if your code contains
16local pipe = pandoc.pipe
17local stringify = (require "pandoc.utils").stringify
19local image_format = "png"
20local image_mime_type = "image/png"
22-- Get the mime type for a given format.
23local function mime_type(img_format)
29 svg = "image/svg+xml",
31 return formats[img_format]
32 or error("unsupported image format `" .. img_format .. "`")
35-- Set options from document metadata.
38 if meta.image_format then
39 image_format = stringify(meta.image_format)
40 image_mime_type = mime_type(image_format)
45local function escape(s, in_attribute)
46 return s:gsub("[<>&\"']",
64-- Helper function to convert an attributes table into
65-- a string that can be put into HTML tags.
66local function attributes(attr)
68 for x,y in pairs(attr) do
70 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
73 return table.concat(attr_table)
76-- Table to store footnotes, so they can be included at the end.
79-- Blocksep is used to separate block elements.
84-- This function is called once for the whole document. Parameters:
85-- body is a string, metadata is a table, variables is a table.
86-- This gives you a fragment. You could use the metadata table to
87-- fill variables in a custom lua template. Or, pass `--template=...`
88-- to pandoc, and pandoc will add do the template processing as
90function Doc(body, metadata, variables)
93 table.insert(buffer, s)
97 add('<ol class="footnotes">')
98 for _,note in pairs(notes) do
103 return table.concat(buffer,'\n') .. '\n'
106-- The functions that follow render corresponding pandoc elements.
107-- s is always a string, attr is always a table of attributes, and
108-- items is always an array of strings (the items in a list).
109-- Comments indicate the types of other variables.
128 return "<em>" .. s .. "</em>"
132 return "<strong>" .. s .. "</strong>"
136 return "<sub>" .. s .. "</sub>"
139function Superscript(s)
140 return "<sup>" .. s .. "</sup>"
144 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
148 return '<del>' .. s .. '</del>'
151function Link(s, src, tit, attr)
152 return "<a href='" .. escape(src,true) .. "' title='" ..
153 escape(tit,true) .. "'>" .. s .. "</a>"
156function Image(s, src, tit, attr)
157 return "<img src='" .. escape(src,true) .. "' title='" ..
158 escape(tit,true) .. "'/>"
161function Code(s, attr)
162 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
165function InlineMath(s)
166 return "\\(" .. escape(s) .. "\\)"
169function DisplayMath(s)
170 return "\\[" .. escape(s) .. "\\]"
174 local num = #notes + 1
175 -- insert the back reference right before the final closing tag.
177 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></')
178 -- add a list item with the note to the note table.
179 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
180 -- return the footnote reference, linked to the note.
181 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
182 '"><sup>' .. num .. '</sup></a>'
185function Span(s, attr)
186 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
189function RawInline(format, str)
190 if format == "html" then
199 for _,cit in ipairs(cs) do
200 table.insert(ids, cit.citationId)
202 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
203 "\">" .. s .. "</span>"
211 return "<p>" .. s .. "</p>"
214-- lev is an integer, the header level.
215function Header(lev, s, attr)
216 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
219function BlockQuote(s)
220 return "<blockquote>\n" .. s .. "\n</blockquote>"
223function HorizontalRule()
227function LineBlock(ls)
228 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
232function CodeBlock(s, attr)
233 -- If code block has class 'dot', pipe the contents through dot
234 -- and base64, and include the base64-encoded png as a data: URL.
235 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
236 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
237 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
238 -- otherwise treat as code (one could pipe through a highlighter)
240 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
245function BulletList(items)
247 for _, item in pairs(items) do
248 table.insert(buffer, "<li>" .. item .. "</li>")
250 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
253function OrderedList(items)
255 for _, item in pairs(items) do
256 table.insert(buffer, "<li>" .. item .. "</li>")
258 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
261function DefinitionList(items)
263 for _,item in pairs(items) do
264 local k, v = next(item)
265 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
266 table.concat(v, "</dd>\n<dd>") .. "</dd>")
268 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
271-- Convert pandoc alignment to something HTML can use.
272-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
273function html_align(align)
274 if align == 'AlignLeft' then
276 elseif align == 'AlignRight' then
278 elseif align == 'AlignCenter' then
285function CaptionedImage(src, tit, caption, attr)
286 return '<div class="figure">\n<img src="' .. escape(src,true) ..
287 '" title="' .. escape(tit,true) .. '"/>\n' ..
288 '<p class="caption">' .. caption .. '</p>\n</div>'
291-- Caption is a string, aligns is an array of strings,
292-- widths is an array of floats, headers is an array of
293-- strings, rows is an array of arrays of strings.
294function Table(caption, aligns, widths, headers, rows)
296 local function add(s)
297 table.insert(buffer, s)
300 if caption ~= "" then
301 add("<caption>" .. caption .. "</caption>")
303 if widths and widths[1] ~= 0 then
304 for _, w in pairs(widths) do
305 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
308 local header_row = {}
309 local empty_header = true
310 for i, h in pairs(headers) do
311 local align = html_align(aligns[i])
312 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
313 empty_header = empty_header and h == ""
318 add('<tr class="header">')
319 for _,h in pairs(header_row) do
325 for _, row in pairs(rows) do
326 class = (class == "even" and "odd") or "even"
327 add('<tr class="' .. class .. '">')
328 for i,c in pairs(row) do
329 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
334 return table.concat(buffer,'\n')
337function RawBlock(format, str)
338 if format == "html" then
346 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
349-- The following code will produce runtime warnings when you haven't defined
350-- all of the functions you need for the custom writer, so it's useful
351-- to include when you're working on a writer.
355 -- Setup is optional, don't warn if it's not present.
356 if key == 'Setup' then
359 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
360 return function() return "" end
362setmetatable(_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