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
19-- The global variable PANDOC_DOCUMENT contains the full AST of
20-- the document which is going to be written. It can be used to
21-- configure the writer.
22local meta = PANDOC_DOCUMENT.meta
24-- Chose the image format based on the value of the
25-- `image_format` meta value.
26local image_format = meta.image_format
27 and stringify(meta.image_format)
29local image_mime_type = ({
34 svg = "image/svg+xml",
36 or error("unsupported image format `" .. image_format .. "`")
39local function escape(s, in_attribute)
40 return s:gsub("[<>&\"']",
58-- Helper function to convert an attributes table into
59-- a string that can be put into HTML tags.
60local function attributes(attr)
62 for x,y in pairs(attr) do
64 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
67 return table.concat(attr_table)
70-- Table to store footnotes, so they can be included at the end.
73-- Blocksep is used to separate block elements.
78-- This function is called once for the whole document. Parameters:
79-- body is a string, metadata is a table, variables is a table.
80-- This gives you a fragment. You could use the metadata table to
81-- fill variables in a custom lua template. Or, pass `--template=...`
82-- to pandoc, and pandoc will do the template processing as usual.
83function Doc(body, metadata, variables)
86 table.insert(buffer, s)
90 add('<ol class="footnotes">')
91 for _,note in pairs(notes) do
96 return table.concat(buffer,'\n') .. '\n'
99-- The functions that follow render corresponding pandoc elements.
100-- s is always a string, attr is always a table of attributes, and
101-- items is always an array of strings (the items in a list).
102-- Comments indicate the types of other variables.
121 return "<em>" .. s .. "</em>"
125 return "<strong>" .. s .. "</strong>"
129 return "<sub>" .. s .. "</sub>"
132function Superscript(s)
133 return "<sup>" .. s .. "</sup>"
137 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
141 return '<del>' .. s .. '</del>'
144function Link(s, src, tit, attr)
145 return "<a href='" .. escape(src,true) .. "' title='" ..
146 escape(tit,true) .. "'>" .. s .. "</a>"
149function Image(s, src, tit, attr)
150 return "<img src='" .. escape(src,true) .. "' title='" ..
151 escape(tit,true) .. "'/>"
154function Code(s, attr)
155 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
158function InlineMath(s)
159 return "\\(" .. escape(s) .. "\\)"
162function DisplayMath(s)
163 return "\\[" .. escape(s) .. "\\]"
166function SingleQuoted(s)
167 return "‘" .. s .. "’"
170function DoubleQuoted(s)
171 return "“" .. s .. "”"
175 local num = #notes + 1
176 -- insert the back reference right before the final closing tag.
178 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></')
179 -- add a list item with the note to the note table.
180 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
181 -- return the footnote reference, linked to the note.
182 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
183 '"><sup>' .. num .. '</sup></a>'
186function Span(s, attr)
187 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
190function RawInline(format, str)
191 if format == "html" then
200 for _,cit in ipairs(cs) do
201 table.insert(ids, cit.citationId)
203 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
204 "\">" .. s .. "</span>"
212 return "<p>" .. s .. "</p>"
215-- lev is an integer, the header level.
216function Header(lev, s, attr)
217 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
220function BlockQuote(s)
221 return "<blockquote>\n" .. s .. "\n</blockquote>"
224function HorizontalRule()
228function LineBlock(ls)
229 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
233function CodeBlock(s, attr)
234 -- If code block has class 'dot', pipe the contents through dot
235 -- and base64, and include the base64-encoded png as a data: URL.
236 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
237 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
238 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
239 -- otherwise treat as code (one could pipe through a highlighter)
241 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
246function BulletList(items)
248 for _, item in pairs(items) do
249 table.insert(buffer, "<li>" .. item .. "</li>")
251 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
254function OrderedList(items)
256 for _, item in pairs(items) do
257 table.insert(buffer, "<li>" .. item .. "</li>")
259 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
262function DefinitionList(items)
264 for _,item in pairs(items) do
265 local k, v = next(item)
266 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
267 table.concat(v, "</dd>\n<dd>") .. "</dd>")
269 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
272-- Convert pandoc alignment to something HTML can use.
273-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
274function html_align(align)
275 if align == 'AlignLeft' then
277 elseif align == 'AlignRight' then
279 elseif align == 'AlignCenter' then
286function CaptionedImage(src, tit, caption, attr)
287 return '<div class="figure">\n<img src="' .. escape(src,true) ..
288 '" title="' .. escape(tit,true) .. '"/>\n' ..
289 '<p class="caption">' .. caption .. '</p>\n</div>'
292-- Caption is a string, aligns is an array of strings,
293-- widths is an array of floats, headers is an array of
294-- strings, rows is an array of arrays of strings.
295function Table(caption, aligns, widths, headers, rows)
297 local function add(s)
298 table.insert(buffer, s)
301 if caption ~= "" then
302 add("<caption>" .. caption .. "</caption>")
304 if widths and widths[1] ~= 0 then
305 for _, w in pairs(widths) do
306 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
309 local header_row = {}
310 local empty_header = true
311 for i, h in pairs(headers) do
312 local align = html_align(aligns[i])
313 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
314 empty_header = empty_header and h == ""
319 add('<tr class="header">')
320 for _,h in pairs(header_row) do
326 for _, row in pairs(rows) do
327 class = (class == "even" and "odd") or "even"
328 add('<tr class="' .. class .. '">')
329 for i,c in pairs(row) do
330 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
335 return table.concat(buffer,'\n')
338function RawBlock(format, str)
339 if format == "html" then
347 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
350-- The following code will produce runtime warnings when you haven't defined
351-- all of the functions you need for the custom writer, so it's useful
352-- to include when you're working on a writer.
356 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
357 return function() return "" end
359setmetatable(_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