| 1 |
|
| /* mds_render_html.c — SAX consumer that writes HTML into an mds_buf. |
| 2 |
|
| * |
| 3 |
|
| * Handles the block kinds the scanner emits |
| 4 |
|
| * (paragraph, heading, thematic-break, fenced code, quote, list, list_item) |
| 5 |
|
| * plus pass-through inline text (HTML-escaped) and pre-escaped raw. |
| 6 |
|
| * |
| 7 |
|
| * Captures aTHX via tTHX in render_state since the inline `mds_buf_write` |
| 8 |
|
| * needs the interpreter context. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #include "mds_render_html.h" |
| 12 |
|
| #include "mds_buf.h" |
| 13 |
|
| #include "mds_ir.h" |
| 14 |
|
| #include "mds.h" |
| 15 |
|
| #include "mds_entity.h" |
| 16 |
|
| #include "mds_slug.h" |
| 17 |
|
| #include "eshu_hl.h" |
| 18 |
|
| |
| 19 |
|
| #include "EXTERN.h" |
| 20 |
|
| #include "perl.h" |
| 21 |
|
| |
| 22 |
|
| #include <string.h> |
| 23 |
|
| #include <stdlib.h> |
| 24 |
|
| #include <ctype.h> |
| 25 |
|
| |
| 26 |
|
| #define CLOSE_STACK_MAX 1024 |
| 27 |
|
| |
| 28 |
|
| typedef enum { |
| 29 |
|
| CLOSE_NONE = 0, |
| 30 |
|
| CLOSE_P, |
| 31 |
|
| CLOSE_H1, CLOSE_H2, CLOSE_H3, CLOSE_H4, CLOSE_H5, CLOSE_H6, |
| 32 |
|
| CLOSE_CODE_FENCED, |
| 33 |
|
| CLOSE_CODE_INDENTED, |
| 34 |
|
| CLOSE_BLOCKQUOTE, |
| 35 |
|
| CLOSE_OL, |
| 36 |
|
| CLOSE_UL, |
| 37 |
|
| CLOSE_OL_TIGHT, |
| 38 |
|
| CLOSE_UL_TIGHT, |
| 39 |
|
| CLOSE_LI, |
| 40 |
|
| CLOSE_TABLE, |
| 41 |
|
| CLOSE_THEAD, |
| 42 |
|
| CLOSE_TBODY, |
| 43 |
|
| CLOSE_TR, |
| 44 |
|
| CLOSE_TH, |
| 45 |
|
| CLOSE_TD, |
| 46 |
|
| CLOSE_TIGHT_PARA, |
| 47 |
|
| CLOSE_FN_SECTION, |
| 48 |
|
| CLOSE_FN_DEF, |
| 49 |
|
| CLOSE_FN_DEF_SKIP, |
| 50 |
|
| CLOSE_CODE_FENCED_HL, |
| 51 |
|
| CLOSE_NOOP |
| 52 |
|
| } close_kind; |
| 53 |
|
| |
| 54 |
|
| typedef struct { |
| 55 |
|
| #ifdef MULTIPLICITY |
| 56 |
|
| tTHX my_perl; /* captured interpreter, used via dTHXa */ |
| 57 |
|
| #endif |
| 58 |
|
| mds_buf* buf; |
| 59 |
|
| close_kind closes[CLOSE_STACK_MAX]; |
| 60 |
|
| int top; |
| 61 |
|
| /* tight_stack[i] = 1 if the i-th open container forces tight rendering |
| 62 |
|
| * (suppress <p>) for direct paragraph children, 0 otherwise. Push on |
| 63 |
|
| * every container enter that affects paragraph rendering (LIST, |
| 64 |
|
| * BLOCKQUOTE); pop on close. Paragraph checks tight_stack[tight_top-1]. |
| 65 |
|
| * BLOCKQUOTE pushes 0 to shield its inner paragraphs from an outer |
| 66 |
|
| * tight list. Loose LIST also pushes 0 to shield deeper paragraphs. */ |
| 67 |
|
| unsigned char tight_stack[CLOSE_STACK_MAX]; |
| 68 |
|
| int tight_top; |
| 69 |
|
| int tight_depth; /* legacy counter; still tracks >0 = any tight list active */ |
| 70 |
|
| int image_depth; /* >0 = inside an <img>; collect alt text */ |
| 71 |
|
| char* alt; /* malloc'd alt-text accumulator */ |
| 72 |
|
| size_t alt_len; |
| 73 |
|
| size_t alt_cap; |
| 74 |
|
| unsigned flags; /* MDS_FLAG_* bitmask */ |
| 75 |
|
| int li_check_pending; /* >0 = look for [ ]/[x] at start of first text in LIST_ITEM */ |
| 76 |
|
| int li_first_block; /* 1 = waiting for first block child after <li> */ |
| 77 |
|
| int need_nl_next; /* 1 = emit '\n' before next block (after suppressed tight para) */ |
| 78 |
|
| int in_thead; /* 1 inside <thead>, 0 inside <tbody> */ |
| 79 |
|
| const mds_align* tbl_aligns; /* current table column alignments */ |
| 80 |
|
| unsigned tbl_ncols; /* column count */ |
| 81 |
|
| unsigned tbl_col; /* next cell index in current row */ |
| 82 |
|
| /* GFM autolink: coalesce consecutive cb_text events so the scanner can |
| 83 |
|
| * see across intra-word `_`/`*` splits emitted by the inline tokenizer. */ |
| 84 |
|
| char* pending_text; |
| 85 |
|
| size_t pending_len; |
| 86 |
|
| size_t pending_cap; |
| 87 |
|
| /* Tier E.1 — footnote tracking. fn_used[i] = malloc'd label string; |
| 88 |
|
| * fn_uses[i] = instance counter (incremented per FOOTNOTE_REF). |
| 89 |
|
| * fn_count = number of distinct labels used. Indices double as 1-based |
| 90 |
|
| * footnote ordinals (first-use order). fn_skip is set while we are |
| 91 |
|
| * inside a FOOTNOTE_DEF whose label was never referenced; it |
| 92 |
|
| * suppresses all output between enter/leave of that def. fn_in_def is |
| 93 |
|
| * the 1-based ordinal of the def we are currently rendering (so the |
| 94 |
|
| * leave callback knows what backref label/idx to emit). fn_section_open |
| 95 |
|
| * tracks whether we have already written the <section><ol> headers. */ |
| 96 |
|
| const char** fn_labels; /* arena-borrowed label pointers */ |
| 97 |
|
| size_t* fn_label_lens; /* parallel label lengths */ |
| 98 |
|
| unsigned* fn_uses; /* parallel use counts */ |
| 99 |
|
| size_t fn_count; |
| 100 |
|
| size_t fn_cap; |
| 101 |
|
| int fn_skip; /* inside an unused def */ |
| 102 |
|
| unsigned fn_in_def; /* 1-based ordinal of current def, 0 if none */ |
| 103 |
|
| const char* fn_in_def_label; /* label of current def (for backref) */ |
| 104 |
|
| size_t fn_in_def_label_len; |
| 105 |
|
| /* Syntax-highlighting accumulator for MDS_FLAG_HIGHLIGHT fenced blocks. */ |
| 106 |
|
| char* hl_buf; |
| 107 |
|
| size_t hl_len; |
| 108 |
|
| size_t hl_cap; |
| 109 |
|
| char hl_lang[64]; |
| 110 |
|
| int in_fenced_hl; |
| 111 |
|
| /* Heading anchors (MDS_FLAG_HEADING_IDS). The renderer is a streaming |
| 112 |
|
| * emitter, so a heading's open tag is due before its inline content - |
| 113 |
|
| * and therefore before the text the id is slugged from - exists. We |
| 114 |
|
| * write no open tag at enter, note where it belongs, let the inlines |
| 115 |
|
| * render into the main buffer as usual, and splice the finished |
| 116 |
|
| * <hN id="..."> in at hd_mark on leave. That keeps every inline |
| 117 |
|
| * construct working against the real buffer, which diverting the |
| 118 |
|
| * heading into a side buffer would not. |
| 119 |
|
| * |
| 120 |
|
| * hd_text accumulates the *plain* text in parallel, because the slug |
| 121 |
|
| * for `## The **bold** bit` comes from "The bold bit" and not from the |
| 122 |
|
| * markup the buffer received. */ |
| 123 |
|
| int in_heading; /* 1 between heading enter and leave */ |
| 124 |
|
| size_t hd_mark; /* buffer offset the open tag belongs at */ |
| 125 |
|
| char* hd_text; /* plain-text accumulator for the slug */ |
| 126 |
|
| size_t hd_len; |
| 127 |
|
| size_t hd_cap; |
| 128 |
|
| mds_slug_table hd_slugs; /* document-scope uniqueness table */ |
| 129 |
|
| AV* toc; /* caller's heading list, or NULL */ |
| 130 |
|
| } render_state; |
| 131 |
|
| |
| 132 |
|
| /* Forward decls for helpers defined later but used by early callbacks. */ |
| 133 |
|
| static void flush_pending_text(pTHX_ render_state* st); |
| 134 |
|
| static unsigned fn_lookup(render_state* st, const char* s, size_t n); |
| 135 |
|
| static unsigned fn_register(render_state* st, const char* s, size_t n); |
| 136 |
|
| static void write_fn_label_attr(pTHX_ mds_buf* b, const char* s, size_t n); |
| 137 |
|
| |
| 138 |
|
| /* Static escape tables: zero entry means byte is safe (run/memcpy through); |
| 139 |
|
| * non-zero entries supply the replacement string and length. Built once at |
| 140 |
|
| * load with the four chars that matter in HTML text/attribute context. */ |
| 141 |
|
| typedef struct { const char* rep; unsigned char rlen; } mds_esc_entry; |
| 142 |
|
| |
| 143 |
|
| static const mds_esc_entry mds_escape_text[256] = { |
| 144 |
|
| ['<'] = { "<", 4 }, |
| 145 |
|
| ['>'] = { ">", 4 }, |
| 146 |
|
| ['&'] = { "&", 5 }, |
| 147 |
|
| ['"'] = { """, 6 }, |
| 148 |
|
| }; |
| 149 |
|
| |
| 150 |
|
| /* Image info stack: 32 nested images max — generous. */ |
| 151 |
|
| #define IMG_STACK_MAX 32 |
| 152 |
|
| typedef struct { |
| 153 |
|
| const char* href; size_t hlen; |
| 154 |
|
| const char* title; size_t tlen; |
| 155 |
|
| } img_info; |
| 156 |
|
| static img_info g_img_stack[IMG_STACK_MAX]; |
| 157 |
|
| static int g_img_stack_top = 0; |
| 158 |
|
| |
| 159 |
|
| /* Accumulate a heading's plain text for slugging. Mirrors alt_append: an |
| 160 |
|
| * OOM here costs the heading its id, which is a degraded anchor rather than |
| 161 |
|
| * a broken document, so it fails quietly. */ |
| 162 |
808063 |
| static void hd_append(render_state* st, const char* s, size_t n) { |
| 163 |
808063 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
808063 |
808063 |
|
| 164 |
808063 |
▶ | if (st->hd_len + n + 1 > st->hd_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->hd_len + n + 1 > st->hd_cap) { |
40433 |
767630 |
808063 |
|
| 165 |
40433 |
▶ | size_t nc = st->hd_cap ? st->hd_cap * 2 : 128; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = st->hd_cap ? st->hd_cap * 2 : 128; |
0 |
40433 |
40433 |
|
| 166 |
|
| char* np; |
| 167 |
40441 |
▶ | while (nc < st->hd_len + n + 1) nc *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (nc < st->hd_len + n + 1) nc *= 2; |
8 |
40433 |
40441 |
|
| 168 |
40433 |
| np = (char*)realloc(st->hd_text, nc); |
| 169 |
40433 |
▶ | if (!np) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!np) return; |
0 |
40433 |
40433 |
|
| 170 |
40433 |
| st->hd_text = np; |
| 171 |
40433 |
| st->hd_cap = nc; |
| 172 |
|
| } |
| 173 |
808063 |
| memcpy(st->hd_text + st->hd_len, s, n); |
| 174 |
808063 |
| st->hd_len += n; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
808101 |
| static void alt_append(render_state* st, const char* s, size_t n) { |
| 178 |
808101 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
808101 |
808101 |
|
| 179 |
808101 |
▶ | if (st->alt_len + n + 1 > st->alt_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->alt_len + n + 1 > st->alt_cap) { |
40474 |
767627 |
808101 |
|
| 180 |
40474 |
▶ | size_t nc = st->alt_cap ? st->alt_cap * 2 : 64; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = st->alt_cap ? st->alt_cap * 2 : 64; |
0 |
40474 |
40474 |
|
| 181 |
40474 |
▶ | while (nc < st->alt_len + n + 1) nc *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (nc < st->alt_len + n + 1) nc *= 2; |
0 |
40474 |
40474 |
|
| 182 |
40474 |
| st->alt = (char*)realloc(st->alt, nc); |
| 183 |
40474 |
| st->alt_cap = nc; |
| 184 |
|
| } |
| 185 |
808101 |
| memcpy(st->alt + st->alt_len, s, n); |
| 186 |
808101 |
| st->alt_len += n; |
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
|
| /* The XS hook keeps this single per-call instance alive in stack scope and |
| 190 |
|
| * passes &st as ud_storage. mds_render_html_install() merely wires the |
| 191 |
|
| * callbacks and stores `buf` (the rest is already initialised). */ |
| 192 |
|
| |
| 193 |
4995075 |
| static void push_close(render_state* st, close_kind k) { |
| 194 |
4995075 |
▶ | if (st->top < CLOSE_STACK_MAX) st->closes[st->top++] = k; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->top < CLOSE_STACK_MAX) st->closes[st->top++] = k; |
4995075 |
0 |
4995075 |
|
| 195 |
4995075 |
| } |
| 196 |
4995075 |
| static close_kind pop_close(render_state* st) { |
| 197 |
4995075 |
▶ | return st->top > 0 ? st->closes[--st->top] : CLOSE_NONE; |
Branch: 1 branch
| Branch | True | False | Total |
| return st->top > 0 ? st->closes[--st->top] : CLOSE_NONE; |
4995075 |
0 |
4995075 |
|
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
|
| /* SWAR-accelerated HTML escape. Every rendered text byte flows |
| 201 |
|
| * through here, so the inner loop matters a lot. We process 8 bytes at a |
| 202 |
|
| * time with the classic "has-zero" trick to detect whether the word |
| 203 |
|
| * contains ANY of '<', '>', '&', '"'. Clean words advance the run |
| 204 |
|
| * pointer without touching `mds_buf_write` until we either hit a hot |
| 205 |
|
| * byte or reach the tail. On corpora dominated by plain prose this skips |
| 206 |
|
| * ~7/8 of the per-byte work. */ |
| 207 |
|
| #define MDS_HASZ(x) (((x) - 0x0101010101010101ULL) & ~(x) & 0x8080808080808080ULL) |
| 208 |
7329558 |
| static inline uint64_t mds_escape_hot64(uint64_t w) { |
| 209 |
|
| /* '<'=0x3C '>'=0x3E '&'=0x26 '"'=0x22 */ |
| 210 |
7329558 |
| uint64_t a = w ^ 0x3C3C3C3C3C3C3C3CULL; |
| 211 |
7329558 |
| uint64_t b = w ^ 0x3E3E3E3E3E3E3E3EULL; |
| 212 |
7329558 |
| uint64_t c = w ^ 0x2626262626262626ULL; |
| 213 |
7329558 |
| uint64_t d = w ^ 0x2222222222222222ULL; |
| 214 |
7329558 |
| return MDS_HASZ(a) | MDS_HASZ(b) | MDS_HASZ(c) | MDS_HASZ(d); |
| 215 |
|
| } |
| 216 |
|
| |
| 217 |
7318336 |
| MDS_HOT static void html_escape(pTHX_ mds_buf* b, const char* s, size_t n) { |
| 218 |
7318336 |
| const char* run = s; |
| 219 |
7318336 |
| const char* end = s + n; |
| 220 |
7318336 |
| const char* p = s; |
| 221 |
|
| /* Fast 8-byte SWAR skip over clean prose. Unaligned loads are fine |
| 222 |
|
| * on arm64/x86_64; misaligned reads near the page boundary are safe |
| 223 |
|
| * because we never dereference past `end`. */ |
| 224 |
14647894 |
▶ | while ((size_t)(end - p) >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((size_t)(end - p) >= 8) { |
7329558 |
7318336 |
14647894 |
|
| 225 |
|
| uint64_t w; |
| 226 |
|
| int k; |
| 227 |
7329558 |
| memcpy(&w, p, 8); |
| 228 |
7329558 |
▶ | if (MDS_LIKELY(!mds_escape_hot64(w))) { p += 8; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (MDS_LIKELY(!mds_escape_hot64(w))) { p += 8; continue; } |
7329414 |
144 |
7329558 |
|
| 229 |
|
| /* Word has at least one hot byte: emit any of the 8 that match |
| 230 |
|
| * via the standard table, then continue. */ |
| 231 |
1296 |
▶ | for (k = 0; k < 8; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < 8; k++) { |
1152 |
144 |
1296 |
|
| 232 |
1152 |
| const mds_esc_entry e = mds_escape_text[(unsigned char)p[k]]; |
| 233 |
1152 |
▶ | if (!e.rlen) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e.rlen) continue; |
959 |
193 |
1152 |
|
| 234 |
193 |
▶ | if (p + k > run) mds_buf_write(aTHX_ b, run, (size_t)((p + k) - run)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (p + k > run) mds_buf_write(aTHX_ b, run, (size_t)((p + k) - run)); |
120 |
73 |
193 |
| if (p + k > run) mds_buf_write(aTHX_ b, run, (size_t)((p + k) - run)); |
0 |
120 |
120 |
|
| 235 |
193 |
▶ | mds_buf_write(aTHX_ b, e.rep, e.rlen); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ b, e.rep, e.rlen); |
0 |
193 |
193 |
|
| 236 |
193 |
| run = p + k + 1; |
| 237 |
|
| } |
| 238 |
144 |
| p += 8; |
| 239 |
|
| } |
| 240 |
|
| /* Scalar tail (<8 bytes). */ |
| 241 |
36867396 |
▶ | for (; p < end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p < end; p++) { |
29549060 |
7318336 |
36867396 |
|
| 242 |
29549060 |
| const mds_esc_entry e = mds_escape_text[(unsigned char)*p]; |
| 243 |
29549060 |
▶ | if (!e.rlen) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e.rlen) continue; |
29548818 |
242 |
29549060 |
|
| 244 |
242 |
▶ | if (p > run) mds_buf_write(aTHX_ b, run, (size_t)(p - run)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (p > run) mds_buf_write(aTHX_ b, run, (size_t)(p - run)); |
119 |
123 |
242 |
| if (p > run) mds_buf_write(aTHX_ b, run, (size_t)(p - run)); |
0 |
119 |
119 |
|
| 245 |
242 |
▶ | mds_buf_write(aTHX_ b, e.rep, e.rlen); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ b, e.rep, e.rlen); |
0 |
242 |
242 |
|
| 246 |
242 |
| run = p + 1; |
| 247 |
|
| } |
| 248 |
7318336 |
▶ | if (run < end) mds_buf_write(aTHX_ b, run, (size_t)(end - run)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (run < end) mds_buf_write(aTHX_ b, run, (size_t)(end - run)); |
7318192 |
144 |
7318336 |
| if (run < end) mds_buf_write(aTHX_ b, run, (size_t)(end - run)); |
60609 |
7257583 |
7318192 |
|
| 249 |
7318336 |
| } |
| 250 |
|
| #undef MDS_HASZ |
| 251 |
|
| |
| 252 |
|
| /* HTML-escape with backslash unescaping for ASCII punctuation. Used for |
| 253 |
|
| * link/image title attributes which honour §6.1 backslash escapes. */ |
| 254 |
158 |
| static void html_escape_unesc(pTHX_ mds_buf* b, const char* s, size_t n) { |
| 255 |
|
| /* expand_entity_at writes through `out` which is just `b` for titles. */ |
| 256 |
|
| extern size_t mds_expand_entity_at(const char* s, size_t i, size_t n, |
| 257 |
|
| char* out, size_t* outlen); |
| 258 |
|
| size_t i; |
| 259 |
|
| mds_esc_entry e; |
| 260 |
|
| unsigned char nx; |
| 261 |
|
| char ebuf[8]; |
| 262 |
|
| size_t elen; |
| 263 |
|
| size_t k; |
| 264 |
|
| size_t z; |
| 265 |
|
| |
| 266 |
158 |
| i = 0; |
| 267 |
1146 |
▶ | while (i < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < n) { |
988 |
158 |
1146 |
|
| 268 |
988 |
▶ | if (s[i] == '\\' && i + 1 < n) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] == '\\' && i + 1 < n) { |
15 |
973 |
988 |
| if (s[i] == '\\' && i + 1 < n) { |
15 |
0 |
15 |
|
| 269 |
15 |
| nx = (unsigned char)s[i+1]; |
| 270 |
15 |
▶ | if ((nx >= 0x21 && nx <= 0x2F) || |
Branch: 2 branches
| Branch | True | False | Total |
| if ((nx >= 0x21 && nx <= 0x2F) || |
15 |
0 |
15 |
| if ((nx >= 0x21 && nx <= 0x2F) || |
3 |
12 |
15 |
|
| 271 |
3 |
▶ | (nx >= 0x3A && nx <= 0x40) || |
Branch: 2 branches
| Branch | True | False | Total |
| (nx >= 0x3A && nx <= 0x40) || |
3 |
0 |
3 |
| (nx >= 0x3A && nx <= 0x40) || |
3 |
0 |
3 |
|
| 272 |
3 |
▶ | (nx >= 0x5B && nx <= 0x60) || |
Branch: 2 branches
| Branch | True | False | Total |
| (nx >= 0x5B && nx <= 0x60) || |
3 |
0 |
3 |
| (nx >= 0x5B && nx <= 0x60) || |
3 |
0 |
3 |
|
| 273 |
3 |
▶ | (nx >= 0x7B && nx <= 0x7E)) { |
Branch: 2 branches
| Branch | True | False | Total |
| (nx >= 0x7B && nx <= 0x7E)) { |
0 |
3 |
3 |
| (nx >= 0x7B && nx <= 0x7E)) { |
0 |
0 |
0 |
|
| 274 |
12 |
| e = mds_escape_text[nx]; |
| 275 |
12 |
▶ | if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
6 |
6 |
12 |
| if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
0 |
6 |
6 |
|
| 276 |
|
| else mds_buf_write(aTHX_ b, (const char*)&nx, 1); |
| 277 |
12 |
| i += 2; |
| 278 |
12 |
| continue; |
| 279 |
|
| } |
| 280 |
|
| } |
| 281 |
976 |
▶ | if (s[i] == '&') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[i] == '&') { |
30 |
946 |
976 |
|
| 282 |
30 |
| k = mds_expand_entity_at(s, i, n, ebuf, &elen); |
| 283 |
30 |
▶ | if (k) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k) { |
21 |
9 |
30 |
|
| 284 |
|
| /* Re-emit decoded bytes through html_escape_text so chars |
| 285 |
|
| * like '<' '&' '"' get re-escaped. */ |
| 286 |
60 |
▶ | for (z = 0; z < elen; z++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (z = 0; z < elen; z++) { |
39 |
21 |
60 |
|
| 287 |
39 |
| e = mds_escape_text[(unsigned char)ebuf[z]]; |
| 288 |
39 |
▶ | if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
3 |
36 |
39 |
| if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
0 |
3 |
3 |
|
| 289 |
36 |
▶ | else mds_buf_write(aTHX_ b, ebuf + z, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| else mds_buf_write(aTHX_ b, ebuf + z, 1); |
0 |
36 |
36 |
|
| 290 |
|
| } |
| 291 |
21 |
| i += k; |
| 292 |
21 |
| continue; |
| 293 |
|
| } |
| 294 |
|
| } |
| 295 |
955 |
| e = mds_escape_text[(unsigned char)s[i]]; |
| 296 |
955 |
▶ | if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
15 |
940 |
955 |
| if (e.rlen) mds_buf_write(aTHX_ b, e.rep, e.rlen); |
0 |
15 |
15 |
|
| 297 |
940 |
▶ | else mds_buf_write(aTHX_ b, s + i, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| else mds_buf_write(aTHX_ b, s + i, 1); |
0 |
940 |
940 |
|
| 298 |
955 |
| i++; |
| 299 |
|
| } |
| 300 |
158 |
| } |
| 301 |
|
| |
| 302 |
|
| /* Decode an entity at s[i..] — either &name; or &#NNN; / &#xHH; — into |
| 303 |
|
| * `out` (up to 8 bytes is enough for any single entity; multi-codepoint |
| 304 |
|
| * entities like ≧̸ need 5). Returns the number of source bytes |
| 305 |
|
| * consumed (0 = not an entity), and stores the UTF-8 length in *outlen. */ |
| 306 |
51 |
| size_t mds_expand_entity_at(const char* s, size_t i, size_t n, |
| 307 |
|
| char* out, size_t* outlen) { |
| 308 |
|
| size_t q; |
| 309 |
|
| unsigned long cp; |
| 310 |
|
| size_t digits; |
| 311 |
|
| char c; |
| 312 |
|
| size_t blen; |
| 313 |
|
| size_t name_start; |
| 314 |
|
| const mds_entity* e; |
| 315 |
|
| mds_entity ent_scratch; |
| 316 |
|
| |
| 317 |
51 |
▶ | if (i >= n || s[i] != '&') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (i >= n || s[i] != '&') return 0; |
51 |
0 |
51 |
| if (i >= n || s[i] != '&') return 0; |
0 |
51 |
51 |
|
| 318 |
51 |
| q = i + 1; |
| 319 |
51 |
▶ | if (q < n && s[q] == '#') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] == '#') { |
51 |
0 |
51 |
| if (q < n && s[q] == '#') { |
0 |
51 |
51 |
|
| 320 |
0 |
| q++; |
| 321 |
0 |
| cp = 0; |
| 322 |
0 |
| digits = 0; |
| 323 |
0 |
▶ | if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
0 |
0 |
0 |
| if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
0 |
0 |
0 |
| if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
0 |
0 |
0 |
|
| 324 |
0 |
| q++; |
| 325 |
0 |
▶ | while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
0 |
0 |
0 |
| while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
0 |
0 |
0 |
| while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
0 |
0 |
0 |
|
| 326 |
0 |
| c = s[q]; |
| 327 |
0 |
▶ | cp = cp * 16 + (c <= '9' ? c - '0' : |
Branch: 2 branches
| Branch | True | False | Total |
| cp = cp * 16 + (c <= '9' ? c - '0' : |
0 |
0 |
0 |
| cp = cp * 16 + (c <= '9' ? c - '0' : |
0 |
0 |
0 |
|
| 328 |
0 |
| (c <= 'F' ? c - 'A' + 10 : c - 'a' + 10)); |
| 329 |
0 |
| q++; digits++; |
| 330 |
|
| } |
| 331 |
|
| } else { |
| 332 |
0 |
▶ | while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
0 |
0 |
0 |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
0 |
0 |
0 |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
0 |
0 |
0 |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
0 |
0 |
0 |
|
| 333 |
0 |
| cp = cp * 10 + (unsigned long)(s[q] - '0'); |
| 334 |
0 |
| q++; digits++; |
| 335 |
|
| } |
| 336 |
|
| } |
| 337 |
0 |
▶ | if (!digits || q >= n || s[q] != ';') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!digits || q >= n || s[q] != ';') return 0; |
0 |
0 |
0 |
| if (!digits || q >= n || s[q] != ';') return 0; |
0 |
0 |
0 |
| if (!digits || q >= n || s[q] != ';') return 0; |
0 |
0 |
0 |
|
| 338 |
0 |
| q++; |
| 339 |
0 |
▶ | if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
0 |
0 |
0 |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
0 |
0 |
0 |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
0 |
0 |
0 |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
0 |
0 |
0 |
|
| 340 |
0 |
| cp = 0xFFFD; |
| 341 |
0 |
▶ | if (cp < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp < 0x80) { |
0 |
0 |
0 |
|
| 342 |
0 |
| out[0] = (char)cp; blen = 1; |
| 343 |
0 |
▶ | } else if (cp < 0x800) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp < 0x800) { |
0 |
0 |
0 |
|
| 344 |
0 |
| out[0] = (char)(0xC0 | (cp >> 6)); |
| 345 |
0 |
| out[1] = (char)(0x80 | (cp & 0x3F)); blen = 2; |
| 346 |
0 |
▶ | } else if (cp < 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp < 0x10000) { |
0 |
0 |
0 |
|
| 347 |
0 |
| out[0] = (char)(0xE0 | (cp >> 12)); |
| 348 |
0 |
| out[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 349 |
0 |
| out[2] = (char)(0x80 | (cp & 0x3F)); blen = 3; |
| 350 |
|
| } else { |
| 351 |
0 |
| out[0] = (char)(0xF0 | (cp >> 18)); |
| 352 |
0 |
| out[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); |
| 353 |
0 |
| out[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 354 |
0 |
| out[3] = (char)(0x80 | (cp & 0x3F)); blen = 4; |
| 355 |
|
| } |
| 356 |
0 |
| *outlen = blen; |
| 357 |
0 |
| return q - i; |
| 358 |
|
| } |
| 359 |
51 |
| name_start = q; |
| 360 |
222 |
▶ | while (q < n && isalnum((unsigned char)s[q])) q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && isalnum((unsigned char)s[q])) q++; |
219 |
3 |
222 |
| while (q < n && isalnum((unsigned char)s[q])) q++; |
171 |
48 |
219 |
|
| 361 |
51 |
▶ | if (q == name_start || q >= n || s[q] != ';') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (q == name_start || q >= n || s[q] != ';') return 0; |
42 |
9 |
51 |
| if (q == name_start || q >= n || s[q] != ';') return 0; |
39 |
3 |
42 |
| if (q == name_start || q >= n || s[q] != ';') return 0; |
3 |
36 |
39 |
|
| 362 |
36 |
| e = mds_entity_lookup(s + name_start, q - name_start, &ent_scratch); |
| 363 |
36 |
▶ | if (!e) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) return 0; |
0 |
36 |
36 |
|
| 364 |
36 |
▶ | if (e->ulen > 8) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->ulen > 8) return 0; |
0 |
36 |
36 |
|
| 365 |
36 |
| memcpy(out, e->utf8, e->ulen); |
| 366 |
36 |
| *outlen = e->ulen; |
| 367 |
36 |
| return (q + 1) - i; |
| 368 |
|
| } |
| 369 |
|
| |
| 370 |
|
| /* Returns 1 if URL starts with a scheme we refuse to emit (xss vectors) |
| 371 |
|
| * unless MDS_FLAG_UNSAFE is set. `for_image` permits data: image MIME. */ |
| 372 |
1616472 |
| static int url_is_dangerous(const char* s, size_t n, int for_image) { |
| 373 |
|
| size_t i; |
| 374 |
|
| size_t j; |
| 375 |
|
| char c; |
| 376 |
|
| size_t sl; |
| 377 |
|
| const char* p; |
| 378 |
|
| size_t rem; |
| 379 |
|
| |
| 380 |
|
| /* Skip leading whitespace (already trimmed upstream but be defensive). */ |
| 381 |
1616472 |
| i = 0; |
| 382 |
1616472 |
▶ | while (i < n && (unsigned char)s[i] <= 0x20) i++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < n && (unsigned char)s[i] <= 0x20) i++; |
1616457 |
15 |
1616472 |
| while (i < n && (unsigned char)s[i] <= 0x20) i++; |
0 |
1616457 |
1616457 |
|
| 383 |
|
| /* Find optional scheme ending at ':'. */ |
| 384 |
1616472 |
| j = i; |
| 385 |
8081260 |
▶ | while (j < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (j < n) { |
8081200 |
60 |
8081260 |
|
| 386 |
8081200 |
| c = s[j]; |
| 387 |
8081200 |
▶ | if (c == ':') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == ':') break; |
1616081 |
6465119 |
8081200 |
|
| 388 |
6465119 |
▶ | if (!isalnum((unsigned char)c) && c != '+' && c != '-' && c != '.') return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!isalnum((unsigned char)c) && c != '+' && c != '-' && c != '.') return 0; |
371 |
6464748 |
6465119 |
| if (!isalnum((unsigned char)c) && c != '+' && c != '-' && c != '.') return 0; |
362 |
9 |
371 |
| if (!isalnum((unsigned char)c) && c != '+' && c != '-' && c != '.') return 0; |
356 |
6 |
362 |
| if (!isalnum((unsigned char)c) && c != '+' && c != '-' && c != '.') return 0; |
331 |
25 |
356 |
|
| 389 |
6464788 |
| j++; |
| 390 |
|
| } |
| 391 |
1616141 |
▶ | if (j >= n || j == i) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (j >= n || j == i) return 0; |
1616081 |
60 |
1616141 |
| if (j >= n || j == i) return 0; |
0 |
1616081 |
1616081 |
|
| 392 |
1616081 |
| sl = j - i; |
| 393 |
|
| /* Case-insensitive scheme compare. */ |
| 394 |
|
| #define SCHEME_EQ(lit) (sl == (sizeof(lit) - 1) && strncasecmp(s + i, (lit), sl) == 0) |
| 395 |
1616081 |
▶ | if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
Branch: 6 branches
| Branch | True | False | Total |
| if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
1 |
1616080 |
1616081 |
| if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
0 |
1 |
1 |
| if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
1 |
1616079 |
1616080 |
| if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
0 |
1 |
1 |
| if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
1616028 |
51 |
1616079 |
| if (SCHEME_EQ("javascript") || SCHEME_EQ("vbscript") || SCHEME_EQ("file")) |
0 |
1616028 |
1616028 |
|
| 396 |
2 |
| return 1; |
| 397 |
1616079 |
▶ | if (SCHEME_EQ("data")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SCHEME_EQ("data")) { |
1616028 |
51 |
1616079 |
| if (SCHEME_EQ("data")) { |
1 |
1616027 |
1616028 |
|
| 398 |
1 |
▶ | if (!for_image) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!for_image) return 1; |
1 |
0 |
1 |
|
| 399 |
|
| /* Allow only data:image/{gif,png,jpeg,webp}[;...]. */ |
| 400 |
0 |
| p = s + j + 1; |
| 401 |
0 |
| rem = n - (j + 1); |
| 402 |
0 |
▶ | if (rem < 6 || strncasecmp(p, "image/", 6) != 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (rem < 6 || strncasecmp(p, "image/", 6) != 0) return 1; |
0 |
0 |
0 |
| if (rem < 6 || strncasecmp(p, "image/", 6) != 0) return 1; |
0 |
0 |
0 |
|
| 403 |
0 |
| p += 6; rem -= 6; |
| 404 |
0 |
▶ | if ((rem >= 3 && strncasecmp(p, "gif", 3) == 0) || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((rem >= 3 && strncasecmp(p, "gif", 3) == 0) || |
0 |
0 |
0 |
| if ((rem >= 3 && strncasecmp(p, "gif", 3) == 0) || |
0 |
0 |
0 |
| if ((rem >= 3 && strncasecmp(p, "gif", 3) == 0) || |
0 |
0 |
0 |
|
| 405 |
0 |
▶ | (rem >= 3 && strncasecmp(p, "png", 3) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (rem >= 3 && strncasecmp(p, "png", 3) == 0) || |
0 |
0 |
0 |
| (rem >= 3 && strncasecmp(p, "png", 3) == 0) || |
0 |
0 |
0 |
|
| 406 |
0 |
▶ | (rem >= 4 && strncasecmp(p, "jpeg", 4) == 0) || |
Branch: 2 branches
| Branch | True | False | Total |
| (rem >= 4 && strncasecmp(p, "jpeg", 4) == 0) || |
0 |
0 |
0 |
| (rem >= 4 && strncasecmp(p, "jpeg", 4) == 0) || |
0 |
0 |
0 |
|
| 407 |
0 |
▶ | (rem >= 4 && strncasecmp(p, "webp", 4) == 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| (rem >= 4 && strncasecmp(p, "webp", 4) == 0)) |
0 |
0 |
0 |
|
| 408 |
0 |
| return 0; |
| 409 |
0 |
| return 1; |
| 410 |
|
| } |
| 411 |
|
| #undef SCHEME_EQ |
| 412 |
1616078 |
| return 0; |
| 413 |
|
| } |
| 414 |
|
| |
| 415 |
|
| /* ------------------ callbacks ------------------ */ |
| 416 |
|
| |
| 417 |
4995075 |
| static void cb_enter_block(void* ud, mds_block_type t, const mds_block_detail* d) { |
| 418 |
4995075 |
| render_state* st = (render_state*)ud; |
| 419 |
|
| dTHXa(st->my_perl); |
| 420 |
4995075 |
| mds_buf* b = st->buf; |
| 421 |
|
| /* While we are inside an unreferenced footnote def, swallow all |
| 422 |
|
| * nested block events. Push CLOSE_NOOP so the matching leave pops |
| 423 |
|
| * cleanly without disturbing the close stack. The FOOTNOTE_DEF |
| 424 |
|
| * event itself is fired BEFORE fn_skip is set (in this function), |
| 425 |
|
| * so the dispatch below still runs for that one event. */ |
| 426 |
4995075 |
▶ | if (st->fn_skip && t != MDS_BLK_FOOTNOTE_DEF && t != MDS_BLK_FOOTNOTES_SECTION) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->fn_skip && t != MDS_BLK_FOOTNOTE_DEF && t != MDS_BLK_FOOTNOTES_SECTION) { |
0 |
4995075 |
4995075 |
| if (st->fn_skip && t != MDS_BLK_FOOTNOTE_DEF && t != MDS_BLK_FOOTNOTES_SECTION) { |
0 |
0 |
0 |
| if (st->fn_skip && t != MDS_BLK_FOOTNOTE_DEF && t != MDS_BLK_FOOTNOTES_SECTION) { |
0 |
0 |
0 |
|
| 427 |
0 |
| push_close(st, CLOSE_NOOP); |
| 428 |
0 |
| return; |
| 429 |
|
| } |
| 430 |
4995075 |
| flush_pending_text(aTHX_ st); |
| 431 |
|
| /* Decide if this block starts on the same line as <li> (tight para) |
| 432 |
|
| * or on a new line. li_first_block fires only for the very first child |
| 433 |
|
| * of a list item; need_nl_next fires after a suppressed tight paragraph |
| 434 |
|
| * when any further block follows in the same item. */ |
| 435 |
4995075 |
▶ | if (st->li_first_block) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->li_first_block) { |
577 |
4994498 |
4995075 |
|
| 436 |
|
| int tight_para; |
| 437 |
577 |
| st->li_first_block = 0; |
| 438 |
1106 |
| tight_para = (t == MDS_BLK_PARAGRAPH && |
| 439 |
1106 |
▶ | st->tight_top > 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| st->tight_top > 0 && |
529 |
48 |
577 |
| st->tight_top > 0 && |
529 |
0 |
529 |
|
| 440 |
529 |
▶ | st->tight_stack[st->tight_top - 1]); |
Branch: 1 branch
| Branch | True | False | Total |
| st->tight_stack[st->tight_top - 1]); |
394 |
135 |
529 |
|
| 441 |
577 |
▶ | if (!tight_para) MDS_BUF_LIT(b, "\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tight_para) MDS_BUF_LIT(b, "\n"); |
183 |
394 |
577 |
|
| 442 |
4994498 |
▶ | } else if (st->need_nl_next) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (st->need_nl_next) { |
53 |
4994445 |
4994498 |
|
| 443 |
53 |
▶ | st->need_nl_next = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| st->need_nl_next = 0; |
0 |
53 |
53 |
|
| 444 |
|
| MDS_BUF_LIT(b, "\n"); |
| 445 |
|
| } |
| 446 |
|
| |
| 447 |
4995075 |
| switch (t) { |
| 448 |
0 |
| case MDS_BLK_DOC: |
| 449 |
0 |
| push_close(st, CLOSE_NOOP); |
| 450 |
0 |
| break; |
| 451 |
3295495 |
| case MDS_BLK_PARAGRAPH: { |
| 452 |
3296198 |
▶ | int suppress = (st->tight_top > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| int suppress = (st->tight_top > 0 && |
703 |
3294792 |
3295495 |
|
| 453 |
703 |
▶ | st->tight_stack[st->tight_top - 1]); |
Branch: 1 branch
| Branch | True | False | Total |
| st->tight_stack[st->tight_top - 1]); |
397 |
306 |
703 |
|
| 454 |
3295495 |
▶ | if (suppress) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (suppress) { |
397 |
3295098 |
3295495 |
|
| 455 |
397 |
| push_close(st, CLOSE_TIGHT_PARA); |
| 456 |
|
| } else { |
| 457 |
|
| MDS_BUF_LIT(b, "<p>"); |
| 458 |
3295098 |
| push_close(st, CLOSE_P); |
| 459 |
|
| } |
| 460 |
3295495 |
| break; |
| 461 |
|
| } |
| 462 |
808274 |
| case MDS_BLK_HEADING: { |
| 463 |
|
| int lvl; |
| 464 |
|
| char open[5]; |
| 465 |
808274 |
| lvl = d->u.heading.level; |
| 466 |
808274 |
▶ | if (lvl < 1) lvl = 1; else if (lvl > 6) lvl = 6; |
Branch: 2 branches
| Branch | True | False | Total |
| if (lvl < 1) lvl = 1; else if (lvl > 6) lvl = 6; |
0 |
808274 |
808274 |
| if (lvl < 1) lvl = 1; else if (lvl > 6) lvl = 6; |
0 |
808274 |
808274 |
|
| 467 |
808274 |
▶ | if ((st->flags & MDS_FLAG_HEADING_IDS) || st->toc) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((st->flags & MDS_FLAG_HEADING_IDS) || st->toc) { |
240 |
808034 |
808274 |
| if ((st->flags & MDS_FLAG_HEADING_IDS) || st->toc) { |
17 |
223 |
240 |
|
| 468 |
|
| /* Defer the open tag: cb_leave_block splices it in once the |
| 469 |
|
| * heading text exists to slug from. */ |
| 470 |
808051 |
| st->in_heading = 1; |
| 471 |
808051 |
| st->hd_mark = (size_t)(b->cur - b->base); |
| 472 |
808051 |
| st->hd_len = 0; |
| 473 |
|
| } else { |
| 474 |
223 |
| open[0] = '<'; |
| 475 |
223 |
| open[1] = 'h'; |
| 476 |
223 |
| open[2] = (char)('0' + lvl); |
| 477 |
223 |
| open[3] = '>'; |
| 478 |
223 |
▶ | open[4] = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| open[4] = 0; |
0 |
223 |
223 |
|
| 479 |
|
| mds_buf_write(aTHX_ b, open, 4); |
| 480 |
|
| } |
| 481 |
808274 |
| push_close(st, (close_kind)(CLOSE_H1 + (lvl - 1))); |
| 482 |
808274 |
| break; |
| 483 |
|
| } |
| 484 |
104 |
▶ | case MDS_BLK_THEMATIC_BREAK: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_THEMATIC_BREAK: |
0 |
104 |
104 |
|
| 485 |
|
| MDS_BUF_LIT(b, "<hr />\n"); |
| 486 |
104 |
| push_close(st, CLOSE_NOOP); |
| 487 |
104 |
| break; |
| 488 |
200 |
| case MDS_BLK_HTML: |
| 489 |
|
| /* Raw HTML block: children are EV_RAW text; nothing to wrap. */ |
| 490 |
200 |
| push_close(st, CLOSE_NOOP); |
| 491 |
200 |
| break; |
| 492 |
808139 |
| case MDS_BLK_CODE_FENCED: { |
| 493 |
808139 |
| const char* info = d->u.code_fenced.info; |
| 494 |
808139 |
| size_t il = d->u.code_fenced.info_len; |
| 495 |
|
| /* Trim info to first word for the language class. */ |
| 496 |
808139 |
| size_t lang_len = 0; |
| 497 |
4040317 |
▶ | while (lang_len < il && info[lang_len] != ' ' && info[lang_len] != '\t') |
Branch: 3 branches
| Branch | True | False | Total |
| while (lang_len < il && info[lang_len] != ' ' && info[lang_len] != '\t') |
3232184 |
808133 |
4040317 |
| while (lang_len < il && info[lang_len] != ' ' && info[lang_len] != '\t') |
3232178 |
6 |
3232184 |
| while (lang_len < il && info[lang_len] != ' ' && info[lang_len] != '\t') |
3232178 |
0 |
3232178 |
|
| 498 |
3232178 |
| lang_len++; |
| 499 |
1616162 |
▶ | if ((st->flags & MDS_FLAG_HIGHLIGHT) && lang_len > 0 && lang_len < 63) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((st->flags & MDS_FLAG_HIGHLIGHT) && lang_len > 0 && lang_len < 63) { |
808024 |
115 |
808139 |
| if ((st->flags & MDS_FLAG_HIGHLIGHT) && lang_len > 0 && lang_len < 63) { |
808023 |
1 |
808024 |
| if ((st->flags & MDS_FLAG_HIGHLIGHT) && lang_len > 0 && lang_len < 63) { |
808023 |
0 |
808023 |
|
| 500 |
|
| /* Accumulate raw code text; highlight at leave time. */ |
| 501 |
|
| size_t i; |
| 502 |
4040094 |
▶ | for (i = 0; i < lang_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < lang_len; i++) |
3232071 |
808023 |
4040094 |
|
| 503 |
3232071 |
| st->hl_lang[i] = (char)tolower((unsigned char)info[i]); |
| 504 |
808023 |
| st->hl_lang[lang_len] = '\0'; |
| 505 |
808023 |
| st->hl_len = 0; |
| 506 |
808023 |
| st->in_fenced_hl = 1; |
| 507 |
808023 |
| push_close(st, CLOSE_CODE_FENCED_HL); |
| 508 |
|
| } else { |
| 509 |
116 |
▶ | if (lang_len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lang_len > 0) { |
20 |
96 |
116 |
|
| 510 |
|
| MDS_BUF_LIT(b, "<pre><code class=\"language-"); |
| 511 |
20 |
| html_escape_unesc(aTHX_ b, info, lang_len); |
| 512 |
|
| MDS_BUF_LIT(b, "\">"); |
| 513 |
|
| } else { |
| 514 |
|
| MDS_BUF_LIT(b, "<pre><code>"); |
| 515 |
|
| } |
| 516 |
116 |
| push_close(st, CLOSE_CODE_FENCED); |
| 517 |
|
| } |
| 518 |
808139 |
| break; |
| 519 |
|
| } |
| 520 |
170 |
▶ | case MDS_BLK_CODE_INDENTED: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_CODE_INDENTED: |
0 |
170 |
170 |
|
| 521 |
|
| MDS_BUF_LIT(b, "<pre><code>"); |
| 522 |
170 |
| push_close(st, CLOSE_CODE_INDENTED); |
| 523 |
170 |
| break; |
| 524 |
177 |
▶ | case MDS_BLK_QUOTE: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_QUOTE: |
3 |
174 |
177 |
|
| 525 |
|
| MDS_BUF_LIT(b, "<blockquote>\n"); |
| 526 |
177 |
▶ | if (st->tight_top < CLOSE_STACK_MAX) |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top < CLOSE_STACK_MAX) |
177 |
0 |
177 |
|
| 527 |
177 |
| st->tight_stack[st->tight_top++] = 0; |
| 528 |
177 |
| push_close(st, CLOSE_BLOCKQUOTE); |
| 529 |
177 |
| break; |
| 530 |
379 |
| case MDS_BLK_LIST: { |
| 531 |
379 |
| int tight = d->u.list.is_tight; |
| 532 |
379 |
▶ | if (tight) st->tight_depth++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tight) st->tight_depth++; |
278 |
101 |
379 |
|
| 533 |
379 |
▶ | if (st->tight_top < CLOSE_STACK_MAX) |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top < CLOSE_STACK_MAX) |
379 |
0 |
379 |
|
| 534 |
379 |
▶ | st->tight_stack[st->tight_top++] = (unsigned char)(tight ? 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| st->tight_stack[st->tight_top++] = (unsigned char)(tight ? 1 : 0); |
278 |
101 |
379 |
|
| 535 |
379 |
▶ | if (d->u.list.is_ordered) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->u.list.is_ordered) { |
97 |
282 |
379 |
|
| 536 |
97 |
▶ | if (d->u.list.start != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->u.list.start != 1) { |
28 |
69 |
97 |
|
| 537 |
|
| char buf[64]; |
| 538 |
28 |
| int n = snprintf(buf, sizeof buf, "<ol start=\"%d\">\n", |
| 539 |
28 |
| d->u.list.start); |
| 540 |
28 |
▶ | if (n > 0) mds_buf_write(aTHX_ b, buf, (size_t)n); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0) mds_buf_write(aTHX_ b, buf, (size_t)n); |
28 |
0 |
28 |
| if (n > 0) mds_buf_write(aTHX_ b, buf, (size_t)n); |
0 |
28 |
28 |
|
| 541 |
|
| } else { |
| 542 |
|
| MDS_BUF_LIT(b, "<ol>\n"); |
| 543 |
|
| } |
| 544 |
97 |
▶ | push_close(st, tight ? CLOSE_OL_TIGHT : CLOSE_OL); |
Branch: 1 branch
| Branch | True | False | Total |
| push_close(st, tight ? CLOSE_OL_TIGHT : CLOSE_OL); |
55 |
42 |
97 |
|
| 545 |
|
| } else { |
| 546 |
|
| MDS_BUF_LIT(b, "<ul>\n"); |
| 547 |
282 |
▶ | push_close(st, tight ? CLOSE_UL_TIGHT : CLOSE_UL); |
Branch: 1 branch
| Branch | True | False | Total |
| push_close(st, tight ? CLOSE_UL_TIGHT : CLOSE_UL); |
223 |
59 |
282 |
|
| 548 |
|
| } |
| 549 |
379 |
| break; |
| 550 |
|
| } |
| 551 |
595 |
▶ | case MDS_BLK_LIST_ITEM: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_LIST_ITEM: |
0 |
595 |
595 |
|
| 552 |
|
| MDS_BUF_LIT(b, "<li>"); |
| 553 |
595 |
| st->li_check_pending = 1; |
| 554 |
595 |
| st->li_first_block = 1; |
| 555 |
595 |
| st->need_nl_next = 0; |
| 556 |
595 |
| push_close(st, CLOSE_LI); |
| 557 |
595 |
| break; |
| 558 |
69 |
▶ | case MDS_BLK_TABLE: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_TABLE: |
0 |
69 |
69 |
|
| 559 |
|
| MDS_BUF_LIT(b, "<table>\n"); |
| 560 |
69 |
| st->tbl_aligns = d->u.table.aligns; |
| 561 |
69 |
| st->tbl_ncols = d->u.table.ncols; |
| 562 |
69 |
| push_close(st, CLOSE_TABLE); |
| 563 |
69 |
| break; |
| 564 |
69 |
▶ | case MDS_BLK_TABLE_HEAD: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_TABLE_HEAD: |
0 |
69 |
69 |
|
| 565 |
|
| MDS_BUF_LIT(b, "<thead>\n"); |
| 566 |
69 |
| st->in_thead = 1; |
| 567 |
69 |
| push_close(st, CLOSE_THEAD); |
| 568 |
69 |
| break; |
| 569 |
57 |
▶ | case MDS_BLK_TABLE_BODY: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_TABLE_BODY: |
0 |
57 |
57 |
|
| 570 |
|
| MDS_BUF_LIT(b, "<tbody>\n"); |
| 571 |
57 |
| st->in_thead = 0; |
| 572 |
57 |
| push_close(st, CLOSE_TBODY); |
| 573 |
57 |
| break; |
| 574 |
166 |
▶ | case MDS_BLK_TABLE_ROW: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_TABLE_ROW: |
0 |
166 |
166 |
|
| 575 |
|
| MDS_BUF_LIT(b, "<tr>\n"); |
| 576 |
166 |
| st->tbl_col = 0; |
| 577 |
166 |
| push_close(st, CLOSE_TR); |
| 578 |
166 |
| break; |
| 579 |
354 |
| case MDS_BLK_TABLE_CELL: { |
| 580 |
354 |
▶ | mds_align al = (st->tbl_col < st->tbl_ncols && st->tbl_aligns) |
Branch: 1 branch
| Branch | True | False | Total |
| mds_align al = (st->tbl_col < st->tbl_ncols && st->tbl_aligns) |
354 |
0 |
354 |
|
| 581 |
708 |
▶ | ? st->tbl_aligns[st->tbl_col] : MDS_ALIGN_NONE; |
Branch: 1 branch
| Branch | True | False | Total |
| ? st->tbl_aligns[st->tbl_col] : MDS_ALIGN_NONE; |
354 |
0 |
354 |
|
| 582 |
354 |
▶ | if (MDS_LIKELY(al == MDS_ALIGN_NONE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (MDS_LIKELY(al == MDS_ALIGN_NONE)) { |
312 |
42 |
354 |
|
| 583 |
|
| /* Fast path — 4-byte literal in a single buf write. |
| 584 |
|
| * synth-tables corpus hits this on every cell. */ |
| 585 |
312 |
▶ | if (st->in_thead) MDS_BUF_LIT(b, "<th>"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->in_thead) MDS_BUF_LIT(b, "<th>"); |
122 |
190 |
312 |
|
| 586 |
|
| else MDS_BUF_LIT(b, "<td>"); |
| 587 |
|
| } else { |
| 588 |
42 |
▶ | const char* tag = st->in_thead ? "th" : "td"; |
Branch: 1 branch
| Branch | True | False | Total |
| const char* tag = st->in_thead ? "th" : "td"; |
21 |
21 |
42 |
|
| 589 |
42 |
▶ | const char* aname = al == MDS_ALIGN_LEFT ? "left" |
Branch: 1 branch
| Branch | True | False | Total |
| const char* aname = al == MDS_ALIGN_LEFT ? "left" |
0 |
42 |
42 |
|
| 590 |
42 |
▶ | : al == MDS_ALIGN_RIGHT ? "right" : "center"; |
Branch: 2 branches
| Branch | True | False | Total |
| : al == MDS_ALIGN_RIGHT ? "right" : "center"; |
28 |
14 |
42 |
| : al == MDS_ALIGN_RIGHT ? "right" : "center"; |
14 |
14 |
28 |
|
| 591 |
|
| mds_buf_write(aTHX_ b, "<", 1); |
| 592 |
|
| mds_buf_write(aTHX_ b, tag, 2); |
| 593 |
|
| MDS_BUF_LIT(b, " align=\""); |
| 594 |
42 |
▶ | mds_buf_write(aTHX_ b, aname, strlen(aname)); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ b, aname, strlen(aname)); |
0 |
42 |
42 |
|
| 595 |
|
| MDS_BUF_LIT(b, "\">"); |
| 596 |
|
| } |
| 597 |
354 |
| st->tbl_col++; |
| 598 |
354 |
▶ | push_close(st, st->in_thead ? CLOSE_TH : CLOSE_TD); |
Branch: 1 branch
| Branch | True | False | Total |
| push_close(st, st->in_thead ? CLOSE_TH : CLOSE_TD); |
143 |
211 |
354 |
|
| 599 |
354 |
| break; |
| 600 |
|
| } |
| 601 |
0 |
| default: |
| 602 |
0 |
| push_close(st, CLOSE_NOOP); |
| 603 |
0 |
| break; |
| 604 |
40409 |
▶ | case MDS_BLK_FOOTNOTES_SECTION: |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_BLK_FOOTNOTES_SECTION: |
3 |
40406 |
40409 |
|
| 605 |
|
| MDS_BUF_LIT(b, "<section class=\"footnotes\" data-footnotes>\n<ol>\n"); |
| 606 |
40409 |
| push_close(st, CLOSE_FN_SECTION); |
| 607 |
40409 |
| break; |
| 608 |
40418 |
| case MDS_BLK_FOOTNOTE_DEF: { |
| 609 |
40418 |
| const char* lab = d->u.footnote_def.label; |
| 610 |
40418 |
| size_t ll = d->u.footnote_def.label_len; |
| 611 |
40418 |
| unsigned idx = fn_lookup(st, lab, ll); |
| 612 |
40418 |
▶ | if (!idx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!idx) { |
0 |
40418 |
40418 |
|
| 613 |
|
| /* Unreferenced def: drop the entire subtree. */ |
| 614 |
0 |
| st->fn_skip = 1; |
| 615 |
0 |
| push_close(st, CLOSE_FN_DEF_SKIP); |
| 616 |
0 |
| break; |
| 617 |
|
| } |
| 618 |
40418 |
| st->fn_in_def = idx; |
| 619 |
40418 |
| st->fn_in_def_label = lab; |
| 620 |
40418 |
▶ | st->fn_in_def_label_len = ll; |
Branch: 1 branch
| Branch | True | False | Total |
| st->fn_in_def_label_len = ll; |
0 |
40418 |
40418 |
|
| 621 |
|
| MDS_BUF_LIT(b, "<li id=\"fn-"); |
| 622 |
40418 |
| write_fn_label_attr(aTHX_ b, lab, ll); |
| 623 |
|
| MDS_BUF_LIT(b, "\">\n"); |
| 624 |
40418 |
| push_close(st, CLOSE_FN_DEF); |
| 625 |
40418 |
| break; |
| 626 |
|
| } |
| 627 |
|
| } |
| 628 |
|
| } |
| 629 |
|
| |
| 630 |
|
| /* Close a heading, splicing in the deferred open tag when ids are on. |
| 631 |
|
| * |
| 632 |
|
| * The slug can only contain [a-z0-9_-] and bytes >= 0x80, so it needs no |
| 633 |
|
| * attribute escaping: mds_slugify drops every byte that would need it. */ |
| 634 |
808274 |
| static void heading_close(pTHX_ render_state* st, int lvl) { |
| 635 |
808274 |
| mds_buf* b = st->buf; |
| 636 |
|
| char close_tag[6]; |
| 637 |
|
| |
| 638 |
808274 |
▶ | if (st->in_heading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->in_heading) { |
808051 |
223 |
808274 |
|
| 639 |
|
| char slug[MDS_SLUG_MAX + 16]; |
| 640 |
|
| char open[MDS_SLUG_MAX + 32]; |
| 641 |
808051 |
| size_t sl, ol = 0; |
| 642 |
808051 |
▶ | const char* txt = st->hd_text ? st->hd_text : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| const char* txt = st->hd_text ? st->hd_text : ""; |
808051 |
0 |
808051 |
|
| 643 |
|
| |
| 644 |
808051 |
| st->in_heading = 0; |
| 645 |
808051 |
| sl = mds_slugify(txt, st->hd_len, slug); |
| 646 |
808051 |
▶ | if (sl == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sl == 0) { |
3 |
808048 |
808051 |
|
| 647 |
|
| /* A heading of pure punctuation or emoji has nothing to slug. |
| 648 |
|
| * Give it a positional stem so the anchor still exists and stays |
| 649 |
|
| * stable, and let the uniqueness pass number it. */ |
| 650 |
3 |
| memcpy(slug, "section", 7); |
| 651 |
3 |
| sl = 7; |
| 652 |
3 |
| slug[sl] = '\0'; |
| 653 |
|
| } |
| 654 |
808051 |
| sl = mds_slug_uniq(&st->hd_slugs, slug, sl); |
| 655 |
|
| |
| 656 |
808051 |
| open[ol++] = '<'; |
| 657 |
808051 |
| open[ol++] = 'h'; |
| 658 |
808051 |
| open[ol++] = (char)('0' + lvl); |
| 659 |
808051 |
| memcpy(open + ol, " id=\"", 5); ol += 5; |
| 660 |
808051 |
| memcpy(open + ol, slug, sl); ol += sl; |
| 661 |
808051 |
| open[ol++] = '"'; |
| 662 |
808051 |
| open[ol++] = '>'; |
| 663 |
808051 |
| mds_buf_insert(aTHX_ b, st->hd_mark, open, ol); |
| 664 |
|
| |
| 665 |
808051 |
▶ | if (st->toc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->toc) { |
404017 |
404034 |
808051 |
|
| 666 |
|
| /* Trim the plain text: a heading's trailing newline and any |
| 667 |
|
| * padding around setext underlines are not part of its title. */ |
| 668 |
404017 |
| size_t s0 = 0, s1 = st->hd_len; |
| 669 |
|
| HV* h; |
| 670 |
404017 |
▶ | while (s0 < s1 && (txt[s0] == ' ' || txt[s0] == '\t' || |
Branch: 3 branches
| Branch | True | False | Total |
| while (s0 < s1 && (txt[s0] == ' ' || txt[s0] == '\t' || |
404017 |
0 |
404017 |
| while (s0 < s1 && (txt[s0] == ' ' || txt[s0] == '\t' || |
0 |
404017 |
404017 |
| while (s0 < s1 && (txt[s0] == ' ' || txt[s0] == '\t' || |
0 |
404017 |
404017 |
|
| 671 |
404017 |
▶ | txt[s0] == '\n' || txt[s0] == '\r')) s0++; |
Branch: 2 branches
| Branch | True | False | Total |
| txt[s0] == '\n' || txt[s0] == '\r')) s0++; |
0 |
404017 |
404017 |
| txt[s0] == '\n' || txt[s0] == '\r')) s0++; |
0 |
404017 |
404017 |
|
| 672 |
404017 |
▶ | while (s1 > s0 && (txt[s1 - 1] == ' ' || txt[s1 - 1] == '\t' || |
Branch: 3 branches
| Branch | True | False | Total |
| while (s1 > s0 && (txt[s1 - 1] == ' ' || txt[s1 - 1] == '\t' || |
404017 |
0 |
404017 |
| while (s1 > s0 && (txt[s1 - 1] == ' ' || txt[s1 - 1] == '\t' || |
0 |
404017 |
404017 |
| while (s1 > s0 && (txt[s1 - 1] == ' ' || txt[s1 - 1] == '\t' || |
0 |
404017 |
404017 |
|
| 673 |
404017 |
▶ | txt[s1 - 1] == '\n' || txt[s1 - 1] == '\r')) s1--; |
Branch: 2 branches
| Branch | True | False | Total |
| txt[s1 - 1] == '\n' || txt[s1 - 1] == '\r')) s1--; |
0 |
404017 |
404017 |
| txt[s1 - 1] == '\n' || txt[s1 - 1] == '\r')) s1--; |
0 |
404017 |
404017 |
|
| 674 |
404017 |
| h = newHV(); |
| 675 |
404017 |
| (void)hv_stores(h, "level", newSViv(lvl)); |
| 676 |
404017 |
| (void)hv_stores(h, "text", newSVpvn(txt + s0, s1 - s0)); |
| 677 |
404017 |
| (void)hv_stores(h, "id", newSVpvn(slug, sl)); |
| 678 |
404017 |
| av_push(st->toc, newRV_noinc((SV*)h)); |
| 679 |
|
| } |
| 680 |
808051 |
| st->hd_len = 0; |
| 681 |
|
| } |
| 682 |
|
| |
| 683 |
808274 |
| close_tag[0] = '<'; close_tag[1] = '/'; close_tag[2] = 'h'; |
| 684 |
808274 |
| close_tag[3] = (char)('0' + lvl); |
| 685 |
808274 |
▶ | close_tag[4] = '>'; close_tag[5] = '\n'; |
Branch: 1 branch
| Branch | True | False | Total |
| close_tag[4] = '>'; close_tag[5] = '\n'; |
0 |
808274 |
808274 |
|
| 686 |
|
| mds_buf_write(aTHX_ b, close_tag, 6); |
| 687 |
808274 |
| } |
| 688 |
|
| |
| 689 |
4995075 |
| static void cb_leave_block(void* ud, mds_block_type t) { |
| 690 |
4995075 |
| render_state* st = (render_state*)ud; |
| 691 |
|
| dTHXa(st->my_perl); |
| 692 |
4995075 |
| mds_buf* b = st->buf; |
| 693 |
|
| close_kind k; |
| 694 |
|
| (void)t; |
| 695 |
|
| /* Mirror cb_enter_block: in fn_skip mode, nested events were given |
| 696 |
|
| * CLOSE_NOOP on the stack so pop here and return without flushing. */ |
| 697 |
4995075 |
▶ | if (st->fn_skip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_skip) { |
0 |
4995075 |
4995075 |
|
| 698 |
0 |
| k = pop_close(st); |
| 699 |
0 |
▶ | if (k == CLOSE_FN_DEF_SKIP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == CLOSE_FN_DEF_SKIP) { |
0 |
0 |
0 |
|
| 700 |
0 |
| st->fn_skip = 0; |
| 701 |
|
| } |
| 702 |
0 |
| return; |
| 703 |
|
| } |
| 704 |
4995075 |
| flush_pending_text(aTHX_ st); |
| 705 |
4995075 |
▶ | if (st->li_check_pending == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->li_check_pending == 2) { |
0 |
4995075 |
4995075 |
|
| 706 |
|
| MDS_BUF_LIT(b, "["); |
| 707 |
|
| } |
| 708 |
4995075 |
| st->li_check_pending = 0; |
| 709 |
4995075 |
| k = pop_close(st); |
| 710 |
4995075 |
| switch (k) { |
| 711 |
304 |
| case CLOSE_NONE: |
| 712 |
|
| case CLOSE_NOOP: |
| 713 |
304 |
| break; |
| 714 |
6590196 |
▶ | case CLOSE_P: MDS_BUF_LIT(b, "</p>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_P: MDS_BUF_LIT(b, "</p>\n"); break; |
20203 |
3274895 |
3295098 |
|
| 715 |
808274 |
| case CLOSE_H1: case CLOSE_H2: case CLOSE_H3: |
| 716 |
|
| case CLOSE_H4: case CLOSE_H5: case CLOSE_H6: |
| 717 |
808274 |
| heading_close(aTHX_ st, (int)(k - CLOSE_H1) + 1); |
| 718 |
808274 |
| break; |
| 719 |
232 |
▶ | case CLOSE_CODE_FENCED: MDS_BUF_LIT(b, "</code></pre>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_CODE_FENCED: MDS_BUF_LIT(b, "</code></pre>\n"); break; |
3 |
113 |
116 |
|
| 720 |
340 |
▶ | case CLOSE_CODE_INDENTED: MDS_BUF_LIT(b, "</code></pre>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_CODE_INDENTED: MDS_BUF_LIT(b, "</code></pre>\n"); break; |
0 |
170 |
170 |
|
| 721 |
808023 |
| case CLOSE_CODE_FENCED_HL: { |
| 722 |
808023 |
| size_t out_len = 0; |
| 723 |
808023 |
| char* hl_out = eshu_highlight(st->hl_buf ? st->hl_buf : "", |
| 724 |
808023 |
▶ | st->hl_len, st->hl_lang, &out_len); |
Branch: 1 branch
| Branch | True | False | Total |
| st->hl_len, st->hl_lang, &out_len); |
808023 |
0 |
808023 |
|
| 725 |
|
| MDS_BUF_LIT(b, "<pre><code class=\"language-"); |
| 726 |
808023 |
▶ | mds_buf_write(aTHX_ b, st->hl_lang, strlen(st->hl_lang)); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ b, st->hl_lang, strlen(st->hl_lang)); |
0 |
808023 |
808023 |
|
| 727 |
|
| MDS_BUF_LIT(b, "\">"); |
| 728 |
808023 |
▶ | if (hl_out) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hl_out) { |
808023 |
0 |
808023 |
|
| 729 |
808023 |
▶ | mds_buf_write(aTHX_ b, hl_out, out_len); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ b, hl_out, out_len); |
40410 |
767613 |
808023 |
|
| 730 |
808023 |
| free(hl_out); |
| 731 |
|
| } else { |
| 732 |
0 |
▶ | html_escape(aTHX_ b, st->hl_buf ? st->hl_buf : "", st->hl_len); |
Branch: 1 branch
| Branch | True | False | Total |
| html_escape(aTHX_ b, st->hl_buf ? st->hl_buf : "", st->hl_len); |
0 |
0 |
0 |
|
| 733 |
|
| } |
| 734 |
|
| MDS_BUF_LIT(b, "</code></pre>\n"); |
| 735 |
808023 |
| st->in_fenced_hl = 0; |
| 736 |
808023 |
| st->hl_len = 0; |
| 737 |
808023 |
| break; |
| 738 |
|
| } |
| 739 |
177 |
▶ | case CLOSE_BLOCKQUOTE: MDS_BUF_LIT(b, "</blockquote>\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_BLOCKQUOTE: MDS_BUF_LIT(b, "</blockquote>\n"); |
6 |
171 |
177 |
|
| 740 |
177 |
▶ | if (st->tight_top > 0) st->tight_top--; break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top > 0) st->tight_top--; break; |
177 |
0 |
177 |
|
| 741 |
42 |
▶ | case CLOSE_OL: MDS_BUF_LIT(b, "</ol>\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_OL: MDS_BUF_LIT(b, "</ol>\n"); |
0 |
42 |
42 |
|
| 742 |
42 |
▶ | if (st->tight_top > 0) st->tight_top--; break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top > 0) st->tight_top--; break; |
42 |
0 |
42 |
|
| 743 |
59 |
▶ | case CLOSE_UL: MDS_BUF_LIT(b, "</ul>\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_UL: MDS_BUF_LIT(b, "</ul>\n"); |
0 |
59 |
59 |
|
| 744 |
59 |
▶ | if (st->tight_top > 0) st->tight_top--; break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top > 0) st->tight_top--; break; |
59 |
0 |
59 |
|
| 745 |
55 |
▶ | case CLOSE_OL_TIGHT: MDS_BUF_LIT(b, "</ol>\n"); st->tight_depth--; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_OL_TIGHT: MDS_BUF_LIT(b, "</ol>\n"); st->tight_depth--; |
0 |
55 |
55 |
|
| 746 |
55 |
▶ | if (st->tight_top > 0) st->tight_top--; break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top > 0) st->tight_top--; break; |
55 |
0 |
55 |
|
| 747 |
223 |
▶ | case CLOSE_UL_TIGHT: MDS_BUF_LIT(b, "</ul>\n"); st->tight_depth--; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_UL_TIGHT: MDS_BUF_LIT(b, "</ul>\n"); st->tight_depth--; |
0 |
223 |
223 |
|
| 748 |
223 |
▶ | if (st->tight_top > 0) st->tight_top--; break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->tight_top > 0) st->tight_top--; break; |
223 |
0 |
223 |
|
| 749 |
595 |
▶ | case CLOSE_LI: st->need_nl_next = 0; st->li_first_block = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_LI: st->need_nl_next = 0; st->li_first_block = 0; |
0 |
595 |
595 |
|
| 750 |
595 |
| MDS_BUF_LIT(b, "</li>\n"); break; |
| 751 |
397 |
| case CLOSE_TIGHT_PARA: st->need_nl_next = 1; break; |
| 752 |
138 |
▶ | case CLOSE_TABLE: MDS_BUF_LIT(b, "</table>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_TABLE: MDS_BUF_LIT(b, "</table>\n"); break; |
3 |
66 |
69 |
|
| 753 |
138 |
▶ | case CLOSE_THEAD: MDS_BUF_LIT(b, "</thead>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_THEAD: MDS_BUF_LIT(b, "</thead>\n"); break; |
0 |
69 |
69 |
|
| 754 |
114 |
▶ | case CLOSE_TBODY: MDS_BUF_LIT(b, "</tbody>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_TBODY: MDS_BUF_LIT(b, "</tbody>\n"); break; |
4 |
53 |
57 |
|
| 755 |
332 |
▶ | case CLOSE_TR: MDS_BUF_LIT(b, "</tr>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_TR: MDS_BUF_LIT(b, "</tr>\n"); break; |
4 |
162 |
166 |
|
| 756 |
286 |
▶ | case CLOSE_TH: MDS_BUF_LIT(b, "</th>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_TH: MDS_BUF_LIT(b, "</th>\n"); break; |
0 |
143 |
143 |
|
| 757 |
422 |
▶ | case CLOSE_TD: MDS_BUF_LIT(b, "</td>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_TD: MDS_BUF_LIT(b, "</td>\n"); break; |
7 |
204 |
211 |
|
| 758 |
80818 |
▶ | case CLOSE_FN_SECTION: MDS_BUF_LIT(b, "</ol>\n</section>\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case CLOSE_FN_SECTION: MDS_BUF_LIT(b, "</ol>\n</section>\n"); break; |
0 |
40409 |
40409 |
|
| 759 |
0 |
| case CLOSE_FN_DEF_SKIP: st->fn_skip = 0; break; |
| 760 |
40418 |
| case CLOSE_FN_DEF: { |
| 761 |
|
| /* Sub-scanned body has now emitted all its blocks. If the very |
| 762 |
|
| * last bytes are `</p>\n` we rewind 5 bytes and inject the |
| 763 |
|
| * backref(s) before the </p>; otherwise the def ended in a |
| 764 |
|
| * code block / quote / etc. and the backref goes after as a |
| 765 |
|
| * bare <a>. One backref per ref-instance; first is plain `↩`, |
| 766 |
|
| * subsequent are `↩<sup>N</sup>`. */ |
| 767 |
|
| unsigned idx; |
| 768 |
|
| unsigned uses; |
| 769 |
|
| int inject_in_p; |
| 770 |
|
| unsigned u; |
| 771 |
|
| char nbuf[16]; |
| 772 |
|
| int nn; |
| 773 |
|
| char ibuf[32]; |
| 774 |
|
| int in; |
| 775 |
|
| |
| 776 |
40418 |
| idx = st->fn_in_def; |
| 777 |
40418 |
▶ | uses = (idx >= 1 && idx <= st->fn_count) ? st->fn_uses[idx - 1] : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| uses = (idx >= 1 && idx <= st->fn_count) ? st->fn_uses[idx - 1] : 1; |
40418 |
0 |
40418 |
| uses = (idx >= 1 && idx <= st->fn_count) ? st->fn_uses[idx - 1] : 1; |
40418 |
0 |
40418 |
|
| 778 |
40418 |
| inject_in_p = 0; |
| 779 |
40418 |
▶ | if (b->cur - b->base >= 5 && memcmp(b->cur - 5, "</p>\n", 5) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->cur - b->base >= 5 && memcmp(b->cur - 5, "</p>\n", 5) == 0) { |
40418 |
0 |
40418 |
| if (b->cur - b->base >= 5 && memcmp(b->cur - 5, "</p>\n", 5) == 0) { |
40415 |
3 |
40418 |
|
| 780 |
40415 |
| b->cur -= 5; |
| 781 |
40415 |
| inject_in_p = 1; |
| 782 |
|
| } |
| 783 |
848442 |
▶ | for (u = 1; u <= uses; u++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (u = 1; u <= uses; u++) { |
808024 |
40418 |
848442 |
|
| 784 |
|
| /* Inside <p>: separate anchors from preceding content with |
| 785 |
|
| * a space. As a bare trailing anchor (after </pre>/<blockquote> |
| 786 |
|
| * etc.) no leading space — the newline from the prior block |
| 787 |
|
| * already provides the visual gap. Subsequent anchors in the |
| 788 |
|
| * same line always get a single space separator. */ |
| 789 |
808024 |
▶ | if (inject_in_p || u > 1) MDS_BUF_LIT(b, " "); |
Branch: 2 branches
| Branch | True | False | Total |
| if (inject_in_p || u > 1) MDS_BUF_LIT(b, " "); |
3 |
808021 |
808024 |
| if (inject_in_p || u > 1) MDS_BUF_LIT(b, " "); |
0 |
3 |
3 |
|
| 790 |
|
| MDS_BUF_LIT(b, "<a href=\"#fnref-"); |
| 791 |
808024 |
| write_fn_label_attr(aTHX_ b, st->fn_in_def_label, st->fn_in_def_label_len); |
| 792 |
808024 |
▶ | if (u > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (u > 1) { |
767606 |
40418 |
808024 |
|
| 793 |
767606 |
| nn = snprintf(nbuf, sizeof nbuf, "-%u", u); |
| 794 |
767606 |
▶ | if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
Branch: 2 branches
| Branch | True | False | Total |
| if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
767606 |
0 |
767606 |
| if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
0 |
767606 |
767606 |
|
| 795 |
|
| } |
| 796 |
|
| MDS_BUF_LIT(b, "\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\""); |
| 797 |
808024 |
▶ | if (u == 1) in = snprintf(ibuf, sizeof ibuf, "%u", idx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (u == 1) in = snprintf(ibuf, sizeof ibuf, "%u", idx); |
40418 |
767606 |
808024 |
|
| 798 |
767606 |
| else in = snprintf(ibuf, sizeof ibuf, "%u-%u", idx, u); |
| 799 |
808024 |
▶ | if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
Branch: 2 branches
| Branch | True | False | Total |
| if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
808024 |
0 |
808024 |
| if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
0 |
808024 |
808024 |
|
| 800 |
|
| MDS_BUF_LIT(b, "\" aria-label=\"Back to reference "); |
| 801 |
808024 |
▶ | if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
Branch: 2 branches
| Branch | True | False | Total |
| if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
808024 |
0 |
808024 |
| if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
0 |
808024 |
808024 |
|
| 802 |
|
| MDS_BUF_LIT(b, "\">\xe2\x86\xa9"); |
| 803 |
808024 |
▶ | if (u > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (u > 1) { |
767606 |
40418 |
808024 |
|
| 804 |
|
| MDS_BUF_LIT(b, "<sup class=\"footnote-ref\">"); |
| 805 |
767606 |
| nn = snprintf(nbuf, sizeof nbuf, "%u", u); |
| 806 |
767606 |
▶ | if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
Branch: 2 branches
| Branch | True | False | Total |
| if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
767606 |
0 |
767606 |
| if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
0 |
767606 |
767606 |
|
| 807 |
|
| MDS_BUF_LIT(b, "</sup>"); |
| 808 |
|
| } |
| 809 |
|
| MDS_BUF_LIT(b, "</a>"); |
| 810 |
|
| } |
| 811 |
40418 |
▶ | if (inject_in_p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (inject_in_p) { |
40415 |
3 |
40418 |
|
| 812 |
|
| MDS_BUF_LIT(b, "</p>\n"); |
| 813 |
|
| } else { |
| 814 |
|
| MDS_BUF_LIT(b, "\n"); |
| 815 |
|
| } |
| 816 |
|
| MDS_BUF_LIT(b, "</li>\n"); |
| 817 |
40418 |
| st->fn_in_def = 0; |
| 818 |
40418 |
| st->fn_in_def_label = NULL; |
| 819 |
40418 |
| st->fn_in_def_label_len = 0; |
| 820 |
40418 |
| break; |
| 821 |
|
| } |
| 822 |
|
| } |
| 823 |
|
| } |
| 824 |
|
| |
| 825 |
|
| /* ------------------ GFM autolink extension (§6.9) ------------------ */ |
| 826 |
|
| |
| 827 |
|
| static void write_url_attr(pTHX_ mds_buf* b, const char* s, size_t n); |
| 828 |
|
| |
| 829 |
|
| /* GFM footnote label encoder. Percent-encodes bytes that would be unsafe |
| 830 |
|
| * in an HTML attribute / URL fragment. Per GFM expected output, leaves |
| 831 |
|
| * unreserved + a few sub-delim bytes literal: `( ) ! * - . _ ~ /` and |
| 832 |
|
| * alphanumerics. Everything else (incl. control bytes, space, `"`, `<`, |
| 833 |
|
| * `>`, `&`, `\\`, `^`, backtick, `{ | }`, `[ ]`, `%`, `?`, `#`, `+`, |
| 834 |
|
| * `=`, `,`, `;`, `:`, `'`, `@`, `$`, `/`) is %XX-encoded. Multi-byte |
| 835 |
|
| * UTF-8 sequences are encoded byte-by-byte. */ |
| 836 |
2464490 |
| static void write_fn_label_attr(pTHX_ mds_buf* b, const char* s, size_t n) { |
| 837 |
|
| static const char hex[] = "0123456789ABCDEF"; |
| 838 |
|
| size_t i; |
| 839 |
4929910 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
2465420 |
2464490 |
4929910 |
|
| 840 |
2465420 |
| unsigned char c = (unsigned char)s[i]; |
| 841 |
2465318 |
▶ | int safe = (c >= '0' && c <= '9') || |
Branch: 1 branch
| Branch | True | False | Total |
| int safe = (c >= '0' && c <= '9') || |
2465294 |
24 |
2465318 |
|
| 842 |
2465396 |
▶ | (c >= 'A' && c <= 'Z') || |
Branch: 2 branches
| Branch | True | False | Total |
| (c >= 'A' && c <= 'Z') || |
2465234 |
162 |
2465396 |
| (c >= 'A' && c <= 'Z') || |
2465234 |
0 |
2465234 |
|
| 843 |
2465396 |
▶ | (c >= 'a' && c <= 'z') || |
Branch: 2 branches
| Branch | True | False | Total |
| (c >= 'a' && c <= 'z') || |
2465234 |
162 |
2465396 |
| (c >= 'a' && c <= 'z') || |
0 |
2465234 |
2465234 |
|
| 844 |
162 |
▶ | c == '(' || c == ')' || c == '!' || c == '*' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '(' || c == ')' || c == '!' || c == '*' || |
150 |
12 |
162 |
| c == '(' || c == ')' || c == '!' || c == '*' || |
138 |
12 |
150 |
| c == '(' || c == ')' || c == '!' || c == '*' || |
138 |
0 |
138 |
| c == '(' || c == ')' || c == '!' || c == '*' || |
138 |
0 |
138 |
|
| 845 |
4930924 |
▶ | c == '-' || c == '.' || c == '_' || c == '~' || |
Branch: 5 branches
| Branch | True | False | Total |
| c == '-' || c == '.' || c == '_' || c == '~' || |
2465318 |
102 |
2465420 |
| c == '-' || c == '.' || c == '_' || c == '~' || |
84 |
54 |
138 |
| c == '-' || c == '.' || c == '_' || c == '~' || |
84 |
0 |
84 |
| c == '-' || c == '.' || c == '_' || c == '~' || |
84 |
0 |
84 |
| c == '-' || c == '.' || c == '_' || c == '~' || |
84 |
0 |
84 |
|
| 846 |
84 |
▶ | c == '/'; |
Branch: 1 branch
| Branch | True | False | Total |
| c == '/'; |
12 |
72 |
84 |
|
| 847 |
2465420 |
▶ | if (safe) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (safe) { |
2465348 |
72 |
2465420 |
|
| 848 |
|
| mds_buf_write(aTHX_ b, (const char*)&c, 1); |
| 849 |
|
| } else { |
| 850 |
72 |
▶ | char esc[3] = { '%', hex[c >> 4], hex[c & 0xF] }; |
Branch: 1 branch
| Branch | True | False | Total |
| char esc[3] = { '%', hex[c >> 4], hex[c & 0xF] }; |
0 |
72 |
72 |
|
| 851 |
|
| mds_buf_write(aTHX_ b, esc, 3); |
| 852 |
|
| } |
| 853 |
|
| } |
| 854 |
2464490 |
| } |
| 855 |
|
| |
| 856 |
|
| /* Look up label in render_state's used set. Returns 1-based ordinal |
| 857 |
|
| * if found, 0 if not. Linear scan; footnote counts are tiny. */ |
| 858 |
848442 |
| static unsigned fn_lookup(render_state* st, const char* s, size_t n) { |
| 859 |
|
| size_t i; |
| 860 |
848478 |
▶ | for (i = 0; i < st->fn_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < st->fn_count; i++) { |
808060 |
40418 |
848478 |
|
| 861 |
808060 |
▶ | if (st->fn_label_lens[i] == n && |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_label_lens[i] == n && |
808024 |
36 |
808060 |
|
| 862 |
808024 |
▶ | memcmp(st->fn_labels[i], s, n) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(st->fn_labels[i], s, n) == 0) { |
808024 |
0 |
808024 |
|
| 863 |
808024 |
| return (unsigned)(i + 1); |
| 864 |
|
| } |
| 865 |
|
| } |
| 866 |
40418 |
| return 0; |
| 867 |
|
| } |
| 868 |
|
| |
| 869 |
|
| /* Register or increment usage. Returns 1-based ordinal. */ |
| 870 |
808024 |
| static unsigned fn_register(render_state* st, const char* s, size_t n) { |
| 871 |
808024 |
| unsigned idx = fn_lookup(st, s, n); |
| 872 |
808024 |
▶ | if (idx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx) { |
767606 |
40418 |
808024 |
|
| 873 |
767606 |
| st->fn_uses[idx - 1]++; |
| 874 |
767606 |
| return idx; |
| 875 |
|
| } |
| 876 |
40418 |
▶ | if (st->fn_count == st->fn_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_count == st->fn_cap) { |
40409 |
9 |
40418 |
|
| 877 |
40409 |
▶ | size_t nc = st->fn_cap ? st->fn_cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = st->fn_cap ? st->fn_cap * 2 : 8; |
0 |
40409 |
40409 |
|
| 878 |
40409 |
| st->fn_labels = (const char**)realloc(st->fn_labels, nc * sizeof(*st->fn_labels)); |
| 879 |
40409 |
| st->fn_label_lens = (size_t*)realloc(st->fn_label_lens, nc * sizeof(*st->fn_label_lens)); |
| 880 |
40409 |
| st->fn_uses = (unsigned*)realloc(st->fn_uses, nc * sizeof(*st->fn_uses)); |
| 881 |
40409 |
| st->fn_cap = nc; |
| 882 |
|
| } |
| 883 |
|
| /* Label bytes are stable in the arena (footnote table arena_dup). */ |
| 884 |
40418 |
| st->fn_labels[st->fn_count] = s; |
| 885 |
40418 |
| st->fn_label_lens[st->fn_count] = n; |
| 886 |
40418 |
| st->fn_uses[st->fn_count] = 1; |
| 887 |
40418 |
| st->fn_count++; |
| 888 |
40418 |
| return (unsigned)st->fn_count; |
| 889 |
|
| } |
| 890 |
|
| |
| 891 |
71053816 |
| static int gfm_word_byte(unsigned char c) { |
| 892 |
71053816 |
▶ | return isalnum(c) || c == '_'; |
Branch: 2 branches
| Branch | True | False | Total |
| return isalnum(c) || c == '_'; |
11358456 |
59695360 |
71053816 |
| return isalnum(c) || c == '_'; |
17 |
11358439 |
11358456 |
|
| 893 |
|
| } |
| 894 |
|
| |
| 895 |
|
| /* Domain scanner. p..end points at start of would-be domain. Returns |
| 896 |
|
| * length on success (>=1 dot, valid segments, no underscores in last |
| 897 |
|
| * two segments), else 0. */ |
| 898 |
137 |
| static size_t gfm_scan_domain_ex(const char* p, const char* end, int need_dot) { |
| 899 |
|
| const char* q; |
| 900 |
|
| int dots; |
| 901 |
|
| const char* last_dot; |
| 902 |
|
| const char* prev_dot; |
| 903 |
|
| const char* check_end; |
| 904 |
|
| const char* r; |
| 905 |
|
| const char* l_s; |
| 906 |
|
| const char* l_e; |
| 907 |
|
| const char* m_s; |
| 908 |
|
| const char* m_e; |
| 909 |
|
| |
| 910 |
137 |
| q = p; |
| 911 |
137 |
| dots = 0; |
| 912 |
137 |
| last_dot = NULL; |
| 913 |
137 |
| prev_dot = NULL; |
| 914 |
1527 |
▶ | while (q < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q < end) { |
1459 |
68 |
1527 |
|
| 915 |
1459 |
| unsigned char c = (unsigned char)*q; |
| 916 |
1459 |
▶ | if (c == '.') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '.') { |
176 |
1283 |
1459 |
|
| 917 |
176 |
▶ | if (q == p || q[-1] == '.') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q == p || q[-1] == '.') break; |
164 |
12 |
176 |
| if (q == p || q[-1] == '.') break; |
164 |
0 |
164 |
|
| 918 |
164 |
| prev_dot = last_dot; last_dot = q; dots++; |
| 919 |
164 |
| q++; continue; |
| 920 |
|
| } |
| 921 |
1283 |
▶ | if (isalnum(c) || c == '-' || c == '_' || c >= 0x80) { q++; continue; } |
Branch: 4 branches
| Branch | True | False | Total |
| if (isalnum(c) || c == '-' || c == '_' || c >= 0x80) { q++; continue; } |
90 |
1193 |
1283 |
| if (isalnum(c) || c == '-' || c == '_' || c >= 0x80) { q++; continue; } |
90 |
0 |
90 |
| if (isalnum(c) || c == '-' || c == '_' || c >= 0x80) { q++; continue; } |
81 |
9 |
90 |
| if (isalnum(c) || c == '-' || c == '_' || c >= 0x80) { q++; continue; } |
24 |
57 |
81 |
|
| 922 |
57 |
| break; |
| 923 |
|
| } |
| 924 |
137 |
▶ | if (q == p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q == p) return 0; |
12 |
125 |
137 |
|
| 925 |
125 |
▶ | if (need_dot && dots == 0) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (need_dot && dots == 0) return 0; |
84 |
41 |
125 |
| if (need_dot && dots == 0) return 0; |
0 |
84 |
84 |
|
| 926 |
|
| /* Permit trailing dot in domain; strip-trail handles it. Roll q back |
| 927 |
|
| * just past the dot for the underscore check. */ |
| 928 |
125 |
| check_end = q; |
| 929 |
125 |
▶ | if (check_end > p && check_end[-1] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (check_end > p && check_end[-1] == '.') { |
125 |
0 |
125 |
| if (check_end > p && check_end[-1] == '.') { |
9 |
116 |
125 |
|
| 930 |
9 |
| check_end--; |
| 931 |
|
| /* recompute last_dot/prev_dot if the trailing dot was the last one */ |
| 932 |
9 |
▶ | if (last_dot == check_end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_dot == check_end) { |
9 |
0 |
9 |
|
| 933 |
9 |
| last_dot = prev_dot; |
| 934 |
9 |
| prev_dot = NULL; |
| 935 |
72 |
▶ | for (r = p; r < last_dot; r++) if (*r == '.') prev_dot = r; |
Branch: 2 branches
| Branch | True | False | Total |
| for (r = p; r < last_dot; r++) if (*r == '.') prev_dot = r; |
0 |
63 |
63 |
| for (r = p; r < last_dot; r++) if (*r == '.') prev_dot = r; |
63 |
9 |
72 |
|
| 936 |
|
| } |
| 937 |
|
| } |
| 938 |
125 |
▶ | if (last_dot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_dot) { |
122 |
3 |
125 |
|
| 939 |
|
| /* No underscores in last two segments. */ |
| 940 |
122 |
| l_s = last_dot + 1; |
| 941 |
122 |
| l_e = check_end; |
| 942 |
467 |
▶ | for (r = l_s; r < l_e; r++) if (*r == '_') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (r = l_s; r < l_e; r++) if (*r == '_') return 0; |
3 |
345 |
348 |
| for (r = l_s; r < l_e; r++) if (*r == '_') return 0; |
348 |
119 |
467 |
|
| 943 |
119 |
▶ | m_s = prev_dot ? prev_dot + 1 : p; |
Branch: 1 branch
| Branch | True | False | Total |
| m_s = prev_dot ? prev_dot + 1 : p; |
21 |
98 |
119 |
|
| 944 |
119 |
| m_e = last_dot; |
| 945 |
844 |
▶ | for (r = m_s; r < m_e; r++) if (*r == '_') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (r = m_s; r < m_e; r++) if (*r == '_') return 0; |
3 |
725 |
728 |
| for (r = m_s; r < m_e; r++) if (*r == '_') return 0; |
728 |
116 |
844 |
|
| 946 |
|
| } else { |
| 947 |
|
| /* No dot at all (only allowed when need_dot==0): also disallow `_`. */ |
| 948 |
24 |
▶ | for (r = p; r < check_end; r++) if (*r == '_') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (r = p; r < check_end; r++) if (*r == '_') return 0; |
0 |
21 |
21 |
| for (r = p; r < check_end; r++) if (*r == '_') return 0; |
21 |
3 |
24 |
|
| 949 |
|
| } |
| 950 |
119 |
| return (size_t)(q - p); |
| 951 |
|
| } |
| 952 |
|
| |
| 953 |
96 |
| static size_t gfm_scan_domain(const char* p, const char* end) { |
| 954 |
96 |
| return gfm_scan_domain_ex(p, end, 1); |
| 955 |
|
| } |
| 956 |
|
| |
| 957 |
|
| /* Strip GFM trailing-punct from URL run p..q. Returns new q. */ |
| 958 |
125 |
| static const char* gfm_strip_trail(const char* p, const char* q) { |
| 959 |
152 |
▶ | while (q > p) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q > p) { |
152 |
0 |
152 |
|
| 960 |
152 |
| unsigned char c = (unsigned char)q[-1]; |
| 961 |
152 |
▶ | if (c == '?' || c == '!' || c == '.' || c == ',' || |
Branch: 5 branches
| Branch | True | False | Total |
| if (c == '?' || c == '!' || c == '.' || c == ',' || |
152 |
0 |
152 |
| if (c == '?' || c == '!' || c == '.' || c == ',' || |
152 |
0 |
152 |
| if (c == '?' || c == '!' || c == '.' || c == ',' || |
134 |
18 |
152 |
| if (c == '?' || c == '!' || c == '.' || c == ',' || |
134 |
0 |
134 |
| if (c == '?' || c == '!' || c == '.' || c == ',' || |
134 |
0 |
134 |
|
| 962 |
134 |
▶ | c == ':' || c == '*' || c == '_' || c == '~' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == ':' || c == '*' || c == '_' || c == '~' || |
134 |
0 |
134 |
| c == ':' || c == '*' || c == '_' || c == '~' || |
134 |
0 |
134 |
| c == ':' || c == '*' || c == '_' || c == '~' || |
134 |
0 |
134 |
| c == ':' || c == '*' || c == '_' || c == '~' || |
131 |
3 |
134 |
|
| 963 |
131 |
▶ | c == '\'' || c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| c == '\'' || c == '"') { |
3 |
128 |
131 |
|
| 964 |
24 |
| q--; continue; |
| 965 |
|
| } |
| 966 |
128 |
▶ | if (c == ')') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == ')') { |
3 |
125 |
128 |
|
| 967 |
3 |
| int opens = 0, closes = 0; |
| 968 |
|
| const char* r; |
| 969 |
48 |
▶ | for (r = p; r < q; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = p; r < q; r++) { |
45 |
3 |
48 |
|
| 970 |
45 |
▶ | if (*r == '(') opens++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*r == '(') opens++; |
0 |
45 |
45 |
|
| 971 |
45 |
▶ | else if (*r == ')') closes++; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*r == ')') closes++; |
3 |
42 |
45 |
|
| 972 |
|
| } |
| 973 |
3 |
▶ | if (closes > opens) { q--; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (closes > opens) { q--; continue; } |
3 |
0 |
3 |
|
| 974 |
0 |
| break; |
| 975 |
|
| } |
| 976 |
125 |
▶ | if (c == ';') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == ';') { |
0 |
125 |
125 |
|
| 977 |
0 |
| const char* r = q - 1; |
| 978 |
0 |
▶ | while (r > p && isalnum((unsigned char)*r)) r--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r > p && isalnum((unsigned char)*r)) r--; |
0 |
0 |
0 |
| while (r > p && isalnum((unsigned char)*r)) r--; |
0 |
0 |
0 |
|
| 979 |
0 |
▶ | if (r > p && *r == '&') { q = r; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (r > p && *r == '&') { q = r; continue; } |
0 |
0 |
0 |
| if (r > p && *r == '&') { q = r; continue; } |
0 |
0 |
0 |
|
| 980 |
|
| } |
| 981 |
125 |
| break; |
| 982 |
|
| } |
| 983 |
125 |
| return q; |
| 984 |
|
| } |
| 985 |
|
| |
| 986 |
|
| /* Scan body bytes for a URL after a scheme (http/https/ftp) or after a |
| 987 |
|
| * www. prefix. Returns length consumed past start of domain, 0 if no |
| 988 |
|
| * valid URL. */ |
| 989 |
41 |
| static size_t gfm_scan_url_body(const char* p, const char* end) { |
| 990 |
|
| size_t dl; |
| 991 |
|
| const char* q; |
| 992 |
41 |
| dl = gfm_scan_domain_ex(p, end, 0); |
| 993 |
41 |
▶ | if (!dl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dl) return 0; |
0 |
41 |
41 |
|
| 994 |
41 |
| q = p + dl; |
| 995 |
|
| /* Optional path: byte run until whitespace, '<', or end. */ |
| 996 |
89 |
▶ | while (q < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q < end) { |
57 |
32 |
89 |
|
| 997 |
57 |
| unsigned char c = (unsigned char)*q; |
| 998 |
57 |
▶ | if (c <= 0x20 || c == '<') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c <= 0x20 || c == '<') break; |
48 |
9 |
57 |
| if (c <= 0x20 || c == '<') break; |
48 |
0 |
48 |
|
| 999 |
48 |
| q++; |
| 1000 |
|
| } |
| 1001 |
|
| /* Strip trailing punctuation, applied to entire URL [p,q). */ |
| 1002 |
41 |
| q = gfm_strip_trail(p, q); |
| 1003 |
41 |
| return (size_t)(q - p); |
| 1004 |
|
| } |
| 1005 |
|
| |
| 1006 |
|
| /* Email body scan starting at @. base is start of text run so we can |
| 1007 |
|
| * walk backwards. Sets *l_out to local-part length before @, *d_out to |
| 1008 |
|
| * domain length after @. Returns 1 on success. */ |
| 1009 |
72 |
| static int gfm_scan_email_at(const char* base, const char* at, const char* end, |
| 1010 |
|
| size_t* l_out, size_t* d_out) { |
| 1011 |
|
| const char* L; |
| 1012 |
|
| const char* D; |
| 1013 |
|
| size_t dl; |
| 1014 |
|
| const char* de; |
| 1015 |
|
| |
| 1016 |
|
| /* local part: [a-zA-Z0-9._+-] walking back */ |
| 1017 |
72 |
| L = at; |
| 1018 |
552 |
▶ | while (L > base) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (L > base) { |
480 |
72 |
552 |
|
| 1019 |
480 |
| unsigned char c = (unsigned char)L[-1]; |
| 1020 |
480 |
▶ | if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') L--; |
Branch: 5 branches
| Branch | True | False | Total |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') L--; |
24 |
456 |
480 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') L--; |
9 |
15 |
24 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') L--; |
6 |
3 |
9 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') L--; |
3 |
3 |
6 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') L--; |
3 |
0 |
3 |
|
| 1021 |
|
| else break; |
| 1022 |
|
| } |
| 1023 |
72 |
▶ | if (L == at) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (L == at) return 0; |
0 |
72 |
72 |
|
| 1024 |
72 |
▶ | if (*L == '.') return 0; /* can't start with dot */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*L == '.') return 0; /* can't start with dot */ |
0 |
72 |
72 |
|
| 1025 |
|
| /* domain: alphanum, _, -, ., must have a dot, no _ in last two segs */ |
| 1026 |
72 |
| D = at + 1; |
| 1027 |
72 |
| dl = gfm_scan_domain(D, end); |
| 1028 |
72 |
▶ | if (!dl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dl) return 0; |
12 |
60 |
72 |
|
| 1029 |
|
| /* Trailing punct stripping on domain. */ |
| 1030 |
60 |
| de = gfm_strip_trail(D, D + dl); |
| 1031 |
60 |
| dl = (size_t)(de - D); |
| 1032 |
60 |
▶ | if (!dl) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dl) return 0; |
0 |
60 |
60 |
|
| 1033 |
60 |
| *l_out = (size_t)(at - L); |
| 1034 |
60 |
| *d_out = dl; |
| 1035 |
60 |
| return 1; |
| 1036 |
|
| } |
| 1037 |
|
| |
| 1038 |
|
| /* Emit a chunk that may contain autolinks; non-autolink bytes go through |
| 1039 |
|
| * html_escape. Caller must have already handled li_check / image alt. */ |
| 1040 |
|
| /* Emit a chunk that may contain autolinks; non-autolink bytes go through |
| 1041 |
|
| * html_escape. Caller must have already handled li_check / image alt. */ |
| 1042 |
|
| static void gfm_emit_autolinked(pTHX_ render_state* st, |
| 1043 |
|
| const char* s, size_t n); |
| 1044 |
|
| |
| 1045 |
|
| /* Flush any buffered text accumulated from consecutive cb_text events. |
| 1046 |
|
| * Called by every non-text event so the AUTOLINK scanner sees the whole |
| 1047 |
|
| * contiguous text run (the inline tokenizer splits at intra-word `_`/`*`). */ |
| 1048 |
16417593 |
| static void flush_pending_text(pTHX_ render_state* st) { |
| 1049 |
|
| size_t n; |
| 1050 |
16417593 |
▶ | if (st->pending_len == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->pending_len == 0) return; |
9911691 |
6505902 |
16417593 |
|
| 1051 |
6505902 |
| n = st->pending_len; |
| 1052 |
6505902 |
| st->pending_len = 0; /* clear first so re-entry is safe */ |
| 1053 |
6505902 |
| gfm_emit_autolinked(aTHX_ st, st->pending_text, n); |
| 1054 |
|
| } |
| 1055 |
|
| |
| 1056 |
6505902 |
| static void gfm_emit_autolinked(pTHX_ render_state* st, |
| 1057 |
|
| const char* s, size_t n) { |
| 1058 |
6505902 |
| mds_buf* b = st->buf; |
| 1059 |
6505902 |
| const char* end = s + n; |
| 1060 |
6505902 |
| const char* run = s; |
| 1061 |
6505902 |
| const char* p = s; |
| 1062 |
84065620 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
77559718 |
6505902 |
84065620 |
|
| 1063 |
|
| unsigned char c; |
| 1064 |
|
| int boundary; |
| 1065 |
|
| size_t url_len; |
| 1066 |
|
| size_t scheme_len; |
| 1067 |
|
| int is_email; |
| 1068 |
|
| const char* scheme_prefix; |
| 1069 |
|
| size_t dl; |
| 1070 |
|
| |
| 1071 |
77559718 |
| c = (unsigned char)*p; |
| 1072 |
77559718 |
▶ | boundary = (p == s) ? 1 : !gfm_word_byte((unsigned char)p[-1]); |
Branch: 2 branches
| Branch | True | False | Total |
| boundary = (p == s) ? 1 : !gfm_word_byte((unsigned char)p[-1]); |
71053816 |
6505902 |
77559718 |
| boundary = (p == s) ? 1 : !gfm_word_byte((unsigned char)p[-1]); |
11358439 |
59695377 |
71053816 |
|
| 1073 |
77559718 |
▶ | if (!boundary) { p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!boundary) { p++; continue; } |
59695377 |
17864341 |
77559718 |
|
| 1074 |
|
| /* http://, https://, ftp:// */ |
| 1075 |
17864341 |
| url_len = 0; scheme_len = 0; is_email = 0; |
| 1076 |
17864341 |
| scheme_prefix = NULL; |
| 1077 |
17864341 |
▶ | if (c == 'h' && (size_t)(end - p) >= 7 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == 'h' && (size_t)(end - p) >= 7 && |
2424124 |
15440217 |
17864341 |
| if (c == 'h' && (size_t)(end - p) >= 7 && |
81 |
2424043 |
2424124 |
|
| 1078 |
81 |
▶ | memcmp(p, "http://", 7) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p, "http://", 7) == 0) { |
35 |
46 |
81 |
|
| 1079 |
35 |
| scheme_len = 7; |
| 1080 |
35 |
| url_len = gfm_scan_url_body(p + 7, end); |
| 1081 |
17864306 |
▶ | } else if (c == 'h' && (size_t)(end - p) >= 8 && |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c == 'h' && (size_t)(end - p) >= 8 && |
2424089 |
15440217 |
17864306 |
| } else if (c == 'h' && (size_t)(end - p) >= 8 && |
40 |
2424049 |
2424089 |
|
| 1082 |
40 |
▶ | memcmp(p, "https://", 8) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p, "https://", 8) == 0) { |
6 |
34 |
40 |
|
| 1083 |
6 |
| scheme_len = 8; |
| 1084 |
6 |
| url_len = gfm_scan_url_body(p + 8, end); |
| 1085 |
17864300 |
▶ | } else if (c == 'f' && (size_t)(end - p) >= 6 && |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c == 'f' && (size_t)(end - p) >= 6 && |
43 |
17864257 |
17864300 |
| } else if (c == 'f' && (size_t)(end - p) >= 6 && |
16 |
27 |
43 |
|
| 1086 |
16 |
▶ | memcmp(p, "ftp://", 6) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p, "ftp://", 6) == 0) { |
0 |
16 |
16 |
|
| 1087 |
0 |
| scheme_len = 6; |
| 1088 |
0 |
| url_len = gfm_scan_url_body(p + 6, end); |
| 1089 |
17864300 |
▶ | } else if (c == 'w' && (size_t)(end - p) >= 4 && |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (c == 'w' && (size_t)(end - p) >= 4 && |
812038 |
17052262 |
17864300 |
| } else if (c == 'w' && (size_t)(end - p) >= 4 && |
812038 |
0 |
812038 |
|
| 1090 |
812038 |
▶ | memcmp(p, "www.", 4) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p, "www.", 4) == 0) { |
24 |
812014 |
812038 |
|
| 1091 |
24 |
| scheme_len = 0; |
| 1092 |
|
| /* www. variant: require at least one further dot in domain. */ |
| 1093 |
24 |
| dl = gfm_scan_domain(p, end); |
| 1094 |
24 |
▶ | if (dl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dl) { |
18 |
6 |
24 |
|
| 1095 |
|
| /* Optional path. */ |
| 1096 |
18 |
| const char* qq = p + dl; |
| 1097 |
39 |
▶ | while (qq < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (qq < end) { |
24 |
15 |
39 |
|
| 1098 |
24 |
| unsigned char x = (unsigned char)*qq; |
| 1099 |
24 |
▶ | if (x <= 0x20 || x == '<') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x <= 0x20 || x == '<') break; |
21 |
3 |
24 |
| if (x <= 0x20 || x == '<') break; |
21 |
0 |
21 |
|
| 1100 |
21 |
| qq++; |
| 1101 |
|
| } |
| 1102 |
18 |
| qq = gfm_strip_trail(p, qq); |
| 1103 |
18 |
| url_len = (size_t)(qq - p); |
| 1104 |
|
| } |
| 1105 |
24 |
▶ | if (url_len) scheme_prefix = "http://"; |
Branch: 1 branch
| Branch | True | False | Total |
| if (url_len) scheme_prefix = "http://"; |
18 |
6 |
24 |
|
| 1106 |
|
| } |
| 1107 |
17864341 |
▶ | if (scheme_len || scheme_prefix) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (scheme_len || scheme_prefix) { |
17864300 |
41 |
17864341 |
| if (scheme_len || scheme_prefix) { |
18 |
17864282 |
17864300 |
|
| 1108 |
59 |
▶ | if (url_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (url_len) { |
59 |
0 |
59 |
|
| 1109 |
59 |
▶ | size_t total = (scheme_prefix ? url_len : scheme_len + url_len); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t total = (scheme_prefix ? url_len : scheme_len + url_len); |
41 |
18 |
59 |
|
| 1110 |
|
| /* flush text before */ |
| 1111 |
59 |
▶ | if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
38 |
21 |
59 |
|
| 1112 |
|
| MDS_BUF_LIT(b, "<a href=\""); |
| 1113 |
59 |
▶ | if (scheme_prefix) mds_buf_write(aTHX_ b, scheme_prefix, strlen(scheme_prefix)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (scheme_prefix) mds_buf_write(aTHX_ b, scheme_prefix, strlen(scheme_prefix)); |
18 |
41 |
59 |
| if (scheme_prefix) mds_buf_write(aTHX_ b, scheme_prefix, strlen(scheme_prefix)); |
0 |
18 |
18 |
|
| 1114 |
59 |
| write_url_attr(aTHX_ b, p, total); |
| 1115 |
|
| MDS_BUF_LIT(b, "\">"); |
| 1116 |
59 |
| html_escape(aTHX_ b, p, total); |
| 1117 |
|
| MDS_BUF_LIT(b, "</a>"); |
| 1118 |
59 |
| p += total; |
| 1119 |
59 |
| run = p; |
| 1120 |
59 |
| continue; |
| 1121 |
|
| } |
| 1122 |
|
| } |
| 1123 |
|
| /* xmpp:user@host or mailto:user@host */ |
| 1124 |
17864282 |
▶ | if (c == 'x' && (size_t)(end - p) >= 5 && memcmp(p, "xmpp:", 5) == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == 'x' && (size_t)(end - p) >= 5 && memcmp(p, "xmpp:", 5) == 0) { |
45 |
17864237 |
17864282 |
| if (c == 'x' && (size_t)(end - p) >= 5 && memcmp(p, "xmpp:", 5) == 0) { |
22 |
23 |
45 |
| if (c == 'x' && (size_t)(end - p) >= 5 && memcmp(p, "xmpp:", 5) == 0) { |
12 |
10 |
22 |
|
| 1125 |
12 |
| const char* at = (const char*)memchr(p + 5, '@', (size_t)(end - p - 5)); |
| 1126 |
12 |
▶ | if (at) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (at) { |
12 |
0 |
12 |
|
| 1127 |
|
| size_t ll, dl; |
| 1128 |
12 |
▶ | if (gfm_scan_email_at(p + 5, at, end, &ll, &dl) && |
Branch: 1 branch
| Branch | True | False | Total |
| if (gfm_scan_email_at(p + 5, at, end, &ll, &dl) && |
12 |
0 |
12 |
|
| 1129 |
12 |
▶ | (const char*)at - ll == p + 5) { |
Branch: 1 branch
| Branch | True | False | Total |
| (const char*)at - ll == p + 5) { |
12 |
0 |
12 |
|
| 1130 |
12 |
| size_t total = 5 + ll + 1 + dl; |
| 1131 |
|
| /* xmpp: includes optional /path in the autolink. */ |
| 1132 |
12 |
| const char* q = p + total; |
| 1133 |
12 |
▶ | if (q < end && *q == '/') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < end && *q == '/') { |
9 |
3 |
12 |
| if (q < end && *q == '/') { |
6 |
3 |
9 |
|
| 1134 |
57 |
▶ | while (q < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q < end) { |
51 |
6 |
57 |
|
| 1135 |
51 |
| unsigned char x = (unsigned char)*q; |
| 1136 |
51 |
▶ | if (x <= 0x20 || x == '<') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (x <= 0x20 || x == '<') break; |
51 |
0 |
51 |
| if (x <= 0x20 || x == '<') break; |
51 |
0 |
51 |
|
| 1137 |
51 |
| q++; |
| 1138 |
|
| } |
| 1139 |
6 |
| q = gfm_strip_trail(p + total, q); |
| 1140 |
6 |
| total = (size_t)(q - p); |
| 1141 |
|
| } |
| 1142 |
12 |
▶ | if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
0 |
12 |
12 |
|
| 1143 |
|
| MDS_BUF_LIT(b, "<a href=\""); |
| 1144 |
12 |
| write_url_attr(aTHX_ b, p, total); |
| 1145 |
|
| MDS_BUF_LIT(b, "\">"); |
| 1146 |
12 |
| html_escape(aTHX_ b, p, total); |
| 1147 |
|
| MDS_BUF_LIT(b, "</a>"); |
| 1148 |
12 |
| p += total; |
| 1149 |
12 |
| run = p; |
| 1150 |
12 |
| continue; |
| 1151 |
|
| } |
| 1152 |
|
| } |
| 1153 |
|
| } |
| 1154 |
17864270 |
▶ | if (c == 'm' && (size_t)(end - p) >= 7 && memcmp(p, "mailto:", 7) == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == 'm' && (size_t)(end - p) >= 7 && memcmp(p, "mailto:", 7) == 0) { |
40 |
17864230 |
17864270 |
| if (c == 'm' && (size_t)(end - p) >= 7 && memcmp(p, "mailto:", 7) == 0) { |
37 |
3 |
40 |
| if (c == 'm' && (size_t)(end - p) >= 7 && memcmp(p, "mailto:", 7) == 0) { |
21 |
16 |
37 |
|
| 1155 |
21 |
| const char* at = (const char*)memchr(p + 7, '@', (size_t)(end - p - 7)); |
| 1156 |
21 |
▶ | if (at) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (at) { |
21 |
0 |
21 |
|
| 1157 |
|
| size_t ll, dl; |
| 1158 |
21 |
▶ | if (gfm_scan_email_at(p + 7, at, end, &ll, &dl) && |
Branch: 1 branch
| Branch | True | False | Total |
| if (gfm_scan_email_at(p + 7, at, end, &ll, &dl) && |
21 |
0 |
21 |
|
| 1159 |
21 |
▶ | (const char*)at - ll == p + 7) { |
Branch: 1 branch
| Branch | True | False | Total |
| (const char*)at - ll == p + 7) { |
21 |
0 |
21 |
|
| 1160 |
21 |
| size_t total = 7 + ll + 1 + dl; |
| 1161 |
21 |
▶ | if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
6 |
15 |
21 |
|
| 1162 |
|
| MDS_BUF_LIT(b, "<a href=\"mailto:"); |
| 1163 |
21 |
| write_url_attr(aTHX_ b, p + 7, total - 7); |
| 1164 |
|
| MDS_BUF_LIT(b, "\">"); |
| 1165 |
21 |
| html_escape(aTHX_ b, p, total); |
| 1166 |
|
| MDS_BUF_LIT(b, "</a>"); |
| 1167 |
21 |
| p += total; |
| 1168 |
21 |
| run = p; |
| 1169 |
21 |
| continue; |
| 1170 |
|
| } |
| 1171 |
|
| } |
| 1172 |
|
| } |
| 1173 |
|
| /* Bare email: look forward for '@', not just at this byte. */ |
| 1174 |
17864249 |
▶ | if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') { |
1616892 |
16247357 |
17864249 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') { |
808840 |
808052 |
1616892 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') { |
808816 |
24 |
808840 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') { |
808816 |
0 |
808816 |
| if (isalnum(c) || c == '.' || c == '_' || c == '+' || c == '-') { |
64 |
808752 |
808816 |
|
| 1175 |
|
| /* Find next '@' within this text run, bounded by whitespace. */ |
| 1176 |
17055497 |
| const char* q = p; |
| 1177 |
82448934 |
▶ | while (q < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q < end) { |
76751350 |
5697584 |
82448934 |
|
| 1178 |
76751350 |
| unsigned char x = (unsigned char)*q; |
| 1179 |
76751350 |
▶ | if (x == '@') break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (x == '@') break; |
39 |
76751311 |
76751350 |
|
| 1180 |
76751311 |
▶ | if (!(isalnum(x) || x == '.' || x == '_' || x == '+' || x == '-')) { |
Branch: 5 branches
| Branch | True | False | Total |
| if (!(isalnum(x) || x == '.' || x == '_' || x == '+' || x == '-')) { |
14590223 |
62161088 |
76751311 |
| if (!(isalnum(x) || x == '.' || x == '_' || x == '+' || x == '-')) { |
11358049 |
3232174 |
14590223 |
| if (!(isalnum(x) || x == '.' || x == '_' || x == '+' || x == '-')) { |
11358002 |
47 |
11358049 |
| if (!(isalnum(x) || x == '.' || x == '_' || x == '+' || x == '-')) { |
11357999 |
3 |
11358002 |
| if (!(isalnum(x) || x == '.' || x == '_' || x == '+' || x == '-')) { |
11357874 |
125 |
11357999 |
|
| 1181 |
11357874 |
| q = NULL; break; |
| 1182 |
|
| } |
| 1183 |
65393437 |
| q++; |
| 1184 |
|
| } |
| 1185 |
17055497 |
▶ | if (q && q < end && *q == '@') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (q && q < end && *q == '@') { |
5697623 |
11357874 |
17055497 |
| if (q && q < end && *q == '@') { |
39 |
5697584 |
5697623 |
| if (q && q < end && *q == '@') { |
39 |
0 |
39 |
|
| 1186 |
|
| size_t ll, dl; |
| 1187 |
39 |
▶ | if (gfm_scan_email_at(p, q, end, &ll, &dl) && |
Branch: 1 branch
| Branch | True | False | Total |
| if (gfm_scan_email_at(p, q, end, &ll, &dl) && |
27 |
12 |
39 |
|
| 1188 |
27 |
▶ | q - ll == p) { |
Branch: 1 branch
| Branch | True | False | Total |
| q - ll == p) { |
27 |
0 |
27 |
|
| 1189 |
27 |
| size_t total = ll + 1 + dl; |
| 1190 |
27 |
▶ | if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > run) html_escape(aTHX_ b, run, (size_t)(p - run)); |
9 |
18 |
27 |
|
| 1191 |
|
| MDS_BUF_LIT(b, "<a href=\"mailto:"); |
| 1192 |
27 |
| write_url_attr(aTHX_ b, p, total); |
| 1193 |
|
| MDS_BUF_LIT(b, "\">"); |
| 1194 |
27 |
| html_escape(aTHX_ b, p, total); |
| 1195 |
|
| MDS_BUF_LIT(b, "</a>"); |
| 1196 |
27 |
| p += total; |
| 1197 |
27 |
| run = p; |
| 1198 |
27 |
| continue; |
| 1199 |
|
| } |
| 1200 |
|
| } |
| 1201 |
|
| } |
| 1202 |
17864222 |
| p++; |
| 1203 |
|
| } |
| 1204 |
6505902 |
▶ | if (run < end) html_escape(aTHX_ b, run, (size_t)(end - run)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run < end) html_escape(aTHX_ b, run, (size_t)(end - run)); |
6505834 |
68 |
6505902 |
|
| 1205 |
6505902 |
| } |
| 1206 |
|
| |
| 1207 |
8126406 |
| static void cb_text(void* ud, const char* s, size_t n) { |
| 1208 |
8126406 |
| render_state* st = (render_state*)ud; |
| 1209 |
|
| dTHXa(st->my_perl); |
| 1210 |
8126406 |
▶ | if (st->fn_skip) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_skip) return; |
0 |
8126406 |
8126406 |
|
| 1211 |
8126406 |
▶ | if (st->li_check_pending) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->li_check_pending) { |
616 |
8125790 |
8126406 |
|
| 1212 |
|
| /* Inline tokenizer may emit `[` as its own text event, then the |
| 1213 |
|
| * remainder. Coalesce up to two text events to detect [ ]/[x]/[X] |
| 1214 |
|
| * followed by space. State 1 = fresh LI; state 2 = saw lone `[`. */ |
| 1215 |
616 |
▶ | if (st->li_check_pending == 1 && n == 1 && s[0] == '[') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (st->li_check_pending == 1 && n == 1 && s[0] == '[') { |
556 |
60 |
616 |
| if (st->li_check_pending == 1 && n == 1 && s[0] == '[') { |
205 |
351 |
556 |
| if (st->li_check_pending == 1 && n == 1 && s[0] == '[') { |
60 |
145 |
205 |
|
| 1216 |
60 |
| st->li_check_pending = 2; |
| 1217 |
60 |
| return; |
| 1218 |
|
| } |
| 1219 |
556 |
▶ | if (st->li_check_pending == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->li_check_pending == 2) { |
60 |
496 |
556 |
|
| 1220 |
60 |
| st->li_check_pending = 0; |
| 1221 |
60 |
▶ | if (n >= 3 && s[1] == ']' && s[2] == ' ' && |
Branch: 3 branches
| Branch | True | False | Total |
| if (n >= 3 && s[1] == ']' && s[2] == ' ' && |
60 |
0 |
60 |
| if (n >= 3 && s[1] == ']' && s[2] == ' ' && |
60 |
0 |
60 |
| if (n >= 3 && s[1] == ']' && s[2] == ' ' && |
60 |
0 |
60 |
|
| 1222 |
60 |
▶ | (s[0] == ' ' || s[0] == 'x' || s[0] == 'X')) { |
Branch: 3 branches
| Branch | True | False | Total |
| (s[0] == ' ' || s[0] == 'x' || s[0] == 'X')) { |
41 |
19 |
60 |
| (s[0] == ' ' || s[0] == 'x' || s[0] == 'X')) { |
24 |
17 |
41 |
| (s[0] == ' ' || s[0] == 'x' || s[0] == 'X')) { |
0 |
24 |
24 |
|
| 1223 |
36 |
▶ | if (s[0] == ' ') |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[0] == ' ') |
19 |
17 |
36 |
|
| 1224 |
19 |
▶ | MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" disabled=\"\" /> "); |
Branch: 1 branch
| Branch | True | False | Total |
| MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" disabled=\"\" /> "); |
0 |
19 |
19 |
|
| 1225 |
|
| else |
| 1226 |
17 |
▶ | MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" checked=\"\" disabled=\"\" /> "); |
Branch: 1 branch
| Branch | True | False | Total |
| MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" checked=\"\" disabled=\"\" /> "); |
4 |
13 |
17 |
|
| 1227 |
36 |
| s += 3; n -= 3; |
| 1228 |
36 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
36 |
36 |
|
| 1229 |
|
| } else { |
| 1230 |
|
| /* Not a task marker — flush buffered `[` then fall through. */ |
| 1231 |
24 |
▶ | MDS_BUF_LIT(st->buf, "["); |
Branch: 1 branch
| Branch | True | False | Total |
| MDS_BUF_LIT(st->buf, "["); |
0 |
24 |
24 |
|
| 1232 |
|
| } |
| 1233 |
|
| } else { |
| 1234 |
496 |
| st->li_check_pending = 0; |
| 1235 |
496 |
▶ | if (n >= 4 && s[0] == '[' && s[2] == ']' && s[3] == ' ' && |
Branch: 4 branches
| Branch | True | False | Total |
| if (n >= 4 && s[0] == '[' && s[2] == ']' && s[3] == ' ' && |
86 |
410 |
496 |
| if (n >= 4 && s[0] == '[' && s[2] == ']' && s[3] == ' ' && |
0 |
86 |
86 |
| if (n >= 4 && s[0] == '[' && s[2] == ']' && s[3] == ' ' && |
0 |
0 |
0 |
| if (n >= 4 && s[0] == '[' && s[2] == ']' && s[3] == ' ' && |
0 |
0 |
0 |
|
| 1236 |
0 |
▶ | (s[1] == ' ' || s[1] == 'x' || s[1] == 'X')) { |
Branch: 3 branches
| Branch | True | False | Total |
| (s[1] == ' ' || s[1] == 'x' || s[1] == 'X')) { |
0 |
0 |
0 |
| (s[1] == ' ' || s[1] == 'x' || s[1] == 'X')) { |
0 |
0 |
0 |
| (s[1] == ' ' || s[1] == 'x' || s[1] == 'X')) { |
0 |
0 |
0 |
|
| 1237 |
0 |
▶ | if (s[1] == ' ') |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[1] == ' ') |
0 |
0 |
0 |
|
| 1238 |
0 |
▶ | MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" disabled=\"\" /> "); |
Branch: 1 branch
| Branch | True | False | Total |
| MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" disabled=\"\" /> "); |
0 |
0 |
0 |
|
| 1239 |
|
| else |
| 1240 |
0 |
▶ | MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" disabled=\"\" checked=\"\" /> "); |
Branch: 1 branch
| Branch | True | False | Total |
| MDS_BUF_LIT(st->buf, "<input type=\"checkbox\" disabled=\"\" checked=\"\" /> "); |
0 |
0 |
0 |
|
| 1241 |
0 |
| s += 4; n -= 4; |
| 1242 |
0 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
0 |
0 |
|
| 1243 |
|
| } |
| 1244 |
|
| } |
| 1245 |
|
| } |
| 1246 |
|
| /* Capture the heading's plain text before any of the branches below can |
| 1247 |
|
| * divert or return. Placed after the task-list check so a `[ ] ` marker |
| 1248 |
|
| * never reaches the slug, and before the image branch so alt text does, |
| 1249 |
|
| * which is what GitHub slugs for a heading that is just an image. */ |
| 1250 |
8126346 |
▶ | if (st->in_heading) hd_append(st, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->in_heading) hd_append(st, s, n); |
808063 |
7318283 |
8126346 |
|
| 1251 |
|
| |
| 1252 |
8126346 |
▶ | if (st->image_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->image_depth > 0) { |
808101 |
7318245 |
8126346 |
|
| 1253 |
|
| /* For alt: append raw bytes (no HTML escape — done later when |
| 1254 |
|
| * emitting alt="..." attribute via html_escape on accumulated). */ |
| 1255 |
808101 |
| alt_append(st, s, n); |
| 1256 |
808101 |
| return; |
| 1257 |
|
| } |
| 1258 |
7318245 |
▶ | if (st->in_fenced_hl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->in_fenced_hl) { |
808023 |
6510222 |
7318245 |
|
| 1259 |
|
| /* Accumulate raw bytes for later syntax-highlighting at leave time. */ |
| 1260 |
808023 |
| size_t need = st->hl_len + n; |
| 1261 |
808023 |
▶ | if (need > st->hl_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (need > st->hl_cap) { |
40422 |
767601 |
808023 |
|
| 1262 |
40422 |
▶ | size_t nc = st->hl_cap ? st->hl_cap : 256; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = st->hl_cap ? st->hl_cap : 256; |
0 |
40422 |
40422 |
|
| 1263 |
|
| char* np; |
| 1264 |
40422 |
▶ | while (nc < need) nc *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (nc < need) nc *= 2; |
0 |
40422 |
40422 |
|
| 1265 |
40422 |
| np = (char*)realloc(st->hl_buf, nc); |
| 1266 |
40422 |
▶ | if (np) { st->hl_buf = np; st->hl_cap = nc; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (np) { st->hl_buf = np; st->hl_cap = nc; } |
40422 |
0 |
40422 |
|
| 1267 |
0 |
| else { html_escape(aTHX_ st->buf, s, n); return; } /* OOM fallback */ |
| 1268 |
|
| } |
| 1269 |
808023 |
| memcpy(st->hl_buf + st->hl_len, s, n); |
| 1270 |
808023 |
| st->hl_len += n; |
| 1271 |
808023 |
| return; |
| 1272 |
|
| } |
| 1273 |
6510222 |
▶ | if (st->flags & MDS_FLAG_AUTOLINK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->flags & MDS_FLAG_AUTOLINK) { |
6506023 |
4199 |
6510222 |
|
| 1274 |
|
| /* Accumulate; flushed by any non-text callback. */ |
| 1275 |
6506023 |
| size_t need = st->pending_len + n; |
| 1276 |
6506023 |
▶ | if (need > st->pending_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (need > st->pending_cap) { |
121689 |
6384334 |
6506023 |
|
| 1277 |
|
| size_t nc; |
| 1278 |
|
| char* np; |
| 1279 |
121689 |
▶ | nc = st->pending_cap ? st->pending_cap : 64; |
Branch: 1 branch
| Branch | True | False | Total |
| nc = st->pending_cap ? st->pending_cap : 64; |
9 |
121680 |
121689 |
|
| 1280 |
121707 |
▶ | while (nc < need) nc *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (nc < need) nc *= 2; |
18 |
121689 |
121707 |
|
| 1281 |
121689 |
| np = (char*)realloc(st->pending_text, nc); |
| 1282 |
121689 |
▶ | if (!np) { /* OOM: bypass coalescing for this chunk */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!np) { /* OOM: bypass coalescing for this chunk */ |
0 |
121689 |
121689 |
|
| 1283 |
0 |
| flush_pending_text(aTHX_ st); |
| 1284 |
0 |
| gfm_emit_autolinked(aTHX_ st, s, n); |
| 1285 |
0 |
| return; |
| 1286 |
|
| } |
| 1287 |
121689 |
| st->pending_text = np; |
| 1288 |
121689 |
| st->pending_cap = nc; |
| 1289 |
|
| } |
| 1290 |
6506023 |
| memcpy(st->pending_text + st->pending_len, s, n); |
| 1291 |
6506023 |
| st->pending_len += n; |
| 1292 |
6506023 |
| return; |
| 1293 |
|
| } |
| 1294 |
4199 |
| html_escape(aTHX_ st->buf, s, n); |
| 1295 |
|
| } |
| 1296 |
|
| |
| 1297 |
339 |
| static void cb_raw(void* ud, const char* s, size_t n) { |
| 1298 |
339 |
| render_state* st = (render_state*)ud; |
| 1299 |
|
| dTHXa(st->my_perl); |
| 1300 |
339 |
▶ | if (st->fn_skip) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_skip) return; |
0 |
339 |
339 |
|
| 1301 |
339 |
| flush_pending_text(aTHX_ st); |
| 1302 |
339 |
▶ | if (st->image_depth > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->image_depth > 0) { |
0 |
339 |
339 |
|
| 1303 |
|
| /* raw HTML inside alt: stripped to text per spec; just append as text */ |
| 1304 |
0 |
| alt_append(st, s, n); |
| 1305 |
0 |
| return; |
| 1306 |
|
| } |
| 1307 |
339 |
▶ | if (st->flags & MDS_FLAG_DISALLOW_RAW_HTML) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->flags & MDS_FLAG_DISALLOW_RAW_HTML) { |
57 |
282 |
339 |
|
| 1308 |
|
| /* GFM §6.11: rewrite the opening `<` of these tags as `<`. */ |
| 1309 |
|
| static const char* const banned[] = { |
| 1310 |
|
| "title", "textarea", "style", "xmp", "iframe", |
| 1311 |
|
| "noembed", "noframes", "script", "plaintext", NULL |
| 1312 |
|
| }; |
| 1313 |
57 |
| const char* run = s; |
| 1314 |
57 |
| const char* end = s + n; |
| 1315 |
|
| const char* p; |
| 1316 |
|
| const char* q; |
| 1317 |
|
| const char* name; |
| 1318 |
|
| size_t nlen; |
| 1319 |
|
| int hit; |
| 1320 |
|
| int i; |
| 1321 |
|
| size_t bl; |
| 1322 |
1578 |
▶ | for (p = s; p < end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = s; p < end; p++) { |
1521 |
57 |
1578 |
|
| 1323 |
1521 |
▶ | if (*p != '<') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p != '<') continue; |
1431 |
90 |
1521 |
|
| 1324 |
90 |
| q = p + 1; |
| 1325 |
90 |
▶ | if (q < end && *q == '/') q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < end && *q == '/') q++; |
90 |
0 |
90 |
| if (q < end && *q == '/') q++; |
28 |
62 |
90 |
|
| 1326 |
90 |
| name = q; |
| 1327 |
420 |
▶ | while (q < end && isalnum((unsigned char)*q)) q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < end && isalnum((unsigned char)*q)) q++; |
420 |
0 |
420 |
| while (q < end && isalnum((unsigned char)*q)) q++; |
330 |
90 |
420 |
|
| 1328 |
90 |
| nlen = (size_t)(q - name); |
| 1329 |
90 |
▶ | if (nlen == 0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlen == 0) continue; |
16 |
74 |
90 |
|
| 1330 |
74 |
| hit = 0; |
| 1331 |
648 |
▶ | for (i = 0; banned[i]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; banned[i]; i++) { |
590 |
58 |
648 |
|
| 1332 |
590 |
| bl = strlen(banned[i]); |
| 1333 |
590 |
▶ | if (nlen == bl && strncasecmp(name, banned[i], bl) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nlen == bl && strncasecmp(name, banned[i], bl) == 0) { |
86 |
504 |
590 |
| if (nlen == bl && strncasecmp(name, banned[i], bl) == 0) { |
16 |
70 |
86 |
|
| 1334 |
16 |
| hit = 1; break; |
| 1335 |
|
| } |
| 1336 |
|
| } |
| 1337 |
74 |
▶ | if (!hit) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hit) continue; |
58 |
16 |
74 |
|
| 1338 |
16 |
▶ | if (p > run) mds_buf_write(aTHX_ st->buf, run, (size_t)(p - run)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (p > run) mds_buf_write(aTHX_ st->buf, run, (size_t)(p - run)); |
8 |
8 |
16 |
| if (p > run) mds_buf_write(aTHX_ st->buf, run, (size_t)(p - run)); |
0 |
8 |
8 |
|
| 1339 |
16 |
▶ | mds_buf_write(aTHX_ st->buf, "<", 4); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ st->buf, "<", 4); |
0 |
16 |
16 |
|
| 1340 |
16 |
| run = p + 1; |
| 1341 |
|
| } |
| 1342 |
57 |
▶ | if (run < end) mds_buf_write(aTHX_ st->buf, run, (size_t)(end - run)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (run < end) mds_buf_write(aTHX_ st->buf, run, (size_t)(end - run)); |
57 |
0 |
57 |
| if (run < end) mds_buf_write(aTHX_ st->buf, run, (size_t)(end - run)); |
0 |
57 |
57 |
|
| 1343 |
57 |
| return; |
| 1344 |
|
| } |
| 1345 |
282 |
▶ | mds_buf_write(aTHX_ st->buf, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| mds_buf_write(aTHX_ st->buf, s, n); |
0 |
282 |
282 |
|
| 1346 |
|
| } |
| 1347 |
|
| |
| 1348 |
|
| /* ------------------ inline callbacks ------------------ */ |
| 1349 |
|
| |
| 1350 |
1616588 |
| static void write_url_attr_impl(pTHX_ mds_buf* b, const char* s, size_t n, int unesc) { |
| 1351 |
|
| /* Percent-escape URL: leave already-safe chars; escape <>"'`{}|\ ^ and ws. |
| 1352 |
|
| * Then HTML-escape & to &. When unesc=1, also unescape \X for ASCII |
| 1353 |
|
| * punctuation (§6.1 — backslash escapes work in link destinations and |
| 1354 |
|
| * titles, but NOT in autolinks). Entities (named + numeric) are expanded |
| 1355 |
|
| * before percent-encoding so the resulting UTF-8 bytes are escaped. */ |
| 1356 |
|
| static const char hex[] = "0123456789ABCDEF"; |
| 1357 |
|
| size_t i; |
| 1358 |
38790683 |
▶ | for (i = 0; i < n; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ) { |
37174095 |
1616588 |
38790683 |
|
| 1359 |
37174095 |
| unsigned char c = (unsigned char)s[i]; |
| 1360 |
37174095 |
▶ | if (unesc && c == '\\' && i + 1 < n) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (unesc && c == '\\' && i + 1 < n) { |
37172803 |
1292 |
37174095 |
| if (unesc && c == '\\' && i + 1 < n) { |
36 |
37172767 |
37172803 |
| if (unesc && c == '\\' && i + 1 < n) { |
36 |
0 |
36 |
|
| 1361 |
36 |
| unsigned char nx = (unsigned char)s[i+1]; |
| 1362 |
36 |
▶ | if ((nx >= 0x21 && nx <= 0x2F) || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((nx >= 0x21 && nx <= 0x2F) || |
36 |
0 |
36 |
| if ((nx >= 0x21 && nx <= 0x2F) || |
9 |
27 |
36 |
| if ((nx >= 0x21 && nx <= 0x2F) || |
9 |
0 |
9 |
|
| 1363 |
9 |
▶ | (nx >= 0x3A && nx <= 0x40) || |
Branch: 2 branches
| Branch | True | False | Total |
| (nx >= 0x3A && nx <= 0x40) || |
6 |
3 |
9 |
| (nx >= 0x3A && nx <= 0x40) || |
6 |
0 |
6 |
|
| 1364 |
6 |
▶ | (nx >= 0x5B && nx <= 0x60) || |
Branch: 2 branches
| Branch | True | False | Total |
| (nx >= 0x5B && nx <= 0x60) || |
6 |
0 |
6 |
| (nx >= 0x5B && nx <= 0x60) || |
0 |
6 |
6 |
|
| 1365 |
0 |
▶ | (nx >= 0x7B && nx <= 0x7E)) { |
Branch: 1 branch
| Branch | True | False | Total |
| (nx >= 0x7B && nx <= 0x7E)) { |
0 |
0 |
0 |
|
| 1366 |
30 |
| c = nx; |
| 1367 |
30 |
| i += 2; |
| 1368 |
30 |
| goto emit_byte; |
| 1369 |
|
| } |
| 1370 |
|
| } |
| 1371 |
37174065 |
▶ | if (c == '&') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '&') { |
21 |
37174044 |
37174065 |
|
| 1372 |
|
| char ebuf[8]; size_t elen; |
| 1373 |
21 |
| size_t k = mds_expand_entity_at(s, i, n, ebuf, &elen); |
| 1374 |
21 |
▶ | if (k) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k) { |
15 |
6 |
21 |
|
| 1375 |
|
| /* Re-feed decoded bytes through the same loop logic, so |
| 1376 |
|
| * non-ASCII gets percent-encoded and '&' itself becomes |
| 1377 |
|
| * & (CommonMark expects e.g. & → %26? No, spec |
| 1378 |
|
| * says ENT decoded to '&' renders as & in href, and |
| 1379 |
|
| * ä → ä → %C3%A4). */ |
| 1380 |
|
| size_t z; |
| 1381 |
45 |
▶ | for (z = 0; z < elen; z++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (z = 0; z < elen; z++) { |
30 |
15 |
45 |
|
| 1382 |
30 |
| unsigned char ec = (unsigned char)ebuf[z]; |
| 1383 |
60 |
▶ | if (ec == '&') { mds_buf_write(aTHX_ b, "&", 5); continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ec == '&') { mds_buf_write(aTHX_ b, "&", 5); continue; } |
0 |
30 |
30 |
|
| 1384 |
30 |
▶ | if (ec <= 0x20 || ec == 0x7f || |
Branch: 2 branches
| Branch | True | False | Total |
| if (ec <= 0x20 || ec == 0x7f || |
30 |
0 |
30 |
| if (ec <= 0x20 || ec == 0x7f || |
30 |
0 |
30 |
|
| 1385 |
30 |
▶ | ec >= 0x80 || |
Branch: 1 branch
| Branch | True | False | Total |
| ec >= 0x80 || |
0 |
30 |
30 |
|
| 1386 |
0 |
▶ | ec == '"' || ec == '<' || ec == '>' || ec == '`' || |
Branch: 4 branches
| Branch | True | False | Total |
| ec == '"' || ec == '<' || ec == '>' || ec == '`' || |
0 |
0 |
0 |
| ec == '"' || ec == '<' || ec == '>' || ec == '`' || |
0 |
0 |
0 |
| ec == '"' || ec == '<' || ec == '>' || ec == '`' || |
0 |
0 |
0 |
| ec == '"' || ec == '<' || ec == '>' || ec == '`' || |
0 |
0 |
0 |
|
| 1387 |
0 |
▶ | ec == '{' || ec == '}' || ec == '|' || ec == '\\' || |
Branch: 4 branches
| Branch | True | False | Total |
| ec == '{' || ec == '}' || ec == '|' || ec == '\\' || |
0 |
0 |
0 |
| ec == '{' || ec == '}' || ec == '|' || ec == '\\' || |
0 |
0 |
0 |
| ec == '{' || ec == '}' || ec == '|' || ec == '\\' || |
0 |
0 |
0 |
| ec == '{' || ec == '}' || ec == '|' || ec == '\\' || |
0 |
0 |
0 |
|
| 1388 |
0 |
▶ | ec == '^' || ec == '[' || ec == ']') { |
Branch: 3 branches
| Branch | True | False | Total |
| ec == '^' || ec == '[' || ec == ']') { |
0 |
0 |
0 |
| ec == '^' || ec == '[' || ec == ']') { |
0 |
0 |
0 |
| ec == '^' || ec == '[' || ec == ']') { |
0 |
0 |
0 |
|
| 1389 |
30 |
▶ | char esc[3] = { '%', hex[ec >> 4], hex[ec & 0xF] }; |
Branch: 1 branch
| Branch | True | False | Total |
| char esc[3] = { '%', hex[ec >> 4], hex[ec & 0xF] }; |
0 |
30 |
30 |
|
| 1390 |
|
| mds_buf_write(aTHX_ b, esc, 3); |
| 1391 |
30 |
| continue; |
| 1392 |
|
| } |
| 1393 |
|
| mds_buf_write(aTHX_ b, (const char*)&ec, 1); |
| 1394 |
|
| } |
| 1395 |
15 |
| i += k; |
| 1396 |
15 |
| continue; |
| 1397 |
|
| } |
| 1398 |
|
| mds_buf_write(aTHX_ b, "&", 5); |
| 1399 |
6 |
| i++; |
| 1400 |
6 |
| continue; |
| 1401 |
|
| } |
| 1402 |
37174044 |
| i++; |
| 1403 |
37174074 |
| emit_byte: |
| 1404 |
37174074 |
▶ | if (c <= 0x20 || c == 0x7f || c >= 0x80 || |
Branch: 3 branches
| Branch | True | False | Total |
| if (c <= 0x20 || c == 0x7f || c >= 0x80 || |
37174068 |
6 |
37174074 |
| if (c <= 0x20 || c == 0x7f || c >= 0x80 || |
37174068 |
0 |
37174068 |
| if (c <= 0x20 || c == 0x7f || c >= 0x80 || |
37174002 |
66 |
37174068 |
|
| 1405 |
37174002 |
▶ | c == '"' || c == '<' || c == '>' || c == '`' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '"' || c == '<' || c == '>' || c == '`' || |
37173990 |
12 |
37174002 |
| c == '"' || c == '<' || c == '>' || c == '`' || |
37173990 |
0 |
37173990 |
| c == '"' || c == '<' || c == '>' || c == '`' || |
37173990 |
0 |
37173990 |
| c == '"' || c == '<' || c == '>' || c == '`' || |
37173987 |
3 |
37173990 |
|
| 1406 |
37173987 |
▶ | c == '{' || c == '}' || c == '|' || c == '\\' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '{' || c == '}' || c == '|' || c == '\\' || |
37173987 |
0 |
37173987 |
| c == '{' || c == '}' || c == '|' || c == '\\' || |
37173987 |
0 |
37173987 |
| c == '{' || c == '}' || c == '|' || c == '\\' || |
37173987 |
0 |
37173987 |
| c == '{' || c == '}' || c == '|' || c == '\\' || |
37173972 |
15 |
37173987 |
|
| 1407 |
37173972 |
▶ | c == '^' || c == '[' || c == ']') { |
Branch: 3 branches
| Branch | True | False | Total |
| c == '^' || c == '[' || c == ']') { |
37173972 |
0 |
37173972 |
| c == '^' || c == '[' || c == ']') { |
37173966 |
6 |
37173972 |
| c == '^' || c == '[' || c == ']') { |
9 |
37173957 |
37173966 |
|
| 1408 |
117 |
▶ | char esc[3] = { '%', hex[c >> 4], hex[c & 0xF] }; |
Branch: 1 branch
| Branch | True | False | Total |
| char esc[3] = { '%', hex[c >> 4], hex[c & 0xF] }; |
0 |
117 |
117 |
|
| 1409 |
|
| mds_buf_write(aTHX_ b, esc, 3); |
| 1410 |
117 |
| continue; |
| 1411 |
|
| } |
| 1412 |
|
| mds_buf_write(aTHX_ b, (const char*)&c, 1); |
| 1413 |
|
| } |
| 1414 |
1616588 |
| } |
| 1415 |
|
| |
| 1416 |
1616531 |
| static void write_url_attr(pTHX_ mds_buf* b, const char* s, size_t n) { |
| 1417 |
1616531 |
| write_url_attr_impl(aTHX_ b, s, n, /*unesc=*/1); |
| 1418 |
1616531 |
| } |
| 1419 |
57 |
| static void write_url_attr_raw(pTHX_ mds_buf* b, const char* s, size_t n) { |
| 1420 |
57 |
| write_url_attr_impl(aTHX_ b, s, n, /*unesc=*/0); |
| 1421 |
57 |
| } |
| 1422 |
|
| |
| 1423 |
3213552 |
| static void cb_enter_inline(void* ud, mds_inline_type t, const mds_inline_detail* d) { |
| 1424 |
3213552 |
| render_state* st = (render_state*)ud; |
| 1425 |
|
| dTHXa(st->my_perl); |
| 1426 |
3213552 |
| mds_buf* b = st->buf; |
| 1427 |
3213552 |
▶ | if (st->fn_skip) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_skip) return; |
0 |
3213552 |
3213552 |
|
| 1428 |
3213552 |
| flush_pending_text(aTHX_ st); |
| 1429 |
|
| /* If the LIST_ITEM had a lone `[` buffered, flush it before this inline. */ |
| 1430 |
3213552 |
▶ | if (st->li_check_pending == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->li_check_pending == 2) { |
0 |
3213552 |
3213552 |
|
| 1431 |
|
| MDS_BUF_LIT(b, "["); |
| 1432 |
0 |
| st->li_check_pending = 0; |
| 1433 |
3213552 |
▶ | } else if (st->li_check_pending && t != MDS_INL_TEXT) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (st->li_check_pending && t != MDS_INL_TEXT) { |
6 |
3213546 |
3213552 |
| } else if (st->li_check_pending && t != MDS_INL_TEXT) { |
6 |
0 |
6 |
|
| 1434 |
6 |
| st->li_check_pending = 0; |
| 1435 |
|
| } |
| 1436 |
|
| /* Inside an image: alt text is plain text per CommonMark. Suppress all |
| 1437 |
|
| * markup wrappers; child TEXT/RAW bytes are routed through alt_append. */ |
| 1438 |
3213552 |
▶ | if (st->image_depth > 0 && t != MDS_INL_IMAGE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->image_depth > 0 && t != MDS_INL_IMAGE) { |
24 |
3213528 |
3213552 |
| if (st->image_depth > 0 && t != MDS_INL_IMAGE) { |
21 |
3 |
24 |
|
| 1439 |
21 |
| return; |
| 1440 |
|
| } |
| 1441 |
3213531 |
| switch (t) { |
| 1442 |
656 |
▶ | case MDS_INL_EMPH: MDS_BUF_LIT(b, "<em>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_EMPH: MDS_BUF_LIT(b, "<em>"); break; |
0 |
328 |
328 |
|
| 1443 |
480 |
▶ | case MDS_INL_STRONG: MDS_BUF_LIT(b, "<strong>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_STRONG: MDS_BUF_LIT(b, "<strong>"); break; |
0 |
240 |
240 |
|
| 1444 |
52 |
▶ | case MDS_INL_STRIKE: MDS_BUF_LIT(b, "<del>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_STRIKE: MDS_BUF_LIT(b, "<del>"); break; |
0 |
26 |
26 |
|
| 1445 |
292 |
▶ | case MDS_INL_CODE: MDS_BUF_LIT(b, "<code>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_CODE: MDS_BUF_LIT(b, "<code>"); break; |
3 |
143 |
146 |
|
| 1446 |
788124 |
| case MDS_INL_SOFTBREAK: |
| 1447 |
788124 |
▶ | if (st->flags & MDS_FLAG_HARD_BREAKS) MDS_BUF_LIT(b, "<br />\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->flags & MDS_FLAG_HARD_BREAKS) MDS_BUF_LIT(b, "<br />\n"); |
1 |
788123 |
788124 |
|
| 1448 |
|
| else MDS_BUF_LIT(b, "\n"); |
| 1449 |
788124 |
| break; |
| 1450 |
58 |
▶ | case MDS_INL_LINEBREAK: MDS_BUF_LIT(b, "<br />\n"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_LINEBREAK: MDS_BUF_LIT(b, "<br />\n"); break; |
0 |
29 |
29 |
|
| 1451 |
808338 |
▶ | case MDS_INL_LINK: { |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_LINK: { |
0 |
808338 |
808338 |
|
| 1452 |
|
| MDS_BUF_LIT(b, "<a href=\""); |
| 1453 |
1616676 |
| if ((st->flags & MDS_FLAG_UNSAFE) || |
| 1454 |
808338 |
| !url_is_dangerous(d->u.link.href, d->u.link.href_len, 0)) { |
| 1455 |
808335 |
| write_url_attr(aTHX_ b, d->u.link.href, d->u.link.href_len); |
| 1456 |
|
| } |
| 1457 |
|
| MDS_BUF_LIT(b, "\""); |
| 1458 |
808338 |
▶ | if (d->u.link.title_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->u.link.title_len) { |
100 |
808238 |
808338 |
|
| 1459 |
|
| MDS_BUF_LIT(b, " title=\""); |
| 1460 |
100 |
| html_escape_unesc(aTHX_ b, d->u.link.title, d->u.link.title_len); |
| 1461 |
|
| MDS_BUF_LIT(b, "\""); |
| 1462 |
|
| } |
| 1463 |
|
| MDS_BUF_LIT(b, ">"); |
| 1464 |
808338 |
| break; |
| 1465 |
|
| } |
| 1466 |
808080 |
| case MDS_INL_IMAGE: { |
| 1467 |
808080 |
▶ | if (st->image_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->image_depth == 0) { |
808077 |
3 |
808080 |
|
| 1468 |
808077 |
| st->alt_len = 0; |
| 1469 |
|
| } |
| 1470 |
808080 |
| st->image_depth++; |
| 1471 |
808080 |
▶ | if (g_img_stack_top < IMG_STACK_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (g_img_stack_top < IMG_STACK_MAX) { |
808080 |
0 |
808080 |
|
| 1472 |
808080 |
| g_img_stack[g_img_stack_top].href = d->u.image.href; |
| 1473 |
808080 |
| g_img_stack[g_img_stack_top].hlen = d->u.image.href_len; |
| 1474 |
808080 |
| g_img_stack[g_img_stack_top].title = d->u.image.title; |
| 1475 |
808080 |
| g_img_stack[g_img_stack_top].tlen = d->u.image.title_len; |
| 1476 |
808080 |
| g_img_stack_top++; |
| 1477 |
|
| } |
| 1478 |
808080 |
| break; |
| 1479 |
|
| } |
| 1480 |
57 |
▶ | case MDS_INL_AUTOLINK: { |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_AUTOLINK: { |
0 |
57 |
57 |
|
| 1481 |
|
| MDS_BUF_LIT(b, "<a href=\""); |
| 1482 |
57 |
▶ | if (d->u.autolink.is_email) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (d->u.autolink.is_email) { |
7 |
50 |
57 |
|
| 1483 |
|
| MDS_BUF_LIT(b, "mailto:"); |
| 1484 |
|
| } |
| 1485 |
114 |
| if ((st->flags & MDS_FLAG_UNSAFE) || |
| 1486 |
57 |
| !url_is_dangerous(d->u.autolink.uri, d->u.autolink.uri_len, 0)) { |
| 1487 |
57 |
| write_url_attr_raw(aTHX_ b, d->u.autolink.uri, d->u.autolink.uri_len); |
| 1488 |
|
| } |
| 1489 |
|
| MDS_BUF_LIT(b, "\">"); |
| 1490 |
57 |
| html_escape(aTHX_ b, d->u.autolink.uri, d->u.autolink.uri_len); |
| 1491 |
57 |
| break; |
| 1492 |
|
| } |
| 1493 |
139 |
| case MDS_INL_HTML_INLINE: |
| 1494 |
|
| /* opens nothing; children come via cb_raw */ |
| 1495 |
139 |
| break; |
| 1496 |
808024 |
| case MDS_INL_FOOTNOTE_REF: { |
| 1497 |
|
| const char* lab; |
| 1498 |
|
| size_t ll; |
| 1499 |
|
| unsigned idx; |
| 1500 |
|
| unsigned uses; |
| 1501 |
|
| char ibuf[16]; |
| 1502 |
|
| int in; |
| 1503 |
|
| char nbuf[16]; |
| 1504 |
|
| int nn; |
| 1505 |
|
| |
| 1506 |
808024 |
| lab = d->u.footnote_ref.label; |
| 1507 |
808024 |
| ll = d->u.footnote_ref.label_len; |
| 1508 |
808024 |
| idx = fn_register(st, lab, ll); |
| 1509 |
808024 |
▶ | uses = st->fn_uses[idx - 1]; |
Branch: 1 branch
| Branch | True | False | Total |
| uses = st->fn_uses[idx - 1]; |
20200 |
787824 |
808024 |
|
| 1510 |
|
| MDS_BUF_LIT(b, "<sup class=\"footnote-ref\"><a href=\"#fn-"); |
| 1511 |
808024 |
| write_fn_label_attr(aTHX_ b, lab, ll); |
| 1512 |
|
| MDS_BUF_LIT(b, "\" id=\"fnref-"); |
| 1513 |
808024 |
| write_fn_label_attr(aTHX_ b, lab, ll); |
| 1514 |
808024 |
▶ | if (uses > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uses > 1) { |
767606 |
40418 |
808024 |
|
| 1515 |
767606 |
| nn = snprintf(nbuf, sizeof nbuf, "-%u", uses); |
| 1516 |
767606 |
▶ | if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
Branch: 2 branches
| Branch | True | False | Total |
| if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
767606 |
0 |
767606 |
| if (nn > 0) mds_buf_write(aTHX_ b, nbuf, (size_t)nn); |
0 |
767606 |
767606 |
|
| 1517 |
|
| } |
| 1518 |
|
| MDS_BUF_LIT(b, "\" data-footnote-ref>"); |
| 1519 |
808024 |
| in = snprintf(ibuf, sizeof ibuf, "%u", idx); |
| 1520 |
808024 |
▶ | if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
Branch: 2 branches
| Branch | True | False | Total |
| if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
808024 |
0 |
808024 |
| if (in > 0) mds_buf_write(aTHX_ b, ibuf, (size_t)in); |
0 |
808024 |
808024 |
|
| 1521 |
|
| MDS_BUF_LIT(b, "</a></sup>"); |
| 1522 |
808024 |
| break; |
| 1523 |
|
| } |
| 1524 |
0 |
| case MDS_INL_TEXT: |
| 1525 |
|
| case MDS_INL__COUNT: |
| 1526 |
0 |
| break; |
| 1527 |
|
| } |
| 1528 |
|
| } |
| 1529 |
|
| |
| 1530 |
|
| /* (image-info stack defined above) */ |
| 1531 |
|
| |
| 1532 |
3213552 |
| static void cb_leave_inline(void* ud, mds_inline_type t) { |
| 1533 |
3213552 |
| render_state* st = (render_state*)ud; |
| 1534 |
|
| dTHXa(st->my_perl); |
| 1535 |
3213552 |
| mds_buf* b = st->buf; |
| 1536 |
3213552 |
▶ | if (st->fn_skip) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->fn_skip) return; |
0 |
3213552 |
3213552 |
|
| 1537 |
3213552 |
| flush_pending_text(aTHX_ st); |
| 1538 |
|
| /* Suppress closing wrappers while we are still inside an image (alt mode). |
| 1539 |
|
| * The leave for the image itself still fires this function with image_depth |
| 1540 |
|
| * about to be decremented, so only short-circuit for nested non-image kinds. */ |
| 1541 |
3213552 |
▶ | if (st->image_depth > 0 && t != MDS_INL_IMAGE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (st->image_depth > 0 && t != MDS_INL_IMAGE) { |
808101 |
2405451 |
3213552 |
| if (st->image_depth > 0 && t != MDS_INL_IMAGE) { |
21 |
808080 |
808101 |
|
| 1542 |
21 |
| return; |
| 1543 |
|
| } |
| 1544 |
3213531 |
| switch (t) { |
| 1545 |
656 |
▶ | case MDS_INL_EMPH: MDS_BUF_LIT(b, "</em>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_EMPH: MDS_BUF_LIT(b, "</em>"); break; |
0 |
328 |
328 |
|
| 1546 |
480 |
▶ | case MDS_INL_STRONG: MDS_BUF_LIT(b, "</strong>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_STRONG: MDS_BUF_LIT(b, "</strong>"); break; |
0 |
240 |
240 |
|
| 1547 |
52 |
▶ | case MDS_INL_STRIKE: MDS_BUF_LIT(b, "</del>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_STRIKE: MDS_BUF_LIT(b, "</del>"); break; |
3 |
23 |
26 |
|
| 1548 |
292 |
▶ | case MDS_INL_CODE: MDS_BUF_LIT(b, "</code>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_CODE: MDS_BUF_LIT(b, "</code>"); break; |
0 |
146 |
146 |
|
| 1549 |
1616676 |
▶ | case MDS_INL_LINK: MDS_BUF_LIT(b, "</a>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_LINK: MDS_BUF_LIT(b, "</a>"); break; |
0 |
808338 |
808338 |
|
| 1550 |
114 |
▶ | case MDS_INL_AUTOLINK: MDS_BUF_LIT(b, "</a>"); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case MDS_INL_AUTOLINK: MDS_BUF_LIT(b, "</a>"); break; |
0 |
57 |
57 |
|
| 1551 |
808024 |
| case MDS_INL_FOOTNOTE_REF: break; /* closing tags emitted in enter */ |
| 1552 |
808080 |
| case MDS_INL_IMAGE: { |
| 1553 |
808080 |
| st->image_depth--; |
| 1554 |
808080 |
▶ | if (g_img_stack_top > 0) g_img_stack_top--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g_img_stack_top > 0) g_img_stack_top--; |
808080 |
0 |
808080 |
|
| 1555 |
808080 |
▶ | if (st->image_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->image_depth == 0) { |
808077 |
3 |
808080 |
|
| 1556 |
808077 |
| mds_buf* rb = st->buf; |
| 1557 |
808077 |
▶ | img_info inf = g_img_stack[g_img_stack_top]; |
Branch: 1 branch
| Branch | True | False | Total |
| img_info inf = g_img_stack[g_img_stack_top]; |
0 |
808077 |
808077 |
|
| 1558 |
|
| MDS_BUF_LIT(rb, "<img src=\""); |
| 1559 |
1616154 |
| if ((st->flags & MDS_FLAG_UNSAFE) || |
| 1560 |
808077 |
| !url_is_dangerous(inf.href, inf.hlen, 1)) { |
| 1561 |
808077 |
| write_url_attr(aTHX_ rb, inf.href, inf.hlen); |
| 1562 |
|
| } |
| 1563 |
|
| MDS_BUF_LIT(rb, "\" alt=\""); |
| 1564 |
808077 |
▶ | if (st->alt_len) html_escape(aTHX_ rb, st->alt, st->alt_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if (st->alt_len) html_escape(aTHX_ rb, st->alt, st->alt_len); |
808074 |
3 |
808077 |
|
| 1565 |
|
| MDS_BUF_LIT(rb, "\""); |
| 1566 |
808077 |
▶ | if (inf.tlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (inf.tlen) { |
38 |
808039 |
808077 |
|
| 1567 |
|
| MDS_BUF_LIT(rb, " title=\""); |
| 1568 |
38 |
| html_escape_unesc(aTHX_ rb, inf.title, inf.tlen); |
| 1569 |
|
| MDS_BUF_LIT(rb, "\""); |
| 1570 |
|
| } |
| 1571 |
|
| MDS_BUF_LIT(rb, " />"); |
| 1572 |
808077 |
| st->alt_len = 0; |
| 1573 |
|
| } |
| 1574 |
808080 |
| break; |
| 1575 |
|
| } |
| 1576 |
788292 |
| case MDS_INL_SOFTBREAK: |
| 1577 |
|
| case MDS_INL_LINEBREAK: |
| 1578 |
|
| case MDS_INL_HTML_INLINE: |
| 1579 |
|
| case MDS_INL_TEXT: |
| 1580 |
|
| case MDS_INL__COUNT: |
| 1581 |
788292 |
| break; |
| 1582 |
|
| } |
| 1583 |
|
| } |
| 1584 |
|
| |
| 1585 |
|
| /* ------------------ install ------------------ */ |
| 1586 |
|
| |
| 1587 |
|
| /* The caller allocates a `render_state` (on the stack) and passes &st as |
| 1588 |
|
| * ud_storage. We initialise its `buf` field, capture the current aTHX, |
| 1589 |
|
| * and wire up the callbacks. ud_out is set to point at the state. */ |
| 1590 |
164109 |
| void mds_render_html_install(mds_callbacks* cb, void** ud_out, mds_buf* buf, |
| 1591 |
|
| unsigned flags, AV* toc) { |
| 1592 |
|
| dTHX; |
| 1593 |
164109 |
| render_state* st = (render_state*)*ud_out; |
| 1594 |
|
| #ifdef MULTIPLICITY |
| 1595 |
|
| st->my_perl = aTHX; |
| 1596 |
|
| #endif |
| 1597 |
164109 |
| st->buf = buf; |
| 1598 |
164109 |
| st->top = 0; |
| 1599 |
164109 |
| st->tight_depth = 0; |
| 1600 |
164109 |
| st->image_depth = 0; |
| 1601 |
164109 |
| st->alt = NULL; st->alt_len = 0; st->alt_cap = 0; |
| 1602 |
164109 |
| st->flags = flags; |
| 1603 |
164109 |
| st->li_check_pending = 0; |
| 1604 |
164109 |
| st->in_thead = 0; |
| 1605 |
164109 |
| st->tbl_aligns = NULL; |
| 1606 |
164109 |
| st->tbl_ncols = 0; |
| 1607 |
164109 |
| st->tbl_col = 0; |
| 1608 |
164109 |
| st->pending_text = NULL; |
| 1609 |
164109 |
| st->pending_len = 0; |
| 1610 |
164109 |
| st->pending_cap = 0; |
| 1611 |
164109 |
| st->fn_labels = NULL; |
| 1612 |
164109 |
| st->fn_label_lens = NULL; |
| 1613 |
164109 |
| st->fn_uses = NULL; |
| 1614 |
164109 |
| st->fn_count = 0; |
| 1615 |
164109 |
| st->fn_cap = 0; |
| 1616 |
164109 |
| st->fn_skip = 0; |
| 1617 |
164109 |
| st->fn_in_def = 0; |
| 1618 |
164109 |
| st->fn_in_def_label = NULL; |
| 1619 |
164109 |
| st->fn_in_def_label_len = 0; |
| 1620 |
164109 |
| st->hl_buf = NULL; |
| 1621 |
164109 |
| st->hl_len = 0; |
| 1622 |
164109 |
| st->hl_cap = 0; |
| 1623 |
164109 |
| st->hl_lang[0] = '\0'; |
| 1624 |
164109 |
| st->in_fenced_hl = 0; |
| 1625 |
164109 |
| st->in_heading = 0; |
| 1626 |
164109 |
| st->hd_mark = 0; |
| 1627 |
164109 |
| st->hd_text = NULL; |
| 1628 |
164109 |
| st->hd_len = 0; |
| 1629 |
164109 |
| st->hd_cap = 0; |
| 1630 |
164109 |
| st->hd_slugs.slugs = NULL; |
| 1631 |
164109 |
| st->hd_slugs.len = 0; |
| 1632 |
164109 |
| st->hd_slugs.cap = 0; |
| 1633 |
164109 |
| st->toc = toc; |
| 1634 |
164109 |
| g_img_stack_top = 0; |
| 1635 |
|
| |
| 1636 |
164109 |
| cb->enter_block = cb_enter_block; |
| 1637 |
164109 |
| cb->leave_block = cb_leave_block; |
| 1638 |
164109 |
| cb->enter_inline = cb_enter_inline; |
| 1639 |
164109 |
| cb->leave_inline = cb_leave_inline; |
| 1640 |
164109 |
| cb->text = cb_text; |
| 1641 |
164109 |
| cb->raw = cb_raw; |
| 1642 |
164109 |
| } |
| 1643 |
|
| |
| 1644 |
164109 |
| void mds_render_html_cleanup(void* ud) { |
| 1645 |
164109 |
| render_state* st = (render_state*)ud; |
| 1646 |
164109 |
▶ | if (!st) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!st) return; |
0 |
164109 |
164109 |
|
| 1647 |
164109 |
| free(st->alt); st->alt = NULL; |
| 1648 |
164109 |
| st->alt_len = st->alt_cap = 0; |
| 1649 |
164109 |
| free(st->pending_text); st->pending_text = NULL; |
| 1650 |
164109 |
| st->pending_len = st->pending_cap = 0; |
| 1651 |
164109 |
| free(st->hl_buf); st->hl_buf = NULL; |
| 1652 |
164109 |
| st->hl_len = st->hl_cap = 0; |
| 1653 |
164109 |
| free(st->fn_labels); st->fn_labels = NULL; |
| 1654 |
164109 |
| free(st->fn_label_lens); st->fn_label_lens = NULL; |
| 1655 |
164109 |
| free(st->fn_uses); st->fn_uses = NULL; |
| 1656 |
164109 |
| st->fn_count = st->fn_cap = 0; |
| 1657 |
164109 |
| free(st->hd_text); st->hd_text = NULL; |
| 1658 |
164109 |
| st->hd_len = st->hd_cap = 0; |
| 1659 |
164109 |
| mds_slug_table_free(&st->hd_slugs); |
| 1660 |
|
| } |
| 1661 |
|
| |
| 1662 |
121245 |
| int mds_render_html_used_footnote(void* ud, size_t i, |
| 1663 |
|
| const char** label_out, |
| 1664 |
|
| size_t* label_len_out) { |
| 1665 |
121245 |
| render_state* st = (render_state*)ud; |
| 1666 |
121245 |
▶ | if (!st || i >= st->fn_count) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!st || i >= st->fn_count) return 0; |
121245 |
0 |
121245 |
| if (!st || i >= st->fn_count) return 0; |
40409 |
80836 |
121245 |
|
| 1667 |
80836 |
▶ | if (label_out) *label_out = st->fn_labels[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (label_out) *label_out = st->fn_labels[i]; |
80836 |
0 |
80836 |
|
| 1668 |
80836 |
▶ | if (label_len_out) *label_len_out = st->fn_label_lens[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (label_len_out) *label_len_out = st->fn_label_lens[i]; |
80836 |
0 |
80836 |
|
| 1669 |
80836 |
| return 1; |
| 1670 |
|
| } |