| 1 | (* Markup: the text of the site's pages as HTML: escaping, the colouring |
| 2 | of raz source, and the little markup the lessons are written in |
| 3 | (`code`, **bold**, [links](url), "- " lists and pipe tables). Each |
| 4 | procedure adds to a Buf. It needs only Buf, Str and Site. It leaves |
| 5 | out the rest of Markdown, and any letter but ASCII's in a word. *) |
| 6 | module Markup; |
| 7 | import Buf, Str, Heap, Site; |
| 8 | import Names := Vec([]const U8); |
| 9 | |
| 10 | const |
| 11 | (* the reserved words that are coloured *) |
| 12 | Keywords = "and begin break by code const defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to try type until var variant VOLATILE while xor"; |
| 13 | (* the predeclared names that are coloured *) |
| 14 | Predeclared = "INT UINT I8 I16 I32 I64 U8 U16 U32 U64 F32 F64 BOOL ERROR true false nil LEN ADR NEW SIZE INC DEC MSG XADD CAS XCHG"; |
| 15 | |
| 16 | (* ---- escaping ---------------------------------------------------- *) |
| 17 | |
| 18 | (* Adds s with &, < and > written as entities, and, if quote, " and ' too: |
| 19 | the usual HTML escaping. *) |
| 20 | proc escape*(var b: Buf.Buf; s: []const U8; quote: BOOL); |
| 21 | var i: INT; |
| 22 | begin |
| 23 | for i := 0 to LEN(s) - 1 do |
| 24 | if s[i] = '&' then Buf.str(b, "&"); |
| 25 | elsif s[i] = '<' then Buf.str(b, "<"); |
| 26 | elsif s[i] = '>' then Buf.str(b, ">"); |
| 27 | elsif quote and (s[i] = '"') then Buf.str(b, """); |
| 28 | elsif quote and (s[i] = '\'') then Buf.str(b, "'"); |
| 29 | else Buf.add(b, s[i]); |
| 30 | end; |
| 31 | end; |
| 32 | end; |
| 33 | |
| 34 | (* s escaped as escape does, as a string. *) |
| 35 | proc escaped*(s: []const U8; quote: BOOL): []const U8; |
| 36 | var b: Buf.Buf; |
| 37 | begin |
| 38 | Buf.init(b, Heap.heap); |
| 39 | escape(b, s, quote); |
| 40 | return Buf.bytes(b); |
| 41 | end; |
| 42 | |
| 43 | (* ---- raz source -------------------------------------------------- *) |
| 44 | |
| 45 | (* Whether c is a letter, a digit or an underscore. *) |
| 46 | proc isWord(c: U8): BOOL; |
| 47 | begin |
| 48 | return Str.isLetter(c) or Str.isDigit(c) or (c = '_'); |
| 49 | end; |
| 50 | |
| 51 | (* Whether c is a hexadecimal digit, capital or small. *) |
| 52 | proc isHex(c: U8): BOOL; |
| 53 | begin |
| 54 | return Str.hexVal(c) >= 0; |
| 55 | end; |
| 56 | |
| 57 | (* Whether w is one of the words of list, which are separated by single |
| 58 | spaces. *) |
| 59 | proc among(w, list: []const U8): BOOL; |
| 60 | var i, j: INT; |
| 61 | begin |
| 62 | i := 0; |
| 63 | while i < LEN(list) do |
| 64 | j := i; |
| 65 | while (j < LEN(list)) and (list[j] # ' ') do INC(j); end; |
| 66 | if list[i..j] = w then return true; end; |
| 67 | i := j + 1; |
| 68 | end; |
| 69 | return false; |
| 70 | end; |
| 71 | |
| 72 | (* Where the comment that starts at i ends, or -1 if it has no end. *) |
| 73 | proc commentEnd(s: []const U8; i: INT): INT; |
| 74 | var j: INT; |
| 75 | begin |
| 76 | if (i + 1 >= LEN(s)) or (s[i] # '(') or (s[i + 1] # '*') then return -1; end; |
| 77 | j := Str.findFrom(s, "*)", i + 2); |
| 78 | if j < 0 then return -1; end; |
| 79 | return j + 2; |
| 80 | end; |
| 81 | |
| 82 | (* Where the string or quoted text that starts at i ends, or -1 if it |
| 83 | has none. A " or ' string stops at its line's end and takes a |
| 84 | backslash and the byte after it as one; a backquoted one may run on |
| 85 | over lines. *) |
| 86 | proc stringEnd(s: []const U8; i: INT): INT; |
| 87 | var q: U8; j: INT; |
| 88 | begin |
| 89 | q := s[i]; |
| 90 | if q = '`' then |
| 91 | j := Str.findFrom(s, "`", i + 1); |
| 92 | if j < 0 then return -1; end; |
| 93 | return j + 1; |
| 94 | end; |
| 95 | if (q # '"') and (q # '\'') then return -1; end; |
| 96 | j := i + 1; |
| 97 | while j < LEN(s) do |
| 98 | if s[j] = q then return j + 1; |
| 99 | elsif s[j] = '\\' then INC(j, 2); |
| 100 | elsif s[j] = '\n' then return -1; |
| 101 | else INC(j); |
| 102 | end; |
| 103 | end; |
| 104 | return -1; |
| 105 | end; |
| 106 | |
| 107 | (* Whether a number may end at e: no letter, digit or underscore follows. *) |
| 108 | proc numberStops(s: []const U8; e: INT): BOOL; |
| 109 | begin |
| 110 | return (e >= LEN(s)) or not isWord(s[e]); |
| 111 | end; |
| 112 | |
| 113 | (* Past the digits that start at p. *) |
| 114 | proc digitsEnd(s: []const U8; p: INT): INT; |
| 115 | begin |
| 116 | while (p < LEN(s)) and Str.isDigit(s[p]) do INC(p); end; |
| 117 | return p; |
| 118 | end; |
| 119 | |
| 120 | (* The end of a number's last part, an H, from p, or -1: an H after p is |
| 121 | taken if the number can end past it, else the number ends at p. *) |
| 122 | proc hexSuffixEnd(s: []const U8; p: INT): INT; |
| 123 | begin |
| 124 | if (p < LEN(s)) and (s[p] = 'H') and numberStops(s, p + 1) then return p + 1; end; |
| 125 | if numberStops(s, p) then return p; end; |
| 126 | return -1; |
| 127 | end; |
| 128 | |
| 129 | (* The end of a number from p with its exponent, E, a sign if any and |
| 130 | digits, tried before the number ends without one; or -1. *) |
| 131 | proc exponentEnd(s: []const U8; p: INT): INT; |
| 132 | var q, e: INT; |
| 133 | begin |
| 134 | if (p < LEN(s)) and (s[p] = 'E') then |
| 135 | q := p + 1; |
| 136 | if (q < LEN(s)) and ((s[q] = '-') or (s[q] = '+')) then INC(q); end; |
| 137 | if (q < LEN(s)) and Str.isDigit(s[q]) then |
| 138 | e := hexSuffixEnd(s, digitsEnd(s, q)); |
| 139 | if e >= 0 then return e; end; |
| 140 | end; |
| 141 | end; |
| 142 | return hexSuffixEnd(s, p); |
| 143 | end; |
| 144 | |
| 145 | (* The end of a number from p with its fraction, a point and digits, |
| 146 | tried before the number ends without one; or -1. *) |
| 147 | proc fractionEnd(s: []const U8; p: INT): INT; |
| 148 | var e: INT; |
| 149 | begin |
| 150 | if (p + 1 < LEN(s)) and (s[p] = '.') and Str.isDigit(s[p + 1]) then |
| 151 | e := exponentEnd(s, digitsEnd(s, p + 1)); |
| 152 | if e >= 0 then return e; end; |
| 153 | end; |
| 154 | return exponentEnd(s, p); |
| 155 | end; |
| 156 | |
| 157 | (* Where the number that starts at i ends, or -1. It starts with a digit |
| 158 | not in the middle of a word; goes on over hexadecimal digits as far |
| 159 | as that lets it end, as a regular expression would back off, then a |
| 160 | fraction, an exponent and an H. *) |
| 161 | proc numberEnd(s: []const U8; i: INT): INT; |
| 162 | var q, e: INT; |
| 163 | begin |
| 164 | if not Str.isDigit(s[i]) or ((i > 0) and isWord(s[i - 1])) then return -1; end; |
| 165 | q := i + 1; |
| 166 | while (q < LEN(s)) and isHex(s[q]) do INC(q); end; |
| 167 | while q > i do |
| 168 | e := fractionEnd(s, q); |
| 169 | if e >= 0 then return e; end; |
| 170 | DEC(q); |
| 171 | end; |
| 172 | return -1; |
| 173 | end; |
| 174 | |
| 175 | (* Where the word that starts at i ends, or -1 if none does. *) |
| 176 | proc wordEnd(s: []const U8; i: INT): INT; |
| 177 | begin |
| 178 | if not (Str.isLetter(s[i]) or (s[i] = '_')) then return -1; end; |
| 179 | while (i < LEN(s)) and isWord(s[i]) do INC(i); end; |
| 180 | return i; |
| 181 | end; |
| 182 | |
| 183 | (* Adds src as HTML, its comments, strings, numbers and words in |
| 184 | spans. A token is looked for at each place in turn: a comment, a |
| 185 | string, a number, then a word. *) |
| 186 | proc highlight*(var b: Buf.Buf; src: []const U8); |
| 187 | var at, i, e: INT; class: []const U8; |
| 188 | begin |
| 189 | at := 0; |
| 190 | i := 0; |
| 191 | while i < LEN(src) do |
| 192 | class := ""; |
| 193 | e := commentEnd(src, i); |
| 194 | if e >= 0 then class := "comment"; end; |
| 195 | if e < 0 then e := stringEnd(src, i); if e >= 0 then class := "string"; end; end; |
| 196 | if e < 0 then e := numberEnd(src, i); if e >= 0 then class := "number"; end; end; |
| 197 | if e < 0 then |
| 198 | e := wordEnd(src, i); |
| 199 | if e >= 0 then |
| 200 | if among(src[i..e], Keywords) then class := "kw"; |
| 201 | elsif among(src[i..e], Predeclared) then class := "ty"; |
| 202 | end; |
| 203 | end; |
| 204 | end; |
| 205 | if e < 0 then |
| 206 | INC(i); |
| 207 | else |
| 208 | if LEN(class) > 0 then |
| 209 | escape(b, src[at..i], true); |
| 210 | Buf.str(b, "<span class=\""); |
| 211 | Buf.str(b, class); |
| 212 | Buf.str(b, "\">"); |
| 213 | escape(b, src[i..e], true); |
| 214 | Buf.str(b, "</span>"); |
| 215 | at := e; |
| 216 | end; |
| 217 | i := e; |
| 218 | end; |
| 219 | end; |
| 220 | escape(b, src[at..LEN(src)], true); |
| 221 | end; |
| 222 | |
| 223 | (* ---- inline markup ----------------------------------------------- *) |
| 224 | |
| 225 | (* Adds s with each `text` made <code>text</code>. *) |
| 226 | proc codePass(var b: Buf.Buf; s: []const U8); |
| 227 | var i, j: INT; |
| 228 | begin |
| 229 | i := 0; |
| 230 | while i < LEN(s) do |
| 231 | j := -1; |
| 232 | if s[i] = '`' then j := Str.findFrom(s, "`", i + 1); end; |
| 233 | if j > i + 1 then |
| 234 | Buf.str(b, "<code>"); Buf.str(b, s[i + 1..j]); Buf.str(b, "</code>"); |
| 235 | i := j + 1; |
| 236 | else |
| 237 | Buf.add(b, s[i]); |
| 238 | INC(i); |
| 239 | end; |
| 240 | end; |
| 241 | end; |
| 242 | |
| 243 | (* Adds s with each **text** made <strong>text</strong>; the text has |
| 244 | no star in it. *) |
| 245 | proc strongPass(var b: Buf.Buf; s: []const U8); |
| 246 | var i, j: INT; |
| 247 | begin |
| 248 | i := 0; |
| 249 | while i < LEN(s) do |
| 250 | j := -1; |
| 251 | if (i + 1 < LEN(s)) and (s[i] = '*') and (s[i + 1] = '*') then |
| 252 | j := i + 2; |
| 253 | while (j < LEN(s)) and (s[j] # '*') do INC(j); end; |
| 254 | if (j = i + 2) or (j + 1 >= LEN(s)) or (s[j + 1] # '*') then j := -1; end; |
| 255 | end; |
| 256 | if j >= 0 then |
| 257 | Buf.str(b, "<strong>"); Buf.str(b, s[i + 2..j]); Buf.str(b, "</strong>"); |
| 258 | i := j + 2; |
| 259 | else |
| 260 | Buf.add(b, s[i]); |
| 261 | INC(i); |
| 262 | end; |
| 263 | end; |
| 264 | end; |
| 265 | |
| 266 | (* The index of the first c in s from i, or -1. *) |
| 267 | proc indexOf(s: []const U8; c: U8; i: INT): INT; |
| 268 | begin |
| 269 | while (i < LEN(s)) and (s[i] # c) do INC(i); end; |
| 270 | if i >= LEN(s) then return -1; end; |
| 271 | return i; |
| 272 | end; |
| 273 | |
| 274 | (* Adds s with each [text](url) made <a href="url">text</a>. *) |
| 275 | proc linkPass(var b: Buf.Buf; s: []const U8); |
| 276 | var i, j, k: INT; |
| 277 | begin |
| 278 | i := 0; |
| 279 | while i < LEN(s) do |
| 280 | k := -1; |
| 281 | if s[i] = '[' then |
| 282 | j := indexOf(s, ']', i + 1); |
| 283 | if (j > i + 1) and (j + 1 < LEN(s)) and (s[j + 1] = '(') then |
| 284 | k := indexOf(s, ')', j + 2); |
| 285 | if k <= j + 2 then k := -1; end; |
| 286 | end; |
| 287 | end; |
| 288 | if k >= 0 then |
| 289 | Buf.str(b, "<a href=\""); Buf.str(b, s[j + 2..k]); Buf.str(b, "\">"); |
| 290 | Buf.str(b, s[i + 1..j]); Buf.str(b, "</a>"); |
| 291 | i := k + 1; |
| 292 | else |
| 293 | Buf.add(b, s[i]); |
| 294 | INC(i); |
| 295 | end; |
| 296 | end; |
| 297 | end; |
| 298 | |
| 299 | (* Adds s as HTML: escaped, then its `code`, **bold** and [links](url) |
| 300 | made tags, one kind after the other over the whole text. *) |
| 301 | proc inline*(var b: Buf.Buf; s: []const U8); |
| 302 | var plain, coded, bold: Buf.Buf; |
| 303 | begin |
| 304 | Buf.init(plain, Heap.heap); |
| 305 | Buf.init(coded, Heap.heap); |
| 306 | Buf.init(bold, Heap.heap); |
| 307 | escape(plain, s, false); |
| 308 | codePass(coded, Buf.bytes(plain)); |
| 309 | strongPass(bold, Buf.bytes(coded)); |
| 310 | linkPass(b, Buf.bytes(bold)); |
| 311 | Buf.done(plain); |
| 312 | Buf.done(coded); |
| 313 | Buf.done(bold); |
| 314 | end; |
| 315 | |
| 316 | (* ---- blocks ------------------------------------------------------ *) |
| 317 | |
| 318 | (* The cells of a table row: its white space and the bars at its ends |
| 319 | cut, then split at the bars, each cell trimmed. *) |
| 320 | proc cells(row: []const U8): [][]const U8; |
| 321 | var v: Names.Vec; at, i: INT; |
| 322 | begin |
| 323 | row := Str.trim(row); |
| 324 | while (LEN(row) > 0) and (row[0] = '|') do row := row[1..LEN(row)]; end; |
| 325 | while (LEN(row) > 0) and (row[LEN(row) - 1] = '|') do row := row[0..LEN(row) - 1]; end; |
| 326 | Names.init(v, Heap.heap); |
| 327 | at := 0; |
| 328 | for i := 0 to LEN(row) - 1 do |
| 329 | if row[i] = '|' then Names.push(v, Str.trim(row[at..i])); at := i + 1; end; |
| 330 | end; |
| 331 | Names.push(v, Str.trim(row[at..LEN(row)])); |
| 332 | return Names.all(v); |
| 333 | end; |
| 334 | |
| 335 | (* Adds a pipe table (header, a |---| rule, rows) as HTML. Each cell |
| 336 | carries its column's name, which the style sheet shows when the |
| 337 | screen is too narrow for columns. *) |
| 338 | proc table(var b: Buf.Buf; rows: [][]const U8); |
| 339 | var head, row: [][]const U8; i, k: INT; |
| 340 | begin |
| 341 | head := cells(rows[0]); |
| 342 | Buf.str(b, "<table><thead><tr>"); |
| 343 | for k := 0 to LEN(head) - 1 do |
| 344 | Buf.str(b, "<th>"); inline(b, head[k]); Buf.str(b, "</th>"); |
| 345 | end; |
| 346 | Buf.str(b, "</tr></thead><tbody>"); |
| 347 | for i := 2 to LEN(rows) - 1 do |
| 348 | row := cells(rows[i]); |
| 349 | Buf.str(b, "<tr>"); |
| 350 | for k := 0 to LEN(head) - 1 do |
| 351 | if k < LEN(row) then |
| 352 | Buf.str(b, "<td data-label=\""); |
| 353 | escape(b, head[k], true); |
| 354 | Buf.str(b, "\">"); |
| 355 | inline(b, row[k]); |
| 356 | Buf.str(b, "</td>"); |
| 357 | end; |
| 358 | end; |
| 359 | Buf.str(b, "</tr>"); |
| 360 | end; |
| 361 | Buf.str(b, "</tbody></table>"); |
| 362 | end; |
| 363 | |
| 364 | (* The paragraphs of text: it is cut at each line feed that ends a blank |
| 365 | line, however many blank lines there are. *) |
| 366 | proc paragraphs(text: []const U8): [][]const U8; |
| 367 | var v: Names.Vec; start, i, k, last: INT; |
| 368 | begin |
| 369 | Names.init(v, Heap.heap); |
| 370 | start := 0; |
| 371 | i := 0; |
| 372 | while i < LEN(text) do |
| 373 | last := -1; |
| 374 | if text[i] = '\n' then |
| 375 | k := i + 1; |
| 376 | while (k < LEN(text)) and Str.isSpace(text[k]) do |
| 377 | if text[k] = '\n' then last := k; end; |
| 378 | INC(k); |
| 379 | end; |
| 380 | end; |
| 381 | if last >= 0 then |
| 382 | Names.push(v, text[start..i]); |
| 383 | start := last + 1; |
| 384 | i := last + 1; |
| 385 | else |
| 386 | INC(i); |
| 387 | end; |
| 388 | end; |
| 389 | Names.push(v, text[start..LEN(text)]); |
| 390 | return Names.all(v); |
| 391 | end; |
| 392 | |
| 393 | (* Whether every one of lines starts with prefix. *) |
| 394 | proc allStart(lines: [][]const U8; prefix: []const U8): BOOL; |
| 395 | var i: INT; |
| 396 | begin |
| 397 | for i := 0 to LEN(lines) - 1 do |
| 398 | if not Str.hasPrefix(lines[i], prefix) then return false; end; |
| 399 | end; |
| 400 | return true; |
| 401 | end; |
| 402 | |
| 403 | (* Adds a paragraph as HTML: a "## " heading, a table, a "- " list, or |
| 404 | a paragraph of its lines joined by blanks. *) |
| 405 | proc paragraph(var b: Buf.Buf; para: []const U8); |
| 406 | var lines: [][]const U8; joined: Buf.Buf; i: INT; |
| 407 | begin |
| 408 | lines := Site.lines(para); |
| 409 | if Str.hasPrefix(para, "## ") then |
| 410 | Buf.str(b, "<h2>"); inline(b, para[3..LEN(para)]); Buf.str(b, "</h2>"); |
| 411 | elsif allStart(lines, "|") then |
| 412 | table(b, lines); |
| 413 | elsif allStart(lines, "- ") then |
| 414 | Buf.str(b, "<ul>"); |
| 415 | for i := 0 to LEN(lines) - 1 do |
| 416 | Buf.str(b, "<li>"); inline(b, lines[i][2..LEN(lines[i])]); Buf.str(b, "</li>"); |
| 417 | end; |
| 418 | Buf.str(b, "</ul>"); |
| 419 | else |
| 420 | Buf.init(joined, Heap.heap); |
| 421 | Str.join(joined, lines, " "); |
| 422 | Buf.str(b, "<p>"); inline(b, Buf.bytes(joined)); Buf.str(b, "</p>"); |
| 423 | Buf.done(joined); |
| 424 | end; |
| 425 | end; |
| 426 | |
| 427 | (* Adds the paragraphs of text as HTML, one to a line. *) |
| 428 | proc prose*(var b: Buf.Buf; text: []const U8); |
| 429 | var paras: [][]const U8; i: INT; |
| 430 | begin |
| 431 | paras := paragraphs(Str.trim(text)); |
| 432 | for i := 0 to LEN(paras) - 1 do |
| 433 | if i > 0 then Buf.add(b, '\n'); end; |
| 434 | paragraph(b, paras[i]); |
| 435 | end; |
| 436 | end; |
| 437 | |
| 438 | end Markup. |