interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / browse / b_md.om
| 1 | note browse/b_md.om (ForgeBrowse): safe Markdown renderer (CommonMark subset plus GFM tables, |
| 2 | note strikethrough, task items and bare https autolinks). Source: fxo[a, b); output: fob. |
| 3 | note Safety: every source byte reaches fob through fescc or the URL encoder; raw HTML is escaped, |
| 4 | note never passed through; link schemes other than http, https and mailto render as plain text; |
| 5 | note an HTML comment ("<!--" up to the first "-->") renders nothing (comment section below); an |
| 6 | note opener without a closing "-->" stays escaped text; |
| 7 | note images load only from this repo (relative paths -> /raw/<ref>/...); an external image renders |
| 8 | note as its alt text only. Relative links resolve against gbmd (the document's directory) at gbref. |
| 9 | global gbm ulong 40 |
| 10 | global gbmlm ulong 8 |
| 11 | global gbmlc ulong 8 |
| 12 | global gbmlt ulong 8 |
| 13 | global gbmlk ulong 8 |
| 14 | global gbmta uchar 64 |
| 15 | global gbmd uchar 4096 |
| 16 | global gbmdl ulong 1 |
| 17 | global gbmu uchar 4096 |
| 18 | global gbmul ulong 1 |
| 19 | global gbmrd ulong 512 |
| 20 | global gbmrn ulong 1 |
| 21 | const BMP ulong 0 |
| 22 | const BMPW ulong 1 |
| 23 | const BMF ulong 2 |
| 24 | const BMFC ulong 3 |
| 25 | const BMFN ulong 4 |
| 26 | const BMFI ulong 5 |
| 27 | const BMQ ulong 6 |
| 28 | const BMT ulong 7 |
| 29 | const BMTC ulong 8 |
| 30 | const BMIC ulong 9 |
| 31 | const BMIB ulong 10 |
| 32 | const BML ulong 11 |
| 33 | const BMB ulong 12 |
| 34 | const BMSK ulong 13 |
| 35 | const BMHB ulong 14 |
| 36 | const BMFP ulong 15 |
| 37 | const BMCP ulong 16 |
| 38 | const BMMC ulong 17 |
| 39 | const BMMP ulong 18 |
| 40 | const BMMN ulong 19 |
| 41 | const BMDS ulong 20 |
| 42 | const BMDE ulong 21 |
| 43 | const BMTS ulong 22 |
| 44 | const BMTE ulong 23 |
| 45 | const BMNS ulong 24 |
| 46 | const BMNE ulong 25 |
| 47 | const BMTK ulong 26 |
| 48 | const BMMK ulong 27 |
| 49 | note BMXE: end after the "-->" of an open comment block (0 = none); BMDB: end of the document; |
| 50 | note BMCV/BMCF/BMCX: cached comment-closer lookup (no "-->" starts in [BMCF, BMCX); BMCX is one or BMDB) |
| 51 | const BMXE ulong 28 |
| 52 | const BMDB ulong 29 |
| 53 | const BMCV ulong 30 |
| 54 | const BMCF ulong 31 |
| 55 | const BMCX ulong 32 |
| 56 | text tbmp "<p>" |
| 57 | text tbmpe "</p>\n" |
| 58 | text tbmpre "<pre><code>" |
| 59 | text tbmprl "<pre><code class=\"language-" |
| 60 | text tbmpree "</code></pre>\n" |
| 61 | text tbmbq "<blockquote>\n" |
| 62 | text tbmbqe "</blockquote>\n" |
| 63 | text tbmul "<ul>\n" |
| 64 | text tbmule "</ul>\n" |
| 65 | text tbmol "<ol>\n" |
| 66 | text tbmols "<ol start=\"" |
| 67 | text tbmole "</ol>\n" |
| 68 | text tbmli "<li>" |
| 69 | text tbmlie "</li>\n" |
| 70 | text tbmhr "<hr>\n" |
| 71 | text tbmbr "<br>\n" |
| 72 | text tbmtab "<table class=\"md\">\n<thead>\n<tr>" |
| 73 | text tbmthe "</tr>\n</thead>\n<tbody>\n" |
| 74 | text tbmtabe "</tbody>\n</table>\n" |
| 75 | text tbmtr "<tr>" |
| 76 | text tbmtre "</tr>\n" |
| 77 | text tbmthl "<th class=\"md-l\">" |
| 78 | text tbmthc "<th class=\"md-c\">" |
| 79 | text tbmthr "<th class=\"md-r\">" |
| 80 | text tbmth "<th>" |
| 81 | text tbmthx "</th>" |
| 82 | text tbmtdl "<td class=\"md-l\">" |
| 83 | text tbmtdc "<td class=\"md-c\">" |
| 84 | text tbmtdr "<td class=\"md-r\">" |
| 85 | text tbmtd "<td>" |
| 86 | text tbmtdx "</td>" |
| 87 | text tbmcode "<code>" |
| 88 | text tbmcodx "</code>" |
| 89 | text tbmem "<em>" |
| 90 | text tbmemx "</em>" |
| 91 | text tbmst "<strong>" |
| 92 | text tbmstx "</strong>" |
| 93 | text tbmdel "<del>" |
| 94 | text tbmdelx "</del>" |
| 95 | note tbmaext: a link that leaves the site (http, https, mailto or protocol-relative, and the http(s) |
| 96 | note autolinks) gets class "ext" (the stylesheet draws an arrow and says "leaves this site") and |
| 97 | note neither the referrer nor the opener |
| 98 | text tbmaext "\" class=\"ext\" rel=\"nofollow noopener noreferrer\">" |
| 99 | text tbmtitl "\" title=\"" |
| 100 | text tbmimg "<img src=\"" |
| 101 | text tbmalt "\" alt=\"" |
| 102 | text tbmimgx "\">" |
| 103 | text tbmblob "/blob/" |
| 104 | text tbmraw "/raw/" |
| 105 | text tbmmail "mailto:" |
| 106 | text tbmhttp "http" |
| 107 | text tbmhttps "https" |
| 108 | text tbmmto "mailto" |
| 109 | text tbmbh "http://" |
| 110 | text tbmbhs "https://" |
| 111 | text tbmchk "<input type=\"checkbox\" disabled> " |
| 112 | text tbmchkd "<input type=\"checkbox\" disabled checked> " |
| 113 | text tbmimgw "image" |
| 114 | text tbmaq "\">\n" |
| 115 | text tbmh "<h" |
| 116 | text tbmhid " id=\"" |
| 117 | text tbmhx "</h" |
| 118 | text tbmgt ">\n" |
| 119 | note ---- state helpers |
| 120 | fn brmg k ulong to ulong |
| 121 | ret get gbm k |
| 122 | end |
| 123 | fn brms k ulong v ulong to void |
| 124 | put gbm k v |
| 125 | end |
| 126 | fn brmreset to void |
| 127 | for i ulong 40 |
| 128 | put gbm i 0 |
| 129 | end |
| 130 | put gbmrn 0 0 |
| 131 | end |
| 132 | fn brmc a ulong to uchar |
| 133 | ret get fxo a |
| 134 | end |
| 135 | fn brpunct c uchar to bool |
| 136 | if and ge c 33 le c 47 |
| 137 | ret true |
| 138 | end |
| 139 | if and ge c 58 le c 64 |
| 140 | ret true |
| 141 | end |
| 142 | if and ge c 91 le c 96 |
| 143 | ret true |
| 144 | end |
| 145 | ret and ge c 123 le c 126 |
| 146 | end |
| 147 | note brmcols: whitespace columns at the start of fxo[p, e) (tab = to the next multiple of 4); |
| 148 | note the first non-space position goes to gbm[BMFP] |
| 149 | fn brmcols p ulong e ulong to ulong |
| 150 | c = ulong 0 |
| 151 | i = p |
| 152 | while lt i e bound BRBIG |
| 153 | x = get fxo i |
| 154 | if eq x 32 |
| 155 | c = add c 1 |
| 156 | else |
| 157 | if ne x 9 |
| 158 | break |
| 159 | end |
| 160 | c = add sub c rem c 4 4 |
| 161 | end |
| 162 | i = add i 1 |
| 163 | end |
| 164 | brms BMFP i |
| 165 | ret c |
| 166 | end |
| 167 | note brmskipc: position after up to `cols` whitespace columns of fxo[p, e) |
| 168 | fn brmskipc p ulong e ulong cols ulong to ulong |
| 169 | c = ulong 0 |
| 170 | i = p |
| 171 | while lt i e bound BRBIG |
| 172 | if ge c cols |
| 173 | ret i |
| 174 | end |
| 175 | x = get fxo i |
| 176 | if eq x 32 |
| 177 | c = add c 1 |
| 178 | else |
| 179 | if ne x 9 |
| 180 | ret i |
| 181 | end |
| 182 | c = add sub c rem c 4 4 |
| 183 | end |
| 184 | i = add i 1 |
| 185 | end |
| 186 | ret i |
| 187 | end |
| 188 | fn brmblank p ulong e ulong to bool |
| 189 | i = p |
| 190 | while lt i e bound BRBIG |
| 191 | if not brspc get fxo i |
| 192 | ret false |
| 193 | end |
| 194 | i = add i 1 |
| 195 | end |
| 196 | ret true |
| 197 | end |
| 198 | note brmtrim: end of fxo[p, e) without trailing spaces and tabs |
| 199 | fn brmtrim p ulong e ulong to ulong |
| 200 | k = e |
| 201 | while gt k p bound BRBIG |
| 202 | if not brspc get fxo sub k 1 |
| 203 | ret k |
| 204 | end |
| 205 | k = sub k 1 |
| 206 | end |
| 207 | ret k |
| 208 | end |
| 209 | fn brmspan p ulong e ulong to void |
| 210 | i = p |
| 211 | while lt i e bound BRBIG |
| 212 | fescc get fxo i |
| 213 | i = add i 1 |
| 214 | end |
| 215 | end |
| 216 | note ---- block closers |
| 217 | fn brmcp to void |
| 218 | if eq brmg BMP 1 |
| 219 | if eq brmg BMPW 1 |
| 220 | fput tbmpe |
| 221 | else |
| 222 | fputc 10 |
| 223 | end |
| 224 | end |
| 225 | brms BMP 0 |
| 226 | brms BMHB 0 |
| 227 | end |
| 228 | fn brmct to void |
| 229 | if eq brmg BMT 1 |
| 230 | fput tbmtabe |
| 231 | end |
| 232 | brms BMT 0 |
| 233 | end |
| 234 | fn brmci to void |
| 235 | if eq brmg BMIC 1 |
| 236 | fput tbmpree |
| 237 | end |
| 238 | brms BMIC 0 |
| 239 | brms BMIB 0 |
| 240 | end |
| 241 | fn brmcf to void |
| 242 | if eq brmg BMF 1 |
| 243 | fput tbmpree |
| 244 | end |
| 245 | brms BMF 0 |
| 246 | end |
| 247 | fn brmcleaf to void |
| 248 | brmcp |
| 249 | brmct |
| 250 | brmci |
| 251 | brmcf |
| 252 | end |
| 253 | fn brmlclose to void |
| 254 | l = brmg BML |
| 255 | if eq l 0 |
| 256 | ret |
| 257 | end |
| 258 | brmcleaf |
| 259 | fput tbmlie |
| 260 | if eq get gbmlt sub l 1 1 |
| 261 | fput tbmole |
| 262 | else |
| 263 | fput tbmule |
| 264 | end |
| 265 | brms BML sub l 1 |
| 266 | end |
| 267 | fn brmclist k ulong to void |
| 268 | while gt brmg BML k bound BRBIG |
| 269 | brmlclose |
| 270 | end |
| 271 | end |
| 272 | fn brmsetq q ulong to void |
| 273 | brmclist 0 |
| 274 | brmcleaf |
| 275 | while gt brmg BMQ q bound BRBIG |
| 276 | fput tbmbqe |
| 277 | brms BMQ sub brmg BMQ 1 |
| 278 | end |
| 279 | while lt brmg BMQ q bound BRBIG |
| 280 | fput tbmbq |
| 281 | brms BMQ add brmg BMQ 1 |
| 282 | end |
| 283 | end |
| 284 | note ---- entry point |
| 285 | fn brmd a ulong b ulong to void |
| 286 | brmreset |
| 287 | brms BMDB b |
| 288 | brmdefs a b |
| 289 | s = a |
| 290 | while lt s b bound BRBIG |
| 291 | e = brxfind s b 10 |
| 292 | k = e |
| 293 | if gt k s |
| 294 | if eq get fxo sub k 1 13 |
| 295 | k = sub k 1 |
| 296 | end |
| 297 | end |
| 298 | if eq brmg BMSK 1 |
| 299 | brms BMSK 0 |
| 300 | else |
| 301 | if gt brmg BMXE 0 |
| 302 | brmcline k |
| 303 | else |
| 304 | brmline s k add e 1 b |
| 305 | end |
| 306 | end |
| 307 | s = add e 1 |
| 308 | end |
| 309 | brmsetq 0 |
| 310 | end |
| 311 | note brmq: count blockquote markers of fxo[s, e) (at most `lim`); content start to gbm[BMCP] |
| 312 | fn brmq s ulong e ulong lim ulong to ulong |
| 313 | q = ulong 0 |
| 314 | p = s |
| 315 | while lt q lim bound BRBIG |
| 316 | i = brmskipc p e 3 |
| 317 | if ge i e |
| 318 | break |
| 319 | end |
| 320 | if ne get fxo i 62 |
| 321 | break |
| 322 | end |
| 323 | q = add q 1 |
| 324 | p = add i 1 |
| 325 | if lt p e |
| 326 | if eq get fxo p 32 |
| 327 | p = add p 1 |
| 328 | end |
| 329 | end |
| 330 | end |
| 331 | brms BMCP p |
| 332 | ret q |
| 333 | end |
| 334 | fn brmline s ulong e ulong nx ulong b ulong to void |
| 335 | if eq brmg BMF 1 |
| 336 | if brmfline s e |
| 337 | ret |
| 338 | end |
| 339 | end |
| 340 | q = brmq s e 64 |
| 341 | p = brmg BMCP |
| 342 | bl = brmblank p e |
| 343 | if ne q brmg BMQ |
| 344 | if and lt q brmg BMQ brmlazy p e bl |
| 345 | brmpline p e |
| 346 | brms BMB 0 |
| 347 | ret |
| 348 | end |
| 349 | brmsetq q |
| 350 | end |
| 351 | if bl |
| 352 | brmcp |
| 353 | brmct |
| 354 | if eq brmg BMIC 1 |
| 355 | brms BMIB add brmg BMIB 1 |
| 356 | end |
| 357 | brms BMB 1 |
| 358 | ret |
| 359 | end |
| 360 | brmblock p e nx b |
| 361 | brms BMB 0 |
| 362 | end |
| 363 | note brmlazy: a line continuing an open paragraph without its container markers |
| 364 | fn brmlazy p ulong e ulong bl bool to bool |
| 365 | if bl |
| 366 | ret false |
| 367 | end |
| 368 | if ne brmg BMP 1 |
| 369 | ret false |
| 370 | end |
| 371 | ret brmplain p e |
| 372 | end |
| 373 | fn brmplain p ulong e ulong to bool |
| 374 | i = brmcols p e |
| 375 | f = brmg BMFP |
| 376 | if ge i 4 |
| 377 | ret true |
| 378 | end |
| 379 | c = get fxo f |
| 380 | if or eq c 35 eq c 62 |
| 381 | ret false |
| 382 | end |
| 383 | if eq c 60 |
| 384 | if gt brmcbx f e 0 |
| 385 | ret false |
| 386 | end |
| 387 | end |
| 388 | if or brmisfence f e brmhr f e |
| 389 | ret false |
| 390 | end |
| 391 | ret eq brmmark f e 0 |
| 392 | end |
| 393 | note brmfline: a line while a fence is open; true when consumed |
| 394 | fn brmfline s ulong e ulong to bool |
| 395 | fq = brmg BMQ |
| 396 | q = brmq s e fq |
| 397 | if lt q fq |
| 398 | brmcf |
| 399 | ret false |
| 400 | end |
| 401 | p = brmskipc brmg BMCP e brmg BMFI |
| 402 | if brmfend p e |
| 403 | brmcf |
| 404 | ret true |
| 405 | end |
| 406 | brmspan p e |
| 407 | fputc 10 |
| 408 | ret true |
| 409 | end |
| 410 | note brmfend: fxo[p, e) closes the open fence (same char, at least as long, nothing after) |
| 411 | fn brmfend p ulong e ulong to bool |
| 412 | i = brmcols p e |
| 413 | f = brmg BMFP |
| 414 | if ge i 4 |
| 415 | ret false |
| 416 | end |
| 417 | if ge f e |
| 418 | ret false |
| 419 | end |
| 420 | c = get fxo f |
| 421 | if ne ulong c brmg BMFC |
| 422 | ret false |
| 423 | end |
| 424 | n = brmrun f e c |
| 425 | if lt n brmg BMFN |
| 426 | ret false |
| 427 | end |
| 428 | ret brmblank add f n e |
| 429 | end |
| 430 | fn brmrun f ulong e ulong c uchar to ulong |
| 431 | n = ulong 0 |
| 432 | i = f |
| 433 | while lt i e bound BRBIG |
| 434 | if ne get fxo i c |
| 435 | ret n |
| 436 | end |
| 437 | n = add n 1 |
| 438 | i = add i 1 |
| 439 | end |
| 440 | ret n |
| 441 | end |
| 442 | note ---- containers: lists |
| 443 | fn brmblock p ulong e ulong nx ulong b ulong to void |
| 444 | i = brmcols p e |
| 445 | f = brmg BMFP |
| 446 | if eq brmg BMIC 1 |
| 447 | if ge i add brmlci 4 |
| 448 | brmicode p e |
| 449 | ret |
| 450 | end |
| 451 | brmci |
| 452 | end |
| 453 | m = ulong 0 |
| 454 | if lt i add brmlci 4 |
| 455 | if not brmhr f e |
| 456 | m = brmmark f e |
| 457 | end |
| 458 | end |
| 459 | if gt m 0 |
| 460 | brmitem p e i m nx |
| 461 | ret |
| 462 | end |
| 463 | if eq brmg BML 0 |
| 464 | brmleaf p e nx b 0 |
| 465 | ret |
| 466 | end |
| 467 | brmlcont p e nx b i |
| 468 | end |
| 469 | note brmlci: content indent of the innermost open list item (0 outside lists) |
| 470 | fn brmlci to ulong |
| 471 | l = brmg BML |
| 472 | if eq l 0 |
| 473 | ret 0 |
| 474 | end |
| 475 | ret get gbmlc sub l 1 |
| 476 | end |
| 477 | fn brmicode p ulong e ulong to void |
| 478 | if eq brmg BMIC 0 |
| 479 | brmcp |
| 480 | fput tbmpre |
| 481 | brms BMIC 1 |
| 482 | brms BMIB 0 |
| 483 | end |
| 484 | for k ulong brmg BMIB bound BRBIG |
| 485 | fputc 10 |
| 486 | end |
| 487 | brms BMIB 0 |
| 488 | brmspan brmskipc p e add brmlci 4 e |
| 489 | fputc 10 |
| 490 | end |
| 491 | note brmlcont: a non-marker line while a list is open |
| 492 | fn brmlcont p ulong e ulong nx ulong b ulong i ulong to void |
| 493 | if and eq brmg BMB 0 eq brmg BMP 1 |
| 494 | if lt i brmlci |
| 495 | if brmplain p e |
| 496 | brmpline p e |
| 497 | ret |
| 498 | end |
| 499 | end |
| 500 | end |
| 501 | while gt brmg BML 0 bound BRBIG |
| 502 | if ge i brmlci |
| 503 | break |
| 504 | end |
| 505 | brmlclose |
| 506 | end |
| 507 | if eq brmg BML 0 |
| 508 | brmleaf p e nx b 0 |
| 509 | ret |
| 510 | end |
| 511 | if eq brmg BMB 1 |
| 512 | brmcp |
| 513 | end |
| 514 | brmleaf brmskipc p e brmlci e nx b brmlci |
| 515 | end |
| 516 | note brmmark: list marker at f? 1 bullet, 2 ordered, 0 none. Sets BMMP (content position), |
| 517 | note BMMC (marker + spacing width) and BMMN (ordered start number) |
| 518 | fn brmmark f ulong e ulong to ulong |
| 519 | c = get fxo f |
| 520 | t = ulong 0 |
| 521 | k = f |
| 522 | if or eq c 45 or eq c 42 eq c 43 |
| 523 | t = 1 |
| 524 | brms BMMK ulong c |
| 525 | k = add f 1 |
| 526 | else |
| 527 | v = ulong 0 |
| 528 | while lt k e bound BRBIG |
| 529 | if not brdig get fxo k |
| 530 | break |
| 531 | end |
| 532 | v = add mul v 10 ulong sub get fxo k 48 |
| 533 | k = add k 1 |
| 534 | end |
| 535 | if or eq k f gt sub k f 9 |
| 536 | ret 0 |
| 537 | end |
| 538 | if ge k e |
| 539 | ret 0 |
| 540 | end |
| 541 | if and ne get fxo k 46 ne get fxo k 41 |
| 542 | ret 0 |
| 543 | end |
| 544 | t = 2 |
| 545 | brms BMMK ulong get fxo k |
| 546 | k = add k 1 |
| 547 | brms BMMN v |
| 548 | end |
| 549 | if lt k e |
| 550 | if not brspc get fxo k |
| 551 | ret 0 |
| 552 | end |
| 553 | end |
| 554 | w = sub k f |
| 555 | sp = ulong 0 |
| 556 | while lt k e bound BRBIG |
| 557 | if or ne get fxo k 32 ge sp 4 |
| 558 | break |
| 559 | end |
| 560 | sp = add sp 1 |
| 561 | k = add k 1 |
| 562 | end |
| 563 | if eq sp 0 |
| 564 | sp = 1 |
| 565 | end |
| 566 | if ge sp 4 |
| 567 | if ge k e |
| 568 | sp = 1 |
| 569 | end |
| 570 | end |
| 571 | brms BMMP k |
| 572 | brms BMMC add w sp |
| 573 | ret t |
| 574 | end |
| 575 | fn brmlopen t ulong to void |
| 576 | if eq t 2 |
| 577 | n = brmg BMMN |
| 578 | if eq n 1 |
| 579 | fput tbmol |
| 580 | else |
| 581 | fput tbmols |
| 582 | fnum n |
| 583 | fput tbmaq |
| 584 | end |
| 585 | else |
| 586 | fput tbmul |
| 587 | end |
| 588 | end |
| 589 | fn brmitem p ulong e ulong i ulong t ulong nx ulong to void |
| 590 | brmcleaf |
| 591 | cp = brmg BMMP |
| 592 | w = brmg BMMC |
| 593 | while gt brmg BML 0 bound BRBIG |
| 594 | if ge i get gbmlm sub brmg BML 1 |
| 595 | break |
| 596 | end |
| 597 | brmlclose |
| 598 | end |
| 599 | l = brmg BML |
| 600 | sib = false |
| 601 | if gt l 0 |
| 602 | sib = lt i brmlci |
| 603 | end |
| 604 | mk = brmg BMMK |
| 605 | if sib |
| 606 | fput tbmlie |
| 607 | if ne get gbmlk sub l 1 mk |
| 608 | if eq get gbmlt sub l 1 1 |
| 609 | fput tbmole |
| 610 | else |
| 611 | fput tbmule |
| 612 | end |
| 613 | brms BMB 0 |
| 614 | brmlopen t |
| 615 | end |
| 616 | l = sub l 1 |
| 617 | else |
| 618 | if ge l 8 |
| 619 | brmclist 7 |
| 620 | l = 7 |
| 621 | end |
| 622 | brms BMB 0 |
| 623 | brmlopen t |
| 624 | end |
| 625 | put gbmlk l mk |
| 626 | put gbmlm l i |
| 627 | put gbmlc l add i w |
| 628 | put gbmlt l sub t 1 |
| 629 | brms BML add l 1 |
| 630 | fput tbmli |
| 631 | brmtask cp e nx |
| 632 | end |
| 633 | note brmtask: item text after the marker, with a GFM task box when it starts "[ ] " / "[x] " |
| 634 | fn brmtask cp ulong e ulong nx ulong to void |
| 635 | if ge cp e |
| 636 | ret |
| 637 | end |
| 638 | p = cp |
| 639 | if le add cp 3 e |
| 640 | if and eq get fxo cp 91 eq get fxo add cp 2 93 |
| 641 | x = get fxo add cp 1 |
| 642 | ok = or eq add cp 3 e brspc get fxo add cp 3 |
| 643 | if and ok eq x 32 |
| 644 | fput tbmchk |
| 645 | p = add cp 3 |
| 646 | end |
| 647 | if and ok or eq x 120 eq x 88 |
| 648 | fput tbmchkd |
| 649 | p = add cp 3 |
| 650 | end |
| 651 | end |
| 652 | end |
| 653 | brmleaf p e nx e 0 |
| 654 | end |
| 655 | note ---- leaf blocks |
| 656 | fn brmleaf p ulong e ulong nx ulong b ulong ci ulong to void |
| 657 | i = brmcols p e |
| 658 | f = brmg BMFP |
| 659 | if ge f e |
| 660 | ret |
| 661 | end |
| 662 | if and ge i 4 eq brmg BMP 0 |
| 663 | brmicode p e |
| 664 | ret |
| 665 | end |
| 666 | if brmisfence f e |
| 667 | brmfopen f e add ci i |
| 668 | ret |
| 669 | end |
| 670 | c = get fxo f |
| 671 | if eq c 60 |
| 672 | if lt i 4 |
| 673 | x = brmcbx f e |
| 674 | if gt x 0 |
| 675 | brmcleaf |
| 676 | if gt x e |
| 677 | brms BMXE x |
| 678 | end |
| 679 | ret |
| 680 | end |
| 681 | end |
| 682 | end |
| 683 | if eq c 35 |
| 684 | if brmhead f e |
| 685 | ret |
| 686 | end |
| 687 | end |
| 688 | if brmhr f e |
| 689 | brmcleaf |
| 690 | fput tbmhr |
| 691 | ret |
| 692 | end |
| 693 | if eq brmg BMP 0 |
| 694 | if brmleaf2 f e nx b |
| 695 | ret |
| 696 | end |
| 697 | end |
| 698 | if eq brmg BMT 1 |
| 699 | brmct |
| 700 | end |
| 701 | brmpline f e |
| 702 | end |
| 703 | note brmleaf2: definitions, tables and setext headings (only where no paragraph is open) |
| 704 | fn brmleaf2 f ulong e ulong nx ulong b ulong to bool |
| 705 | if brmdefln f e false |
| 706 | ret true |
| 707 | end |
| 708 | if eq brmg BMT 1 |
| 709 | if brmhaspipe f e |
| 710 | brmrow f e |
| 711 | ret true |
| 712 | end |
| 713 | brmct |
| 714 | end |
| 715 | if lt nx b |
| 716 | brmnext nx b |
| 717 | ns = brmg BMNS |
| 718 | nn = brmg BMNE |
| 719 | if brmhaspipe f e |
| 720 | n = brmdelim ns nn |
| 721 | if gt n 0 |
| 722 | brmthead f e n |
| 723 | brms BMSK 1 |
| 724 | ret true |
| 725 | end |
| 726 | end |
| 727 | h = brmsetx ns nn |
| 728 | if gt h 0 |
| 729 | brmhopen h f e |
| 730 | brmi f brmtrim f e |
| 731 | brmhclose h |
| 732 | brms BMSK 1 |
| 733 | ret true |
| 734 | end |
| 735 | end |
| 736 | ret false |
| 737 | end |
| 738 | note brmnext: the next line, its quote markers and indentation skipped, into BMNS/BMNE |
| 739 | fn brmnext nx ulong b ulong to void |
| 740 | e = brxfind nx b 10 |
| 741 | if gt e nx |
| 742 | if eq get fxo sub e 1 13 |
| 743 | e = sub e 1 |
| 744 | end |
| 745 | end |
| 746 | q = brmq nx e 64 |
| 747 | p = brmg BMCP |
| 748 | i = brmcols p e |
| 749 | brms BMNS brmg BMFP |
| 750 | brms BMNE e |
| 751 | end |
| 752 | fn brmsetx s ulong e ulong to ulong |
| 753 | if ge s e |
| 754 | ret 0 |
| 755 | end |
| 756 | c = get fxo s |
| 757 | if and ne c 61 ne c 45 |
| 758 | ret 0 |
| 759 | end |
| 760 | n = brmrun s e c |
| 761 | if not brmblank add s n e |
| 762 | ret 0 |
| 763 | end |
| 764 | if eq c 61 |
| 765 | ret 1 |
| 766 | end |
| 767 | if ge n 2 |
| 768 | ret 2 |
| 769 | end |
| 770 | ret 0 |
| 771 | end |
| 772 | fn brmisfence f ulong e ulong to bool |
| 773 | c = get fxo f |
| 774 | if and ne c 96 ne c 126 |
| 775 | ret false |
| 776 | end |
| 777 | n = brmrun f e c |
| 778 | if lt n 3 |
| 779 | ret false |
| 780 | end |
| 781 | if eq c 126 |
| 782 | ret true |
| 783 | end |
| 784 | ret ge brxfind add f n e 96 e |
| 785 | end |
| 786 | fn brmfopen f ulong e ulong ind ulong to void |
| 787 | brmcleaf |
| 788 | c = get fxo f |
| 789 | n = brmrun f e c |
| 790 | brms BMF 1 |
| 791 | brms BMFC ulong c |
| 792 | brms BMFN n |
| 793 | brms BMFI ind |
| 794 | s = add f n |
| 795 | while lt s e bound BRBIG |
| 796 | if not brspc get fxo s |
| 797 | break |
| 798 | end |
| 799 | s = add s 1 |
| 800 | end |
| 801 | k = s |
| 802 | while lt k e bound BRBIG |
| 803 | if brspc get fxo k |
| 804 | break |
| 805 | end |
| 806 | k = add k 1 |
| 807 | end |
| 808 | if gt k s |
| 809 | fput tbmprl |
| 810 | brmspan s k |
| 811 | fput tbaq |
| 812 | else |
| 813 | fput tbmpre |
| 814 | end |
| 815 | end |
| 816 | fn brmhr f ulong e ulong to bool |
| 817 | c = get fxo f |
| 818 | if and ne c 45 and ne c 42 ne c 95 |
| 819 | ret false |
| 820 | end |
| 821 | n = ulong 0 |
| 822 | i = f |
| 823 | while lt i e bound BRBIG |
| 824 | x = get fxo i |
| 825 | if eq x c |
| 826 | n = add n 1 |
| 827 | else |
| 828 | if not brspc x |
| 829 | ret false |
| 830 | end |
| 831 | end |
| 832 | i = add i 1 |
| 833 | end |
| 834 | ret ge n 3 |
| 835 | end |
| 836 | note brmpline: one paragraph line (opens the paragraph when needed) |
| 837 | fn brmpline f ulong e ulong to void |
| 838 | if eq brmg BMP 0 |
| 839 | brmcleaf |
| 840 | brms BMP 1 |
| 841 | brms BMPW 1 |
| 842 | if and gt brmg BML 0 eq brmg BMB 0 |
| 843 | brms BMPW 0 |
| 844 | end |
| 845 | if eq brmg BMPW 1 |
| 846 | fput tbmp |
| 847 | end |
| 848 | else |
| 849 | if eq brmg BMHB 1 |
| 850 | fput tbmbr |
| 851 | else |
| 852 | fputc 10 |
| 853 | end |
| 854 | end |
| 855 | k = brmtrim f e |
| 856 | hb = ulong 0 |
| 857 | if ge sub e k 2 |
| 858 | hb = 1 |
| 859 | end |
| 860 | if gt k f |
| 861 | if eq get fxo sub k 1 92 |
| 862 | hb = 1 |
| 863 | k = sub k 1 |
| 864 | end |
| 865 | end |
| 866 | brms BMHB hb |
| 867 | brmi f k |
| 868 | end |
| 869 | note ---- HTML comments |
| 870 | note "<!--" up to the first "-->" renders nothing ("<!-->" and "<!--->" included, as in CommonMark |
| 871 | note 0.31). No byte of a comment is ever written, so a comment cannot carry markup through; "--!>", |
| 872 | note "-- >" and a nested "<!--" do not end it, only the first "-->" (also inside "--->") does. An opener with no |
| 873 | note "-->" after it in the document is not a comment: it stays text and is escaped like any other "<". |
| 874 | note Fenced and indented code and code spans never reach these checks, so comments there stay text. |
| 875 | note Block: a line starting with "<!--" (under 4 columns of indent) closes the open leaf block and |
| 876 | note renders nothing up to the first "-->", across lines, when only spaces and further comments |
| 877 | note follow the "-->" or the "-->" is on a later line; text after a "-->" on a later line is paragraph |
| 878 | note text. A comment that closes on its own line with other text after it is paragraph text instead |
| 879 | note (the inline rule drops it). Inline: a comment that opens and closes inside one inline run is |
| 880 | note dropped from it (headings, emphasis, link text, table cells, list items); heading anchors skip it. |
| 881 | fn brmisopen i ulong to bool |
| 882 | if ne get fxo i 60 |
| 883 | ret false |
| 884 | end |
| 885 | if ne get fxo add i 1 33 |
| 886 | ret false |
| 887 | end |
| 888 | if ne get fxo add i 2 45 |
| 889 | ret false |
| 890 | end |
| 891 | ret eq get fxo add i 3 45 |
| 892 | end |
| 893 | note brmcmend: the first "-->" starting at or after p, BMDB when none. The cached scan (no "-->" in |
| 894 | note [BMCF, BMCX)) answers every later lookup inside it, and a lookup before BMCF scans only up to |
| 895 | note BMCF, so the lookups of one document (made in document order) scan each byte about once. |
| 896 | fn brmcmend p ulong to ulong |
| 897 | b = brmg BMDB |
| 898 | lim = b |
| 899 | if eq brmg BMCV 1 |
| 900 | cf = brmg BMCF |
| 901 | cx = brmg BMCX |
| 902 | if le cf p |
| 903 | if le p cx |
| 904 | ret cx |
| 905 | end |
| 906 | else |
| 907 | lim = cf |
| 908 | end |
| 909 | end |
| 910 | j = p |
| 911 | r = b |
| 912 | while lt j lim bound BRBIG |
| 913 | if ge add j 2 b |
| 914 | break |
| 915 | end |
| 916 | if eq get fxo j 45 |
| 917 | if eq get fxo add j 1 45 |
| 918 | if eq get fxo add j 2 62 |
| 919 | r = j |
| 920 | break |
| 921 | end |
| 922 | end |
| 923 | end |
| 924 | j = add j 1 |
| 925 | end |
| 926 | if eq r b |
| 927 | if lt lim b |
| 928 | r = brmg BMCX |
| 929 | end |
| 930 | end |
| 931 | brms BMCV 1 |
| 932 | brms BMCF p |
| 933 | brms BMCX r |
| 934 | ret r |
| 935 | end |
| 936 | note brmicmt: an inline comment at i that closes before e: the end after its "-->", else 0 |
| 937 | fn brmicmt i ulong e ulong to ulong |
| 938 | if gt add i 4 e |
| 939 | ret 0 |
| 940 | end |
| 941 | if not brmisopen i |
| 942 | ret 0 |
| 943 | end |
| 944 | j = brmcmend add i 2 |
| 945 | if gt add j 3 e |
| 946 | ret 0 |
| 947 | end |
| 948 | ret add j 3 |
| 949 | end |
| 950 | note brmcbx: a comment block at f (under 4 columns of indent, checked by the callers) on the line |
| 951 | note ending at e: the end after its last "-->" when the line holds only comments and spaces or its |
| 952 | note last comment closes on a later line, else 0 |
| 953 | fn brmcbx f ulong e ulong to ulong |
| 954 | p = f |
| 955 | while lt p e bound BRBIG |
| 956 | if gt add p 4 e |
| 957 | ret 0 |
| 958 | end |
| 959 | if not brmisopen p |
| 960 | ret 0 |
| 961 | end |
| 962 | j = brmcmend add p 2 |
| 963 | if ge j brmg BMDB |
| 964 | ret 0 |
| 965 | end |
| 966 | x = add j 3 |
| 967 | if gt x e |
| 968 | ret x |
| 969 | end |
| 970 | p = x |
| 971 | while lt p e bound BRBIG |
| 972 | if not brspc get fxo p |
| 973 | break |
| 974 | end |
| 975 | p = add p 1 |
| 976 | end |
| 977 | if ge p e |
| 978 | ret x |
| 979 | end |
| 980 | end |
| 981 | ret 0 |
| 982 | end |
| 983 | note brmcline: a line ending at k while a comment block is open (BMXE): nothing before the "-->" |
| 984 | note renders; text after it on the closing line is paragraph text |
| 985 | fn brmcline k ulong to void |
| 986 | x = brmg BMXE |
| 987 | if gt x k |
| 988 | ret |
| 989 | end |
| 990 | brms BMXE 0 |
| 991 | if brmblank x k |
| 992 | ret |
| 993 | end |
| 994 | i = brmcols x k |
| 995 | brmpline brmg BMFP k |
| 996 | brms BMB 0 |
| 997 | end |
| 998 | note ---- headings |
| 999 | fn brmhead f ulong e ulong to bool |
| 1000 | n = brmrun f e 35 |
| 1001 | if gt n 6 |
| 1002 | ret false |
| 1003 | end |
| 1004 | s = add f n |
| 1005 | if lt s e |
| 1006 | if not brspc get fxo s |
| 1007 | ret false |
| 1008 | end |
| 1009 | end |
| 1010 | while lt s e bound BRBIG |
| 1011 | if not brspc get fxo s |
| 1012 | break |
| 1013 | end |
| 1014 | s = add s 1 |
| 1015 | end |
| 1016 | k = brmtrim s e |
| 1017 | j = k |
| 1018 | while gt j s bound BRBIG |
| 1019 | if ne get fxo sub j 1 35 |
| 1020 | break |
| 1021 | end |
| 1022 | j = sub j 1 |
| 1023 | end |
| 1024 | if lt j k |
| 1025 | if eq j s |
| 1026 | k = j |
| 1027 | else |
| 1028 | if brspc get fxo sub j 1 |
| 1029 | k = brmtrim s j |
| 1030 | end |
| 1031 | end |
| 1032 | end |
| 1033 | brmcleaf |
| 1034 | brmhopen n s k |
| 1035 | brmi s k |
| 1036 | brmhclose n |
| 1037 | ret true |
| 1038 | end |
| 1039 | fn brmhopen n ulong s ulong k ulong to void |
| 1040 | brmcleaf |
| 1041 | fput tbmh |
| 1042 | fputc uchar add n 48 |
| 1043 | fput tbmhid |
| 1044 | brmslug s k |
| 1045 | fput tbaq |
| 1046 | end |
| 1047 | fn brmhclose n ulong to void |
| 1048 | fput tbmhx |
| 1049 | fputc uchar add n 48 |
| 1050 | fput tbmgt |
| 1051 | end |
| 1052 | note brmslug: heading anchor: ASCII letters lower-cased, digits, "-" and "_" kept, spaces to "-", |
| 1053 | note bytes >= 128 kept, HTML comments and everything else dropped |
| 1054 | fn brmslug s ulong k ulong to void |
| 1055 | i = s |
| 1056 | while lt i k bound BRBIG |
| 1057 | c = get fxo i |
| 1058 | if eq c 60 |
| 1059 | r = brmicmt i k |
| 1060 | if gt r 0 |
| 1061 | i = r |
| 1062 | continue |
| 1063 | end |
| 1064 | end |
| 1065 | if bralnum c |
| 1066 | fputc brlow c |
| 1067 | end |
| 1068 | if or eq c 45 eq c 95 |
| 1069 | fputc c |
| 1070 | end |
| 1071 | if eq c 32 |
| 1072 | fputc 45 |
| 1073 | end |
| 1074 | if ge c 128 |
| 1075 | fputc c |
| 1076 | end |
| 1077 | i = add i 1 |
| 1078 | end |
| 1079 | end |
| 1080 | note ---- tables |
| 1081 | fn brmhaspipe f ulong e ulong to bool |
| 1082 | i = f |
| 1083 | while lt i e bound BRBIG |
| 1084 | c = get fxo i |
| 1085 | if eq c 92 |
| 1086 | i = add i 1 |
| 1087 | else |
| 1088 | if eq c 124 |
| 1089 | ret true |
| 1090 | end |
| 1091 | end |
| 1092 | i = add i 1 |
| 1093 | end |
| 1094 | ret false |
| 1095 | end |
| 1096 | note brmdelim: GFM delimiter row -> column count (aligns into gbmta), 0 when not one |
| 1097 | fn brmdelim s ulong e ulong to ulong |
| 1098 | e2 = brmtrim s e |
| 1099 | if ge s e2 |
| 1100 | ret 0 |
| 1101 | end |
| 1102 | a = s |
| 1103 | if eq get fxo a 124 |
| 1104 | a = add a 1 |
| 1105 | end |
| 1106 | if gt e2 a |
| 1107 | if eq get fxo sub e2 1 124 |
| 1108 | e2 = sub e2 1 |
| 1109 | end |
| 1110 | end |
| 1111 | n = ulong 0 |
| 1112 | c = a |
| 1113 | while le c e2 bound BRBIG |
| 1114 | k = brxfind c e2 124 |
| 1115 | if not brmdcell c k n |
| 1116 | ret 0 |
| 1117 | end |
| 1118 | n = add n 1 |
| 1119 | if ge n 64 |
| 1120 | ret 0 |
| 1121 | end |
| 1122 | c = add k 1 |
| 1123 | end |
| 1124 | ret n |
| 1125 | end |
| 1126 | fn brmdcell a ulong k ulong n ulong to bool |
| 1127 | s = a |
| 1128 | while lt s k bound BRBIG |
| 1129 | if not brspc get fxo s |
| 1130 | break |
| 1131 | end |
| 1132 | s = add s 1 |
| 1133 | end |
| 1134 | t = brmtrim s k |
| 1135 | if ge s t |
| 1136 | ret false |
| 1137 | end |
| 1138 | al = ulong 0 |
| 1139 | if eq get fxo s 58 |
| 1140 | al = 1 |
| 1141 | s = add s 1 |
| 1142 | end |
| 1143 | if lt s t |
| 1144 | if eq get fxo sub t 1 58 |
| 1145 | al = add al 2 |
| 1146 | t = sub t 1 |
| 1147 | end |
| 1148 | end |
| 1149 | if ge s t |
| 1150 | ret false |
| 1151 | end |
| 1152 | for i ulong sub t s bound BRBIG |
| 1153 | if ne get fxo add s i 45 |
| 1154 | ret false |
| 1155 | end |
| 1156 | end |
| 1157 | put gbmta n uchar al |
| 1158 | ret true |
| 1159 | end |
| 1160 | fn brmthead f ulong e ulong n ulong to void |
| 1161 | brmcleaf |
| 1162 | brms BMT 1 |
| 1163 | brms BMTC n |
| 1164 | fput tbmtab |
| 1165 | brmcells f e 1 |
| 1166 | fput tbmthe |
| 1167 | end |
| 1168 | fn brmrow f ulong e ulong to void |
| 1169 | fput tbmtr |
| 1170 | brmcells f e 0 |
| 1171 | fput tbmtre |
| 1172 | end |
| 1173 | note brmcells: split fxo[f, e) on unescaped "|" (outside code spans) into BMTC cells |
| 1174 | fn brmcells f ulong e ulong th ulong to void |
| 1175 | e2 = brmtrim f e |
| 1176 | a = f |
| 1177 | if lt a e2 |
| 1178 | if eq get fxo a 124 |
| 1179 | a = add a 1 |
| 1180 | end |
| 1181 | end |
| 1182 | if gt e2 a |
| 1183 | if eq get fxo sub e2 1 124 |
| 1184 | if ne get fxo sub e2 2 92 |
| 1185 | e2 = sub e2 1 |
| 1186 | end |
| 1187 | end |
| 1188 | end |
| 1189 | n = brmg BMTC |
| 1190 | col = ulong 0 |
| 1191 | c = a |
| 1192 | while lt col n bound BRBIG |
| 1193 | k = brmcend c e2 |
| 1194 | if gt c e2 |
| 1195 | k = c |
| 1196 | end |
| 1197 | brmcell c k col th |
| 1198 | col = add col 1 |
| 1199 | c = add k 1 |
| 1200 | end |
| 1201 | end |
| 1202 | fn brmcend c ulong e ulong to ulong |
| 1203 | i = c |
| 1204 | while lt i e bound BRBIG |
| 1205 | x = get fxo i |
| 1206 | if eq x 92 |
| 1207 | i = add i 2 |
| 1208 | else |
| 1209 | if eq x 124 |
| 1210 | ret i |
| 1211 | end |
| 1212 | if eq x 96 |
| 1213 | i = brmcskip i e |
| 1214 | else |
| 1215 | i = add i 1 |
| 1216 | end |
| 1217 | end |
| 1218 | end |
| 1219 | ret e |
| 1220 | end |
| 1221 | fn brmcell a ulong k ulong col ulong th ulong to void |
| 1222 | al = get gbmta col |
| 1223 | if eq th 1 |
| 1224 | if eq al 1 |
| 1225 | fput tbmthl |
| 1226 | end |
| 1227 | if eq al 2 |
| 1228 | fput tbmthr |
| 1229 | end |
| 1230 | if eq al 3 |
| 1231 | fput tbmthc |
| 1232 | end |
| 1233 | if eq al 0 |
| 1234 | fput tbmth |
| 1235 | end |
| 1236 | else |
| 1237 | if eq al 1 |
| 1238 | fput tbmtdl |
| 1239 | end |
| 1240 | if eq al 2 |
| 1241 | fput tbmtdr |
| 1242 | end |
| 1243 | if eq al 3 |
| 1244 | fput tbmtdc |
| 1245 | end |
| 1246 | if eq al 0 |
| 1247 | fput tbmtd |
| 1248 | end |
| 1249 | end |
| 1250 | if lt a k |
| 1251 | s = a |
| 1252 | while lt s k bound BRBIG |
| 1253 | if not brspc get fxo s |
| 1254 | break |
| 1255 | end |
| 1256 | s = add s 1 |
| 1257 | end |
| 1258 | brmi s brmtrim s k |
| 1259 | end |
| 1260 | if eq th 1 |
| 1261 | fput tbmthx |
| 1262 | else |
| 1263 | fput tbmtdx |
| 1264 | end |
| 1265 | end |
| 1266 | note ---- link reference definitions: "[label]: url" (title ignored) |
| 1267 | fn brmdefs a ulong b ulong to void |
| 1268 | s = a |
| 1269 | while lt s b bound BRBIG |
| 1270 | e = brxfind s b 10 |
| 1271 | k = e |
| 1272 | if gt k s |
| 1273 | if eq get fxo sub k 1 13 |
| 1274 | k = sub k 1 |
| 1275 | end |
| 1276 | end |
| 1277 | i = brmskipc s k 3 |
| 1278 | if lt i k |
| 1279 | if eq get fxo i 91 |
| 1280 | x = brmdefln i k true |
| 1281 | end |
| 1282 | end |
| 1283 | s = add e 1 |
| 1284 | end |
| 1285 | end |
| 1286 | fn brmdefln f ulong e ulong rec bool to bool |
| 1287 | if ge f e |
| 1288 | ret false |
| 1289 | end |
| 1290 | if ne get fxo f 91 |
| 1291 | ret false |
| 1292 | end |
| 1293 | j = add f 1 |
| 1294 | while lt j e bound BRBIG |
| 1295 | c = get fxo j |
| 1296 | if or eq c 93 eq c 91 |
| 1297 | break |
| 1298 | end |
| 1299 | j = add j 1 |
| 1300 | end |
| 1301 | if or ge j e eq j add f 1 |
| 1302 | ret false |
| 1303 | end |
| 1304 | if ne get fxo j 93 |
| 1305 | ret false |
| 1306 | end |
| 1307 | if ge add j 1 e |
| 1308 | ret false |
| 1309 | end |
| 1310 | if ne get fxo add j 1 58 |
| 1311 | ret false |
| 1312 | end |
| 1313 | u = add j 2 |
| 1314 | while lt u e bound BRBIG |
| 1315 | if not brspc get fxo u |
| 1316 | break |
| 1317 | end |
| 1318 | u = add u 1 |
| 1319 | end |
| 1320 | if ge u e |
| 1321 | ret false |
| 1322 | end |
| 1323 | ue = u |
| 1324 | while lt ue e bound BRBIG |
| 1325 | if brspc get fxo ue |
| 1326 | break |
| 1327 | end |
| 1328 | ue = add ue 1 |
| 1329 | end |
| 1330 | if eq get fxo u 60 |
| 1331 | if eq get fxo sub ue 1 62 |
| 1332 | u = add u 1 |
| 1333 | ue = sub ue 1 |
| 1334 | end |
| 1335 | end |
| 1336 | if rec |
| 1337 | brmdefrec add f 1 j u ue |
| 1338 | end |
| 1339 | ret true |
| 1340 | end |
| 1341 | fn brmdefrec ls ulong lz ulong us ulong ue ulong to void |
| 1342 | n = get gbmrn 0 |
| 1343 | if ge n 128 |
| 1344 | ret |
| 1345 | end |
| 1346 | b = mul n 4 |
| 1347 | put gbmrd b ls |
| 1348 | put gbmrd add b 1 lz |
| 1349 | put gbmrd add b 2 us |
| 1350 | put gbmrd add b 3 ue |
| 1351 | put gbmrn 0 add n 1 |
| 1352 | end |
| 1353 | note brmref: find the definition labelled fxo[s, e) (ASCII case-insensitive); sets BMDS/BMDE |
| 1354 | fn brmref s ulong e ulong to bool |
| 1355 | n = get gbmrn 0 |
| 1356 | for d ulong n bound BRBIG |
| 1357 | b = mul d 4 |
| 1358 | ls = get gbmrd b |
| 1359 | lz = get gbmrd add b 1 |
| 1360 | if eq sub lz ls sub e s |
| 1361 | if brmlabeq ls s sub e s |
| 1362 | brms BMDS get gbmrd add b 2 |
| 1363 | brms BMDE get gbmrd add b 3 |
| 1364 | brms BMTS 0 |
| 1365 | brms BMTE 0 |
| 1366 | ret true |
| 1367 | end |
| 1368 | end |
| 1369 | end |
| 1370 | ret false |
| 1371 | end |
| 1372 | fn brmlabeq a ulong b ulong n ulong to bool |
| 1373 | for i ulong n bound BRBIG |
| 1374 | if ne brlow get fxo add a i brlow get fxo add b i |
| 1375 | ret false |
| 1376 | end |
| 1377 | end |
| 1378 | ret true |
| 1379 | end |
| 1380 | note ---- inline |
| 1381 | fn brmi a ulong e ulong to void |
| 1382 | i = a |
| 1383 | while lt i e bound BRBIG |
| 1384 | i = brmi1 i e a |
| 1385 | end |
| 1386 | end |
| 1387 | fn brmi1 i ulong e ulong a ulong to ulong |
| 1388 | c = get fxo i |
| 1389 | if eq c 92 |
| 1390 | if lt add i 1 e |
| 1391 | if brpunct get fxo add i 1 |
| 1392 | fescc get fxo add i 1 |
| 1393 | ret add i 2 |
| 1394 | end |
| 1395 | end |
| 1396 | fputc 92 |
| 1397 | ret add i 1 |
| 1398 | end |
| 1399 | if eq c 96 |
| 1400 | ret brmcode i e |
| 1401 | end |
| 1402 | if or eq c 33 eq c 91 |
| 1403 | r = brmlnk i e |
| 1404 | if gt r 0 |
| 1405 | ret r |
| 1406 | end |
| 1407 | fputc c |
| 1408 | ret add i 1 |
| 1409 | end |
| 1410 | if eq c 60 |
| 1411 | r = brmicmt i e |
| 1412 | if gt r 0 |
| 1413 | ret r |
| 1414 | end |
| 1415 | r = brmauto i e |
| 1416 | if gt r 0 |
| 1417 | ret r |
| 1418 | end |
| 1419 | fescc c |
| 1420 | ret add i 1 |
| 1421 | end |
| 1422 | ret brmi2 i e a |
| 1423 | end |
| 1424 | fn brmi2 i ulong e ulong a ulong to ulong |
| 1425 | c = get fxo i |
| 1426 | if or eq c 42 eq c 95 |
| 1427 | r = brmemph i e a |
| 1428 | if gt r 0 |
| 1429 | ret r |
| 1430 | end |
| 1431 | n = brmrun i e c |
| 1432 | for k ulong n bound BRBIG |
| 1433 | fputc c |
| 1434 | end |
| 1435 | ret add i n |
| 1436 | end |
| 1437 | if eq c 126 |
| 1438 | r = brmstrike i e |
| 1439 | if gt r 0 |
| 1440 | ret r |
| 1441 | end |
| 1442 | fputc c |
| 1443 | ret add i 1 |
| 1444 | end |
| 1445 | if eq c 104 |
| 1446 | r = brmbare i e a |
| 1447 | if gt r 0 |
| 1448 | ret r |
| 1449 | end |
| 1450 | end |
| 1451 | if eq c 38 |
| 1452 | r = brment i e |
| 1453 | if gt r 0 |
| 1454 | ret r |
| 1455 | end |
| 1456 | end |
| 1457 | fescc c |
| 1458 | ret add i 1 |
| 1459 | end |
| 1460 | note brmcskip: past the code span starting at i (or past its backtick run when unmatched) |
| 1461 | fn brmcskip i ulong e ulong to ulong |
| 1462 | n = brmrun i e 96 |
| 1463 | j = add i n |
| 1464 | while lt j e bound BRBIG |
| 1465 | if eq get fxo j 96 |
| 1466 | m = brmrun j e 96 |
| 1467 | if eq m n |
| 1468 | ret add j m |
| 1469 | end |
| 1470 | j = add j m |
| 1471 | else |
| 1472 | j = add j 1 |
| 1473 | end |
| 1474 | end |
| 1475 | ret add i n |
| 1476 | end |
| 1477 | fn brmcode i ulong e ulong to ulong |
| 1478 | n = brmrun i e 96 |
| 1479 | k = brmcskip i e |
| 1480 | if eq k add i n |
| 1481 | for x ulong n bound BRBIG |
| 1482 | fputc 96 |
| 1483 | end |
| 1484 | ret k |
| 1485 | end |
| 1486 | s = add i n |
| 1487 | t = sub k n |
| 1488 | if gt sub t s 1 |
| 1489 | if and eq get fxo s 32 eq get fxo sub t 1 32 |
| 1490 | if not brmblank s t |
| 1491 | s = add s 1 |
| 1492 | t = sub t 1 |
| 1493 | end |
| 1494 | end |
| 1495 | end |
| 1496 | fput tbmcode |
| 1497 | brmspan s t |
| 1498 | fput tbmcodx |
| 1499 | ret k |
| 1500 | end |
| 1501 | note brmbrk: matching "]" for the "[" at i (nested brackets, escapes and code spans honoured) |
| 1502 | fn brmbrk i ulong e ulong to ulong |
| 1503 | d = ulong 0 |
| 1504 | j = i |
| 1505 | while lt j e bound BRBIG |
| 1506 | c = get fxo j |
| 1507 | if eq c 92 |
| 1508 | j = add j 2 |
| 1509 | continue |
| 1510 | end |
| 1511 | if eq c 96 |
| 1512 | j = brmcskip j e |
| 1513 | continue |
| 1514 | end |
| 1515 | if eq c 91 |
| 1516 | d = add d 1 |
| 1517 | end |
| 1518 | if eq c 93 |
| 1519 | d = sub d 1 |
| 1520 | if eq d 0 |
| 1521 | ret j |
| 1522 | end |
| 1523 | end |
| 1524 | j = add j 1 |
| 1525 | end |
| 1526 | ret 0 |
| 1527 | end |
| 1528 | note brmdest: "(dest "title")" at k -> end after ")" or 0; dest to BMDS/BMDE, title BMTS/BMTE |
| 1529 | fn brmdest k ulong e ulong to ulong |
| 1530 | j = brmsksp add k 1 e |
| 1531 | s = j |
| 1532 | if and lt j e eq get fxo j 60 |
| 1533 | s = add j 1 |
| 1534 | j = brxfind s e 62 |
| 1535 | if ge j e |
| 1536 | ret 0 |
| 1537 | end |
| 1538 | brms BMDS s |
| 1539 | brms BMDE j |
| 1540 | j = add j 1 |
| 1541 | else |
| 1542 | d = ulong 0 |
| 1543 | while lt j e bound BRBIG |
| 1544 | c = get fxo j |
| 1545 | if brspc c |
| 1546 | break |
| 1547 | end |
| 1548 | if eq c 40 |
| 1549 | d = add d 1 |
| 1550 | end |
| 1551 | if eq c 41 |
| 1552 | if eq d 0 |
| 1553 | break |
| 1554 | end |
| 1555 | d = sub d 1 |
| 1556 | end |
| 1557 | j = add j 1 |
| 1558 | end |
| 1559 | brms BMDS s |
| 1560 | brms BMDE j |
| 1561 | end |
| 1562 | ret brmtitle j e |
| 1563 | end |
| 1564 | fn brmsksp j ulong e ulong to ulong |
| 1565 | i = j |
| 1566 | while lt i e bound BRBIG |
| 1567 | if not brspc get fxo i |
| 1568 | ret i |
| 1569 | end |
| 1570 | i = add i 1 |
| 1571 | end |
| 1572 | ret i |
| 1573 | end |
| 1574 | fn brmtitle j ulong e ulong to ulong |
| 1575 | brms BMTS 0 |
| 1576 | brms BMTE 0 |
| 1577 | i = brmsksp j e |
| 1578 | if ge i e |
| 1579 | ret 0 |
| 1580 | end |
| 1581 | c = get fxo i |
| 1582 | if or eq c 34 eq c 39 |
| 1583 | t = brxfind add i 1 e c |
| 1584 | if ge t e |
| 1585 | ret 0 |
| 1586 | end |
| 1587 | brms BMTS add i 1 |
| 1588 | brms BMTE t |
| 1589 | i = brmsksp add t 1 e |
| 1590 | end |
| 1591 | if ge i e |
| 1592 | ret 0 |
| 1593 | end |
| 1594 | if ne get fxo i 41 |
| 1595 | ret 0 |
| 1596 | end |
| 1597 | ret add i 1 |
| 1598 | end |
| 1599 | note brmlnk: "[text](dest)", "[text][label]", "[label]" and the same with "!" for images |
| 1600 | fn brmlnk i ulong e ulong to ulong |
| 1601 | img = eq get fxo i 33 |
| 1602 | o = i |
| 1603 | if img |
| 1604 | o = add i 1 |
| 1605 | if ge o e |
| 1606 | ret 0 |
| 1607 | end |
| 1608 | if ne get fxo o 91 |
| 1609 | ret 0 |
| 1610 | end |
| 1611 | end |
| 1612 | k = brmbrk o e |
| 1613 | if eq k 0 |
| 1614 | ret 0 |
| 1615 | end |
| 1616 | r = brmtarget o k e |
| 1617 | if eq r 0 |
| 1618 | ret 0 |
| 1619 | end |
| 1620 | if img |
| 1621 | brmimgout add o 1 k |
| 1622 | else |
| 1623 | brmaout add o 1 k |
| 1624 | end |
| 1625 | ret r |
| 1626 | end |
| 1627 | note brmtarget: the destination after "]" at k; end of the construct or 0 |
| 1628 | fn brmtarget o ulong k ulong e ulong to ulong |
| 1629 | n = add k 1 |
| 1630 | if lt n e |
| 1631 | if eq get fxo n 40 |
| 1632 | r = brmdest n e |
| 1633 | if gt r 0 |
| 1634 | ret r |
| 1635 | end |
| 1636 | end |
| 1637 | if eq get fxo n 91 |
| 1638 | j = brxfind n e 93 |
| 1639 | if lt j e |
| 1640 | if eq j add n 1 |
| 1641 | if brmref add o 1 k |
| 1642 | ret add j 1 |
| 1643 | end |
| 1644 | ret 0 |
| 1645 | end |
| 1646 | if brmref add n 1 j |
| 1647 | ret add j 1 |
| 1648 | end |
| 1649 | ret 0 |
| 1650 | end |
| 1651 | end |
| 1652 | end |
| 1653 | if brmref add o 1 k |
| 1654 | ret n |
| 1655 | end |
| 1656 | ret 0 |
| 1657 | end |
| 1658 | note brmkind: 0 unsafe scheme, 1 http(s)/mailto/protocol-relative, 2 "#anchor" or empty, 3 relative |
| 1659 | fn brmkind s ulong e ulong to ulong |
| 1660 | if ge s e |
| 1661 | ret 2 |
| 1662 | end |
| 1663 | c = get fxo s |
| 1664 | if eq c 35 |
| 1665 | ret 2 |
| 1666 | end |
| 1667 | if eq c 47 |
| 1668 | if lt add s 1 e |
| 1669 | if eq get fxo add s 1 47 |
| 1670 | ret 1 |
| 1671 | end |
| 1672 | end |
| 1673 | ret 3 |
| 1674 | end |
| 1675 | j = s |
| 1676 | while lt j e bound BRBIG |
| 1677 | x = get fxo j |
| 1678 | if not or bralnum x or eq x 43 or eq x 45 eq x 46 |
| 1679 | break |
| 1680 | end |
| 1681 | j = add j 1 |
| 1682 | end |
| 1683 | if and lt j e gt j s |
| 1684 | if eq get fxo j 58 |
| 1685 | if brmschem s j |
| 1686 | ret 1 |
| 1687 | end |
| 1688 | ret 0 |
| 1689 | end |
| 1690 | end |
| 1691 | j = s |
| 1692 | while lt j e bound BRBIG |
| 1693 | x = get fxo j |
| 1694 | if or eq x 58 or eq x 47 or eq x 63 eq x 35 |
| 1695 | break |
| 1696 | end |
| 1697 | j = add j 1 |
| 1698 | end |
| 1699 | if lt j e |
| 1700 | if eq get fxo j 58 |
| 1701 | ret 0 |
| 1702 | end |
| 1703 | end |
| 1704 | ret 3 |
| 1705 | end |
| 1706 | fn brmschem s ulong j ulong to bool |
| 1707 | if brmlabt s j tbmhttp |
| 1708 | ret true |
| 1709 | end |
| 1710 | if brmlabt s j tbmhttps |
| 1711 | ret true |
| 1712 | end |
| 1713 | ret brmlabt s j tbmmto |
| 1714 | end |
| 1715 | fn brmlabt s ulong j ulong t view uchar * to bool |
| 1716 | if ne sub j s len t |
| 1717 | ret false |
| 1718 | end |
| 1719 | for i ulong len t |
| 1720 | if ne brlow get fxo add s i get t i |
| 1721 | ret false |
| 1722 | end |
| 1723 | end |
| 1724 | ret true |
| 1725 | end |
| 1726 | note brmuc: a byte of a relative destination: existing %XX kept, other unsafe bytes encoded |
| 1727 | fn brmuc c uchar to void |
| 1728 | if eq c 37 |
| 1729 | fputc c |
| 1730 | ret |
| 1731 | end |
| 1732 | bruc c |
| 1733 | end |
| 1734 | note brmhref: href/src value of fxo[s, e) of kind k; raw = 1 for images (repo /raw/ URL) |
| 1735 | fn brmhref s ulong e ulong k ulong raw ulong to void |
| 1736 | if ne k 3 |
| 1737 | brmspan s e |
| 1738 | ret |
| 1739 | end |
| 1740 | q = s |
| 1741 | while lt q e bound BRBIG |
| 1742 | x = get fxo q |
| 1743 | if or eq x 35 eq x 63 |
| 1744 | break |
| 1745 | end |
| 1746 | q = add q 1 |
| 1747 | end |
| 1748 | if gt q s |
| 1749 | brmres s q |
| 1750 | brurepo |
| 1751 | if eq raw 1 |
| 1752 | fput tbmraw |
| 1753 | else |
| 1754 | fput tbmblob |
| 1755 | end |
| 1756 | bruref |
| 1757 | for i ulong get gbmul 0 bound BRBIG |
| 1758 | brmuc get gbmu i |
| 1759 | end |
| 1760 | end |
| 1761 | brmspan q e |
| 1762 | end |
| 1763 | note brmres: resolve the relative path fxo[s, q) against gbmd into gbmu ("/a/b", "" = root) |
| 1764 | fn brmres s ulong q ulong to void |
| 1765 | put gbmul 0 0 |
| 1766 | if ne get fxo s 47 |
| 1767 | if gt get gbmdl 0 0 |
| 1768 | brmuadd 47 |
| 1769 | for i ulong get gbmdl 0 bound BRBIG |
| 1770 | brmuadd get gbmd i |
| 1771 | end |
| 1772 | end |
| 1773 | end |
| 1774 | a = s |
| 1775 | while le a q bound BRBIG |
| 1776 | j = brxfind a q 47 |
| 1777 | brmuseg a j |
| 1778 | a = add j 1 |
| 1779 | end |
| 1780 | end |
| 1781 | fn brmuadd c uchar to void |
| 1782 | n = get gbmul 0 |
| 1783 | if lt n 4096 |
| 1784 | put gbmu n c |
| 1785 | put gbmul 0 add n 1 |
| 1786 | end |
| 1787 | end |
| 1788 | fn brmuseg a ulong j ulong to void |
| 1789 | n = sub j a |
| 1790 | if eq n 0 |
| 1791 | ret |
| 1792 | end |
| 1793 | if and eq n 1 eq get fxo a 46 |
| 1794 | ret |
| 1795 | end |
| 1796 | if and eq n 2 and eq get fxo a 46 eq get fxo add a 1 46 |
| 1797 | k = get gbmul 0 |
| 1798 | while gt k 0 bound BRBIG |
| 1799 | k = sub k 1 |
| 1800 | if eq get gbmu k 47 |
| 1801 | break |
| 1802 | end |
| 1803 | end |
| 1804 | put gbmul 0 k |
| 1805 | ret |
| 1806 | end |
| 1807 | brmuadd 47 |
| 1808 | for i ulong n bound BRBIG |
| 1809 | brmuadd get fxo add a i |
| 1810 | end |
| 1811 | end |
| 1812 | fn brmaout ts ulong te ulong to void |
| 1813 | ds = brmg BMDS |
| 1814 | de = brmg BMDE |
| 1815 | k = brmkind ds de |
| 1816 | if eq k 0 |
| 1817 | brmi ts te |
| 1818 | ret |
| 1819 | end |
| 1820 | fput tbah |
| 1821 | brmhref ds de k 0 |
| 1822 | if lt brmg BMTS brmg BMTE |
| 1823 | fput tbmtitl |
| 1824 | brmspan brmg BMTS brmg BMTE |
| 1825 | end |
| 1826 | if eq k 1 |
| 1827 | fput tbmaext |
| 1828 | else |
| 1829 | fput tbaq |
| 1830 | end |
| 1831 | brmi ts te |
| 1832 | fput tbae |
| 1833 | end |
| 1834 | fn brmimgout ts ulong te ulong to void |
| 1835 | ds = brmg BMDS |
| 1836 | de = brmg BMDE |
| 1837 | k = brmkind ds de |
| 1838 | if eq k 3 |
| 1839 | fput tbmimg |
| 1840 | brmhref ds de k 1 |
| 1841 | fput tbmalt |
| 1842 | brmspan ts te |
| 1843 | fput tbmimgx |
| 1844 | ret |
| 1845 | end |
| 1846 | brmspan ts te |
| 1847 | end |
| 1848 | note brmauto: "<scheme:...>" or "<user@host>" autolinks |
| 1849 | fn brmauto i ulong e ulong to ulong |
| 1850 | j = add i 1 |
| 1851 | hat = false |
| 1852 | while lt j e bound BRBIG |
| 1853 | c = get fxo j |
| 1854 | if or eq c 62 or eq c 60 brspc c |
| 1855 | break |
| 1856 | end |
| 1857 | if eq c 64 |
| 1858 | hat = true |
| 1859 | end |
| 1860 | j = add j 1 |
| 1861 | end |
| 1862 | if ge j e |
| 1863 | ret 0 |
| 1864 | end |
| 1865 | if ne get fxo j 62 |
| 1866 | ret 0 |
| 1867 | end |
| 1868 | s = add i 1 |
| 1869 | if ge s j |
| 1870 | ret 0 |
| 1871 | end |
| 1872 | k = brmkind s j |
| 1873 | if and eq k 1 ne get fxo s 47 |
| 1874 | fput tbah |
| 1875 | brmspan s j |
| 1876 | fput tbmaext |
| 1877 | brmspan s j |
| 1878 | fput tbae |
| 1879 | ret add j 1 |
| 1880 | end |
| 1881 | if and hat eq k 3 |
| 1882 | fput tbah |
| 1883 | fput tbmmail |
| 1884 | brmspan s j |
| 1885 | fput tbmaext |
| 1886 | brmspan s j |
| 1887 | fput tbae |
| 1888 | ret add j 1 |
| 1889 | end |
| 1890 | ret 0 |
| 1891 | end |
| 1892 | note brmbare: GFM-style bare http(s):// autolink at a word start |
| 1893 | fn brmbare i ulong e ulong a ulong to ulong |
| 1894 | if gt i a |
| 1895 | if bralnum get fxo sub i 1 |
| 1896 | ret 0 |
| 1897 | end |
| 1898 | end |
| 1899 | n = ulong 0 |
| 1900 | if brxat i e tbmbhs |
| 1901 | n = len tbmbhs |
| 1902 | end |
| 1903 | if eq n 0 |
| 1904 | if brxat i e tbmbh |
| 1905 | n = len tbmbh |
| 1906 | end |
| 1907 | end |
| 1908 | if eq n 0 |
| 1909 | ret 0 |
| 1910 | end |
| 1911 | j = add i n |
| 1912 | while lt j e bound BRBIG |
| 1913 | c = get fxo j |
| 1914 | if or brspc c eq c 60 |
| 1915 | break |
| 1916 | end |
| 1917 | j = add j 1 |
| 1918 | end |
| 1919 | while gt j add i n bound BRBIG |
| 1920 | c = get fxo sub j 1 |
| 1921 | if not brmtrail c |
| 1922 | break |
| 1923 | end |
| 1924 | j = sub j 1 |
| 1925 | end |
| 1926 | if le j add i n |
| 1927 | ret 0 |
| 1928 | end |
| 1929 | fput tbah |
| 1930 | brmspan i j |
| 1931 | fput tbmaext |
| 1932 | brmspan i j |
| 1933 | fput tbae |
| 1934 | ret j |
| 1935 | end |
| 1936 | fn brmtrail c uchar to bool |
| 1937 | if or eq c 46 or eq c 44 or eq c 58 eq c 59 |
| 1938 | ret true |
| 1939 | end |
| 1940 | if or eq c 33 or eq c 63 or eq c 41 eq c 39 |
| 1941 | ret true |
| 1942 | end |
| 1943 | ret or eq c 34 or eq c 42 or eq c 95 eq c 126 |
| 1944 | end |
| 1945 | note brment: a named or numeric character reference kept as written |
| 1946 | fn brment i ulong e ulong to ulong |
| 1947 | j = add i 1 |
| 1948 | hex = false |
| 1949 | num = false |
| 1950 | if lt j e |
| 1951 | if eq get fxo j 35 |
| 1952 | num = true |
| 1953 | j = add j 1 |
| 1954 | if lt j e |
| 1955 | if or eq get fxo j 120 eq get fxo j 88 |
| 1956 | hex = true |
| 1957 | j = add j 1 |
| 1958 | end |
| 1959 | end |
| 1960 | end |
| 1961 | end |
| 1962 | s = j |
| 1963 | while lt j e bound BRBIG |
| 1964 | c = get fxo j |
| 1965 | ok = bralnum c |
| 1966 | if num |
| 1967 | ok = brdig c |
| 1968 | if hex |
| 1969 | ok = lt brhexv c 16 |
| 1970 | end |
| 1971 | end |
| 1972 | if not ok |
| 1973 | break |
| 1974 | end |
| 1975 | j = add j 1 |
| 1976 | end |
| 1977 | n = sub j s |
| 1978 | if or lt n 1 gt n 31 |
| 1979 | ret 0 |
| 1980 | end |
| 1981 | if and not num lt n 2 |
| 1982 | ret 0 |
| 1983 | end |
| 1984 | if or ge j e ne get fxo j 59 |
| 1985 | ret 0 |
| 1986 | end |
| 1987 | for k ulong add sub j i 1 bound BRBIG |
| 1988 | fputc get fxo add i k |
| 1989 | end |
| 1990 | ret add j 1 |
| 1991 | end |
| 1992 | note ---- emphasis |
| 1993 | fn brmemph i ulong e ulong a ulong to ulong |
| 1994 | c = get fxo i |
| 1995 | n = brmrun i e c |
| 1996 | s = add i n |
| 1997 | if ge s e |
| 1998 | ret 0 |
| 1999 | end |
| 2000 | if brspc get fxo s |
| 2001 | ret 0 |
| 2002 | end |
| 2003 | if and eq c 95 gt i a |
| 2004 | if bralnum get fxo sub i 1 |
| 2005 | ret 0 |
| 2006 | end |
| 2007 | end |
| 2008 | if ge n 3 |
| 2009 | k = brmclose add i 3 e c 3 |
| 2010 | if gt k 0 |
| 2011 | fput tbmem |
| 2012 | fput tbmst |
| 2013 | brmi add i 3 k |
| 2014 | fput tbmstx |
| 2015 | fput tbmemx |
| 2016 | ret add k 3 |
| 2017 | end |
| 2018 | end |
| 2019 | if ge n 2 |
| 2020 | k = brmclose add i 2 e c 2 |
| 2021 | if gt k 0 |
| 2022 | fput tbmst |
| 2023 | brmi add i 2 k |
| 2024 | fput tbmstx |
| 2025 | ret add k 2 |
| 2026 | end |
| 2027 | end |
| 2028 | if ne n 1 |
| 2029 | ret 0 |
| 2030 | end |
| 2031 | k = brmclose add i 1 e c 1 |
| 2032 | if eq k 0 |
| 2033 | ret 0 |
| 2034 | end |
| 2035 | fput tbmem |
| 2036 | brmi add i 1 k |
| 2037 | fput tbmemx |
| 2038 | ret add k 1 |
| 2039 | end |
| 2040 | note brmclose: closing run of c (length w; for w = 1 a lone c, double runs are nested strong) |
| 2041 | fn brmclose s ulong e ulong c uchar w ulong to ulong |
| 2042 | j = s |
| 2043 | while lt j e bound BRBIG |
| 2044 | x = get fxo j |
| 2045 | if eq x 92 |
| 2046 | j = add j 2 |
| 2047 | continue |
| 2048 | end |
| 2049 | if eq x 96 |
| 2050 | j = brmcskip j e |
| 2051 | continue |
| 2052 | end |
| 2053 | if ne x c |
| 2054 | j = add j 1 |
| 2055 | continue |
| 2056 | end |
| 2057 | r = brmrun j e c |
| 2058 | if brmcfit j e r w c s |
| 2059 | ret j |
| 2060 | end |
| 2061 | j = add j r |
| 2062 | end |
| 2063 | ret 0 |
| 2064 | end |
| 2065 | fn brmcfit j ulong e ulong r ulong w ulong c uchar s ulong to bool |
| 2066 | if le j s |
| 2067 | ret false |
| 2068 | end |
| 2069 | if brspc get fxo sub j 1 |
| 2070 | ret false |
| 2071 | end |
| 2072 | if eq w 1 |
| 2073 | if eq r 2 |
| 2074 | ret false |
| 2075 | end |
| 2076 | else |
| 2077 | if lt r w |
| 2078 | ret false |
| 2079 | end |
| 2080 | end |
| 2081 | if eq c 95 |
| 2082 | if lt add j w e |
| 2083 | if bralnum get fxo add j w |
| 2084 | ret false |
| 2085 | end |
| 2086 | end |
| 2087 | end |
| 2088 | ret true |
| 2089 | end |
| 2090 | fn brmstrike i ulong e ulong to ulong |
| 2091 | n = brmrun i e 126 |
| 2092 | if ne n 2 |
| 2093 | ret 0 |
| 2094 | end |
| 2095 | s = add i 2 |
| 2096 | if ge s e |
| 2097 | ret 0 |
| 2098 | end |
| 2099 | if brspc get fxo s |
| 2100 | ret 0 |
| 2101 | end |
| 2102 | k = brmclose s e 126 2 |
| 2103 | if eq k 0 |
| 2104 | ret 0 |
| 2105 | end |
| 2106 | fput tbmdel |
| 2107 | brmi s k |
| 2108 | fput tbmdelx |
| 2109 | ret add k 2 |
| 2110 | end |