Mercurial > irccd
view doc/html/html.lua @ 645:a63d73b456d5
Irccd: add transport_error
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 23 Mar 2018 21:15:10 +0100 |
parents | 27587ff92a64 |
children |
line wrap: on
line source
-- -- html.lua -- irccd HTML writer -- -- Copyright (c) 2013-2018 David Demelier <markand@malikania.fr> -- -- Permission to use, copy, modify, and/or distribute this software for any -- purpose with or without fee is hereby granted, provided that the above -- copyright notice and this permission notice appear in all copies. -- -- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- local function escape(s, in_attribute) return s:gsub("[<>&\"']", function(x) if x == '<' then return '<' elseif x == '>' then return '>' elseif x == '&' then return '&' elseif x == '"' then return '"' elseif x == "'" then return ''' else return x end end) end local function attributes(attr) local attr_table = {} for x,y in pairs(attr) do if y and y ~= "" then table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"') end end return table.concat(attr_table) end local function html_align(align) if align == 'AlignLeft' then return 'left' elseif align == 'AlignRight' then return 'right' elseif align == 'AlignCenter' then return 'center' else return 'left' end end local notes = {} function Blocksep() return "\n\n" end function Doc(body, metadata, variables) for k, v in pairs(metadata) do print(k .. " = " .. tostring(v)) end local buffer = {} local function add(s) table.insert(buffer, s) end add(body) if #notes > 0 then add('<ol class="footnotes">') for _,note in pairs(notes) do add(note) end add('</ol>') end return table.concat(buffer,'\n') .. '\n' end function Str(s) return escape(s) end function Space() return " " end function SoftBreak() return "\n" end function LineBreak() return "<br/>" end function Emph(s) return "<em>" .. s .. "</em>" end function Strong(s) return "<strong>" .. s .. "</strong>" end function Subscript(s) return "<sub>" .. s .. "</sub>" end function Superscript(s) return "<sup>" .. s .. "</sup>" end function SmallCaps(s) return '<span style="font-variant: small-caps;">' .. s .. '</span>' end function Strikeout(s) return '<del>' .. s .. '</del>' end function Link(s, src, tit, attr) return "<a href='" .. escape(src,true) .. "' title='" .. escape(tit,true) .. "'>" .. s .. "</a>" end function Image(s, src, tit, attr) return "<img src='" .. escape(src,true) .. "' title='" .. escape(tit,true) .. "'/>" end function Code(s, attr) return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>" end function InlineMath(s) return "\\(" .. escape(s) .. "\\)" end function DisplayMath(s) return "\\[" .. escape(s) .. "\\]" end function Note(s) local num = #notes + 1 -- insert the back reference right before the final closing tag. s = string.gsub(s, '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></') -- add a list item with the note to the note table. table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>') -- return the footnote reference, linked to the note. return '<a id="fnref' .. num .. '" href="#fn' .. num .. '"><sup>' .. num .. '</sup></a>' end function Span(s, attr) return "<span" .. attributes(attr) .. ">" .. s .. "</span>" end function RawInline(format, str) if format == "html" then return str else return '' end end function Cite(s, cs) local ids = {} for _,cit in ipairs(cs) do table.insert(ids, cit.citationId) end return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") .. "\">" .. s .. "</span>" end function Plain(s) return s end function Para(s) return "<p>" .. s .. "</p>" end function Header(lev, s, attr) return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">" end function BlockQuote(s) return "<blockquote>\n" .. s .. "\n</blockquote>" end function HorizontalRule() return "<hr/>" end function LineBlock(ls) return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') .. '</div>' end function CodeBlock(s, attr) return "<pre><code" .. attributes(attr) .. ">" .. escape(s) .. "</code></pre>" end function BulletList(items) local buffer = {} for _, item in pairs(items) do table.insert(buffer, "<li>" .. item .. "</li>") end return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>" end function OrderedList(items) local buffer = {} for _, item in pairs(items) do table.insert(buffer, "<li>" .. item .. "</li>") end return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>" end -- Revisit association list STackValue instance. function DefinitionList(items) local buffer = {} for _,item in pairs(items) do for k, v in pairs(item) do table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" .. table.concat(v,"</dd>\n<dd>") .. "</dd>") end end return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>" end function CaptionedImage(src, tit, caption, attr) return '<div class="figure">\n<img src="' .. escape(src,true) .. '" title="' .. escape(tit,true) .. '"/>\n' .. '<p class="caption">' .. caption .. '</p>\n</div>' end function Table(caption, aligns, widths, headers, rows) local buffer = {} local function add(s) table.insert(buffer, s) end add("<table>") if caption ~= "" then add("<caption>" .. caption .. "</caption>") end if widths and widths[1] ~= 0 then for _, w in pairs(widths) do add('<col width="' .. string.format("%d%%", w * 100) .. '" />') end end local header_row = {} local empty_header = true for i, h in pairs(headers) do local align = html_align(aligns[i]) table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>') empty_header = empty_header and h == "" end if empty_header then head = "" else add('<tr class="header">') for _,h in pairs(header_row) do add(h) end add('</tr>') end local class = "even" for _, row in pairs(rows) do class = (class == "even" and "odd") or "even" add('<tr class="' .. class .. '">') for i,c in pairs(row) do add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>') end add('</tr>') end add('</table') return table.concat(buffer,'\n') end function RawBlock(format, str) if format == "html" then return str else return '' end end function Div(s, attr) return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>" end local meta = {} function meta.__index(_, key) io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) return function () return "" end end setmetatable(_G, meta)