Mercurial > irccd
comparison doc/html/html.lua @ 607:bb9771fb5f44
Docs: rework documentation
- Change directories,
- Remove handwritten manual pages.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 08 Dec 2017 20:11:22 +0100 |
parents | |
children | 27587ff92a64 |
comparison
equal
deleted
inserted
replaced
606:4f5f306d13ac | 607:bb9771fb5f44 |
---|---|
1 -- | |
2 -- html.lua -- irccd HTML writer | |
3 -- | |
4 -- Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> | |
5 -- | |
6 -- Permission to use, copy, modify, and/or distribute this software for any | |
7 -- purpose with or without fee is hereby granted, provided that the above | |
8 -- copyright notice and this permission notice appear in all copies. | |
9 -- | |
10 -- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 -- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 -- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 -- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 -- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 -- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 -- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 -- | |
18 | |
19 local function escape(s, in_attribute) | |
20 return s:gsub("[<>&\"']", | |
21 function(x) | |
22 if x == '<' then | |
23 return '<' | |
24 elseif x == '>' then | |
25 return '>' | |
26 elseif x == '&' then | |
27 return '&' | |
28 elseif x == '"' then | |
29 return '"' | |
30 elseif x == "'" then | |
31 return ''' | |
32 else | |
33 return x | |
34 end | |
35 end) | |
36 end | |
37 | |
38 local function attributes(attr) | |
39 local attr_table = {} | |
40 for x,y in pairs(attr) do | |
41 if y and y ~= "" then | |
42 table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"') | |
43 end | |
44 end | |
45 return table.concat(attr_table) | |
46 end | |
47 | |
48 local function html_align(align) | |
49 if align == 'AlignLeft' then | |
50 return 'left' | |
51 elseif align == 'AlignRight' then | |
52 return 'right' | |
53 elseif align == 'AlignCenter' then | |
54 return 'center' | |
55 else | |
56 return 'left' | |
57 end | |
58 end | |
59 | |
60 local notes = {} | |
61 | |
62 function Blocksep() | |
63 return "\n\n" | |
64 end | |
65 | |
66 function Doc(body, metadata, variables) | |
67 | |
68 for k, v in pairs(metadata) do | |
69 print(k .. " = " .. tostring(v)) | |
70 end | |
71 | |
72 | |
73 | |
74 local buffer = {} | |
75 local function add(s) | |
76 table.insert(buffer, s) | |
77 end | |
78 add(body) | |
79 if #notes > 0 then | |
80 add('<ol class="footnotes">') | |
81 for _,note in pairs(notes) do | |
82 add(note) | |
83 end | |
84 add('</ol>') | |
85 end | |
86 return table.concat(buffer,'\n') .. '\n' | |
87 end | |
88 | |
89 function Str(s) | |
90 return escape(s) | |
91 end | |
92 | |
93 function Space() | |
94 return " " | |
95 end | |
96 | |
97 function SoftBreak() | |
98 return "\n" | |
99 end | |
100 | |
101 function LineBreak() | |
102 return "<br/>" | |
103 end | |
104 | |
105 function Emph(s) | |
106 return "<em>" .. s .. "</em>" | |
107 end | |
108 | |
109 function Strong(s) | |
110 return "<strong>" .. s .. "</strong>" | |
111 end | |
112 | |
113 function Subscript(s) | |
114 return "<sub>" .. s .. "</sub>" | |
115 end | |
116 | |
117 function Superscript(s) | |
118 return "<sup>" .. s .. "</sup>" | |
119 end | |
120 | |
121 function SmallCaps(s) | |
122 return '<span style="font-variant: small-caps;">' .. s .. '</span>' | |
123 end | |
124 | |
125 function Strikeout(s) | |
126 return '<del>' .. s .. '</del>' | |
127 end | |
128 | |
129 function Link(s, src, tit, attr) | |
130 return "<a href='" .. escape(src,true) .. "' title='" .. | |
131 escape(tit,true) .. "'>" .. s .. "</a>" | |
132 end | |
133 | |
134 function Image(s, src, tit, attr) | |
135 return "<img src='" .. escape(src,true) .. "' title='" .. | |
136 escape(tit,true) .. "'/>" | |
137 end | |
138 | |
139 function Code(s, attr) | |
140 return "<code" .. attributes(attr) .. ">" .. escape(s) .. "</code>" | |
141 end | |
142 | |
143 function InlineMath(s) | |
144 return "\\(" .. escape(s) .. "\\)" | |
145 end | |
146 | |
147 function DisplayMath(s) | |
148 return "\\[" .. escape(s) .. "\\]" | |
149 end | |
150 | |
151 function Note(s) | |
152 local num = #notes + 1 | |
153 -- insert the back reference right before the final closing tag. | |
154 s = string.gsub(s, | |
155 '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></') | |
156 -- add a list item with the note to the note table. | |
157 table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>') | |
158 -- return the footnote reference, linked to the note. | |
159 return '<a id="fnref' .. num .. '" href="#fn' .. num .. | |
160 '"><sup>' .. num .. '</sup></a>' | |
161 end | |
162 | |
163 function Span(s, attr) | |
164 return "<span" .. attributes(attr) .. ">" .. s .. "</span>" | |
165 end | |
166 | |
167 function RawInline(format, str) | |
168 if format == "html" then | |
169 return str | |
170 else | |
171 return '' | |
172 end | |
173 end | |
174 | |
175 function Cite(s, cs) | |
176 local ids = {} | |
177 for _,cit in ipairs(cs) do | |
178 table.insert(ids, cit.citationId) | |
179 end | |
180 return "<span class=\"cite\" data-citation-ids=\"" .. table.concat(ids, ",") .. | |
181 "\">" .. s .. "</span>" | |
182 end | |
183 | |
184 function Plain(s) | |
185 return s | |
186 end | |
187 | |
188 function Para(s) | |
189 return "<p>" .. s .. "</p>" | |
190 end | |
191 | |
192 function Header(lev, s, attr) | |
193 return "<h" .. lev .. attributes(attr) .. ">" .. s .. "</h" .. lev .. ">" | |
194 end | |
195 | |
196 function BlockQuote(s) | |
197 return "<blockquote>\n" .. s .. "\n</blockquote>" | |
198 end | |
199 | |
200 function HorizontalRule() | |
201 return "<hr/>" | |
202 end | |
203 | |
204 function LineBlock(ls) | |
205 return '<div style="white-space: pre-line;">' .. table.concat(ls, '\n') .. | |
206 '</div>' | |
207 end | |
208 | |
209 function CodeBlock(s, attr) | |
210 return "<pre><code" .. attributes(attr) .. ">" .. escape(s) .. | |
211 "</code></pre>" | |
212 end | |
213 | |
214 function BulletList(items) | |
215 local buffer = {} | |
216 for _, item in pairs(items) do | |
217 table.insert(buffer, "<li>" .. item .. "</li>") | |
218 end | |
219 return "<ul>\n" .. table.concat(buffer, "\n") .. "\n</ul>" | |
220 end | |
221 | |
222 function OrderedList(items) | |
223 local buffer = {} | |
224 for _, item in pairs(items) do | |
225 table.insert(buffer, "<li>" .. item .. "</li>") | |
226 end | |
227 return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>" | |
228 end | |
229 | |
230 -- Revisit association list STackValue instance. | |
231 function DefinitionList(items) | |
232 local buffer = {} | |
233 for _,item in pairs(items) do | |
234 for k, v in pairs(item) do | |
235 table.insert(buffer,"<dt>" .. k .. "</dt>\n<dd>" .. | |
236 table.concat(v,"</dd>\n<dd>") .. "</dd>") | |
237 end | |
238 end | |
239 return "<dl>\n" .. table.concat(buffer, "\n") .. "\n</dl>" | |
240 end | |
241 | |
242 function CaptionedImage(src, tit, caption, attr) | |
243 return '<div class="figure">\n<img src="' .. escape(src,true) .. | |
244 '" title="' .. escape(tit,true) .. '"/>\n' .. | |
245 '<p class="caption">' .. caption .. '</p>\n</div>' | |
246 end | |
247 | |
248 function Table(caption, aligns, widths, headers, rows) | |
249 local buffer = {} | |
250 local function add(s) | |
251 table.insert(buffer, s) | |
252 end | |
253 add("<table>") | |
254 if caption ~= "" then | |
255 add("<caption>" .. caption .. "</caption>") | |
256 end | |
257 if widths and widths[1] ~= 0 then | |
258 for _, w in pairs(widths) do | |
259 add('<col width="' .. string.format("%d%%", w * 100) .. '" />') | |
260 end | |
261 end | |
262 local header_row = {} | |
263 local empty_header = true | |
264 for i, h in pairs(headers) do | |
265 local align = html_align(aligns[i]) | |
266 table.insert(header_row,'<th align="' .. align .. '">' .. h .. '</th>') | |
267 empty_header = empty_header and h == "" | |
268 end | |
269 if empty_header then | |
270 head = "" | |
271 else | |
272 add('<tr class="header">') | |
273 for _,h in pairs(header_row) do | |
274 add(h) | |
275 end | |
276 add('</tr>') | |
277 end | |
278 local class = "even" | |
279 for _, row in pairs(rows) do | |
280 class = (class == "even" and "odd") or "even" | |
281 add('<tr class="' .. class .. '">') | |
282 for i,c in pairs(row) do | |
283 add('<td align="' .. html_align(aligns[i]) .. '">' .. c .. '</td>') | |
284 end | |
285 add('</tr>') | |
286 end | |
287 add('</table') | |
288 return table.concat(buffer,'\n') | |
289 end | |
290 | |
291 function RawBlock(format, str) | |
292 if format == "html" then | |
293 return str | |
294 else | |
295 return '' | |
296 end | |
297 end | |
298 | |
299 function Div(s, attr) | |
300 return "<div" .. attributes(attr) .. ">\n" .. s .. "</div>" | |
301 end | |
302 | |
303 local meta = {} | |
304 | |
305 function meta.__index(_, key) | |
306 io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) | |
307 | |
308 return function () | |
309 return "" | |
310 end | |
311 end | |
312 | |
313 setmetatable(_G, meta) |