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-- One could use some kind of templating
71-- system here; this just gives you a simple standalone HTML file.
72function Doc(body, metadata, variables)
75 table.insert(buffer, s)
77 add('<!DOCTYPE html>')
80 add('<title>' .. (metadata['title'] or '') .. '</title>')
83 if metadata['title'] and metadata['title'] ~= "" then
84 add('<h1 class="title">' .. metadata['title'] .. '</h1>')
86 for _, author in pairs(metadata['author'] or {}) do
87 add('<h2 class="author">' .. author .. '</h2>')
89 if metadata['date'] and metadata['date'] ~= "" then
90 add('<h3 class="date">' .. metadata.date .. '</h3>')
94 add('<ol class="footnotes">')
95 for _,note in pairs(notes) do
102 return table.concat(buffer,'\n')
105-- The functions that follow render corresponding pandoc elements.
106-- s is always a string, attr is always a table of attributes, and
107-- items is always an array of strings (the items in a list).
108-- Comments indicate the types of other variables.
123 return "<em>" .. s .. "</em>"
127 return "<strong>" .. s .. "</strong>"
131 return "<sub>" .. s .. "</sub>"
134function Superscript(s)
135 return "<sup>" .. s .. "</sup>"
139 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
143 return '<del>' .. s .. '</del>'
146function Link(s, src, tit)
147 return "<a href='" .. escape(src,true) .. "' title='" ..
148 escape(tit,true) .. "'>" .. s .. "</a>"
151function Image(s, src, tit)
152 return "<img src='" .. escape(src,true) .. "' title='" ..
153 escape(tit,true) .. "'/>"
156function Code(s, attr)
157 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
160function InlineMath(s)
161 return "\\(" .. escape(s) .. "\\)"
164function DisplayMath(s)
165 return "\\[" .. escape(s) .. "\\]"
169 local num = #notes + 1
170 -- insert the back reference right before the final closing tag.
172 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></')
173 -- add a list item with the note to the note table.
174 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
175 -- return the footnote reference, linked to the note.
176 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
177 '"><sup>' .. num .. '</sup></a>'
180function Span(s, attr)
181 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
189 return "<p>" .. s .. "</p>"
192-- lev is an integer, the header level.
193function Header(lev, s, attr)
194 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
197function BlockQuote(s)
198 return "<blockquote>\n" .. s .. "\n</blockquote>"
201function HorizontalRule()
205function CodeBlock(s, attr)
206 -- If code block has class 'dot', pipe the contents through dot
207 -- and base64, and include the base64-encoded png as a data: URL.
208 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
209 local png = pipe("base64", pipe("dot -Tpng", s))
210 return '<img src="data:image/png;base64,' .. png .. '"/>'
211 -- otherwise treat as code (one could pipe through a highlighter)
213 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
218function BulletList(items)
220 for _, item in pairs(items) do
221 table.insert(buffer, "<li>" .. item .. "</li>")
223 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
226function OrderedList(items)
228 for _, item in pairs(items) do
229 table.insert(buffer, "<li>" .. item .. "</li>")
231 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
234-- Revisit association list STackValue instance.
235function DefinitionList(items)
237 for _,item in pairs(items) do
238 for k, v in pairs(item) do
239 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
240 table.concat(v,"</dd>\n<dd>") .. "</dd>")
243 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
246-- Convert pandoc alignment to something HTML can use.
247-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
248function html_align(align)
249 if align == 'AlignLeft' then
251 elseif align == 'AlignRight' then
253 elseif align == 'AlignCenter' then
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