| 1 |
|
| 2 |
|
| 3 |
local notes = {} |
| 4 |
|
| 5 |
|
| 6 |
local in_slide = false |
| 7 |
local build_slide = false |
| 8 |
local in_notes = false |
| 9 |
|
| 10 |
|
| 11 |
local function escape(s, in_attribute) |
| 12 |
s = s:gsub("[<>&\"']", |
| 13 |
function(x) |
| 14 |
if x == '<' then |
| 15 |
return '<' |
| 16 |
elseif x == '>' then |
| 17 |
return '>' |
| 18 |
elseif x == '&' then |
| 19 |
return '&' |
| 20 |
elseif x == '"' then |
| 21 |
return '"' |
| 22 |
elseif x == "'" then |
| 23 |
return ''' |
| 24 |
else |
| 25 |
return x |
| 26 |
end |
| 27 |
end) |
| 28 |
return s |
| 29 |
end |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 53 |
|
| 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 |
|
| 67 |
|
| 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 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
function Doc(body, metadata, variables) |
| 98 |
|
| 99 |
local suffix = CompleteSlide() |
| 100 |
|
| 101 |
return body .. suffix |
| 102 |
end |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
function Str(s) |
| 110 |
s = escape(s) |
| 111 |
if smart then |
| 112 |
s = s:gsub("%-%-%-", "—") |
| 113 |
s = s:gsub("%-%-", "–") |
| 114 |
s = s:gsub("%.%.%.", "…") |
| 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 '‘' .. s .. '’' |
| 162 |
else |
| 163 |
return '"' .. s .. '"' |
| 164 |
end |
| 165 |
end |
| 166 |
|
| 167 |
function DoubleQuoted(s) |
| 168 |
if smart then |
| 169 |
return '“' .. s .. '”' |
| 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 |
|
| 218 |
s = string.gsub(s, |
| 219 |
'(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></') |
| 220 |
|
| 221 |
table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>') |
| 222 |
|
| 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 |
|
| 244 |
function Header(lev, s, attr) |
| 245 |
|
| 246 |
|
| 247 |
local slide_class = "" |
| 248 |
local hgroup_class = "" |
| 249 |
local slide_style = "" |
| 250 |
|
| 251 |
|
| 252 |
if lev < slide_level then |
| 253 |
|
| 254 |
slide_class = "segue dark nobackground" .. " level" .. lev |
| 255 |
hgroup_class = " class = 'auto-fadein'" |
| 256 |
lev = 2 |
| 257 |
end |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if attr["data-background"] then |
| 265 |
|
| 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 |
|
| 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 |
|
| 284 |
attr["data-background"] = nil |
| 285 |
attr["data-background-size"] = nil |
| 286 |
attr["data-background-position"] = nil |
| 287 |
end |
| 288 |
|
| 289 |
|
| 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 |
|
| 300 |
|
| 301 |
if lev > 2 and lev <= slide_level then |
| 302 |
lev = 2 |
| 303 |
end |
| 304 |
|
| 305 |
|
| 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 |
|
| 312 |
if lev == 2 then |
| 313 |
|
| 314 |
|
| 315 |
local preface = CompleteSlide() |
| 316 |
|
| 317 |
|
| 318 |
in_slide = true |
| 319 |
|
| 320 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 352 |
|
| 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 |
|
| 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 |
|
| 382 |
local code = escape(s) |
| 383 |
code_sub = code:gsub("[ \t]*[#/]+[ \t]*<b>[ \t]*\n", "<b>") |
| 384 |
code = code_sub:gsub("[ \t]*[#/]+[ \t]*</b>[ \t]*\n", "</b>") |
| 385 |
code = code:gsub("[ \t]*[#/]+[ \t]*</b>[ \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 |
|
| 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 |
|
| 427 |
|
| 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 |
|
| 441 |
|
| 442 |
|
| 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 |
|
| 516 |
|
| 517 |
|
| 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) |