git.iko.soy/typeable/pandoc
on 2.11.0.2 help sign in
typeable/pandoc/data/sample.lua
359 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
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-- Choose 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)
28 or "png"
29local image_mime_type = ({
30 jpeg = "image/jpeg",
31 jpg = "image/jpeg",
32 gif = "image/gif",
33 png = "image/png",
34 svg = "image/svg+xml",
35 })[image_format]
36 or error("unsupported image format `" .. image_format .. "`")
38-- Character escaping
39local function escape(s, in_attribute)
40 return s:gsub("[<>&\"']",
41 function(x)
42 if x == '<' then
43 return '&lt;'
44 elseif x == '>' then
45 return '&gt;'
46 elseif x == '&' then
47 return '&amp;'
48 elseif in_attribute and x == '"' then
49 return '&quot;'
50 elseif in_attribute and x == "'" then
51 return '&#39;'
52 else
53 return x
54 end
55 end)
56end
58-- Helper function to convert an attributes table into
59-- a string that can be put into HTML tags.
60local function attributes(attr)
61 local attr_table = {}
62 for x,y in pairs(attr) do
63 if y and y ~= "" then
64 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
65 end
66 end
67 return table.concat(attr_table)
68end
70-- Table to store footnotes, so they can be included at the end.
71local notes = {}
73-- Blocksep is used to separate block elements.
74function Blocksep()
75 return "\n\n"
76end
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 do the template processing as usual.
83function Doc(body, metadata, variables)
84 local buffer = {}
85 local function add(s)
86 table.insert(buffer, s)
87 end
88 add(body)
89 if #notes > 0 then
90 add('<ol class="footnotes">')
91 for _,note in pairs(notes) do
92 add(note)
93 end
94 add('</ol>')
95 end
96 return table.concat(buffer,'\n') .. '\n'
97end
99-- The functions that follow render corresponding pandoc elements.
100-- s is always a string, attr is always a table of attributes, and
101-- items is always an array of strings (the items in a list).
102-- Comments indicate the types of other variables.
104function Str(s)
105 return escape(s)
106end
108function Space()
109 return " "
110end
112function SoftBreak()
113 return "\n"
114end
116function LineBreak()
117 return "<br/>"
118end
120function Emph(s)
121 return "<em>" .. s .. "</em>"
122end
124function Strong(s)
125 return "<strong>" .. s .. "</strong>"
126end
128function Subscript(s)
129 return "<sub>" .. s .. "</sub>"
130end
132function Superscript(s)
133 return "<sup>" .. s .. "</sup>"
134end
136function SmallCaps(s)
137 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
138end
140function Strikeout(s)
141 return '<del>' .. s .. '</del>'
142end
144function Link(s, tgt, tit, attr)
145 return "<a href='" .. escape(tgt,true) .. "' title='" ..
146 escape(tit,true) .. "'>" .. s .. "</a>"
147end
149function Image(s, src, tit, attr)
150 return "<img src='" .. escape(src,true) .. "' title='" ..
151 escape(tit,true) .. "'/>"
152end
154function Code(s, attr)
155 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
156end
158function InlineMath(s)
159 return "\\(" .. escape(s) .. "\\)"
160end
162function DisplayMath(s)
163 return "\\[" .. escape(s) .. "\\]"
164end
166function SingleQuoted(s)
167 return "&lsquo;" .. s .. "&rsquo;"
168end
170function DoubleQuoted(s)
171 return "&ldquo;" .. s .. "&rdquo;"
172end
174function Note(s)
175 local num = #notes + 1
176 -- insert the back reference right before the final closing tag.
177 s = string.gsub(s,
178 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
179 -- add a list item with the note to the note table.
180 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
181 -- return the footnote reference, linked to the note.
182 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
183 '"><sup>' .. num .. '</sup></a>'
184end
186function Span(s, attr)
187 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
188end
190function RawInline(format, str)
191 if format == "html" then
192 return str
193 else
194 return ''
195 end
196end
198function Cite(s, cs)
199 local ids = {}
200 for _,cit in ipairs(cs) do
201 table.insert(ids, cit.citationId)
202 end
203 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
204 "\">" .. s .. "</span>"
205end
207function Plain(s)
208 return s
209end
211function Para(s)
212 return "<p>" .. s .. "</p>"
213end
215-- lev is an integer, the header level.
216function Header(lev, s, attr)
217 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
218end
220function BlockQuote(s)
221 return "<blockquote>\n" .. s .. "\n</blockquote>"
222end
224function HorizontalRule()
225 return "<hr/>"
226end
228function LineBlock(ls)
229 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
230 '</div>'
231end
233function CodeBlock(s, attr)
234 -- If code block has class 'dot', pipe the contents through dot
235 -- and base64, and include the base64-encoded png as a data: URL.
236 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
237 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
238 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
239 -- otherwise treat as code (one could pipe through a highlighter)
240 else
241 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
242 "</code></pre>"
243 end
244end
246function BulletList(items)
247 local buffer = {}
248 for _, item in pairs(items) do
249 table.insert(buffer, "<li>" .. item .. "</li>")
250 end
251 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
252end
254function OrderedList(items)
255 local buffer = {}
256 for _, item in pairs(items) do
257 table.insert(buffer, "<li>" .. item .. "</li>")
258 end
259 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
260end
262function DefinitionList(items)
263 local buffer = {}
264 for _,item in pairs(items) do
265 local k, v = next(item)
266 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
267 table.concat(v, "</dd>\n<dd>") .. "</dd>")
268 end
269 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
270end
272-- Convert pandoc alignment to something HTML can use.
273-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
274local function html_align(align)
275 if align == 'AlignLeft' then
276 return 'left'
277 elseif align == 'AlignRight' then
278 return 'right'
279 elseif align == 'AlignCenter' then
280 return 'center'
281 else
282 return 'left'
283 end
284end
286function CaptionedImage(src, tit, caption, attr)
287 return '<div class="figure">\n<img src="' .. escape(src,true) ..
288 '" title="' .. escape(tit,true) .. '"/>\n' ..
289 '<p class="caption">' .. escape(caption) .. '</p>\n</div>'
290end
292-- Caption is a string, aligns is an array of strings,
293-- widths is an array of floats, headers is an array of
294-- strings, rows is an array of arrays of strings.
295function Table(caption, aligns, widths, headers, rows)
296 local buffer = {}
297 local function add(s)
298 table.insert(buffer, s)
299 end
300 add("<table>")
301 if caption ~= "" then
302 add("<caption>" .. caption .. "</caption>")
303 end
304 if widths and widths[1] ~= 0 then
305 for _, w in pairs(widths) do
306 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
307 end
308 end
309 local header_row = {}
310 local empty_header = true
311 for i, h in pairs(headers) do
312 local align = html_align(aligns[i])
313 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
314 empty_header = empty_header and h == ""
315 end
316 if empty_header then
317 head = ""
318 else
319 add('<tr class="header">')
320 for _,h in pairs(header_row) do
321 add(h)
322 end
323 add('</tr>')
324 end
325 local class = "even"
326 for _, row in pairs(rows) do
327 class = (class == "even" and "odd") or "even"
328 add('<tr class="' .. class .. '">')
329 for i,c in pairs(row) do
330 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
331 end
332 add('</tr>')
333 end
334 add('</table>')
335 return table.concat(buffer,'\n')
336end
338function RawBlock(format, str)
339 if format == "html" then
340 return str
341 else
342 return ''
343 end
344end
346function Div(s, attr)
347 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
348end
350-- The following code will produce runtime warnings when you haven't defined
351-- all of the functions you need for the custom writer, so it's useful
352-- to include when you're working on a writer.
353local meta = {}
354meta.__index =
355 function(_, key)
356 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
357 return function() return "" end
358 end
359setmetatable(_G, meta)