git.iko.soy/typeable/pandoc
on 9b0bd4e help sign in
typeable/pandoc/data/sample.lua
363 lines 9.5 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. The image format can be controlled
6-- via the `image_format` metadata field.
7--
8-- Invoke with: pandoc -t sample.lua
9--
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
14-- syntax errors.
16local pipe = pandoc.pipe
17local stringify = (require "pandoc.utils").stringify
19local image_format = "png"
20local image_mime_type = "image/png"
22-- Get the mime type for a given format.
23local function mime_type(img_format)
24 local formats = {
25 jpeg = "image/jpeg",
26 jpg = "image/jpeg",
27 gif = "image/gif",
28 png = "image/png",
29 svg = "image/svg+xml",
30 }
31 return formats[img_format]
32 or error("unsupported image format `" .. img_format .. "`")
33end
35-- Set options from document metadata.
36function Setup(doc)
37 local meta = doc.meta
38 if meta.image_format then
39 image_format = stringify(meta.image_format)
40 image_mime_type = mime_type(image_format)
41 end
42end
44-- Character escaping
45local function escape(s, in_attribute)
46 return s:gsub("[<>&\"']",
47 function(x)
48 if x == '<' then
49 return '&lt;'
50 elseif x == '>' then
51 return '&gt;'
52 elseif x == '&' then
53 return '&amp;'
54 elseif x == '"' then
55 return '&quot;'
56 elseif x == "'" then
57 return '&#39;'
58 else
59 return x
60 end
61 end)
62end
64-- Helper function to convert an attributes table into
65-- a string that can be put into HTML tags.
66local function attributes(attr)
67 local attr_table = {}
68 for x,y in pairs(attr) do
69 if y and y ~= "" then
70 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
71 end
72 end
73 return table.concat(attr_table)
74end
76-- Table to store footnotes, so they can be included at the end.
77local notes = {}
79-- Blocksep is used to separate block elements.
80function Blocksep()
81 return "\n\n"
82end
84-- This function is called once for the whole document. Parameters:
85-- body is a string, metadata is a table, variables is a table.
86-- This gives you a fragment. You could use the metadata table to
87-- fill variables in a custom lua template. Or, pass `--template=...`
88-- to pandoc, and pandoc will add do the template processing as
89-- usual.
90function Doc(body, metadata, variables)
91 local buffer = {}
92 local function add(s)
93 table.insert(buffer, s)
94 end
95 add(body)
96 if #notes > 0 then
97 add('<ol class="footnotes">')
98 for _,note in pairs(notes) do
99 add(note)
100 end
101 add('</ol>')
102 end
103 return table.concat(buffer,'\n') .. '\n'
104end
106-- The functions that follow render corresponding pandoc elements.
107-- s is always a string, attr is always a table of attributes, and
108-- items is always an array of strings (the items in a list).
109-- Comments indicate the types of other variables.
111function Str(s)
112 return escape(s)
113end
115function Space()
116 return " "
117end
119function SoftBreak()
120 return "\n"
121end
123function LineBreak()
124 return "<br/>"
125end
127function Emph(s)
128 return "<em>" .. s .. "</em>"
129end
131function Strong(s)
132 return "<strong>" .. s .. "</strong>"
133end
135function Subscript(s)
136 return "<sub>" .. s .. "</sub>"
137end
139function Superscript(s)
140 return "<sup>" .. s .. "</sup>"
141end
143function SmallCaps(s)
144 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
145end
147function Strikeout(s)
148 return '<del>' .. s .. '</del>'
149end
151function Link(s, src, tit, attr)
152 return "<a href='" .. escape(src,true) .. "' title='" ..
153 escape(tit,true) .. "'>" .. s .. "</a>"
154end
156function Image(s, src, tit, attr)
157 return "<img src='" .. escape(src,true) .. "' title='" ..
158 escape(tit,true) .. "'/>"
159end
161function Code(s, attr)
162 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
163end
165function InlineMath(s)
166 return "\\(" .. escape(s) .. "\\)"
167end
169function DisplayMath(s)
170 return "\\[" .. escape(s) .. "\\]"
171end
173function Note(s)
174 local num = #notes + 1
175 -- insert the back reference right before the final closing tag.
176 s = string.gsub(s,
177 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
178 -- add a list item with the note to the note table.
179 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
180 -- return the footnote reference, linked to the note.
181 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
182 '"><sup>' .. num .. '</sup></a>'
183end
185function Span(s, attr)
186 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
187end
189function RawInline(format, str)
190 if format == "html" then
191 return str
192 else
193 return ''
194 end
195end
197function Cite(s, cs)
198 local ids = {}
199 for _,cit in ipairs(cs) do
200 table.insert(ids, cit.citationId)
201 end
202 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
203 "\">" .. s .. "</span>"
204end
206function Plain(s)
207 return s
208end
210function Para(s)
211 return "<p>" .. s .. "</p>"
212end
214-- lev is an integer, the header level.
215function Header(lev, s, attr)
216 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
217end
219function BlockQuote(s)
220 return "<blockquote>\n" .. s .. "\n</blockquote>"
221end
223function HorizontalRule()
224 return "<hr/>"
225end
227function LineBlock(ls)
228 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
229 '</div>'
230end
232function CodeBlock(s, attr)
233 -- If code block has class 'dot', pipe the contents through dot
234 -- and base64, and include the base64-encoded png as a data: URL.
235 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
236 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
237 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
238 -- otherwise treat as code (one could pipe through a highlighter)
239 else
240 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
241 "</code></pre>"
242 end
243end
245function BulletList(items)
246 local buffer = {}
247 for _, item in pairs(items) do
248 table.insert(buffer, "<li>" .. item .. "</li>")
249 end
250 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
251end
253function OrderedList(items)
254 local buffer = {}
255 for _, item in pairs(items) do
256 table.insert(buffer, "<li>" .. item .. "</li>")
257 end
258 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
259end
261function DefinitionList(items)
262 local buffer = {}
263 for _,item in pairs(items) do
264 local k, v = next(item)
265 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
266 table.concat(v, "</dd>\n<dd>") .. "</dd>")
267 end
268 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
269end
271-- Convert pandoc alignment to something HTML can use.
272-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
273function html_align(align)
274 if align == 'AlignLeft' then
275 return 'left'
276 elseif align == 'AlignRight' then
277 return 'right'
278 elseif align == 'AlignCenter' then
279 return 'center'
280 else
281 return 'left'
282 end
283end
285function CaptionedImage(src, tit, caption, attr)
286 return '<div class="figure">\n<img src="' .. escape(src,true) ..
287 '" title="' .. escape(tit,true) .. '"/>\n' ..
288 '<p class="caption">' .. caption .. '</p>\n</div>'
289end
291-- Caption is a string, aligns is an array of strings,
292-- widths is an array of floats, headers is an array of
293-- strings, rows is an array of arrays of strings.
294function Table(caption, aligns, widths, headers, rows)
295 local buffer = {}
296 local function add(s)
297 table.insert(buffer, s)
298 end
299 add("<table>")
300 if caption ~= "" then
301 add("<caption>" .. caption .. "</caption>")
302 end
303 if widths and widths[1] ~= 0 then
304 for _, w in pairs(widths) do
305 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
306 end
307 end
308 local header_row = {}
309 local empty_header = true
310 for i, h in pairs(headers) do
311 local align = html_align(aligns[i])
312 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
313 empty_header = empty_header and h == ""
314 end
315 if empty_header then
316 head = ""
317 else
318 add('<tr class="header">')
319 for _,h in pairs(header_row) do
320 add(h)
321 end
322 add('</tr>')
323 end
324 local class = "even"
325 for _, row in pairs(rows) do
326 class = (class == "even" and "odd") or "even"
327 add('<tr class="' .. class .. '">')
328 for i,c in pairs(row) do
329 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
330 end
331 add('</tr>')
332 end
333 add('</table')
334 return table.concat(buffer,'\n')
335end
337function RawBlock(format, str)
338 if format == "html" then
339 return str
340 else
341 return ''
342 end
343end
345function Div(s, attr)
346 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
347end
349-- The following code will produce runtime warnings when you haven't defined
350-- all of the functions you need for the custom writer, so it's useful
351-- to include when you're working on a writer.
352local meta = {}
353meta.__index =
354 function(_, key)
355 -- Setup is optional, don't warn if it's not present.
356 if key == 'Setup' then
357 return
358 end
359 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
360 return function() return "" end
361 end
362setmetatable(_G, meta)