git.iko.soy/typeable/pandoc
on commonmark-github help sign in
typeable/pandoc/data/sample.lua
345 lines 9.0 kB history raw
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.
6--
7-- Invoke with: pandoc -t sample.lua
8--
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
13-- syntax errors.
15-- Character escaping
16local function escape(s, in_attribute)
17 return s:gsub("[<>&\"']",
18 function(x)
19 if x == '<' then
20 return '&lt;'
21 elseif x == '>' then
22 return '&gt;'
23 elseif x == '&' then
24 return '&amp;'
25 elseif x == '"' then
26 return '&quot;'
27 elseif x == "'" then
28 return '&#39;'
29 else
30 return x
31 end
32 end)
33end
35-- Helper function to convert an attributes table into
36-- a string that can be put into HTML tags.
37local function attributes(attr)
38 local attr_table = {}
39 for x,y in pairs(attr) do
40 if y and y ~= "" then
41 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
42 end
43 end
44 return table.concat(attr_table)
45end
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")
51 tmph:write(inp)
52 tmph:close()
53 local outh = io.popen(cmd .. " " .. tmp,"r")
54 local result = outh:read("*all")
55 outh:close()
56 os.remove(tmp)
57 return result
58end
60-- Table to store footnotes, so they can be included at the end.
61local notes = {}
63-- Blocksep is used to separate block elements.
64function Blocksep()
65 return "\n\n"
66end
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
73-- usual.
74function Doc(body, metadata, variables)
75 local buffer = {}
76 local function add(s)
77 table.insert(buffer, s)
78 end
79 add(body)
80 if #notes > 0 then
81 add('<ol class="footnotes">')
82 for _,note in pairs(notes) do
83 add(note)
84 end
85 add('</ol>')
86 end
87 return table.concat(buffer,'\n') .. '\n'
88end
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.
95function Str(s)
96 return escape(s)
97end
99function Space()
100 return " "
101end
103function SoftBreak()
104 return "\n"
105end
107function LineBreak()
108 return "<br/>"
109end
111function Emph(s)
112 return "<em>" .. s .. "</em>"
113end
115function Strong(s)
116 return "<strong>" .. s .. "</strong>"
117end
119function Subscript(s)
120 return "<sub>" .. s .. "</sub>"
121end
123function Superscript(s)
124 return "<sup>" .. s .. "</sup>"
125end
127function SmallCaps(s)
128 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
129end
131function Strikeout(s)
132 return '<del>' .. s .. '</del>'
133end
135function Link(s, src, tit, attr)
136 return "<a href='" .. escape(src,true) .. "' title='" ..
137 escape(tit,true) .. "'>" .. s .. "</a>"
138end
140function Image(s, src, tit, attr)
141 return "<img src='" .. escape(src,true) .. "' title='" ..
142 escape(tit,true) .. "'/>"
143end
145function Code(s, attr)
146 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
147end
149function InlineMath(s)
150 return "\\(" .. escape(s) .. "\\)"
151end
153function DisplayMath(s)
154 return "\\[" .. escape(s) .. "\\]"
155end
157function Note(s)
158 local num = #notes + 1
159 -- insert the back reference right before the final closing tag.
160 s = string.gsub(s,
161 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</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>'
167end
169function Span(s, attr)
170 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
171end
173function RawInline(format, str)
174 if format == "html" then
175 return str
176 else
177 return ''
178 end
179end
181function Cite(s, cs)
182 local ids = {}
183 for _,cit in ipairs(cs) do
184 table.insert(ids, cit.citationId)
185 end
186 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
187 "\">" .. s .. "</span>"
188end
190function Plain(s)
191 return s
192end
194function Para(s)
195 return "<p>" .. s .. "</p>"
196end
198-- lev is an integer, the header level.
199function Header(lev, s, attr)
200 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
201end
203function BlockQuote(s)
204 return "<blockquote>\n" .. s .. "\n</blockquote>"
205end
207function HorizontalRule()
208 return "<hr/>"
209end
211function LineBlock(ls)
212 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
213 '</div>'
214end
216function CodeBlock(s, attr)
217 -- If code block has class 'dot', pipe the contents through dot
218 -- and base64, and include the base64-encoded png as a data: URL.
219 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
220 local png = pipe("base64", pipe("dot -Tpng", s))
221 return '<img src="data:image/png;base64,' .. png .. '"/>'
222 -- otherwise treat as code (one could pipe through a highlighter)
223 else
224 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
225 "</code></pre>"
226 end
227end
229function BulletList(items)
230 local buffer = {}
231 for _, item in pairs(items) do
232 table.insert(buffer, "<li>" .. item .. "</li>")
233 end
234 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
235end
237function OrderedList(items)
238 local buffer = {}
239 for _, item in pairs(items) do
240 table.insert(buffer, "<li>" .. item .. "</li>")
241 end
242 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
243end
245-- Revisit association list STackValue instance.
246function DefinitionList(items)
247 local buffer = {}
248 for _,item in pairs(items) do
249 for k, v in pairs(item) do
250 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
251 table.concat(v,"</dd>\n<dd>") .. "</dd>")
252 end
253 end
254 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
255end
257-- Convert pandoc alignment to something HTML can use.
258-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
259function html_align(align)
260 if align == 'AlignLeft' then
261 return 'left'
262 elseif align == 'AlignRight' then
263 return 'right'
264 elseif align == 'AlignCenter' then
265 return 'center'
266 else
267 return 'left'
268 end
269end
271function CaptionedImage(src, tit, caption, attr)
272 return '<div class="figure">\n<img src="' .. escape(src,true) ..
273 '" title="' .. escape(tit,true) .. '"/>\n' ..
274 '<p class="caption">' .. caption .. '</p>\n</div>'
275end
277-- Caption is a string, aligns is an array of strings,
278-- widths is an array of floats, headers is an array of
279-- strings, rows is an array of arrays of strings.
280function Table(caption, aligns, widths, headers, rows)
281 local buffer = {}
282 local function add(s)
283 table.insert(buffer, s)
284 end
285 add("<table>")
286 if caption ~= "" then
287 add("<caption>" .. caption .. "</caption>")
288 end
289 if widths and widths[1] ~= 0 then
290 for _, w in pairs(widths) do
291 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
292 end
293 end
294 local header_row = {}
295 local empty_header = true
296 for i, h in pairs(headers) do
297 local align = html_align(aligns[i])
298 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
299 empty_header = empty_header and h == ""
300 end
301 if empty_header then
302 head = ""
303 else
304 add('<tr class="header">')
305 for _,h in pairs(header_row) do
306 add(h)
307 end
308 add('</tr>')
309 end
310 local class = "even"
311 for _, row in pairs(rows) do
312 class = (class == "even" and "odd") or "even"
313 add('<tr class="' .. class .. '">')
314 for i,c in pairs(row) do
315 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
316 end
317 add('</tr>')
318 end
319 add('</table')
320 return table.concat(buffer,'\n')
321end
323function RawBlock(format, str)
324 if format == "html" then
325 return str
326 else
327 return ''
328 end
329end
331function Div(s, attr)
332 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
333end
335-- The following code will produce runtime warnings when you haven't defined
336-- all of the functions you need for the custom writer, so it's useful
337-- to include when you're working on a writer.
338local meta = {}
339meta.__index =
340 function(_, key)
341 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
342 return function() return "" end
343 end
344setmetatable(_G, meta)