git.iko.soy/typeable/pandoc
on 1.14.0.3 help sign in
typeable/pandoc/data/sample.lua
314 lines 8.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.
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')
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 LineBreak()
104 return "<br/>"
105end
107function Emph(s)
108 return "<em>" .. s .. "</em>"
109end
111function Strong(s)
112 return "<strong>" .. s .. "</strong>"
113end
115function Subscript(s)
116 return "<sub>" .. s .. "</sub>"
117end
119function Superscript(s)
120 return "<sup>" .. s .. "</sup>"
121end
123function SmallCaps(s)
124 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
125end
127function Strikeout(s)
128 return '<del>' .. s .. '</del>'
129end
131function Link(s, src, tit)
132 return "<a href='" .. escape(src,true) .. "' title='" ..
133 escape(tit,true) .. "'>" .. s .. "</a>"
134end
136function Image(s, src, tit)
137 return "<img src='" .. escape(src,true) .. "' title='" ..
138 escape(tit,true) .. "'/>"
139end
141function Code(s, attr)
142 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
143end
145function InlineMath(s)
146 return "\\(" .. escape(s) .. "\\)"
147end
149function DisplayMath(s)
150 return "\\[" .. escape(s) .. "\\]"
151end
153function Note(s)
154 local num = #notes + 1
155 -- insert the back reference right before the final closing tag.
156 s = string.gsub(s,
157 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
158 -- add a list item with the note to the note table.
159 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
160 -- return the footnote reference, linked to the note.
161 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
162 '"><sup>' .. num .. '</sup></a>'
163end
165function Span(s, attr)
166 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
167end
169function Cite(s, cs)
170 local ids = {}
171 for _,cit in ipairs(cs) do
172 table.insert(ids, cit.citationId)
173 end
174 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") ..
175 "\">" .. s .. "</span>"
176end
178function Plain(s)
179 return s
180end
182function Para(s)
183 return "<p>" .. s .. "</p>"
184end
186-- lev is an integer, the header level.
187function Header(lev, s, attr)
188 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">"
189end
191function BlockQuote(s)
192 return "<blockquote>\n" .. s .. "\n</blockquote>"
193end
195function HorizontalRule()
196 return "<hr/>"
197end
199function CodeBlock(s, attr)
200 -- If code block has class 'dot', pipe the contents through dot
201 -- and base64, and include the base64-encoded png as a data: URL.
202 if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then
203 local png = pipe("base64", pipe("dot -Tpng", s))
204 return '<img src="data:image/png;base64,' .. png .. '"/>'
205 -- otherwise treat as code (one could pipe through a highlighter)
206 else
207 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) ..
208 "</code></pre>"
209 end
210end
212function BulletList(items)
213 local buffer = {}
214 for _, item in pairs(items) do
215 table.insert(buffer, "<li>" .. item .. "</li>")
216 end
217 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>"
218end
220function OrderedList(items)
221 local buffer = {}
222 for _, item in pairs(items) do
223 table.insert(buffer, "<li>" .. item .. "</li>")
224 end
225 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
226end
228-- Revisit association list STackValue instance.
229function DefinitionList(items)
230 local buffer = {}
231 for _,item in pairs(items) do
232 for k, v in pairs(item) do
233 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
234 table.concat(v,"</dd>\n<dd>") .. "</dd>")
235 end
236 end
237 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
238end
240-- Convert pandoc alignment to something HTML can use.
241-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
242function html_align(align)
243 if align == 'AlignLeft' then
244 return 'left'
245 elseif align == 'AlignRight' then
246 return 'right'
247 elseif align == 'AlignCenter' then
248 return 'center'
249 else
250 return 'left'
251 end
252end
254-- Caption is a string, aligns is an array of strings,
255-- widths is an array of floats, headers is an array of
256-- strings, rows is an array of arrays of strings.
257function Table(caption, aligns, widths, headers, rows)
258 local buffer = {}
259 local function add(s)
260 table.insert(buffer, s)
261 end
262 add("<table>")
263 if caption ~= "" then
264 add("<caption>" .. caption .. "</caption>")
265 end
266 if widths and widths[1] ~= 0 then
267 for _, w in pairs(widths) do
268 add('<col width="' .. string.format("%d%%", w * 100) .. '" />')
269 end
270 end
271 local header_row = {}
272 local empty_header = true
273 for i, h in pairs(headers) do
274 local align = html_align(aligns[i])
275 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
276 empty_header = empty_header and h == ""
277 end
278 if empty_header then
279 head = ""
280 else
281 add('<tr class="header">')
282 for _,h in pairs(header_row) do
283 add(h)
284 end
285 add('</tr>')
286 end
287 local class = "even"
288 for _, row in pairs(rows) do
289 class = (class == "even" and "odd") or "even"
290 add('<tr class="' .. class .. '">')
291 for i,c in pairs(row) do
292 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
293 end
294 add('</tr>')
295 end
296 add('</table')
297 return table.concat(buffer,'\n')
298end
300function Div(s, attr)
301 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
302end
304-- The following code will produce runtime warnings when you haven't defined
305-- all of the functions you need for the custom writer, so it's useful
306-- to include when you're working on a writer.
307local meta = {}
308meta.__index =
309 function(_, key)
310 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
311 return function() return "" end
312 end
313setmetatable(_G, meta)