git.iko.soy/typeable/pandoc
on rc/2.8.0.2 help sign in
typeable/pandoc/data/sample.lua
361 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-- Chose 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 `" .. img_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 x == '"' then
49 return '&quot;'
50 elseif 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 add do the template processing as
83-- usual.
84function Doc(body, metadata, variables)
85 local buffer = {}
86 local function add(s)
87 table.insert(buffer, s)
88 end
89 add(body)
90 if #notes > 0 then
91 add('<ol class="footnotes">')
92 for _,note in pairs(notes) do
93 add(note)
94 end
95 add('</ol>')
96 end
97 return table.concat(buffer,'\n') .. '\n'
98end
100-- The functions that follow render corresponding pandoc elements.
101-- s is always a string, attr is always a table of attributes, and
102-- items is always an array of strings (the items in a list).
103-- Comments indicate the types of other variables.
105function Str(s)
106 return escape(s)
107end
109function Space()
110 return " "
111end
113function SoftBreak()
114 return "\n"
115end
117function LineBreak()
118 return "<br/>"
119end
121function Emph(s)
122 return "<em>" .. s .. "</em>"
123end
125function Strong(s)
126 return "<strong>" .. s .. "</strong>"
127end
129function Subscript(s)
130 return "<sub>" .. s .. "</sub>"
131end
133function Superscript(s)
134 return "<sup>" .. s .. "</sup>"
135end
137function SmallCaps(s)
138 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
139end
141function Strikeout(s)
142 return '<del>' .. s .. '</del>'
143end
145function Link(s, src, tit, attr)
146 return "<a href='" .. escape(src,true) .. "' title='" ..
147 escape(tit,true) .. "'>" .. s .. "</a>"
148end
150function Image(s, src, tit, attr)
151 return "<img src='" .. escape(src,true) .. "' title='" ..
152 escape(tit,true) .. "'/>"
153end
155function Code(s, attr)
156 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
157end
159function InlineMath(s)
160 return "\\(" .. escape(s) .. "\\)"
161end
163function DisplayMath(s)
164 return "\\[" .. escape(s) .. "\\]"
165end
167function SingleQuoted(s)
168 return "&lsquo;" .. s .. "&rsquo;"
169end
171function DoubleQuoted(s)
172 return "&ldquo;" .. s .. "&rdquo;"
173end
175function Note(s)
176 local num = #notes + 1
177 -- insert the back reference right before the final closing tag.
178 s = string.gsub(s,
179 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
180 -- add a list item with the note to the note table.
181 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
182 -- return the footnote reference, linked to the note.
183 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
184 '"><sup>' .. num .. '</sup></a>'
185end
187function Span(s, attr)
188 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
189end
191function RawInline(format, str)
192 if format == "html" then
193 return str
194 else
195 return ''
196 end
197end
199function Cite(s, cs)
200 local ids = {}
201 for _,cit in ipairs(cs) do
202 table.insert(ids, cit.citationId)
203 end
204 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
205 "\">" .. s .. "</span>"
206end
208function Plain(s)
209 return s
210end
212function Para(s)
213 return "<p>" .. s .. "</p>"
214end
216-- lev is an integer, the header level.
217function Header(lev, s, attr)
218 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
219end
221function BlockQuote(s)
222 return "<blockquote>\n" .. s .. "\n</blockquote>"
223end
225function HorizontalRule()
226 return "<hr/>"
227end
229function LineBlock(ls)
230 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
231 '</div>'
232end
234function CodeBlock(s, attr)
235 -- If code block has class 'dot', pipe the contents through dot
236 -- and base64, and include the base64-encoded png as a data: URL.
237 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
238 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
239 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
240 -- otherwise treat as code (one could pipe through a highlighter)
241 else
242 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
243 "</code></pre>"
244 end
245end
247function BulletList(items)
248 local buffer = {}
249 for _, item in pairs(items) do
250 table.insert(buffer, "<li>" .. item .. "</li>")
251 end
252 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
253end
255function OrderedList(items)
256 local buffer = {}
257 for _, item in pairs(items) do
258 table.insert(buffer, "<li>" .. item .. "</li>")
259 end
260 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
261end
263function DefinitionList(items)
264 local buffer = {}
265 for _,item in pairs(items) do
266 local k, v = next(item)
267 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
268 table.concat(v, "</dd>\n<dd>") .. "</dd>")
269 end
270 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
271end
273-- Convert pandoc alignment to something HTML can use.
274-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
275function html_align(align)
276 if align == 'AlignLeft' then
277 return 'left'
278 elseif align == 'AlignRight' then
279 return 'right'
280 elseif align == 'AlignCenter' then
281 return 'center'
282 else
283 return 'left'
284 end
285end
287function CaptionedImage(src, tit, caption, attr)
288 return '<div class="figure">\n<img src="' .. escape(src,true) ..
289 '" title="' .. escape(tit,true) .. '"/>\n' ..
290 '<p class="caption">' .. caption .. '</p>\n</div>'
291end
293-- Caption is a string, aligns is an array of strings,
294-- widths is an array of floats, headers is an array of
295-- strings, rows is an array of arrays of strings.
296function Table(caption, aligns, widths, headers, rows)
297 local buffer = {}
298 local function add(s)
299 table.insert(buffer, s)
300 end
301 add("<table>")
302 if caption ~= "" then
303 add("<caption>" .. caption .. "</caption>")
304 end
305 if widths and widths[1] ~= 0 then
306 for _, w in pairs(widths) do
307 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
308 end
309 end
310 local header_row = {}
311 local empty_header = true
312 for i, h in pairs(headers) do
313 local align = html_align(aligns[i])
314 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
315 empty_header = empty_header and h == ""
316 end
317 if empty_header then
318 head = ""
319 else
320 add('<tr class="header">')
321 for _,h in pairs(header_row) do
322 add(h)
323 end
324 add('</tr>')
325 end
326 local class = "even"
327 for _, row in pairs(rows) do
328 class = (class == "even" and "odd") or "even"
329 add('<tr class="' .. class .. '">')
330 for i,c in pairs(row) do
331 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
332 end
333 add('</tr>')
334 end
335 add('</table>')
336 return table.concat(buffer,'\n')
337end
339function RawBlock(format, str)
340 if format == "html" then
341 return str
342 else
343 return ''
344 end
345end
347function Div(s, attr)
348 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
349end
351-- The following code will produce runtime warnings when you haven't defined
352-- all of the functions you need for the custom writer, so it's useful
353-- to include when you're working on a writer.
354local meta = {}
355meta.__index =
356 function(_, key)
357 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
358 return function() return "" end
359 end
360setmetatable(_G, meta)