git.iko.soy/typeable/pandoc
on 1.12.1 help sign in
typeable/pandoc/data/sample.lua
320 lines 8.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.
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-- One could use some kind of templating
71-- system here; this just gives you a simple standalone HTML file.
72function Doc(body, metadata, variables)
73 local buffer = {}
74 local function add(s)
75 table.insert(buffer, s)
76 end
77 add('<!DOCTYPE html>')
78 add('<html>')
79 add('<head>')
80 add('<title>' .. (metadata['title'] or '') .. '</title>')
81 add('</head>')
82 add('<body>')
83 if metadata['title'] and metadata['title'] ~= "" then
84 add('<h1 class="title">' .. metadata['title'] .. '</h1>')
85 end
86 for _, author in pairs(metadata['author'] or {}) do
87 add('<h2 class="author">' .. author .. '</h2>')
88 end
89 if metadata['date'] and metadata['date'] ~= "" then
90 add('<h3 class="date">' .. metadata.date .. '</h3>')
91 end
92 add(body)
93 if #notes > 0 then
94 add('<ol class="footnotes">')
95 for _,note in pairs(notes) do
96 add(note)
97 end
98 add('</ol>')
99 end
100 add('</body>')
101 add('</html>')
102 return table.concat(buffer,'\n')
103end
105-- The functions that follow render corresponding pandoc elements.
106-- s is always a string, attr is always a table of attributes, and
107-- items is always an array of strings (the items in a list).
108-- Comments indicate the types of other variables.
110function Str(s)
111 return escape(s)
112end
114function Space()
115 return " "
116end
118function LineBreak()
119 return "<br/>"
120end
122function Emph(s)
123 return "<em>" .. s .. "</em>"
124end
126function Strong(s)
127 return "<strong>" .. s .. "</strong>"
128end
130function Subscript(s)
131 return "<sub>" .. s .. "</sub>"
132end
134function Superscript(s)
135 return "<sup>" .. s .. "</sup>"
136end
138function SmallCaps(s)
139 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
140end
142function Strikeout(s)
143 return '<del>' .. s .. '</del>'
144end
146function Link(s, src, tit)
147 return "<a href='" .. escape(src,true) .. "' title='" ..
148 escape(tit,true) .. "'>" .. s .. "</a>"
149end
151function Image(s, src, tit)
152 return "<img src='" .. escape(src,true) .. "' title='" ..
153 escape(tit,true) .. "'/>"
154end
156function Code(s, attr)
157 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
158end
160function InlineMath(s)
161 return "\\(" .. escape(s) .. "\\)"
162end
164function DisplayMath(s)
165 return "\\[" .. escape(s) .. "\\]"
166end
168function Note(s)
169 local num = #notes + 1
170 -- insert the back reference right before the final closing tag.
171 s = string.gsub(s,
172 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
173 -- add a list item with the note to the note table.
174 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
175 -- return the footnote reference, linked to the note.
176 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
177 '"><sup>' .. num .. '</sup></a>'
178end
180function Span(s, attr)
181 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
182end
184function Plain(s)
185 return s
186end
188function Para(s)
189 return "<p>" .. s .. "</p>"
190end
192-- lev is an integer, the header level.
193function Header(lev, s, attr)
194 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
195end
197function BlockQuote(s)
198 return "<blockquote>\n" .. s .. "\n</blockquote>"
199end
201function HorizontalRule()
202 return "<hr/>"
203end
205function CodeBlock(s, attr)
206 -- If code block has class 'dot', pipe the contents through dot
207 -- and base64, and include the base64-encoded png as a data: URL.
208 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
209 local png = pipe("base64", pipe("dot -Tpng", s))
210 return '<img src="data:image/png;base64,' .. png .. '"/>'
211 -- otherwise treat as code (one could pipe through a highlighter)
212 else
213 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
214 "</code></pre>"
215 end
216end
218function BulletList(items)
219 local buffer = {}
220 for _, item in pairs(items) do
221 table.insert(buffer, "<li>" .. item .. "</li>")
222 end
223 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
224end
226function OrderedList(items)
227 local buffer = {}
228 for _, item in pairs(items) do
229 table.insert(buffer, "<li>" .. item .. "</li>")
230 end
231 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
232end
234-- Revisit association list STackValue instance.
235function DefinitionList(items)
236 local buffer = {}
237 for _,item in pairs(items) do
238 for k, v in pairs(item) do
239 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
240 table.concat(v,"</dd>\n<dd>") .. "</dd>")
241 end
242 end
243 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
244end
246-- Convert pandoc alignment to something HTML can use.
247-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
248function html_align(align)
249 if align == 'AlignLeft' then
250 return 'left'
251 elseif align == 'AlignRight' then
252 return 'right'
253 elseif align == 'AlignCenter' then
254 return 'center'
255 else
256 return 'left'
257 end
258end
260-- Caption is a string, aligns is an array of strings,
261-- widths is an array of floats, headers is an array of
262-- strings, rows is an array of arrays of strings.
263function Table(caption, aligns, widths, headers, rows)
264 local buffer = {}
265 local function add(s)
266 table.insert(buffer, s)
267 end
268 add("<table>")
269 if caption ~= "" then
270 add("<caption>" .. caption .. "</caption>")
271 end
272 if widths and widths[1] ~= 0 then
273 for _, w in pairs(widths) do
274 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
275 end
276 end
277 local header_row = {}
278 local empty_header = true
279 for i, h in pairs(headers) do
280 local align = html_align(aligns[i])
281 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
282 empty_header = empty_header and h == ""
283 end
284 if empty_header then
285 head = ""
286 else
287 add('<tr class="header">')
288 for _,h in pairs(header_row) do
289 add(h)
290 end
291 add('</tr>')
292 end
293 local class = "even"
294 for _, row in pairs(rows) do
295 class = (class == "even" and "odd") or "even"
296 add('<tr class="' .. class .. '">')
297 for i,c in pairs(row) do
298 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
299 end
300 add('</tr>')
301 end
302 add('</table')
303 return table.concat(buffer,'\n')
304end
306function Div(s, attr)
307 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
308end
310-- The following code will produce runtime warnings when you haven't defined
311-- all of the functions you need for the custom writer, so it's useful
312-- to include when you're working on a writer.
313local meta = {}
314meta.__index =
315 function(_, key)
316 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
317 return function() return "" end
318 end
319setmetatable(_G, meta)