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.
112 return "<em>" .. s .. "</em>"
116 return "<strong>" .. s .. "</strong>"
120 return "<sub>" .. s .. "</sub>"
123function Superscript(s)
124 return "<sup>" .. s .. "</sup>"
128 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
132 return '<del>' .. s .. '</del>'
135function Link(s, src, tit, attr)
136 return "<a href='" .. escape(src,true) .. "' title='" ..
137 escape(tit,true) .. "'>" .. s .. "</a>"
140function Image(s, src, tit, attr)
141 return "<img src='" .. escape(src,true) .. "' title='" ..
142 escape(tit,true) .. "'/>"
145function Code(s, attr)
146 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
149function InlineMath(s)
150 return "\\(" .. escape(s) .. "\\)"
153function DisplayMath(s)
154 return "\\[" .. escape(s) .. "\\]"
158 local num = #notes + 1
159 -- insert the back reference right before the final closing tag.
161 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></')
162 -- add a list item with the note to the note table.
163 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
164 -- return the footnote reference, linked to the note.
165 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
166 '"><sup>' .. num .. '</sup></a>'
169function Span(s, attr)
170 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
175 for _,cit in ipairs(cs) do
176 table.insert(ids, cit.citationId)
178 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
179 "\">" .. s .. "</span>"
187 return "<p>" .. s .. "</p>"
190-- lev is an integer, the header level.
191function Header(lev, s, attr)
192 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
195function BlockQuote(s)
196 return "<blockquote>\n" .. s .. "\n</blockquote>"
199function HorizontalRule()
203function CodeBlock(s, attr)
204 -- If code block has class 'dot', pipe the contents through dot
205 -- and base64, and include the base64-encoded png as a data: URL.
206 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
207 local png = pipe("base64", pipe("dot -Tpng", s))
208 return '<img src="data:image/png;base64,' .. png .. '"/>'
209 -- otherwise treat as code (one could pipe through a highlighter)
211 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
216function BulletList(items)
218 for _, item in pairs(items) do
219 table.insert(buffer, "<li>" .. item .. "</li>")
221 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
224function OrderedList(items)
226 for _, item in pairs(items) do
227 table.insert(buffer, "<li>" .. item .. "</li>")
229 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
232-- Revisit association list STackValue instance.
233function DefinitionList(items)
235 for _,item in pairs(items) do
236 for k, v in pairs(item) do
237 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
238 table.concat(v,"</dd>\n<dd>") .. "</dd>")
241 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
244-- Convert pandoc alignment to something HTML can use.
245-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
246function html_align(align)
247 if align == 'AlignLeft' then
249 elseif align == 'AlignRight' then
251 elseif align == 'AlignCenter' then
258function CaptionedImage(src, tit, caption, attr)
259 return '<div class="figure">\n<img src="' .. escape(src,true) ..
260 '" title="' .. escape(tit,true) .. '"/>\n' ..
261 '<p class="caption">' .. caption .. '</p>\n</div>'
264-- Caption is a string, aligns is an array of strings,
265-- widths is an array of floats, headers is an array of
266-- strings, rows is an array of arrays of strings.
267function Table(caption, aligns, widths, headers, rows)
269 local function add(s)
270 table.insert(buffer, s)
273 if caption ~= "" then
274 add("<caption>" .. caption .. "</caption>")
276 if widths and widths[1] ~= 0 then
277 for _, w in pairs(widths) do
278 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
281 local header_row = {}
282 local empty_header = true
283 for i, h in pairs(headers) do
284 local align = html_align(aligns[i])
285 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
286 empty_header = empty_header and h == ""
291 add('<tr class="header">')
292 for _,h in pairs(header_row) do
298 for _, row in pairs(rows) do
299 class = (class == "even" and "odd") or "even"
300 add('<tr class="' .. class .. '">')
301 for i,c in pairs(row) do
302 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
307 return table.concat(buffer,'\n')
311 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
314-- The following code will produce runtime warnings when you haven't defined
315-- all of the functions you need for the custom writer, so it's useful
316-- to include when you're working on a writer.
320 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
321 return function() return "" end
323setmetatable(_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