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 `" .. img_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 add do the template processing as
84function Doc(body, metadata, variables)
87 table.insert(buffer, s)
91 add('<ol class="footnotes">')
92 for _,note in pairs(notes) do
97 return table.concat(buffer,'\n') .. '\n'
100-- The functions that follow render corresponding pandoc elements.
101-- s is always a string, attr is always a table of attributes, and
102-- items is always an array of strings (the items in a list).
103-- Comments indicate the types of other variables.
122 return "<em>" .. s .. "</em>"
126 return "<strong>" .. s .. "</strong>"
130 return "<sub>" .. s .. "</sub>"
133function Superscript(s)
134 return "<sup>" .. s .. "</sup>"
138 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
142 return '<del>' .. s .. '</del>'
145function Link(s, src, tit, attr)
146 return "<a href='" .. escape(src,true) .. "' title='" ..
147 escape(tit,true) .. "'>" .. s .. "</a>"
150function Image(s, src, tit, attr)
151 return "<img src='" .. escape(src,true) .. "' title='" ..
152 escape(tit,true) .. "'/>"
155function Code(s, attr)
156 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
159function InlineMath(s)
160 return "\\(" .. escape(s) .. "\\)"
163function DisplayMath(s)
164 return "\\[" .. escape(s) .. "\\]"
167function SingleQuoted(s)
168 return "‘" .. s .. "’"
171function DoubleQuoted(s)
172 return "“" .. s .. "”"
176 local num = #notes + 1
177 -- insert the back reference right before the final closing tag.
179 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></')
180 -- add a list item with the note to the note table.
181 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
182 -- return the footnote reference, linked to the note.
183 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
184 '"><sup>' .. num .. '</sup></a>'
187function Span(s, attr)
188 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
191function RawInline(format, str)
192 if format == "html" then
201 for _,cit in ipairs(cs) do
202 table.insert(ids, cit.citationId)
204 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
205 "\">" .. s .. "</span>"
213 return "<p>" .. s .. "</p>"
216-- lev is an integer, the header level.
217function Header(lev, s, attr)
218 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
221function BlockQuote(s)
222 return "<blockquote>\n" .. s .. "\n</blockquote>"
225function HorizontalRule()
229function LineBlock(ls)
230 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
234function CodeBlock(s, attr)
235 -- If code block has class 'dot', pipe the contents through dot
236 -- and base64, and include the base64-encoded png as a data: URL.
237 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
238 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
239 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
240 -- otherwise treat as code (one could pipe through a highlighter)
242 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
247function BulletList(items)
249 for _, item in pairs(items) do
250 table.insert(buffer, "<li>" .. item .. "</li>")
252 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
255function OrderedList(items)
257 for _, item in pairs(items) do
258 table.insert(buffer, "<li>" .. item .. "</li>")
260 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
263function DefinitionList(items)
265 for _,item in pairs(items) do
266 local k, v = next(item)
267 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
268 table.concat(v, "</dd>\n<dd>") .. "</dd>")
270 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
273-- Convert pandoc alignment to something HTML can use.
274-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
275function html_align(align)
276 if align == 'AlignLeft' then
278 elseif align == 'AlignRight' then
280 elseif align == 'AlignCenter' then
287function CaptionedImage(src, tit, caption, attr)
288 return '<div class="figure">\n<img src="' .. escape(src,true) ..
289 '" title="' .. escape(tit,true) .. '"/>\n' ..
290 '<p class="caption">' .. caption .. '</p>\n</div>'
293-- Caption is a string, aligns is an array of strings,
294-- widths is an array of floats, headers is an array of
295-- strings, rows is an array of arrays of strings.
296function Table(caption, aligns, widths, headers, rows)
298 local function add(s)
299 table.insert(buffer, s)
302 if caption ~= "" then
303 add("<caption>" .. caption .. "</caption>")
305 if widths and widths[1] ~= 0 then
306 for _, w in pairs(widths) do
307 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
310 local header_row = {}
311 local empty_header = true
312 for i, h in pairs(headers) do
313 local align = html_align(aligns[i])
314 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
315 empty_header = empty_header and h == ""
320 add('<tr class="header">')
321 for _,h in pairs(header_row) do
327 for _, row in pairs(rows) do
328 class = (class == "even" and "odd") or "even"
329 add('<tr class="' .. class .. '">')
330 for i,c in pairs(row) do
331 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
336 return table.concat(buffer,'\n')
339function RawBlock(format, str)
340 if format == "html" then
348 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
351-- The following code will produce runtime warnings when you haven't defined
352-- all of the functions you need for the custom writer, so it's useful
353-- to include when you're working on a writer.
357 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
358 return function() return "" end
360setmetatable(_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