git.iko.soy/typeable/pandoc
on 5a1796e help sign in
typeable/pandoc/data/sample.lua
341 lines 8.9 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 end
177end
179function Cite(s, cs)
180 local ids = {}
181 for _,cit in ipairs(cs) do
182 table.insert(ids, cit.citationId)
183 end
184 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
185 "\">" .. s .. "</span>"
186end
188function Plain(s)
189 return s
190end
192function Para(s)
193 return "<p>" .. s .. "</p>"
194end
196-- lev is an integer, the header level.
197function Header(lev, s, attr)
198 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
199end
201function BlockQuote(s)
202 return "<blockquote>\n" .. s .. "\n</blockquote>"
203end
205function HorizontalRule()
206 return "<hr/>"
207end
209function LineBlock(ls)
210 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') ..
211 '</div>'
212end
214function CodeBlock(s, attr)
215 -- If code block has class 'dot', pipe the contents through dot
216 -- and base64, and include the base64-encoded png as a data: URL.
217 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
218 local png = pipe("base64", pipe("dot -Tpng", s))
219 return '<img src="data:image/png;base64,' .. png .. '"/>'
220 -- otherwise treat as code (one could pipe through a highlighter)
221 else
222 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
223 "</code></pre>"
224 end
225end
227function BulletList(items)
228 local buffer = {}
229 for _, item in pairs(items) do
230 table.insert(buffer, "<li>" .. item .. "</li>")
231 end
232 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
233end
235function OrderedList(items)
236 local buffer = {}
237 for _, item in pairs(items) do
238 table.insert(buffer, "<li>" .. item .. "</li>")
239 end
240 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
241end
243-- Revisit association list STackValue instance.
244function DefinitionList(items)
245 local buffer = {}
246 for _,item in pairs(items) do
247 for k, v in pairs(item) do
248 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
249 table.concat(v,"</dd>\n<dd>") .. "</dd>")
250 end
251 end
252 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
253end
255-- Convert pandoc alignment to something HTML can use.
256-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
257function html_align(align)
258 if align == 'AlignLeft' then
259 return 'left'
260 elseif align == 'AlignRight' then
261 return 'right'
262 elseif align == 'AlignCenter' then
263 return 'center'
264 else
265 return 'left'
266 end
267end
269function CaptionedImage(src, tit, caption, attr)
270 return '<div class="figure">\n<img src="' .. escape(src,true) ..
271 '" title="' .. escape(tit,true) .. '"/>\n' ..
272 '<p class="caption">' .. caption .. '</p>\n</div>'
273end
275-- Caption is a string, aligns is an array of strings,
276-- widths is an array of floats, headers is an array of
277-- strings, rows is an array of arrays of strings.
278function Table(caption, aligns, widths, headers, rows)
279 local buffer = {}
280 local function add(s)
281 table.insert(buffer, s)
282 end
283 add("<table>")
284 if caption ~= "" then
285 add("<caption>" .. caption .. "</caption>")
286 end
287 if widths and widths[1] ~= 0 then
288 for _, w in pairs(widths) do
289 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
290 end
291 end
292 local header_row = {}
293 local empty_header = true
294 for i, h in pairs(headers) do
295 local align = html_align(aligns[i])
296 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
297 empty_header = empty_header and h == ""
298 end
299 if empty_header then
300 head = ""
301 else
302 add('<tr class="header">')
303 for _,h in pairs(header_row) do
304 add(h)
305 end
306 add('</tr>')
307 end
308 local class = "even"
309 for _, row in pairs(rows) do
310 class = (class == "even" and "odd") or "even"
311 add('<tr class="' .. class .. '">')
312 for i,c in pairs(row) do
313 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
314 end
315 add('</tr>')
316 end
317 add('</table')
318 return table.concat(buffer,'\n')
319end
321function RawBlock(format, str)
322 if format == "html" then
323 return str
324 end
325end
327function Div(s, attr)
328 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
329end
331-- The following code will produce runtime warnings when you haven't defined
332-- all of the functions you need for the custom writer, so it's useful
333-- to include when you're working on a writer.
334local meta = {}
335meta.__index =
336 function(_, key)
337 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
338 return function() return "" end
339 end
340setmetatable(_G, meta)