git.iko.soy/typeable/pandoc
on abc4bca help sign in
typeable/pandoc/data/sample.lua
336 lines 8.8 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 CodeBlock(s, attr)
210 -- If code block has class 'dot', pipe the contents through dot
211 -- and base64, and include the base64-encoded png as a data: URL.
212 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
213 local png = pipe("base64", pipe("dot -Tpng", s))
214 return '<img src="data:image/png;base64,' .. png .. '"/>'
215 -- otherwise treat as code (one could pipe through a highlighter)
216 else
217 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
218 "</code></pre>"
219 end
220end
222function BulletList(items)
223 local buffer = {}
224 for _, item in pairs(items) do
225 table.insert(buffer, "<li>" .. item .. "</li>")
226 end
227 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
228end
230function OrderedList(items)
231 local buffer = {}
232 for _, item in pairs(items) do
233 table.insert(buffer, "<li>" .. item .. "</li>")
234 end
235 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
236end
238-- Revisit association list STackValue instance.
239function DefinitionList(items)
240 local buffer = {}
241 for _,item in pairs(items) do
242 for k, v in pairs(item) do
243 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
244 table.concat(v,"</dd>\n<dd>") .. "</dd>")
245 end
246 end
247 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
248end
250-- Convert pandoc alignment to something HTML can use.
251-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
252function html_align(align)
253 if align == 'AlignLeft' then
254 return 'left'
255 elseif align == 'AlignRight' then
256 return 'right'
257 elseif align == 'AlignCenter' then
258 return 'center'
259 else
260 return 'left'
261 end
262end
264function CaptionedImage(src, tit, caption, attr)
265 return '<div class="figure">\n<img src="' .. escape(src,true) ..
266 '" title="' .. escape(tit,true) .. '"/>\n' ..
267 '<p class="caption">' .. caption .. '</p>\n</div>'
268end
270-- Caption is a string, aligns is an array of strings,
271-- widths is an array of floats, headers is an array of
272-- strings, rows is an array of arrays of strings.
273function Table(caption, aligns, widths, headers, rows)
274 local buffer = {}
275 local function add(s)
276 table.insert(buffer, s)
277 end
278 add("<table>")
279 if caption ~= "" then
280 add("<caption>" .. caption .. "</caption>")
281 end
282 if widths and widths[1] ~= 0 then
283 for _, w in pairs(widths) do
284 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
285 end
286 end
287 local header_row = {}
288 local empty_header = true
289 for i, h in pairs(headers) do
290 local align = html_align(aligns[i])
291 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
292 empty_header = empty_header and h == ""
293 end
294 if empty_header then
295 head = ""
296 else
297 add('<tr class="header">')
298 for _,h in pairs(header_row) do
299 add(h)
300 end
301 add('</tr>')
302 end
303 local class = "even"
304 for _, row in pairs(rows) do
305 class = (class == "even" and "odd") or "even"
306 add('<tr class="' .. class .. '">')
307 for i,c in pairs(row) do
308 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
309 end
310 add('</tr>')
311 end
312 add('</table')
313 return table.concat(buffer,'\n')
314end
316function RawBlock(format, str)
317 if format == "html" then
318 return str
319 end
320end
322function Div(s, attr)
323 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
324end
326-- The following code will produce runtime warnings when you haven't defined
327-- all of the functions you need for the custom writer, so it's useful
328-- to include when you're working on a writer.
329local meta = {}
330meta.__index =
331 function(_, key)
332 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
333 return function() return "" end
334 end
335setmetatable(_G, meta)