git.iko.soy/typeable/pandoc
on fdce771 help sign in
typeable/pandoc/data/sample.lua
353 lines 9.3 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 Note(s)
168 local num = #notes + 1
169 -- insert the back reference right before the final closing tag.
170 s = string.gsub(s,
171 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
172 -- add a list item with the note to the note table.
173 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
174 -- return the footnote reference, linked to the note.
175 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
176 '"><sup>' .. num .. '</sup></a>'
177end
179function Span(s, attr)
180 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
181end
183function RawInline(format, str)
184 if format == "html" then
185 return str
186 else
187 return ''
188 end
189end
191function Cite(s, cs)
192 local ids = {}
193 for _,cit in ipairs(cs) do
194 table.insert(ids, cit.citationId)
195 end
196 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
197 "\">" .. s .. "</span>"
198end
200function Plain(s)
201 return s
202end
204function Para(s)
205 return "<p>" .. s .. "</p>"
206end
208-- lev is an integer, the header level.
209function Header(lev, s, attr)
210 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
211end
213function BlockQuote(s)
214 return "<blockquote>\n" .. s .. "\n</blockquote>"
215end
217function HorizontalRule()
218 return "<hr/>"
219end
221function LineBlock(ls)
222 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
223 '</div>'
224end
226function CodeBlock(s, attr)
227 -- If code block has class 'dot', pipe the contents through dot
228 -- and base64, and include the base64-encoded png as a data: URL.
229 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
230 local img = pipe("base64", {}, pipe("dot", {"-T" .. image_format}, s))
231 return '<img src="data:' .. image_mime_type .. ';base64,' .. img .. '"/>'
232 -- otherwise treat as code (one could pipe through a highlighter)
233 else
234 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
235 "</code></pre>"
236 end
237end
239function BulletList(items)
240 local buffer = {}
241 for _, item in pairs(items) do
242 table.insert(buffer, "<li>" .. item .. "</li>")
243 end
244 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
245end
247function OrderedList(items)
248 local buffer = {}
249 for _, item in pairs(items) do
250 table.insert(buffer, "<li>" .. item .. "</li>")
251 end
252 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
253end
255function DefinitionList(items)
256 local buffer = {}
257 for _,item in pairs(items) do
258 local k, v = next(item)
259 table.insert(buffer, "<dt>" .. k .. "</dt>\n<dd>" ..
260 table.concat(v, "</dd>\n<dd>") .. "</dd>")
261 end
262 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
263end
265-- Convert pandoc alignment to something HTML can use.
266-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
267function html_align(align)
268 if align == 'AlignLeft' then
269 return 'left'
270 elseif align == 'AlignRight' then
271 return 'right'
272 elseif align == 'AlignCenter' then
273 return 'center'
274 else
275 return 'left'
276 end
277end
279function CaptionedImage(src, tit, caption, attr)
280 return '<div class="figure">\n<img src="' .. escape(src,true) ..
281 '" title="' .. escape(tit,true) .. '"/>\n' ..
282 '<p class="caption">' .. caption .. '</p>\n</div>'
283end
285-- Caption is a string, aligns is an array of strings,
286-- widths is an array of floats, headers is an array of
287-- strings, rows is an array of arrays of strings.
288function Table(caption, aligns, widths, headers, rows)
289 local buffer = {}
290 local function add(s)
291 table.insert(buffer, s)
292 end
293 add("<table>")
294 if caption ~= "" then
295 add("<caption>" .. caption .. "</caption>")
296 end
297 if widths and widths[1] ~= 0 then
298 for _, w in pairs(widths) do
299 add('<col width="' .. string.format("%.0f%%", w * 100) .. '" />')
300 end
301 end
302 local header_row = {}
303 local empty_header = true
304 for i, h in pairs(headers) do
305 local align = html_align(aligns[i])
306 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
307 empty_header = empty_header and h == ""
308 end
309 if empty_header then
310 head = ""
311 else
312 add('<tr class="header">')
313 for _,h in pairs(header_row) do
314 add(h)
315 end
316 add('</tr>')
317 end
318 local class = "even"
319 for _, row in pairs(rows) do
320 class = (class == "even" and "odd") or "even"
321 add('<tr class="' .. class .. '">')
322 for i,c in pairs(row) do
323 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
324 end
325 add('</tr>')
326 end
327 add('</table')
328 return table.concat(buffer,'\n')
329end
331function RawBlock(format, str)
332 if format == "html" then
333 return str
334 else
335 return ''
336 end
337end
339function Div(s, attr)
340 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
341end
343-- The following code will produce runtime warnings when you haven't defined
344-- all of the functions you need for the custom writer, so it's useful
345-- to include when you're working on a writer.
346local meta = {}
347meta.__index =
348 function(_, key)
349 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
350 return function() return "" end
351 end
352setmetatable(_G, meta)