Debian Codegraph @

inst/rmd/ioslides/ioslides_presentation.lua

raw symbols SCIP
1
2 -- Table to store footnotes, so they can be included at the end.
3 local notes = {}
4
5 -- Rendering state
6 local in_slide = false
7 local build_slide = false
8 local in_notes = false
9
10 -- Character escaping
11 local function escape(s, in_attribute)
12 s = s:gsub("[<>&\"']",
13 function(x)
14 if x == '<' then
15 return '&lt;'
16 elseif x == '>' then
17 return '&gt;'
18 elseif x == '&' then
19 return '&amp;'
20 elseif x == '"' then
21 return '&quot;'
22 elseif x == "'" then
23 return '&#39;'
24 else
25 return x
26 end
27 end)
28 return s
29 end
30
31
32 -- Helper function to convert an attributes table into
33 -- a string that can be put into HTML tags.
34 local function attributes(attr)
35 local attr_table = {}
36 for x,y in pairs(attr) do
37 if y and y ~= "" then
38 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"')
39 end
40 end
41 return table.concat(attr_table)
42 end
43
44 -- Helper function to split a string on spaces
45 -- returns a table
46 local function split(str)
47 local words = {}
48 for word in str:gmatch("%S+") do table.insert(words, word) end
49 return words
50 end
51
52 -- Helper function to remove duplicates
53 -- returns a table http://stackoverflow.com/a/20067270
54 local function uniq(t)
55 local seen = {}
56 local res = {}
57 for _,v in ipairs(t) do
58 if (not seen[v]) then
59 res[#res+1] = v
60 seen[v] = true
61 end
62 end
63 return res
64 end
65
66 -- Helper function to filter a list
67 -- returns a table
68 local function grep(f, l)
69 local res = {}
70 for _,v in ipairs(l) do
71 if (f(v)) then
72 res[#res+1] = v
73 end
74 end
75 return res
76 end
77
78 -- Blocksep is used to separate block elements.
79 function Blocksep()
80 return "\n\n"
81 end
82
83 local function CompleteSlide()
84 if (in_slide) then
85 in_slide = false
86 return "</article></slide>"
87 else
88 return ""
89 end
90 end
91
92
93 -- This function is called once for the whole document. Parameters:
94 -- body is a string, metadata is a table, variables is a table.
95 -- One could use some kind of templating
96 -- system here; this just gives you a simple standalone HTML file.
97 function Doc(body, metadata, variables)
98 -- complete any active slide
99 local suffix = CompleteSlide()
100
101 return body .. suffix
102 end
103
104 -- The functions that follow render corresponding pandoc elements.
105 -- s is always a string, attr is always a table of attributes, and
106 -- items is always an array of strings (the items in a list).
107 -- Comments indicate the types of other variables.
108
109 function Str(s)
110 s = escape(s)
111 if smart then
112 s = s:gsub("%-%-%-", "&#8212;")
113 s = s:gsub("%-%-", "&#8211;")
114 s = s:gsub("%.%.%.", "&#8230;")
115 end
116 return s
117 end
118
119 function Space()
120 return " "
121 end
122
123 function LineBreak()
124 return "<br/>"
125 end
126
127 function SoftBreak()
128 return " "
129 end
130
131 function Emph(s)
132 return "<em>" .. s .. "</em>"
133 end
134
135 function Underline(s)
136 return "<u>" .. s .. "</u>"
137 end
138
139 function Strong(s)
140 return "<strong>" .. s .. "</strong>"
141 end
142
143 function Subscript(s)
144 return "<sub>" .. s .. "</sub>"
145 end
146
147 function Superscript(s)
148 return "<sup>" .. s .. "</sup>"
149 end
150
151 function SmallCaps(s)
152 return '<span style="font-variant: small-caps;">' .. s .. '</span>'
153 end
154
155 function Strikeout(s)
156 return '<del>' .. s .. '</del>'
157 end
158
159 function SingleQuoted(s)
160 if smart then
161 return '&lsquo;' .. s .. '&rsquo;'
162 else
163 return '"' .. s .. '"'
164 end
165 end
166
167 function DoubleQuoted(s)
168 if smart then
169 return '&ldquo;' .. s .. '&rdquo;'
170 else
171 return '"' .. s .. '"'
172 end
173 end
174
175 function Link(s, src, tit)
176 return "<a href='" .. escape(src,true) .. "' title='" ..
177 escape(tit,true) .. "'>" .. s .. "</a>"
178 end
179
180 function Image(s, src, tit, attr)
181 return "<img" .. attributes(attr) .. " src='" .. escape(src,true) .. "' title='" ..
182 escape(tit,true) .. "'/>"
183 end
184
185 function CaptionedImage(src, tit, s, attr)
186 local caption = ""
187 if fig_caption and (string.len(s) > 0) then
188 caption = "<p class='caption'>" .. s .. "</p>"
189 end
190 return Image(s, src, tit, attr) .. caption
191 end
192
193 function Code(s, attr)
194 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>"
195 end
196
197 function InlineMath(s)
198 s = escape(s)
199 if mathjax then
200 return "\\(" .. s .. "\\)"
201 else
202 return "$" .. s .. "$"
203 end
204 end
205
206 function DisplayMath(s)
207 s = escape(s)
208 if mathjax then
209 return "\\[" .. s .. "\\]"
210 else
211 return "$$" .. s .. "$$"
212 end
213 end
214
215 function Note(s)
216 local num = #notes + 1
217 -- insert the back reference right before the final closing tag.
218 s = string.gsub(s,
219 '(.*)</', '%1 <a href="#fnref' .. num .. '">&#8617;</a></')
220 -- add a list item with the note to the note table.
221 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>')
222 -- return the footnote reference, linked to the note.
223 return '<a id="fnref' .. num .. '" href="#fn' .. num ..
224 '"><sup>' .. num .. '</sup></a>'
225 end
226
227 function Span(s, attr)
228 return "<span" .. attributes(attr) .. ">" .. s .. "</span>"
229 end
230
231 function Cite(s)
232 return "<span class=\"cite\">" .. s .. "</span>"
233 end
234
235 function Plain(s)
236 return s
237 end
238
239 function Para(s)
240 return "<p>" .. s .. "</p>"
241 end
242
243 -- lev is an integer, the header level.
244 function Header(lev, s, attr)
245
246 -- detect level 1 header and convert it to a segue slide
247 local slide_class = ""
248 local hgroup_class = ""
249 local slide_style = ""
250
251 -- make all headers < slide_level as segue slides
252 if lev < slide_level then
253 -- create a segue slide but add lev class for possible customization
254 slide_class = "segue dark nobackground" .. " level" .. lev
255 hgroup_class = " class = 'auto-fadein'"
256 lev = 2
257 end
258
259 -- support for slide specific image backgrounds
260 -- alternative is this css
261 -- slide > slide [data-slide-num="7"] {
262 -- background-image: url("figures/xx.jpg");
263 -- }
264 if attr["data-background"] then
265 -- dark is incompatible with fill and let us uniquify nobackground
266 local slide = split(slide_class .. " fill nobackground")
267 slide = grep(function (v)
268 if v:match("^dark$") then return false else return true end
269 end, slide)
270 slide_class = table.concat(uniq(slide), " ")
271 if attr["data-background"]:match("^#") then
272 slide_style = 'background: ' .. attr["data-background"] .. ';'
273 else
274 -- assume url
275 slide_style = 'background-image: url(' .. attr["data-background"] .. ');'
276 local bg_size = attr["data-background-size"]
277 if not bg_size then bg_size = "contain" end
278 slide_style = slide_style .. ' background-size: ' .. bg_size .. ';'
279 local bg_position = attr["data-background-position"]
280 if not bg_position then bg_position = "center" end
281 slide_style = slide_style .. ' background-position: ' .. bg_position .. ';'
282 end
283 -- remove noise attributes for article
284 attr["data-background"] = nil
285 attr["data-background-size"] = nil
286 attr["data-background-position"] = nil
287 end
288
289 -- extract optional subtitle
290 local subtitle = ""
291 if lev == 2 then
292 local i, j = string.find(s, "|")
293 if i then
294 subtitle = string.sub(s, i+1, string.len(s))
295 s = string.sub(s, 1, i-1)
296 end
297 end
298
299 -- trick: as lev value 2 is used in code below to start a new slide
300 -- we force all lev <= slide_level as new slides
301 if lev > 2 and lev <= slide_level then
302 lev = 2
303 end
304
305 -- build slide header (including optional subtitle)
306 local header = "<h" .. lev .. ">" .. s .. "</h" .. lev .. ">"
307 if string.len(subtitle) > 0 then
308 header = header .. "<h3>" .. subtitle .. "</h3>"
309 end
310
311 -- treat level 2 headers as slides
312 if lev == 2 then
313
314 -- complete previous slide
315 local preface = CompleteSlide()
316
317 -- start a new slide
318 in_slide = true
319
320 -- update build flag (used by ol and ul)
321 if attr["class"] and string.find(attr["class"], "build") then
322 build_slide = true
323 else
324 build_slide = false
325 end
326
327 -- add 'smaller' class if it was globally specified
328 if smaller then
329 if (attr["class"]) then
330 attr["class"] = "smaller " .. attr["class"]
331 else
332 attr["class"] = "smaller"
333 end
334 end
335
336 if string.len(slide_style) > 0 then
337 slide_style = ' style="' .. slide_style .. '"'
338 end
339
340 -- return the beginning of the slide
341 return preface .. "<slide class=\"" .. slide_class .. "\"" .. slide_style .. ">" ..
342 "<hgroup" .. hgroup_class .. ">" .. header .. "</hgroup>" ..
343 "<article " .. attributes(attr) .. ">"
344 else
345 return header
346 end
347
348 end
349
350 function BlockQuote(s)
351 -- if this is a list then blockquote means invert the default
352 -- incremental behavior
353 local prefix = string.sub(s, 1, 3)
354 if prefix == "<ol" or prefix == "<ul" then
355 if not incremental then
356 s = s:gsub(prefix .. ">", prefix .. " class = 'build'>", 1)
357 else
358 s = s:gsub(prefix .. " class = 'build'>", prefix .. ">", 1)
359 end
360 return s
361 else
362 return "<blockquote>\n" .. s .. "\n</blockquote>"
363 end
364 end
365
366 function HorizontalRule()
367 return Header(2, "", {})
368 end
369
370 function CodeBlock(s, attr)
371
372 -- if this code block has a class then add prettyprint to it
373 local class_attrib = ''
374 if attr["class"] then
375 local class = attr["class"]
376 if string.len(class) > 0 then
377 class_attrib = "class = 'prettyprint lang-" .. class .. "'"
378 end
379 end
380
381 -- substitute for code highlighting/emphasis
382 local code = escape(s)
383 code_sub = code:gsub("[ \t]*[#/]+[ \t]*&lt;b&gt;[ \t]*\n", "<b>")
384 code = code_sub:gsub("[ \t]*[#/]+[ \t]*&lt;/b&gt;[ \t]*\n", "</b>")
385 code = code:gsub("[ \t]*[#/]+[ \t]*&lt;/b&gt;[ \t]*$", "</b>")
386
387 return "<pre " .. class_attrib .. ">" .. code .. "</pre>"
388 end
389
390 function BulletList(items)
391 local buffer = {}
392 for _, item in pairs(items) do
393 table.insert(buffer, "<li>" .. item .. "</li>")
394 end
395 list_class = ""
396 if incremental then
397 list_class = " class = 'build'"
398 end
399 return "<ul" .. list_class .. ">\n" .. table.concat(buffer, "\n") .. "\n</ul>"
400 end
401
402 function OrderedList(items)
403 local buffer = {}
404 for _, item in pairs(items) do
405 table.insert(buffer, "<li>" .. item .. "</li>")
406 end
407 list_class = ""
408 if incremental then
409 list_class = " class = 'build'"
410 end
411 return "<ol" .. list_class .. ">\n" .. table.concat(buffer, "\n") .. "\n</ol>"
412 end
413
414 -- Revisit association list STackValue instance.
415 function DefinitionList(items)
416 local buffer = {}
417 for _,item in pairs(items) do
418 for k, v in pairs(item) do
419 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" ..
420 table.concat(v,"</dd>\n<dd>") .. "</dd>")
421 end
422 end
423 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>"
424 end
425
426 -- Convert pandoc alignment to something HTML can use.
427 -- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault.
428 function html_align(align)
429 if align == 'AlignLeft' then
430 return 'left'
431 elseif align == 'AlignRight' then
432 return 'right'
433 elseif align == 'AlignCenter' then
434 return 'center'
435 else
436 return 'left'
437 end
438 end
439
440 -- Caption is a string, aligns is an array of strings,
441 -- widths is an array of floats, headers is an array of
442 -- strings, rows is an array of arrays of strings.
443 function Table(caption, aligns, widths, headers, rows)
444 local buffer = {}
445 local function add(s)
446 table.insert(buffer, s)
447 end
448 add("<table class = 'rmdtable'>")
449 if caption ~= "" then
450 add("<caption>" .. caption .. "</caption>")
451 end
452 if widths and widths[1] ~= 0 then
453 for _, w in pairs(widths) do
454 add('<col width="' .. string.format("%f%%", w * 100) .. '" />')
455 end
456 end
457 local header_row = {}
458 local empty_header = true
459 for i, h in pairs(headers) do
460 local align = html_align(aligns[i])
461 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>')
462 empty_header = empty_header and h == ""
463 end
464 if empty_header then
465 head = ""
466 else
467 add('<tr class="header">')
468 for _,h in pairs(header_row) do
469 add(h)
470 end
471 add('</tr>')
472 end
473 local class = "even"
474 for _, row in pairs(rows) do
475 class = (class == "even" and "odd") or "even"
476 add('<tr class="' .. class .. '">')
477 for i,c in pairs(row) do
478 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>')
479 end
480 add('</tr>')
481 end
482 add('</table>')
483 return table.concat(buffer,'\n')
484 end
485
486 function Div(s, attr)
487 if attr["class"] == "notes" then
488 return "<aside class='note'><section>" .. s ..
489 "</section></aside>"
490 else
491 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>"
492 end
493 end
494
495 function Span(s, attr)
496 return "<span" .. attributes(attr) .. ">\n" .. s .. "</span>"
497 end
498
499 function RawInline(f, s)
500 if f == "html" then
501 return s
502 else
503 return ''
504 end
505 end
506
507 function RawBlock(f, s)
508 if f == "html" then
509 return s
510 else
511 return ''
512 end
513 end
514
515 -- The following code will produce runtime warnings when you haven't defined
516 -- all of the functions you need for the custom writer, so it's useful
517 -- to include when you're working on a writer.
518 local meta = {}
519 meta.__index =
520 function(_, key)
521 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key))
522 return function() return "" end
523 end
524 setmetatable(_G, meta)