markdown.c

filter: Statementstmt 23.9% 274 / 1146 Branchbran 13.9% 194 / 1388 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 18.4% 468 / 2534 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« html.c ↑ Summary buffer.c »
1 /* markdown.c - generic markdown parser */
2  
3 /*
4 * Copyright (c) 2009, Natacha Porté
5 * Copyright (c) 2011, Vicent Marti
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19  
20 #include "markdown.h"
21 #include "array.h"
22  
23 #include <assert.h>
24 #include <string.h>
25 //#include <strings.h> /* for strncasecmp */
26 #include <ctype.h>
27 #include <stdio.h>
28  
29 #define BUFFER_BLOCK 0
30 #define BUFFER_SPAN 1
31  
32 #define MKD_LI_END 8 /* internal list flag */
33  
34 /***************
35 * LOCAL TYPES *
36 ***************/
37  
38 /* link_ref • reference to a link */
39 struct link_ref {
40 struct buf *id;
41 struct buf *link;
42 struct buf *title;
43 };
44  
45 /* char_trigger • function pointer to render active chars */
46 /* returns the number of chars taken care of */
47 /* data is the pointer of the beginning of the span */
48 /* offset is the number of valid chars before data */
49 struct render;
50 typedef size_t
51 (*char_trigger)(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
52  
53 static size_t char_emphasis(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
54 static size_t char_linebreak(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
55 static size_t char_codespan(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
56 static size_t char_escape(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
57 static size_t char_entity(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
58 static size_t char_langle_tag(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
59 static size_t char_autolink_url(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
60 static size_t char_autolink_email(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
61 static size_t char_autolink_www(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
62 static size_t char_link(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size);
63  
64 enum markdown_char_t {
65 MD_CHAR_NONE = 0,
66 MD_CHAR_EMPHASIS,
67 MD_CHAR_CODESPAN,
68 MD_CHAR_LINEBREAK,
69 MD_CHAR_LINK,
70 MD_CHAR_LANGLE,
71 MD_CHAR_ESCAPE,
72 MD_CHAR_ENTITITY,
73 MD_CHAR_AUTOLINK_URL,
74 MD_CHAR_AUTOLINK_EMAIL,
75 MD_CHAR_AUTOLINK_WWW
76 };
77  
78 static char_trigger markdown_char_ptrs[] = {
79 NULL,
80 &char_emphasis,
81 &char_codespan,
82 &char_linebreak,
83 &char_link,
84 &char_langle_tag,
85 &char_escape,
86 &char_entity,
87 &char_autolink_url,
88 &char_autolink_email,
89 &char_autolink_www,
90 };
91  
92 /* render • structure containing one particular render */
93 struct render {
94 struct mkd_renderer make;
95 struct array refs;
96 char active_char[256];
97 struct parray work_bufs[2];
98 unsigned int ext_flags;
99 size_t max_nesting;
100 };
101  
102 /* html_tag • structure for quick HTML tag search (inspired from discount) */
103 struct html_tag {
104 const char *text;
105 size_t size;
106 };
107  
108 static inline struct buf *
109 7 rndr_newbuf(struct render *rndr, int type)
110 {
111 static const size_t buf_size[2] = {256, 64};
112 7 struct buf *work = NULL;
113 7 struct parray *queue = &rndr->work_bufs[type];
114  
115 7 ▶ if (queue->size < queue->asize) {
Branch: 1 branch
BranchTrueFalseTotal
if (queue->size < queue->asize) { 4 3 7
116 4 work = queue->item[queue->size++];
117 4 work->size = 0;
118 } else {
119 3 work = bufnew(buf_size[type]);
120 3 parr_push(queue, work);
121 }
122  
123 7 return work;
124 }
125  
126 static inline void
127 7 rndr_popbuf(struct render *rndr, int type)
128 {
129 7 rndr->work_bufs[type].size--;
130 7 }
131  
132 /********************
133 * GLOBAL VARIABLES *
134 ********************/
135  
136 /* block_tags • recognised block tags, sorted by cmp_html_tag */
137 static struct html_tag block_tags[] = {
138 /*0*/ { "p", 1 },
139 { "dl", 2 },
140 { "h1", 2 },
141 { "h2", 2 },
142 { "h3", 2 },
143 { "h4", 2 },
144 { "h5", 2 },
145 { "h6", 2 },
146 { "ol", 2 },
147 { "ul", 2 },
148 /*10*/ { "del", 3 },
149 { "div", 3 },
150 /*12*/ { "ins", 3 },
151 { "pre", 3 },
152 { "form", 4 },
153 { "math", 4 },
154 { "table", 5 },
155 { "iframe", 6 },
156 { "script", 6 },
157 { "fieldset", 8 },
158 { "noscript", 8 },
159 { "blockquote", 10 }
160 };
161  
162 #define INS_TAG (block_tags + 12)
163 #define DEL_TAG (block_tags + 10)
164  
165 /***************************
166 * HELPER FUNCTIONS *
167 ***************************/
168 static void
169 0 unscape_text(struct buf *ob, struct buf *src)
170 {
171 0 size_t i = 0, org;
172 0 ▶ while (i < src->size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < src->size) { 0 0 0
173 0 org = i;
174 0 ▶ while (i < src->size && src->data[i] != '\\')
Branch: 2 branches
BranchTrueFalseTotal
while (i < src->size && src->data[i] != '\\') 0 0 0
while (i < src->size && src->data[i] != '\\') 0 0 0
175 0 i++;
176  
177 0 ▶ if (i > org)
Branch: 1 branch
BranchTrueFalseTotal
if (i > org) 0 0 0
178 0 bufput(ob, src->data + org, i - org);
179  
180 0 ▶ if (i + 1 >= src->size)
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= src->size) 0 0 0
181 0 break;
182  
183 0 bufputc(ob, src->data[i + 1]);
184 0 i += 2;
185 }
186 0 }
187  
188 /* cmp_link_ref • comparison function for link_ref sorted arrays */
189 static int
190 0 cmp_link_ref(void *key, void *array_entry)
191 {
192 0 struct link_ref *lr = array_entry;
193 0 return bufcasecmp(key, lr->id);
194 }
195  
196 /* cmp_link_ref_sort • comparison function for link_ref qsort */
197 static int
198 0 cmp_link_ref_sort(const void *a, const void *b)
199 {
200 0 const struct link_ref *lra = a;
201 0 const struct link_ref *lrb = b;
202 0 return bufcasecmp(lra->id, lrb->id);
203 }
204  
205 /* cmp_html_tag • comparison function for bsearch() (stolen from discount) */
206 static int
207 0 cmp_html_tag(const void *a, const void *b)
208 {
209 0 const struct html_tag *hta = a;
210 0 const struct html_tag *htb = b;
211 0 ▶ if (hta->size != htb->size) return (int)(hta->size - htb->size);
Branch: 1 branch
BranchTrueFalseTotal
if (hta->size != htb->size) return (int)(hta->size - htb->size); 0 0 0
212 0 return strncasecmp(hta->text, htb->text, hta->size);
213 }
214  
215  
216 /* find_block_tag • returns the current block tag */
217 static struct html_tag *
218 0 find_block_tag(char *data, size_t size)
219 {
220 0 size_t i = 0;
221 struct html_tag key;
222  
223 /* looking for the word end */
224 0 ▶ while (i < size && ((data[i] >= '0' && data[i] <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && ((data[i] >= '0' && data[i] <= '9') 0 0 0
while (i < size && ((data[i] >= '0' && data[i] <= '9') 0 0 0
while (i < size && ((data[i] >= '0' && data[i] <= '9') 0 0 0
225 0 ▶ || (data[i] >= 'A' && data[i] <= 'Z')
Branch: 2 branches
BranchTrueFalseTotal
|| (data[i] >= 'A' && data[i] <= 'Z') 0 0 0
|| (data[i] >= 'A' && data[i] <= 'Z') 0 0 0
226 0 ▶ || (data[i] >= 'a' && data[i] <= 'z')))
Branch: 2 branches
BranchTrueFalseTotal
|| (data[i] >= 'a' && data[i] <= 'z'))) 0 0 0
|| (data[i] >= 'a' && data[i] <= 'z'))) 0 0 0
227 0 i += 1;
228 0 ▶ if (i >= size) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return 0; 0 0 0
229  
230 /* binary search of the tag */
231 0 key.text = data;
232 0 key.size = i;
233 0 return bsearch(&key, block_tags,
234 sizeof block_tags / sizeof block_tags[0],
235 sizeof block_tags[0], cmp_html_tag);
236 }
237  
238 /****************************
239 * INLINE PARSING FUNCTIONS *
240 ****************************/
241  
242 /* is_mail_autolink • looks for the address part of a mail autolink and '>' */
243 /* this is less strict than the original markdown e-mail address matching */
244 static size_t
245 0 is_mail_autolink(char *data, size_t size)
246 {
247 0 size_t i = 0, nb = 0;
248  
249 /* address is assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@' */
250 0 ▶ for (i = 0; i < size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; ++i) { 0 0 0
251 0 ▶ if (isalnum(data[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (isalnum(data[i])) 0 0 0
252 0 continue;
253  
254 0 switch (data[i]) {
255 0 case '@':
256 0 nb++;
257  
258 0 case '-':
259 case '.':
260 case '_':
261 0 break;
262  
263 0 case '>':
264 0 ▶ return (nb == 1) ? i + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return (nb == 1) ? i + 1 : 0; 0 0 0
265  
266 0 default:
267 0 return 0;
268 }
269 }
270  
271 0 return 0;
272 }
273  
274 /* tag_length • returns the length of the given tag, or 0 is it's not valid */
275 static size_t
276 0 tag_length(char *data, size_t size, enum mkd_autolink *autolink)
277 {
278 size_t i, j;
279  
280 /* a valid tag can't be shorter than 3 chars */
281 0 ▶ if (size < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < 3) return 0; 0 0 0
282  
283 /* begins with a '<' optionally followed by '/', followed by letter or number */
284 0 ▶ if (data[0] != '<') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (data[0] != '<') return 0; 0 0 0
285 0 ▶ i = (data[1] == '/') ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
i = (data[1] == '/') ? 2 : 1; 0 0 0
286  
287 0 ▶ if (!isalnum(data[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!isalnum(data[i])) 0 0 0
288 0 return 0;
289  
290 /* scheme test */
291 0 *autolink = MKDA_NOT_AUTOLINK;
292  
293 /* try to find the beginning of an URI */
294 0 ▶ while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-'))
Branch: 5 branches
BranchTrueFalseTotal
while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-')) 0 0 0
while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-')) 0 0 0
while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-')) 0 0 0
while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-')) 0 0 0
while (i < size && (isalnum(data[i]) || data[i] == '.' || data[i] == '+' || data[i] == '-')) 0 0 0
295 0 i++;
296  
297 0 ▶ if (i > 1 && data[i] == '@') {
Branch: 2 branches
BranchTrueFalseTotal
if (i > 1 && data[i] == '@') { 0 0 0
if (i > 1 && data[i] == '@') { 0 0 0
298 0 ▶ if ((j = is_mail_autolink(data + i, size - i)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((j = is_mail_autolink(data + i, size - i)) != 0) { 0 0 0
299 0 *autolink = MKDA_EMAIL;
300 0 return i + j;
301 }
302 }
303  
304 0 ▶ if (i > 2 && data[i] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (i > 2 && data[i] == ':') { 0 0 0
if (i > 2 && data[i] == ':') { 0 0 0
305 0 *autolink = MKDA_NORMAL;
306 0 i++;
307 }
308  
309 /* completing autolink test: no whitespace or ' or " */
310 0 ▶ if (i >= size)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) 0 0 0
311 0 *autolink = MKDA_NOT_AUTOLINK;
312  
313 0 ▶ else if (*autolink) {
Branch: 1 branch
BranchTrueFalseTotal
else if (*autolink) { 0 0 0
314 0 j = i;
315  
316 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
317 0 ▶ if (data[i] == '\\') i += 2;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '\\') i += 2; 0 0 0
318 0 ▶ else if (data[i] == '>' || data[i] == '\'' ||
Branch: 2 branches
BranchTrueFalseTotal
else if (data[i] == '>' || data[i] == '\'' || 0 0 0
else if (data[i] == '>' || data[i] == '\'' || 0 0 0
319 0 ▶ data[i] == '"' || isspace(data[i])) break;
Branch: 2 branches
BranchTrueFalseTotal
data[i] == '"' || isspace(data[i])) break; 0 0 0
data[i] == '"' || isspace(data[i])) break; 0 0 0
320 0 else i += 1;
321 }
322  
323 0 ▶ if (i >= size) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return 0; 0 0 0
324 0 ▶ if (i > j && data[i] == '>') return i + 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i > j && data[i] == '>') return i + 1; 0 0 0
if (i > j && data[i] == '>') return i + 1; 0 0 0
325 /* one of the forbidden chars has been found */
326 0 *autolink = MKDA_NOT_AUTOLINK;
327 }
328  
329 /* looking for sometinhg looking like a tag end */
330 0 ▶ while (i < size && data[i] != '>') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '>') i += 1; 0 0 0
while (i < size && data[i] != '>') i += 1; 0 0 0
331 0 ▶ if (i >= size) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return 0; 0 0 0
332 0 return i + 1;
333 }
334  
335 /* parse_inline • parses inline markdown elements */
336 static void
337 4 parse_inline(struct buf *ob, struct render *rndr, char *data, size_t size)
338 {
339 4 size_t i = 0, end = 0;
340 4 char action = 0;
341 4 struct buf work = { 0, 0, 0, 0, 0 };
342  
343 4 if (rndr->work_bufs[BUFFER_SPAN].size +
344 4 ▶ rndr->work_bufs[BUFFER_BLOCK].size > (int)rndr->max_nesting)
Branch: 1 branch
BranchTrueFalseTotal
rndr->work_bufs[BUFFER_BLOCK].size > (int)rndr->max_nesting) 0 4 4
345 0 return;
346  
347 7 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 7 0 7
348 /* copying inactive chars into the output */
349 121 ▶ while (end < size && (action = rndr->active_char[(unsigned char)data[end]]) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (end < size && (action = rndr->active_char[(unsigned char)data[end]]) == 0) { 117 4 121
while (end < size && (action = rndr->active_char[(unsigned char)data[end]]) == 0) { 114 3 117
350 114 end++;
351 }
352  
353 7 ▶ if (rndr->make.normal_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.normal_text) { 7 0 7
354 7 work.data = data + i;
355 7 work.size = end - i;
356 7 rndr->make.normal_text(ob, &work, rndr->make.opaque);
357 }
358 else
359 0 bufput(ob, data + i, end - i);
360  
361 7 ▶ if (end >= size) break;
Branch: 1 branch
BranchTrueFalseTotal
if (end >= size) break; 4 3 7
362 3 i = end;
363  
364 /* calling the trigger */
365 3 end = markdown_char_ptrs[(int)action](ob, rndr, data + i, i, size - i);
366 3 ▶ if (!end) /* no action from the callback */
Branch: 1 branch
BranchTrueFalseTotal
if (!end) /* no action from the callback */ 3 0 3
367 3 end = i + 1;
368 else {
369 0 i += end;
370 0 end = i;
371 }
372 }
373 }
374  
375 /* find_emph_char • looks for the next emph char, skipping other constructs */
376 static size_t
377 0 find_emph_char(char *data, size_t size, char c)
378 {
379 0 size_t i = 1;
380  
381 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
382 0 ▶ while (i < size && data[i] != c
Branch: 1 branch
BranchTrueFalseTotal
while (i < size && data[i] != c 0 0 0
383 0 ▶ && data[i] != '`' && data[i] != '[')
Branch: 3 branches
BranchTrueFalseTotal
&& data[i] != '`' && data[i] != '[') 0 0 0
&& data[i] != '`' && data[i] != '[') 0 0 0
&& data[i] != '`' && data[i] != '[') 0 0 0
384 0 i += 1;
385 0 ▶ if (data[i] == c) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == c) return i; 0 0 0
386  
387 /* not counting escaped chars */
388 0 ▶ if (i && data[i - 1] == '\\') { i += 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (i && data[i - 1] == '\\') { i += 1; continue; } 0 0 0
if (i && data[i - 1] == '\\') { i += 1; continue; } 0 0 0
389  
390 /* skipping a code span */
391 0 ▶ if (data[i] == '`') {
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '`') { 0 0 0
392 0 size_t tmp_i = 0;
393 0 i += 1;
394 0 ▶ while (i < size && data[i] != '`') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '`') { 0 0 0
while (i < size && data[i] != '`') { 0 0 0
395 0 ▶ if (!tmp_i && data[i] == c) tmp_i = i;
Branch: 2 branches
BranchTrueFalseTotal
if (!tmp_i && data[i] == c) tmp_i = i; 0 0 0
if (!tmp_i && data[i] == c) tmp_i = i; 0 0 0
396 0 i += 1; }
397 0 ▶ if (i >= size) return tmp_i;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return tmp_i; 0 0 0
398 0 i += 1; }
399  
400 /* skipping a link */
401 0 ▶ else if (data[i] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
else if (data[i] == '[') { 0 0 0
402 0 size_t tmp_i = 0;
403 char cc;
404 0 i += 1;
405 0 ▶ while (i < size && data[i] != ']') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != ']') { 0 0 0
while (i < size && data[i] != ']') { 0 0 0
406 0 ▶ if (!tmp_i && data[i] == c) tmp_i = i;
Branch: 2 branches
BranchTrueFalseTotal
if (!tmp_i && data[i] == c) tmp_i = i; 0 0 0
if (!tmp_i && data[i] == c) tmp_i = i; 0 0 0
407 0 i += 1; }
408 0 i += 1;
409 0 ▶ while (i < size && (data[i] == ' '
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && (data[i] == ' ' 0 0 0
while (i < size && (data[i] == ' ' 0 0 0
410 0 ▶ || data[i] == '\t' || data[i] == '\n'))
Branch: 2 branches
BranchTrueFalseTotal
|| data[i] == '\t' || data[i] == '\n')) 0 0 0
|| data[i] == '\t' || data[i] == '\n')) 0 0 0
411 0 i += 1;
412 0 ▶ if (i >= size) return tmp_i;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return tmp_i; 0 0 0
413 0 ▶ if (data[i] != '[' && data[i] != '(') { /* not a link*/
Branch: 2 branches
BranchTrueFalseTotal
if (data[i] != '[' && data[i] != '(') { /* not a link*/ 0 0 0
if (data[i] != '[' && data[i] != '(') { /* not a link*/ 0 0 0
414 0 ▶ if (tmp_i) return tmp_i;
Branch: 1 branch
BranchTrueFalseTotal
if (tmp_i) return tmp_i; 0 0 0
415 0 else continue; }
416 0 cc = data[i];
417 0 i += 1;
418 0 ▶ while (i < size && data[i] != cc) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != cc) { 0 0 0
while (i < size && data[i] != cc) { 0 0 0
419 0 ▶ if (!tmp_i && data[i] == c) tmp_i = i;
Branch: 2 branches
BranchTrueFalseTotal
if (!tmp_i && data[i] == c) tmp_i = i; 0 0 0
if (!tmp_i && data[i] == c) tmp_i = i; 0 0 0
420 0 i += 1; }
421 0 ▶ if (i >= size) return tmp_i;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return tmp_i; 0 0 0
422 0 i += 1; } }
423 0 return 0;
424 }
425  
426 /* parse_emph1 • parsing single emphase */
427 /* closed by a symbol not preceded by whitespace and not followed by symbol */
428 static size_t
429 0 parse_emph1(struct buf *ob, struct render *rndr, char *data, size_t size, char c)
430 {
431 0 size_t i = 0, len;
432 0 struct buf *work = 0;
433 int r;
434  
435 0 ▶ if (!rndr->make.emphasis) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!rndr->make.emphasis) return 0; 0 0 0
436  
437 /* skipping one symbol if coming from emph3 */
438 0 ▶ if (size > 1 && data[0] == c && data[1] == c) i = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (size > 1 && data[0] == c && data[1] == c) i = 1; 0 0 0
if (size > 1 && data[0] == c && data[1] == c) i = 1; 0 0 0
if (size > 1 && data[0] == c && data[1] == c) i = 1; 0 0 0
439  
440 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
441 0 len = find_emph_char(data + i, size - i, c);
442 0 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 0 0
443 0 i += len;
444 0 ▶ if (i >= size) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) return 0; 0 0 0
445  
446 0 ▶ if (i + 1 < size && data[i + 1] == c) {
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 < size && data[i + 1] == c) { 0 0 0
if (i + 1 < size && data[i + 1] == c) { 0 0 0
447 0 i += 1;
448 0 continue;
449 }
450  
451 0 ▶ if (data[i] == c && !isspace(data[i - 1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (data[i] == c && !isspace(data[i - 1])) { 0 0 0
if (data[i] == c && !isspace(data[i - 1])) { 0 0 0
452  
453 0 ▶ if (rndr->ext_flags & MKDEXT_NO_INTRA_EMPHASIS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->ext_flags & MKDEXT_NO_INTRA_EMPHASIS) { 0 0 0
454 0 ▶ if (!(i + 1 == size || isspace(data[i + 1]) || ispunct(data[i + 1])))
Branch: 3 branches
BranchTrueFalseTotal
if (!(i + 1 == size || isspace(data[i + 1]) || ispunct(data[i + 1]))) 0 0 0
if (!(i + 1 == size || isspace(data[i + 1]) || ispunct(data[i + 1]))) 0 0 0
if (!(i + 1 == size || isspace(data[i + 1]) || ispunct(data[i + 1]))) 0 0 0
455 0 continue;
456 }
457  
458 0 work = rndr_newbuf(rndr, BUFFER_SPAN);
459 0 parse_inline(work, rndr, data, i);
460 0 r = rndr->make.emphasis(ob, work, rndr->make.opaque);
461 0 rndr_popbuf(rndr, BUFFER_SPAN);
462 0 ▶ return r ? i + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? i + 1 : 0; 0 0 0
463 }
464 }
465  
466 0 return 0;
467 }
468  
469 /* parse_emph2 • parsing single emphase */
470 static size_t
471 0 parse_emph2(struct buf *ob, struct render *rndr, char *data, size_t size, char c)
472 {
473 int (*render_method)(struct buf *ob, struct buf *text, void *opaque);
474 0 size_t i = 0, len;
475 0 struct buf *work = 0;
476 int r;
477  
478 0 ▶ render_method = (c == '~') ? rndr->make.strikethrough : rndr->make.double_emphasis;
Branch: 1 branch
BranchTrueFalseTotal
render_method = (c == '~') ? rndr->make.strikethrough : rndr->make.double_emphasis; 0 0 0
479  
480 0 ▶ if (!render_method)
Branch: 1 branch
BranchTrueFalseTotal
if (!render_method) 0 0 0
481 0 return 0;
482
483 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
484 0 len = find_emph_char(data + i, size - i, c);
485 0 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 0 0
486 0 i += len;
487  
488 0 ▶ if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !isspace(data[i - 1])) {
Branch: 5 branches
BranchTrueFalseTotal
if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !isspace(data[i - 1])) { 0 0 0
if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !isspace(data[i - 1])) { 0 0 0
if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !isspace(data[i - 1])) { 0 0 0
if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !isspace(data[i - 1])) { 0 0 0
if (i + 1 < size && data[i] == c && data[i + 1] == c && i && !isspace(data[i - 1])) { 0 0 0
489 0 work = rndr_newbuf(rndr, BUFFER_SPAN);
490 0 parse_inline(work, rndr, data, i);
491 0 r = render_method(ob, work, rndr->make.opaque);
492 0 rndr_popbuf(rndr, BUFFER_SPAN);
493 0 ▶ return r ? i + 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? i + 2 : 0; 0 0 0
494 }
495 0 i++;
496 }
497 0 return 0;
498 }
499  
500 /* parse_emph3 • parsing single emphase */
501 /* finds the first closing tag, and delegates to the other emph */
502 static size_t
503 0 parse_emph3(struct buf *ob, struct render *rndr, char *data, size_t size, char c)
504 {
505 0 size_t i = 0, len;
506 int r;
507  
508 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
509 0 len = find_emph_char(data + i, size - i, c);
510 0 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 0 0
511 0 i += len;
512  
513 /* skip whitespace preceded symbols */
514 0 ▶ if (data[i] != c || isspace(data[i - 1]))
Branch: 2 branches
BranchTrueFalseTotal
if (data[i] != c || isspace(data[i - 1])) 0 0 0
if (data[i] != c || isspace(data[i - 1])) 0 0 0
515 0 continue;
516  
517 0 ▶ if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->make.triple_emphasis) {
Branch: 4 branches
BranchTrueFalseTotal
if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->make.triple_emphasis) { 0 0 0
if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->make.triple_emphasis) { 0 0 0
if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->make.triple_emphasis) { 0 0 0
if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->make.triple_emphasis) { 0 0 0
518 /* triple symbol found */
519 0 struct buf *work = rndr_newbuf(rndr, BUFFER_SPAN);
520  
521 0 parse_inline(work, rndr, data, i);
522 0 r = rndr->make.triple_emphasis(ob, work, rndr->make.opaque);
523 0 rndr_popbuf(rndr, BUFFER_SPAN);
524 0 ▶ return r ? i + 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? i + 3 : 0; 0 0 0
525  
526 0 ▶ } else if (i + 1 < size && data[i + 1] == c) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (i + 1 < size && data[i + 1] == c) { 0 0 0
} else if (i + 1 < size && data[i + 1] == c) { 0 0 0
527 /* double symbol found, handing over to emph1 */
528 0 len = parse_emph1(ob, rndr, data - 2, size + 2, c);
529 0 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 0 0
530 0 else return len - 2;
531  
532 } else {
533 /* single symbol found, handing over to emph2 */
534 0 len = parse_emph2(ob, rndr, data - 1, size + 1, c);
535 0 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 0 0
536 0 else return len - 1;
537 }
538 }
539 0 return 0;
540 }
541  
542 /* char_emphasis • single and double emphasis parsing */
543 static size_t
544 0 char_emphasis(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
545 {
546 0 char c = data[0];
547 size_t ret;
548  
549 0 ▶ if (size > 2 && data[1] != c) {
Branch: 2 branches
BranchTrueFalseTotal
if (size > 2 && data[1] != c) { 0 0 0
if (size > 2 && data[1] != c) { 0 0 0
550 /* whitespace cannot follow an opening emphasis;
551 * strikethrough only takes two characters '~~' */
552 0 ▶ if (c == '~' || isspace(data[1]) || (ret = parse_emph1(ob, rndr, data + 1, size - 1, c)) == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (c == '~' || isspace(data[1]) || (ret = parse_emph1(ob, rndr, data + 1, size - 1, c)) == 0) 0 0 0
if (c == '~' || isspace(data[1]) || (ret = parse_emph1(ob, rndr, data + 1, size - 1, c)) == 0) 0 0 0
if (c == '~' || isspace(data[1]) || (ret = parse_emph1(ob, rndr, data + 1, size - 1, c)) == 0) 0 0 0
553 0 return 0;
554  
555 0 return ret + 1;
556 }
557  
558 0 ▶ if (size > 3 && data[1] == c && data[2] != c) {
Branch: 3 branches
BranchTrueFalseTotal
if (size > 3 && data[1] == c && data[2] != c) { 0 0 0
if (size > 3 && data[1] == c && data[2] != c) { 0 0 0
if (size > 3 && data[1] == c && data[2] != c) { 0 0 0
559 0 ▶ if (isspace(data[2]) || (ret = parse_emph2(ob, rndr, data + 2, size - 2, c)) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (isspace(data[2]) || (ret = parse_emph2(ob, rndr, data + 2, size - 2, c)) == 0) 0 0 0
if (isspace(data[2]) || (ret = parse_emph2(ob, rndr, data + 2, size - 2, c)) == 0) 0 0 0
560 0 return 0;
561  
562 0 return ret + 2;
563 }
564  
565 0 ▶ if (size > 4 && data[1] == c && data[2] == c && data[3] != c) {
Branch: 4 branches
BranchTrueFalseTotal
if (size > 4 && data[1] == c && data[2] == c && data[3] != c) { 0 0 0
if (size > 4 && data[1] == c && data[2] == c && data[3] != c) { 0 0 0
if (size > 4 && data[1] == c && data[2] == c && data[3] != c) { 0 0 0
if (size > 4 && data[1] == c && data[2] == c && data[3] != c) { 0 0 0
566 0 ▶ if (c == '~' || isspace(data[3]) || (ret = parse_emph3(ob, rndr, data + 3, size - 3, c)) == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (c == '~' || isspace(data[3]) || (ret = parse_emph3(ob, rndr, data + 3, size - 3, c)) == 0) 0 0 0
if (c == '~' || isspace(data[3]) || (ret = parse_emph3(ob, rndr, data + 3, size - 3, c)) == 0) 0 0 0
if (c == '~' || isspace(data[3]) || (ret = parse_emph3(ob, rndr, data + 3, size - 3, c)) == 0) 0 0 0
567 0 return 0;
568  
569 0 return ret + 3;
570 }
571  
572 0 return 0;
573 }
574  
575  
576 /* char_linebreak • '\n' preceded by two spaces (assuming linebreak != 0) */
577 static size_t
578 3 char_linebreak(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
579 {
580 3 ▶ if (offset < 2 || data[-1] != ' ' || data[-2] != ' ')
Branch: 3 branches
BranchTrueFalseTotal
if (offset < 2 || data[-1] != ' ' || data[-2] != ' ') 3 0 3
if (offset < 2 || data[-1] != ' ' || data[-2] != ' ') 0 3 3
if (offset < 2 || data[-1] != ' ' || data[-2] != ' ') 0 0 0
581 3 return 0;
582  
583 /* removing the last space from ob and rendering */
584 0 ▶ while (ob->size && ob->data[ob->size - 1] == ' ')
Branch: 2 branches
BranchTrueFalseTotal
while (ob->size && ob->data[ob->size - 1] == ' ') 0 0 0
while (ob->size && ob->data[ob->size - 1] == ' ') 0 0 0
585 0 ob->size--;
586  
587 0 return rndr->make.linebreak(ob, rndr->make.opaque) ? 1 : 0;
588 }
589  
590  
591 /* char_codespan • '`' parsing a code span (assuming codespan != 0) */
592 static size_t
593 0 char_codespan(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
594 {
595 0 size_t end, nb = 0, i, f_begin, f_end;
596  
597 /* counting the number of backticks in the delimiter */
598 0 ▶ while (nb < size && data[nb] == '`')
Branch: 2 branches
BranchTrueFalseTotal
while (nb < size && data[nb] == '`') 0 0 0
while (nb < size && data[nb] == '`') 0 0 0
599 0 nb++;
600  
601 /* finding the next delimiter */
602 0 i = 0;
603 0 ▶ for (end = nb; end < size && i < nb; end++) {
Branch: 2 branches
BranchTrueFalseTotal
for (end = nb; end < size && i < nb; end++) { 0 0 0
for (end = nb; end < size && i < nb; end++) { 0 0 0
604 0 ▶ if (data[end] == '`') i++;
Branch: 1 branch
BranchTrueFalseTotal
if (data[end] == '`') i++; 0 0 0
605 0 else i = 0;
606 }
607  
608 0 ▶ if (i < nb && end >= size)
Branch: 2 branches
BranchTrueFalseTotal
if (i < nb && end >= size) 0 0 0
if (i < nb && end >= size) 0 0 0
609 0 return 0; /* no matching delimiter */
610  
611 /* trimming outside whitespaces */
612 0 f_begin = nb;
613 0 ▶ while (f_begin < end && (data[f_begin] == ' ' || data[f_begin] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (f_begin < end && (data[f_begin] == ' ' || data[f_begin] == '\t')) 0 0 0
while (f_begin < end && (data[f_begin] == ' ' || data[f_begin] == '\t')) 0 0 0
while (f_begin < end && (data[f_begin] == ' ' || data[f_begin] == '\t')) 0 0 0
614 0 f_begin++;
615  
616 0 f_end = end - nb;
617 0 ▶ while (f_end > nb && (data[f_end-1] == ' ' || data[f_end-1] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (f_end > nb && (data[f_end-1] == ' ' || data[f_end-1] == '\t')) 0 0 0
while (f_end > nb && (data[f_end-1] == ' ' || data[f_end-1] == '\t')) 0 0 0
while (f_end > nb && (data[f_end-1] == ' ' || data[f_end-1] == '\t')) 0 0 0
618 0 f_end--;
619  
620 /* real code span */
621 0 ▶ if (f_begin < f_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (f_begin < f_end) { 0 0 0
622 0 struct buf work = { data + f_begin, f_end - f_begin, 0, 0, 0 };
623 0 ▶ if (!rndr->make.codespan(ob, &work, rndr->make.opaque))
Branch: 1 branch
BranchTrueFalseTotal
if (!rndr->make.codespan(ob, &work, rndr->make.opaque)) 0 0 0
624 0 end = 0;
625 } else {
626 0 ▶ if (!rndr->make.codespan(ob, 0, rndr->make.opaque))
Branch: 1 branch
BranchTrueFalseTotal
if (!rndr->make.codespan(ob, 0, rndr->make.opaque)) 0 0 0
627 0 end = 0;
628 }
629  
630 0 return end;
631 }
632  
633  
634 /* char_escape • '\\' backslash escape */
635 static size_t
636 0 char_escape(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
637 {
638 static const char *escape_chars = "\\`*_{}[]()#+-.!:|&<>";
639 0 struct buf work = { 0, 0, 0, 0, 0 };
640  
641 0 ▶ if (size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 1) { 0 0 0
642 0 ▶ if (strchr(escape_chars, data[1]) == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(escape_chars, data[1]) == NULL) 0 0 0
643 0 return 0;
644  
645 0 ▶ if (rndr->make.normal_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.normal_text) { 0 0 0
646 0 work.data = data + 1;
647 0 work.size = 1;
648 0 rndr->make.normal_text(ob, &work, rndr->make.opaque);
649 }
650 0 else bufputc(ob, data[1]);
651 }
652  
653 0 return 2;
654 }
655  
656 /* char_entity • '&' escaped when it doesn't belong to an entity */
657 /* valid entities are assumed to be anything matching &#?[A-Za-z0-9]+; */
658 static size_t
659 0 char_entity(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
660 {
661 0 size_t end = 1;
662 struct buf work;
663  
664 0 ▶ if (end < size && data[end] == '#')
Branch: 2 branches
BranchTrueFalseTotal
if (end < size && data[end] == '#') 0 0 0
if (end < size && data[end] == '#') 0 0 0
665 0 end++;
666  
667 0 ▶ while (end < size && isalnum(data[end]))
Branch: 2 branches
BranchTrueFalseTotal
while (end < size && isalnum(data[end])) 0 0 0
while (end < size && isalnum(data[end])) 0 0 0
668 0 end++;
669  
670 0 ▶ if (end < size && data[end] == ';')
Branch: 2 branches
BranchTrueFalseTotal
if (end < size && data[end] == ';') 0 0 0
if (end < size && data[end] == ';') 0 0 0
671 0 end += 1; /* real entity */
672 else
673 0 return 0; /* lone '&' */
674  
675 0 ▶ if (rndr->make.entity) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.entity) { 0 0 0
676 0 work.data = data;
677 0 work.size = end;
678 0 rndr->make.entity(ob, &work, rndr->make.opaque);
679 }
680 0 else bufput(ob, data, end);
681  
682 0 return end;
683 }
684  
685 /* char_langle_tag • '<' when tags or autolinks are allowed */
686 static size_t
687 0 char_langle_tag(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
688 {
689 0 enum mkd_autolink altype = MKDA_NOT_AUTOLINK;
690 0 size_t end = tag_length(data, size, &altype);
691 0 struct buf work = { data, end, 0, 0, 0 };
692 0 int ret = 0;
693  
694 0 ▶ if (end > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (end > 2) { 0 0 0
695 0 ▶ if (rndr->make.autolink && altype != MKDA_NOT_AUTOLINK) {
Branch: 2 branches
BranchTrueFalseTotal
if (rndr->make.autolink && altype != MKDA_NOT_AUTOLINK) { 0 0 0
if (rndr->make.autolink && altype != MKDA_NOT_AUTOLINK) { 0 0 0
696 0 struct buf *u_link = rndr_newbuf(rndr, BUFFER_SPAN);
697 0 work.data = data + 1;
698 0 work.size = end - 2;
699 0 unscape_text(u_link, &work);
700 0 ret = rndr->make.autolink(ob, u_link, altype, rndr->make.opaque);
701 0 rndr_popbuf(rndr, BUFFER_SPAN);
702 }
703 0 ▶ else if (rndr->make.raw_html_tag)
Branch: 1 branch
BranchTrueFalseTotal
else if (rndr->make.raw_html_tag) 0 0 0
704 0 ret = rndr->make.raw_html_tag(ob, &work, rndr->make.opaque);
705 }
706  
707 0 ▶ if (!ret) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ret) return 0; 0 0 0
708 0 else return end;
709 }
710  
711 static size_t
712 0 char_autolink_www(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
713 {
714 struct buf *link, *link_url;
715 size_t link_len, rewind;
716  
717 0 ▶ if (!rndr->make.link)
Branch: 1 branch
BranchTrueFalseTotal
if (!rndr->make.link) 0 0 0
718 0 return 0;
719  
720 0 link = rndr_newbuf(rndr, BUFFER_SPAN);
721  
722 0 ▶ if ((link_len = ups_autolink__www(&rewind, link, data, offset, size)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((link_len = ups_autolink__www(&rewind, link, data, offset, size)) > 0) { 0 0 0
723 0 link_url = rndr_newbuf(rndr, BUFFER_SPAN);
724 0 BUFPUTSL(link_url, "http://");
725 0 bufput(link_url, link->data, link->size);
726  
727 0 ob->size -= rewind;
728 0 rndr->make.link(ob, link_url, NULL, link, rndr->make.opaque);
729 0 rndr_popbuf(rndr, BUFFER_SPAN);
730 }
731  
732 0 rndr_popbuf(rndr, BUFFER_SPAN);
733 0 return link_len;
734 }
735  
736 static size_t
737 0 char_autolink_email(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
738 {
739 struct buf *link;
740 size_t link_len, rewind;
741  
742 0 ▶ if (!rndr->make.autolink)
Branch: 1 branch
BranchTrueFalseTotal
if (!rndr->make.autolink) 0 0 0
743 0 return 0;
744  
745 0 link = rndr_newbuf(rndr, BUFFER_SPAN);
746  
747 0 ▶ if ((link_len = ups_autolink__email(&rewind, link, data, offset, size)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((link_len = ups_autolink__email(&rewind, link, data, offset, size)) > 0) { 0 0 0
748 0 ob->size -= rewind;
749 0 rndr->make.autolink(ob, link, MKDA_EMAIL, rndr->make.opaque);
750 }
751  
752 0 rndr_popbuf(rndr, BUFFER_SPAN);
753 0 return link_len;
754 }
755  
756 static size_t
757 0 char_autolink_url(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
758 {
759 struct buf *link;
760 size_t link_len, rewind;
761  
762 0 ▶ if (!rndr->make.autolink)
Branch: 1 branch
BranchTrueFalseTotal
if (!rndr->make.autolink) 0 0 0
763 0 return 0;
764  
765 0 link = rndr_newbuf(rndr, BUFFER_SPAN);
766  
767 0 ▶ if ((link_len = ups_autolink__url(&rewind, link, data, offset, size)) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((link_len = ups_autolink__url(&rewind, link, data, offset, size)) > 0) { 0 0 0
768 0 ob->size -= rewind;
769 0 rndr->make.autolink(ob, link, MKDA_NORMAL, rndr->make.opaque);
770 }
771  
772 0 rndr_popbuf(rndr, BUFFER_SPAN);
773 0 return link_len;
774 }
775  
776 /* char_link • '[': parsing a link or an image */
777 static size_t
778 0 char_link(struct buf *ob, struct render *rndr, char *data, size_t offset, size_t size)
779 {
780 0 ▶ int is_img = (offset && data[-1] == '!'), level;
Branch: 2 branches
BranchTrueFalseTotal
int is_img = (offset && data[-1] == '!'), level; 0 0 0
int is_img = (offset && data[-1] == '!'), level; 0 0 0
781 0 size_t i = 1, txt_e, link_b = 0, link_e = 0, title_b = 0, title_e = 0;
782 0 struct buf *content = 0;
783 0 struct buf *link = 0;
784 0 struct buf *title = 0;
785 0 struct buf *u_link = 0;
786 0 size_t org_work_size = rndr->work_bufs[BUFFER_SPAN].size;
787 0 int text_has_nl = 0, ret = 0;
788  
789 /* checking whether the correct renderer exists */
790 0 ▶ if ((is_img && !rndr->make.image) || (!is_img && !rndr->make.link))
Branch: 4 branches
BranchTrueFalseTotal
if ((is_img && !rndr->make.image) || (!is_img && !rndr->make.link)) 0 0 0
if ((is_img && !rndr->make.image) || (!is_img && !rndr->make.link)) 0 0 0
if ((is_img && !rndr->make.image) || (!is_img && !rndr->make.link)) 0 0 0
if ((is_img && !rndr->make.image) || (!is_img && !rndr->make.link)) 0 0 0
791 0 goto cleanup;
792  
793 /* looking for the matching closing bracket */
794 0 ▶ for (level = 1; i < size; i += 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (level = 1; i < size; i += 1) { 0 0 0
795 0 ▶ if (data[i] == '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '\n') 0 0 0
796 0 text_has_nl = 1;
797  
798 0 ▶ else if (data[i - 1] == '\\')
Branch: 1 branch
BranchTrueFalseTotal
else if (data[i - 1] == '\\') 0 0 0
799 0 continue;
800  
801 0 ▶ else if (data[i] == '[')
Branch: 1 branch
BranchTrueFalseTotal
else if (data[i] == '[') 0 0 0
802 0 level++;
803  
804 0 ▶ else if (data[i] == ']') {
Branch: 1 branch
BranchTrueFalseTotal
else if (data[i] == ']') { 0 0 0
805 0 level--;
806 0 ▶ if (level <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (level <= 0) 0 0 0
807 0 break;
808 }
809 }
810  
811 0 ▶ if (i >= size)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) 0 0 0
812 0 goto cleanup;
813  
814 0 txt_e = i;
815 0 i += 1;
816  
817 /* skip any amount of whitespace or newline */
818 /* (this is much more laxist than original markdown syntax) */
819 0 ▶ while (i < size && isspace(data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && isspace(data[i])) 0 0 0
while (i < size && isspace(data[i])) 0 0 0
820 0 i++;
821  
822 /* inline style link */
823 0 ▶ if (i < size && data[i] == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == '(') { 0 0 0
if (i < size && data[i] == '(') { 0 0 0
824 /* skipping initial whitespace */
825 0 i += 1;
826  
827 0 ▶ while (i < size && isspace(data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && isspace(data[i])) 0 0 0
while (i < size && isspace(data[i])) 0 0 0
828 0 i++;
829  
830 0 link_b = i;
831  
832 /* looking for link end: ' " ) */
833 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
834 0 ▶ if (data[i] == '\\') i += 2;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '\\') i += 2; 0 0 0
835 0 ▶ else if (data[i] == ')' || data[i] == '\'' || data[i] == '"') break;
Branch: 3 branches
BranchTrueFalseTotal
else if (data[i] == ')' || data[i] == '\'' || data[i] == '"') break; 0 0 0
else if (data[i] == ')' || data[i] == '\'' || data[i] == '"') break; 0 0 0
else if (data[i] == ')' || data[i] == '\'' || data[i] == '"') break; 0 0 0
836 0 else i += 1;
837 }
838  
839 0 ▶ if (i >= size) goto cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) goto cleanup; 0 0 0
840 0 link_e = i;
841  
842 /* looking for title end if present */
843 0 ▶ if (data[i] == '\'' || data[i] == '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (data[i] == '\'' || data[i] == '"') { 0 0 0
if (data[i] == '\'' || data[i] == '"') { 0 0 0
844 0 i++;
845 0 title_b = i;
846  
847 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
848 0 ▶ if (data[i] == '\\') i += 2;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '\\') i += 2; 0 0 0
849 0 ▶ else if (data[i] == ')') break;
Branch: 1 branch
BranchTrueFalseTotal
else if (data[i] == ')') break; 0 0 0
850 0 else i += 1;
851 }
852  
853 0 ▶ if (i >= size) goto cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) goto cleanup; 0 0 0
854  
855 /* skipping whitespaces after title */
856 0 title_e = i - 1;
857 0 ▶ while (title_e > title_b && isspace(data[title_e]))
Branch: 2 branches
BranchTrueFalseTotal
while (title_e > title_b && isspace(data[title_e])) 0 0 0
while (title_e > title_b && isspace(data[title_e])) 0 0 0
858 0 title_e--;
859  
860 /* checking for closing quote presence */
861 0 ▶ if (data[title_e] != '\'' && data[title_e] != '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (data[title_e] != '\'' && data[title_e] != '"') { 0 0 0
if (data[title_e] != '\'' && data[title_e] != '"') { 0 0 0
862 0 title_b = title_e = 0;
863 0 link_e = i;
864 }
865 }
866  
867 /* remove whitespace at the end of the link */
868 0 ▶ while (link_e > link_b && isspace(data[link_e - 1]))
Branch: 2 branches
BranchTrueFalseTotal
while (link_e > link_b && isspace(data[link_e - 1])) 0 0 0
while (link_e > link_b && isspace(data[link_e - 1])) 0 0 0
869 0 link_e--;
870  
871 /* remove optional angle brackets around the link */
872 0 ▶ if (data[link_b] == '<') link_b++;
Branch: 1 branch
BranchTrueFalseTotal
if (data[link_b] == '<') link_b++; 0 0 0
873 0 ▶ if (data[link_e - 1] == '>') link_e--;
Branch: 1 branch
BranchTrueFalseTotal
if (data[link_e - 1] == '>') link_e--; 0 0 0
874  
875 /* building escaped link and title */
876 0 ▶ if (link_e > link_b) {
Branch: 1 branch
BranchTrueFalseTotal
if (link_e > link_b) { 0 0 0
877 0 link = rndr_newbuf(rndr, BUFFER_SPAN);
878 0 bufput(link, data + link_b, link_e - link_b);
879 }
880  
881 0 ▶ if (title_e > title_b) {
Branch: 1 branch
BranchTrueFalseTotal
if (title_e > title_b) { 0 0 0
882 0 title = rndr_newbuf(rndr, BUFFER_SPAN);
883 0 bufput(title, data + title_b, title_e - title_b);
884 }
885  
886 0 i++;
887 }
888  
889 /* reference style link */
890 0 ▶ else if (i < size && data[i] == '[') {
Branch: 2 branches
BranchTrueFalseTotal
else if (i < size && data[i] == '[') { 0 0 0
else if (i < size && data[i] == '[') { 0 0 0
891 0 struct buf id = { 0, 0, 0, 0, 0 };
892 struct link_ref *lr;
893  
894 /* looking for the id */
895 0 i += 1;
896 0 link_b = i;
897 0 ▶ while (i < size && data[i] != ']') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != ']') i++; 0 0 0
while (i < size && data[i] != ']') i++; 0 0 0
898 0 ▶ if (i >= size) goto cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) goto cleanup; 0 0 0
899 0 link_e = i;
900  
901 /* finding the link_ref */
902 0 ▶ if (link_b == link_e) {
Branch: 1 branch
BranchTrueFalseTotal
if (link_b == link_e) { 0 0 0
903 0 ▶ if (text_has_nl) {
Branch: 1 branch
BranchTrueFalseTotal
if (text_has_nl) { 0 0 0
904 0 struct buf *b = rndr_newbuf(rndr, BUFFER_SPAN);
905 size_t j;
906  
907 0 ▶ for (j = 1; j < txt_e; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < txt_e; j++) { 0 0 0
908 0 ▶ if (data[j] != '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (data[j] != '\n') 0 0 0
909 0 bufputc(b, data[j]);
910 0 ▶ else if (data[j - 1] != ' ')
Branch: 1 branch
BranchTrueFalseTotal
else if (data[j - 1] != ' ') 0 0 0
911 0 bufputc(b, ' ');
912 }
913  
914 0 id.data = b->data;
915 0 id.size = b->size;
916 } else {
917 0 id.data = data + 1;
918 0 id.size = txt_e - 1;
919 }
920 } else {
921 0 id.data = data + link_b;
922 0 id.size = link_e - link_b;
923 }
924  
925 0 lr = arr_sorted_find(&rndr->refs, &id, cmp_link_ref);
926 0 ▶ if (!lr) goto cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if (!lr) goto cleanup; 0 0 0
927  
928 /* keeping link and title from link_ref */
929 0 link = lr->link;
930 0 title = lr->title;
931 0 i += 1;
932 }
933  
934 /* shortcut reference style link */
935 else {
936 0 struct buf id = { 0, 0, 0, 0, 0 };
937 struct link_ref *lr;
938  
939 /* crafting the id */
940 0 ▶ if (text_has_nl) {
Branch: 1 branch
BranchTrueFalseTotal
if (text_has_nl) { 0 0 0
941 0 struct buf *b = rndr_newbuf(rndr, BUFFER_SPAN);
942 size_t j;
943  
944 0 ▶ for (j = 1; j < txt_e; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < txt_e; j++) { 0 0 0
945 0 ▶ if (data[j] != '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (data[j] != '\n') 0 0 0
946 0 bufputc(b, data[j]);
947 0 ▶ else if (data[j - 1] != ' ')
Branch: 1 branch
BranchTrueFalseTotal
else if (data[j - 1] != ' ') 0 0 0
948 0 bufputc(b, ' ');
949 }
950  
951 0 id.data = b->data;
952 0 id.size = b->size;
953 } else {
954 0 id.data = data + 1;
955 0 id.size = txt_e - 1;
956 }
957  
958 /* finding the link_ref */
959 0 lr = arr_sorted_find(&rndr->refs, &id, cmp_link_ref);
960 0 ▶ if (!lr) goto cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if (!lr) goto cleanup; 0 0 0
961  
962 /* keeping link and title from link_ref */
963 0 link = lr->link;
964 0 title = lr->title;
965  
966 /* rewinding the whitespace */
967 0 i = txt_e + 1;
968 }
969  
970 /* building content: img alt is escaped, link content is parsed */
971 0 ▶ if (txt_e > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (txt_e > 1) { 0 0 0
972 0 content = rndr_newbuf(rndr, BUFFER_SPAN);
973 0 ▶ if (is_img) bufput(content, data + 1, txt_e - 1);
Branch: 1 branch
BranchTrueFalseTotal
if (is_img) bufput(content, data + 1, txt_e - 1); 0 0 0
974 0 else parse_inline(content, rndr, data + 1, txt_e - 1);
975 }
976  
977 0 ▶ if (link) {
Branch: 1 branch
BranchTrueFalseTotal
if (link) { 0 0 0
978 0 u_link = rndr_newbuf(rndr, BUFFER_SPAN);
979 0 unscape_text(u_link, link);
980 }
981  
982 /* calling the relevant rendering function */
983 0 ▶ if (is_img) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_img) { 0 0 0
984 0 ▶ if (ob->size && ob->data[ob->size - 1] == '!')
Branch: 2 branches
BranchTrueFalseTotal
if (ob->size && ob->data[ob->size - 1] == '!') 0 0 0
if (ob->size && ob->data[ob->size - 1] == '!') 0 0 0
985 0 ob->size -= 1;
986  
987 0 ret = rndr->make.image(ob, u_link, title, content, rndr->make.opaque);
988 } else {
989 0 ret = rndr->make.link(ob, u_link, title, content, rndr->make.opaque);
990 }
991  
992 /* cleanup */
993 0 cleanup:
994 0 rndr->work_bufs[BUFFER_SPAN].size = (int)org_work_size;
995 0 ▶ return ret ? i : 0;
Branch: 1 branch
BranchTrueFalseTotal
return ret ? i : 0; 0 0 0
996 }
997  
998  
999  
1000 /*********************************
1001 * BLOCK-LEVEL PARSING FUNCTIONS *
1002 *********************************/
1003  
1004 /* is_empty • returns the line length when it is empty, 0 otherwise */
1005 static size_t
1006 11 is_empty(char *data, size_t size)
1007 {
1008 size_t i;
1009 11 ▶ for (i = 0; i < size && data[i] != '\n'; i += 1)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < size && data[i] != '\n'; i += 1) 11 0 11
for (i = 0; i < size && data[i] != '\n'; i += 1) 9 2 11
1010 9 ▶ if (data[i] != ' ' && data[i] != '\t') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (data[i] != ' ' && data[i] != '\t') return 0; 9 0 9
if (data[i] != ' ' && data[i] != '\t') return 0; 9 0 9
1011 2 return i + 1;
1012 }
1013  
1014 /* is_hrule • returns whether a line is a horizontal rule */
1015 static int
1016 7 is_hrule(char *data, size_t size)
1017 {
1018 7 size_t i = 0, n = 0;
1019 char c;
1020  
1021 /* skipping initial spaces */
1022 7 ▶ if (size < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < 3) return 0; 0 7 7
1023 7 ▶ if (data[0] == ' ') { i += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[0] == ' ') { i += 1; 0 7 7
1024 0 ▶ if (data[1] == ' ') { i += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[1] == ' ') { i += 1; 0 0 0
1025 0 ▶ if (data[2] == ' ') { i += 1; } } }
Branch: 1 branch
BranchTrueFalseTotal
if (data[2] == ' ') { i += 1; } } } 0 0 0
1026  
1027 /* looking at the hrule char */
1028 7 ▶ if (i + 2 >= size
Branch: 1 branch
BranchTrueFalseTotal
if (i + 2 >= size 7 0 7
1029 7 ▶ || (data[i] != '*' && data[i] != '-' && data[i] != '_'))
Branch: 3 branches
BranchTrueFalseTotal
|| (data[i] != '*' && data[i] != '-' && data[i] != '_')) 5 2 7
|| (data[i] != '*' && data[i] != '-' && data[i] != '_')) 5 0 5
|| (data[i] != '*' && data[i] != '-' && data[i] != '_')) 5 0 5
1030 5 return 0;
1031 2 c = data[i];
1032  
1033 /* the whole line must be the char or whitespace */
1034 6 ▶ while (i < size && data[i] != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '\n') { 6 0 6
while (i < size && data[i] != '\n') { 6 0 6
1035 6 ▶ if (data[i] == c) n += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == c) n += 1; 2 4 6
1036 4 ▶ else if (data[i] != ' ' && data[i] != '\t')
Branch: 2 branches
BranchTrueFalseTotal
else if (data[i] != ' ' && data[i] != '\t') 2 2 4
else if (data[i] != ' ' && data[i] != '\t') 2 0 2
1037 2 return 0;
1038 4 i += 1; }
1039  
1040 0 return n >= 3;
1041 }
1042  
1043 /* check if a line is a code fence; return its size if it is */
1044 static size_t
1045 0 is_codefence(char *data, size_t size, struct buf *syntax)
1046 {
1047 0 size_t i = 0, n = 0;
1048 char c;
1049  
1050 /* skipping initial spaces */
1051 0 ▶ if (size < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < 3) return 0; 0 0 0
1052 0 ▶ if (data[0] == ' ') { i += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[0] == ' ') { i += 1; 0 0 0
1053 0 ▶ if (data[1] == ' ') { i += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[1] == ' ') { i += 1; 0 0 0
1054 0 ▶ if (data[2] == ' ') { i += 1; } } }
Branch: 1 branch
BranchTrueFalseTotal
if (data[2] == ' ') { i += 1; } } } 0 0 0
1055  
1056 /* looking at the hrule char */
1057 0 ▶ if (i + 2 >= size || !(data[i] == '~' || data[i] == '`'))
Branch: 3 branches
BranchTrueFalseTotal
if (i + 2 >= size || !(data[i] == '~' || data[i] == '`')) 0 0 0
if (i + 2 >= size || !(data[i] == '~' || data[i] == '`')) 0 0 0
if (i + 2 >= size || !(data[i] == '~' || data[i] == '`')) 0 0 0
1058 0 return 0;
1059  
1060 0 c = data[i];
1061  
1062 /* the whole line must be the char or whitespace */
1063 0 ▶ while (i < size && data[i] == c) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] == c) { 0 0 0
while (i < size && data[i] == c) { 0 0 0
1064 0 n++; i++;
1065 }
1066  
1067 0 ▶ if (n < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 3) 0 0 0
1068 0 return 0;
1069  
1070 0 ▶ if (syntax != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (syntax != NULL) { 0 0 0
1071 0 size_t syn = 0;
1072  
1073 0 ▶ while (i < size && (data[i] == ' ' || data[i] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i < size && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i < size && (data[i] == ' ' || data[i] == '\t')) 0 0 0
1074 0 i++;
1075  
1076 0 syntax->data = data + i;
1077  
1078 0 ▶ if (i < size && data[i] == '{') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == '{') { 0 0 0
if (i < size && data[i] == '{') { 0 0 0
1079 0 i++; syntax->data++;
1080  
1081 0 ▶ while (i < size && data[i] != '}' && data[i] != '\n') {
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && data[i] != '}' && data[i] != '\n') { 0 0 0
while (i < size && data[i] != '}' && data[i] != '\n') { 0 0 0
while (i < size && data[i] != '}' && data[i] != '\n') { 0 0 0
1082 0 syn++; i++;
1083 }
1084  
1085 0 ▶ if (i == size || data[i] != '}')
Branch: 2 branches
BranchTrueFalseTotal
if (i == size || data[i] != '}') 0 0 0
if (i == size || data[i] != '}') 0 0 0
1086 0 return 0;
1087  
1088 /* strip all whitespace at the beginning and the end
1089 * of the {} block */
1090 0 ▶ while (syn > 0 && isspace(syntax->data[0])) {
Branch: 2 branches
BranchTrueFalseTotal
while (syn > 0 && isspace(syntax->data[0])) { 0 0 0
while (syn > 0 && isspace(syntax->data[0])) { 0 0 0
1091 0 syntax->data++; syn--;
1092 }
1093  
1094 0 ▶ while (syn > 0 && isspace(syntax->data[syn - 1]))
Branch: 2 branches
BranchTrueFalseTotal
while (syn > 0 && isspace(syntax->data[syn - 1])) 0 0 0
while (syn > 0 && isspace(syntax->data[syn - 1])) 0 0 0
1095 0 syn--;
1096  
1097 0 i++;
1098 } else {
1099 0 ▶ while (i < size && !isspace(data[i])) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && !isspace(data[i])) { 0 0 0
while (i < size && !isspace(data[i])) { 0 0 0
1100 0 syn++; i++;
1101 }
1102 }
1103  
1104 0 syntax->size = syn;
1105 }
1106  
1107 0 ▶ while (i < size && data[i] != '\n') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '\n') { 0 0 0
while (i < size && data[i] != '\n') { 0 0 0
1108 0 ▶ if (!isspace(data[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!isspace(data[i])) 0 0 0
1109 0 return 0;
1110  
1111 0 i++;
1112 }
1113  
1114 0 return i + 1;
1115 }
1116  
1117 /* is_atxheader • returns whether the line is a hash-prefixed header */
1118 static int
1119 7 is_atxheader(struct render *rndr, char *data, size_t size)
1120 {
1121 7 ▶ if (data[0] != '#')
Branch: 1 branch
BranchTrueFalseTotal
if (data[0] != '#') 7 0 7
1122 7 return 0;
1123  
1124 0 ▶ if (rndr->ext_flags & MKDEXT_SPACE_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->ext_flags & MKDEXT_SPACE_HEADERS) { 0 0 0
1125 0 size_t level = 0;
1126  
1127 0 ▶ while (level < size && level < 6 && data[level] == '#')
Branch: 3 branches
BranchTrueFalseTotal
while (level < size && level < 6 && data[level] == '#') 0 0 0
while (level < size && level < 6 && data[level] == '#') 0 0 0
while (level < size && level < 6 && data[level] == '#') 0 0 0
1128 0 level++;
1129  
1130 0 ▶ if (level < size && data[level] != ' ' && data[level] != '\t')
Branch: 3 branches
BranchTrueFalseTotal
if (level < size && data[level] != ' ' && data[level] != '\t') 0 0 0
if (level < size && data[level] != ' ' && data[level] != '\t') 0 0 0
if (level < size && data[level] != ' ' && data[level] != '\t') 0 0 0
1131 0 return 0;
1132 }
1133  
1134 0 return 1;
1135 }
1136  
1137 /* is_headerline • returns whether the line is a setext-style hdr underline */
1138 static int
1139 4 is_headerline(char *data, size_t size)
1140 {
1141 4 size_t i = 0;
1142  
1143 /* test of level 1 header */
1144 4 ▶ if (data[i] == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '=') { 1 3 4
1145 7 ▶ for (i = 1; i < size && data[i] == '='; i += 1);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 1; i < size && data[i] == '='; i += 1); 7 0 7
for (i = 1; i < size && data[i] == '='; i += 1); 6 1 7
1146 1 ▶ while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1; 1 0 1
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 1 1
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 1 1
1147 1 ▶ return (i >= size || data[i] == '\n') ? 1 : 0; }
Branch: 2 branches
BranchTrueFalseTotal
return (i >= size || data[i] == '\n') ? 1 : 0; } 1 0 1
return (i >= size || data[i] == '\n') ? 1 : 0; } 1 0 1
1148  
1149 /* test of level 2 header */
1150 3 ▶ if (data[i] == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '-') { 0 3 3
1151 0 ▶ for (i = 1; i < size && data[i] == '-'; i += 1);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 1; i < size && data[i] == '-'; i += 1); 0 0 0
for (i = 1; i < size && data[i] == '-'; i += 1); 0 0 0
1152 0 ▶ while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
1153 0 ▶ return (i >= size || data[i] == '\n') ? 2 : 0; }
Branch: 2 branches
BranchTrueFalseTotal
return (i >= size || data[i] == '\n') ? 2 : 0; } 0 0 0
return (i >= size || data[i] == '\n') ? 2 : 0; } 0 0 0
1154  
1155 3 return 0;
1156 }
1157  
1158 /* prefix_quote • returns blockquote prefix length */
1159 static size_t
1160 3 prefix_quote(char *data, size_t size)
1161 {
1162 3 size_t i = 0;
1163 3 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 3 0 3
if (i < size && data[i] == ' ') i += 1; 0 3 3
1164 3 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 3 0 3
if (i < size && data[i] == ' ') i += 1; 0 3 3
1165 3 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 3 0 3
if (i < size && data[i] == ' ') i += 1; 0 3 3
1166 3 ▶ if (i < size && data[i] == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == '>') { 3 0 3
if (i < size && data[i] == '>') { 0 3 3
1167 0 ▶ if (i + 1 < size && (data[i + 1] == ' ' || data[i+1] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < size && (data[i + 1] == ' ' || data[i+1] == '\t')) 0 0 0
if (i + 1 < size && (data[i + 1] == ' ' || data[i+1] == '\t')) 0 0 0
if (i + 1 < size && (data[i + 1] == ' ' || data[i+1] == '\t')) 0 0 0
1168 0 return i + 2;
1169 0 else return i + 1; }
1170 3 else return 0;
1171 }
1172  
1173 /* prefix_code • returns prefix length for block code*/
1174 static size_t
1175 3 prefix_code(char *data, size_t size)
1176 {
1177 3 ▶ if (size > 0 && data[0] == '\t') return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (size > 0 && data[0] == '\t') return 1; 3 0 3
if (size > 0 && data[0] == '\t') return 1; 0 3 3
1178 3 ▶ if (size > 3 && data[0] == ' ' && data[1] == ' '
Branch: 3 branches
BranchTrueFalseTotal
if (size > 3 && data[0] == ' ' && data[1] == ' ' 3 0 3
if (size > 3 && data[0] == ' ' && data[1] == ' ' 0 3 3
if (size > 3 && data[0] == ' ' && data[1] == ' ' 0 0 0
1179 0 ▶ && data[2] == ' ' && data[3] == ' ') return 4;
Branch: 2 branches
BranchTrueFalseTotal
&& data[2] == ' ' && data[3] == ' ') return 4; 0 0 0
&& data[2] == ' ' && data[3] == ' ') return 4; 0 0 0
1180 3 return 0;
1181 }
1182  
1183 /* prefix_oli • returns ordered list item prefix */
1184 static size_t
1185 3 prefix_oli(char *data, size_t size)
1186 {
1187 3 size_t i = 0;
1188 3 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 3 0 3
if (i < size && data[i] == ' ') i += 1; 0 3 3
1189 3 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 3 0 3
if (i < size && data[i] == ' ') i += 1; 0 3 3
1190 3 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 3 0 3
if (i < size && data[i] == ' ') i += 1; 0 3 3
1191 3 ▶ if (i >= size || data[i] < '0' || data[i] > '9') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (i >= size || data[i] < '0' || data[i] > '9') return 0; 3 0 3
if (i >= size || data[i] < '0' || data[i] > '9') return 0; 3 0 3
if (i >= size || data[i] < '0' || data[i] > '9') return 0; 3 0 3
1192 0 ▶ while (i < size && data[i] >= '0' && data[i] <= '9') i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && data[i] >= '0' && data[i] <= '9') i += 1; 0 0 0
while (i < size && data[i] >= '0' && data[i] <= '9') i += 1; 0 0 0
while (i < size && data[i] >= '0' && data[i] <= '9') i += 1; 0 0 0
1193 0 ▶ if (i + 1 >= size || data[i] != '.'
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 >= size || data[i] != '.' 0 0 0
if (i + 1 >= size || data[i] != '.' 0 0 0
1194 0 ▶ || (data[i + 1] != ' ' && data[i + 1] != '\t')) return 0;
Branch: 2 branches
BranchTrueFalseTotal
|| (data[i + 1] != ' ' && data[i + 1] != '\t')) return 0; 0 0 0
|| (data[i + 1] != ' ' && data[i + 1] != '\t')) return 0; 0 0 0
1195 0 return i + 2;
1196 }
1197  
1198 /* prefix_uli • returns ordered list item prefix */
1199 static size_t
1200 7 prefix_uli(char *data, size_t size)
1201 {
1202 7 size_t i = 0;
1203 7 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 7 0 7
if (i < size && data[i] == ' ') i += 1; 0 7 7
1204 7 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 7 0 7
if (i < size && data[i] == ' ') i += 1; 0 7 7
1205 7 ▶ if (i < size && data[i] == ' ') i += 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == ' ') i += 1; 7 0 7
if (i < size && data[i] == ' ') i += 1; 0 7 7
1206 7 ▶ if (i + 1 >= size
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= size 7 0 7
1207 7 ▶ || (data[i] != '*' && data[i] != '+' && data[i] != '-')
Branch: 3 branches
BranchTrueFalseTotal
|| (data[i] != '*' && data[i] != '+' && data[i] != '-') 3 4 7
|| (data[i] != '*' && data[i] != '+' && data[i] != '-') 3 0 3
|| (data[i] != '*' && data[i] != '+' && data[i] != '-') 0 3 3
1208 4 ▶ || (data[i + 1] != ' ' && data[i + 1] != '\t'))
Branch: 2 branches
BranchTrueFalseTotal
|| (data[i + 1] != ' ' && data[i + 1] != '\t')) 0 4 4
|| (data[i + 1] != ' ' && data[i + 1] != '\t')) 0 0 0
1209 3 return 0;
1210 4 return i + 2;
1211 }
1212  
1213  
1214 /* parse_block • parsing of one block, returning next char to parse */
1215 static void parse_block(struct buf *ob, struct render *rndr,
1216 char *data, size_t size);
1217  
1218  
1219 /* parse_blockquote • handles parsing of a blockquote fragment */
1220 static size_t
1221 0 parse_blockquote(struct buf *ob, struct render *rndr, char *data, size_t size)
1222 {
1223 0 size_t beg, end = 0, pre, work_size = 0;
1224 0 char *work_data = 0;
1225 0 struct buf *out = 0;
1226  
1227 0 out = rndr_newbuf(rndr, BUFFER_BLOCK);
1228 0 beg = 0;
1229 0 ▶ while (beg < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (beg < size) { 0 0 0
1230 0 ▶ for (end = beg + 1; end < size && data[end - 1] != '\n'; end++);
Branch: 2 branches
BranchTrueFalseTotal
for (end = beg + 1; end < size && data[end - 1] != '\n'; end++); 0 0 0
for (end = beg + 1; end < size && data[end - 1] != '\n'; end++); 0 0 0
1231  
1232 0 pre = prefix_quote(data + beg, end - beg);
1233  
1234 0 ▶ if (pre)
Branch: 1 branch
BranchTrueFalseTotal
if (pre) 0 0 0
1235 0 beg += pre; /* skipping prefix */
1236  
1237 /* empty line followed by non-quote line */
1238 0 ▶ else if (is_empty(data + beg, end - beg) &&
Branch: 2 branches
BranchTrueFalseTotal
else if (is_empty(data + beg, end - beg) && 0 0 0
else if (is_empty(data + beg, end - beg) && 0 0 0
1239 0 (end >= size || (prefix_quote(data + end, size - end) == 0 &&
1240 0 !is_empty(data + end, size - end))))
1241 break;
1242  
1243 0 ▶ if (beg < end) { /* copy into the in-place working buffer */
Branch: 1 branch
BranchTrueFalseTotal
if (beg < end) { /* copy into the in-place working buffer */ 0 0 0
1244 /* bufput(work, data + beg, end - beg); */
1245 0 ▶ if (!work_data)
Branch: 1 branch
BranchTrueFalseTotal
if (!work_data) 0 0 0
1246 0 work_data = data + beg;
1247 0 ▶ else if (data + beg != work_data + work_size)
Branch: 1 branch
BranchTrueFalseTotal
else if (data + beg != work_data + work_size) 0 0 0
1248 0 memmove(work_data + work_size, data + beg, end - beg);
1249 0 work_size += end - beg;
1250 }
1251 0 beg = end;
1252 }
1253  
1254 0 parse_block(out, rndr, work_data, work_size);
1255 0 ▶ if (rndr->make.blockquote)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.blockquote) 0 0 0
1256 0 rndr->make.blockquote(ob, out, rndr->make.opaque);
1257 0 rndr_popbuf(rndr, BUFFER_BLOCK);
1258 0 return end;
1259 }
1260  
1261 static size_t
1262 parse_htmlblock(struct buf *ob, struct render *rndr, char *data, size_t size, int do_render);
1263  
1264 /* parse_blockquote • handles parsing of a regular paragraph */
1265 static size_t
1266 2 parse_paragraph(struct buf *ob, struct render *rndr, char *data, size_t size)
1267 {
1268 2 size_t i = 0, end = 0;
1269 2 int level = 0;
1270 2 struct buf work = { data, 0, 0, 0, 0 }; /* volatile working buffer */
1271  
1272 5 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 4 1 5
1273 119 ▶ for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */;
Branch: 2 branches
BranchTrueFalseTotal
for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */; 118 1 119
for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */; 115 3 118
1274  
1275 4 ▶ if (is_empty(data + i, size - i) || (level = is_headerline(data + i, size - i)) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (is_empty(data + i, size - i) || (level = is_headerline(data + i, size - i)) != 0) 4 0 4
if (is_empty(data + i, size - i) || (level = is_headerline(data + i, size - i)) != 0) 3 1 4
1276 break;
1277  
1278 3 ▶ if (rndr->ext_flags & MKDEXT_LAX_HTML_BLOCKS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->ext_flags & MKDEXT_LAX_HTML_BLOCKS) { 0 3 3
1279 0 ▶ if (data[i] == '<' && rndr->make.blockhtml && parse_htmlblock(ob, rndr, data + i, size - i, 0)) {
Branch: 3 branches
BranchTrueFalseTotal
if (data[i] == '<' && rndr->make.blockhtml && parse_htmlblock(ob, rndr, data + i, size - i, 0)) { 0 0 0
if (data[i] == '<' && rndr->make.blockhtml && parse_htmlblock(ob, rndr, data + i, size - i, 0)) { 0 0 0
if (data[i] == '<' && rndr->make.blockhtml && parse_htmlblock(ob, rndr, data + i, size - i, 0)) { 0 0 0
1280 0 end = i;
1281 0 break;
1282 }
1283 }
1284  
1285 3 ▶ if (is_atxheader(rndr, data + i, size - i) || is_hrule(data + i, size - i)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_atxheader(rndr, data + i, size - i) || is_hrule(data + i, size - i)) { 3 0 3
if (is_atxheader(rndr, data + i, size - i) || is_hrule(data + i, size - i)) { 0 3 3
1286 0 end = i;
1287 0 break;
1288 }
1289  
1290 3 i = end;
1291 }
1292  
1293 2 work.size = i;
1294 4 ▶ while (work.size && data[work.size - 1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (work.size && data[work.size - 1] == '\n') 4 0 4
while (work.size && data[work.size - 1] == '\n') 2 2 4
1295 2 work.size--;
1296  
1297 2 ▶ if (!level) {
Branch: 1 branch
BranchTrueFalseTotal
if (!level) { 1 1 2
1298 1 struct buf *tmp = rndr_newbuf(rndr, BUFFER_BLOCK);
1299 1 parse_inline(tmp, rndr, work.data, work.size);
1300 1 ▶ if (rndr->make.paragraph)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.paragraph) 1 0 1
1301 1 rndr->make.paragraph(ob, tmp, rndr->make.opaque);
1302 1 rndr_popbuf(rndr, BUFFER_BLOCK);
1303 } else {
1304 struct buf *header_work;
1305  
1306 1 ▶ if (work.size) {
Branch: 1 branch
BranchTrueFalseTotal
if (work.size) { 1 0 1
1307 size_t beg;
1308 1 i = work.size;
1309 1 work.size -= 1;
1310  
1311 7 ▶ while (work.size && data[work.size] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (work.size && data[work.size] != '\n') 6 1 7
while (work.size && data[work.size] != '\n') 6 0 6
1312 6 work.size -= 1;
1313  
1314 1 beg = work.size + 1;
1315 1 ▶ while (work.size && data[work.size - 1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (work.size && data[work.size - 1] == '\n') 0 1 1
while (work.size && data[work.size - 1] == '\n') 0 0 0
1316 0 work.size -= 1;
1317  
1318 1 ▶ if (work.size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (work.size > 0) { 0 1 1
1319 0 struct buf *tmp = rndr_newbuf(rndr, BUFFER_BLOCK);
1320 0 parse_inline(tmp, rndr, work.data, work.size);
1321  
1322 0 ▶ if (rndr->make.paragraph)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.paragraph) 0 0 0
1323 0 rndr->make.paragraph(ob, tmp, rndr->make.opaque);
1324  
1325 0 rndr_popbuf(rndr, BUFFER_BLOCK);
1326 0 work.data += beg;
1327 0 work.size = i - beg;
1328 }
1329 1 else work.size = i;
1330 }
1331  
1332 1 header_work = rndr_newbuf(rndr, BUFFER_SPAN);
1333 1 parse_inline(header_work, rndr, work.data, work.size);
1334  
1335 1 ▶ if (rndr->make.header)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.header) 1 0 1
1336 1 rndr->make.header(ob, header_work, (int)level, rndr->make.opaque);
1337  
1338 1 rndr_popbuf(rndr, BUFFER_SPAN);
1339 }
1340  
1341 2 return end;
1342 }
1343  
1344 /* parse_fencedcode • handles parsing of a block-level code fragment */
1345 static size_t
1346 0 parse_fencedcode(struct buf *ob, struct render *rndr, char *data, size_t size)
1347 {
1348 size_t beg, end;
1349 0 struct buf *work = 0;
1350 0 struct buf lang = { 0, 0, 0, 0, 0 };
1351  
1352 0 beg = is_codefence(data, size, &lang);
1353 0 ▶ if (beg == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (beg == 0) return 0; 0 0 0
1354  
1355 0 work = rndr_newbuf(rndr, BUFFER_BLOCK);
1356  
1357 0 ▶ while (beg < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (beg < size) { 0 0 0
1358 size_t fence_end;
1359  
1360 0 fence_end = is_codefence(data + beg, size - beg, NULL);
1361 0 ▶ if (fence_end != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fence_end != 0) { 0 0 0
1362 0 beg += fence_end;
1363 0 break;
1364 }
1365  
1366 0 ▶ for (end = beg + 1; end < size && data[end - 1] != '\n'; end += 1);
Branch: 2 branches
BranchTrueFalseTotal
for (end = beg + 1; end < size && data[end - 1] != '\n'; end += 1); 0 0 0
for (end = beg + 1; end < size && data[end - 1] != '\n'; end += 1); 0 0 0
1367  
1368 0 ▶ if (beg < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (beg < end) { 0 0 0
1369 /* verbatim copy to the working buffer,
1370 escaping entities */
1371 0 ▶ if (is_empty(data + beg, end - beg))
Branch: 1 branch
BranchTrueFalseTotal
if (is_empty(data + beg, end - beg)) 0 0 0
1372 0 bufputc(work, '\n');
1373 0 else bufput(work, data + beg, end - beg);
1374 }
1375 0 beg = end;
1376 }
1377  
1378 0 ▶ if (work->size && work->data[work->size - 1] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
if (work->size && work->data[work->size - 1] != '\n') 0 0 0
if (work->size && work->data[work->size - 1] != '\n') 0 0 0
1379 0 bufputc(work, '\n');
1380  
1381 0 ▶ if (rndr->make.blockcode)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.blockcode) 0 0 0
1382 0 ▶ rndr->make.blockcode(ob, work, lang.size ? &lang : NULL, rndr->make.opaque);
Branch: 1 branch
BranchTrueFalseTotal
rndr->make.blockcode(ob, work, lang.size ? &lang : NULL, rndr->make.opaque); 0 0 0
1383  
1384 0 rndr_popbuf(rndr, BUFFER_BLOCK);
1385 0 return beg;
1386 }
1387  
1388 static size_t
1389 0 parse_blockcode(struct buf *ob, struct render *rndr, char *data, size_t size)
1390 {
1391 size_t beg, end, pre;
1392 0 struct buf *work = 0;
1393  
1394 0 work = rndr_newbuf(rndr, BUFFER_BLOCK);
1395  
1396 0 beg = 0;
1397 0 ▶ while (beg < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (beg < size) { 0 0 0
1398 0 ▶ for (end = beg + 1; end < size && data[end - 1] != '\n'; end++) {};
Branch: 2 branches
BranchTrueFalseTotal
for (end = beg + 1; end < size && data[end - 1] != '\n'; end++) {}; 0 0 0
for (end = beg + 1; end < size && data[end - 1] != '\n'; end++) {}; 0 0 0
1399 0 pre = prefix_code(data + beg, end - beg);
1400  
1401 0 ▶ if (pre)
Branch: 1 branch
BranchTrueFalseTotal
if (pre) 0 0 0
1402 0 beg += pre; /* skipping prefix */
1403 0 ▶ else if (!is_empty(data + beg, end - beg))
Branch: 1 branch
BranchTrueFalseTotal
else if (!is_empty(data + beg, end - beg)) 0 0 0
1404 /* non-empty non-prefixed line breaks the pre */
1405 0 break;
1406  
1407 0 ▶ if (beg < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (beg < end) { 0 0 0
1408 /* verbatim copy to the working buffer,
1409 escaping entities */
1410 0 ▶ if (is_empty(data + beg, end - beg))
Branch: 1 branch
BranchTrueFalseTotal
if (is_empty(data + beg, end - beg)) 0 0 0
1411 0 bufputc(work, '\n');
1412 0 else bufput(work, data + beg, end - beg);
1413 }
1414 0 beg = end;
1415 }
1416  
1417 0 ▶ while (work->size && work->data[work->size - 1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (work->size && work->data[work->size - 1] == '\n') 0 0 0
while (work->size && work->data[work->size - 1] == '\n') 0 0 0
1418 0 work->size -= 1;
1419  
1420 0 bufputc(work, '\n');
1421  
1422 0 ▶ if (rndr->make.blockcode)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.blockcode) 0 0 0
1423 0 rndr->make.blockcode(ob, work, NULL, rndr->make.opaque);
1424  
1425 0 rndr_popbuf(rndr, BUFFER_BLOCK);
1426 0 return beg;
1427 }
1428  
1429 /* parse_listitem • parsing of a single list item */
1430 /* assuming initial prefix is already removed */
1431 static size_t
1432 2 parse_listitem(struct buf *ob, struct render *rndr, char *data, size_t size, int *flags)
1433 {
1434 2 struct buf *work = 0, *inter = 0;
1435 2 size_t beg = 0, end, pre, sublist = 0, orgpre = 0, i;
1436 2 int in_empty = 0, has_inside_empty = 0;
1437  
1438 /* keeping track of the first indentation prefix */
1439 2 ▶ while (orgpre < 3 && orgpre < size && data[orgpre] == ' ')
Branch: 3 branches
BranchTrueFalseTotal
while (orgpre < 3 && orgpre < size && data[orgpre] == ' ') 2 0 2
while (orgpre < 3 && orgpre < size && data[orgpre] == ' ') 2 0 2
while (orgpre < 3 && orgpre < size && data[orgpre] == ' ') 0 2 2
1440 0 orgpre++;
1441  
1442 2 beg = prefix_uli(data, size);
1443 2 ▶ if (!beg)
Branch: 1 branch
BranchTrueFalseTotal
if (!beg) 0 2 2
1444 0 beg = prefix_oli(data, size);
1445  
1446 2 ▶ if (!beg)
Branch: 1 branch
BranchTrueFalseTotal
if (!beg) 0 2 2
1447 0 return 0;
1448  
1449 /* skipping to the beginning of the following line */
1450 2 end = beg;
1451 10 ▶ while (end < size && data[end - 1] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (end < size && data[end - 1] != '\n') 10 0 10
while (end < size && data[end - 1] != '\n') 8 2 10
1452 8 end++;
1453  
1454 /* getting working buffers */
1455 2 work = rndr_newbuf(rndr, BUFFER_SPAN);
1456 2 inter = rndr_newbuf(rndr, BUFFER_SPAN);
1457  
1458 /* putting the first line into the working buffer */
1459 2 bufput(work, data + beg, end - beg);
1460 2 beg = end;
1461  
1462 /* process the following lines */
1463 3 ▶ while (beg < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (beg < size) { 3 0 3
1464 3 end++;
1465  
1466 30 ▶ while (end < size && data[end - 1] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (end < size && data[end - 1] != '\n') 30 0 30
while (end < size && data[end - 1] != '\n') 27 3 30
1467 27 end++;
1468  
1469 /* process an empty line */
1470 3 ▶ if (is_empty(data + beg, end - beg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_empty(data + beg, end - beg)) { 1 2 3
1471 1 in_empty = 1;
1472 1 beg = end;
1473 1 continue;
1474 }
1475  
1476 /* calculating the indentation */
1477 2 i = 0;
1478 2 ▶ while (i < 4 && beg + i < end && data[beg + i] == ' ')
Branch: 3 branches
BranchTrueFalseTotal
while (i < 4 && beg + i < end && data[beg + i] == ' ') 2 0 2
while (i < 4 && beg + i < end && data[beg + i] == ' ') 2 0 2
while (i < 4 && beg + i < end && data[beg + i] == ' ') 0 2 2
1479 0 i++;
1480  
1481 2 pre = i;
1482 2 ▶ if (data[beg] == '\t') { i = 1; pre = 8; }
Branch: 1 branch
BranchTrueFalseTotal
if (data[beg] == '\t') { i = 1; pre = 8; } 0 2 2
1483  
1484 /* checking for a new item */
1485 3 if ((prefix_uli(data + beg + i, end - beg - i) &&
1486 2 ▶ !is_hrule(data + beg + i, end - beg - i)) ||
Branch: 1 branch
BranchTrueFalseTotal
!is_hrule(data + beg + i, end - beg - i)) || 0 1 1
1487 1 prefix_oli(data + beg + i, end - beg - i)) {
1488 1 ▶ if (in_empty)
Branch: 1 branch
BranchTrueFalseTotal
if (in_empty) 0 1 1
1489 0 has_inside_empty = 1;
1490  
1491 1 ▶ if (pre == orgpre) /* the following item must have */
Branch: 1 branch
BranchTrueFalseTotal
if (pre == orgpre) /* the following item must have */ 1 0 1
1492 1 break; /* the same indentation */
1493  
1494 0 ▶ if (!sublist)
Branch: 1 branch
BranchTrueFalseTotal
if (!sublist) 0 0 0
1495 0 sublist = work->size;
1496 }
1497 /* joining only indented stuff after empty lines */
1498 1 ▶ else if (in_empty && i < 4 && data[beg] != '\t') {
Branch: 3 branches
BranchTrueFalseTotal
else if (in_empty && i < 4 && data[beg] != '\t') { 1 0 1
else if (in_empty && i < 4 && data[beg] != '\t') { 1 0 1
else if (in_empty && i < 4 && data[beg] != '\t') { 1 0 1
1499 1 *flags |= MKD_LI_END;
1500 1 break;
1501 }
1502 0 ▶ else if (in_empty) {
Branch: 1 branch
BranchTrueFalseTotal
else if (in_empty) { 0 0 0
1503 0 bufputc(work, '\n');
1504 0 has_inside_empty = 1;
1505 }
1506  
1507 0 in_empty = 0;
1508  
1509 /* adding the line without prefix into the working buffer */
1510 0 bufput(work, data + beg + i, end - beg - i);
1511 0 beg = end;
1512 }
1513  
1514 /* render of li contents */
1515 2 ▶ if (has_inside_empty)
Branch: 1 branch
BranchTrueFalseTotal
if (has_inside_empty) 0 2 2
1516 0 *flags |= MKD_LI_BLOCK;
1517  
1518 2 ▶ if (*flags & MKD_LI_BLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (*flags & MKD_LI_BLOCK) { 0 2 2
1519 /* intermediate render of block li */
1520 0 ▶ if (sublist && sublist < work->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (sublist && sublist < work->size) { 0 0 0
if (sublist && sublist < work->size) { 0 0 0
1521 0 parse_block(inter, rndr, work->data, sublist);
1522 0 parse_block(inter, rndr, work->data + sublist, work->size - sublist);
1523 }
1524 else
1525 0 parse_block(inter, rndr, work->data, work->size);
1526 } else {
1527 /* intermediate render of inline li */
1528 2 ▶ if (sublist && sublist < work->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (sublist && sublist < work->size) { 0 2 2
if (sublist && sublist < work->size) { 0 0 0
1529 0 parse_inline(inter, rndr, work->data, sublist);
1530 0 parse_block(inter, rndr, work->data + sublist, work->size - sublist);
1531 }
1532 else
1533 2 parse_inline(inter, rndr, work->data, work->size);
1534 }
1535  
1536 /* render of li itself */
1537 2 ▶ if (rndr->make.listitem)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.listitem) 2 0 2
1538 2 rndr->make.listitem(ob, inter, *flags, rndr->make.opaque);
1539  
1540 2 rndr_popbuf(rndr, BUFFER_SPAN);
1541 2 rndr_popbuf(rndr, BUFFER_SPAN);
1542 2 return beg;
1543 }
1544  
1545  
1546 /* parse_list • parsing ordered or unordered list block */
1547 static size_t
1548 1 parse_list(struct buf *ob, struct render *rndr, char *data, size_t size, int flags)
1549 {
1550 1 struct buf *work = 0;
1551 1 size_t i = 0, j;
1552  
1553 1 work = rndr_newbuf(rndr, BUFFER_BLOCK);
1554  
1555 2 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 2 0 2
1556 2 j = parse_listitem(work, rndr, data + i, size - i, &flags);
1557 2 i += j;
1558  
1559 2 ▶ if (!j || (flags & MKD_LI_END))
Branch: 2 branches
BranchTrueFalseTotal
if (!j || (flags & MKD_LI_END)) 2 0 2
if (!j || (flags & MKD_LI_END)) 1 1 2
1560 break;
1561 }
1562  
1563 1 ▶ if (rndr->make.list)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.list) 1 0 1
1564 1 rndr->make.list(ob, work, flags, rndr->make.opaque);
1565 1 rndr_popbuf(rndr, BUFFER_BLOCK);
1566 1 return i;
1567 }
1568  
1569 /* parse_atxheader • parsing of atx-style headers */
1570 static size_t
1571 0 parse_atxheader(struct buf *ob, struct render *rndr, char *data, size_t size)
1572 {
1573 0 size_t level = 0;
1574 size_t i, end, skip;
1575  
1576 0 ▶ while (level < size && level < 6 && data[level] == '#')
Branch: 3 branches
BranchTrueFalseTotal
while (level < size && level < 6 && data[level] == '#') 0 0 0
while (level < size && level < 6 && data[level] == '#') 0 0 0
while (level < size && level < 6 && data[level] == '#') 0 0 0
1577 0 level++;
1578  
1579 0 ▶ for (i = level; i < size && (data[i] == ' ' || data[i] == '\t'); i++);
Branch: 3 branches
BranchTrueFalseTotal
for (i = level; i < size && (data[i] == ' ' || data[i] == '\t'); i++); 0 0 0
for (i = level; i < size && (data[i] == ' ' || data[i] == '\t'); i++); 0 0 0
for (i = level; i < size && (data[i] == ' ' || data[i] == '\t'); i++); 0 0 0
1580  
1581 0 ▶ for (end = i; end < size && data[end] != '\n'; end++);
Branch: 2 branches
BranchTrueFalseTotal
for (end = i; end < size && data[end] != '\n'; end++); 0 0 0
for (end = i; end < size && data[end] != '\n'; end++); 0 0 0
1582 0 skip = end;
1583  
1584 0 ▶ while (end && data[end - 1] == '#')
Branch: 2 branches
BranchTrueFalseTotal
while (end && data[end - 1] == '#') 0 0 0
while (end && data[end - 1] == '#') 0 0 0
1585 0 end--;
1586  
1587 0 ▶ while (end && (data[end - 1] == ' ' || data[end - 1] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (end && (data[end - 1] == ' ' || data[end - 1] == '\t')) 0 0 0
while (end && (data[end - 1] == ' ' || data[end - 1] == '\t')) 0 0 0
while (end && (data[end - 1] == ' ' || data[end - 1] == '\t')) 0 0 0
1588 0 end--;
1589  
1590 0 ▶ if (end > i) {
Branch: 1 branch
BranchTrueFalseTotal
if (end > i) { 0 0 0
1591 0 struct buf *work = rndr_newbuf(rndr, BUFFER_SPAN);
1592  
1593 0 parse_inline(work, rndr, data + i, end - i);
1594  
1595 0 ▶ if (rndr->make.header)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.header) 0 0 0
1596 0 rndr->make.header(ob, work, (int)level, rndr->make.opaque);
1597  
1598 0 rndr_popbuf(rndr, BUFFER_SPAN);
1599 }
1600  
1601 0 return skip;
1602 }
1603  
1604  
1605 /* htmlblock_end • checking end of HTML block : </tag>[ \t]*\n[ \t*]\n */
1606 /* returns the length on match, 0 otherwise */
1607 static size_t
1608 0 htmlblock_end(struct html_tag *tag, struct render *rndr, char *data, size_t size)
1609 {
1610 size_t i, w;
1611  
1612 /* assuming data[0] == '<' && data[1] == '/' already tested */
1613  
1614 /* checking if tag is a match */
1615 0 ▶ if (tag->size + 3 >= size
Branch: 1 branch
BranchTrueFalseTotal
if (tag->size + 3 >= size 0 0 0
1616 0 ▶ || strncasecmp(data + 2, tag->text, tag->size)
Branch: 1 branch
BranchTrueFalseTotal
|| strncasecmp(data + 2, tag->text, tag->size) 0 0 0
1617 0 ▶ || data[tag->size + 2] != '>')
Branch: 1 branch
BranchTrueFalseTotal
|| data[tag->size + 2] != '>') 0 0 0
1618 0 return 0;
1619  
1620 /* checking white lines */
1621 0 i = tag->size + 3;
1622 0 w = 0;
1623 0 ▶ if (i < size && (w = is_empty(data + i, size - i)) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && (w = is_empty(data + i, size - i)) == 0) 0 0 0
if (i < size && (w = is_empty(data + i, size - i)) == 0) 0 0 0
1624 0 return 0; /* non-blank after tag */
1625 0 i += w;
1626 0 w = 0;
1627  
1628 0 ▶ if (rndr->ext_flags & MKDEXT_LAX_HTML_BLOCKS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->ext_flags & MKDEXT_LAX_HTML_BLOCKS) { 0 0 0
1629 0 ▶ if (i < size)
Branch: 1 branch
BranchTrueFalseTotal
if (i < size) 0 0 0
1630 0 w = is_empty(data + i, size - i);
1631 } else {
1632 0 ▶ if (i < size && (w = is_empty(data + i, size - i)) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && (w = is_empty(data + i, size - i)) == 0) 0 0 0
if (i < size && (w = is_empty(data + i, size - i)) == 0) 0 0 0
1633 0 return 0; /* non-blank line after tag line */
1634 }
1635  
1636 0 return i + w;
1637 }
1638  
1639  
1640 /* parse_htmlblock • parsing of inline HTML block */
1641 static size_t
1642 0 parse_htmlblock(struct buf *ob, struct render *rndr, char *data, size_t size, int do_render)
1643 {
1644 0 size_t i, j = 0;
1645 struct html_tag *curtag;
1646 int found;
1647 0 struct buf work = { data, 0, 0, 0, 0 };
1648  
1649 /* identification of the opening tag */
1650 0 ▶ if (size < 2 || data[0] != '<') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (size < 2 || data[0] != '<') return 0; 0 0 0
if (size < 2 || data[0] != '<') return 0; 0 0 0
1651 0 curtag = find_block_tag(data + 1, size - 1);
1652  
1653 /* handling of special cases */
1654 0 ▶ if (!curtag) {
Branch: 1 branch
BranchTrueFalseTotal
if (!curtag) { 0 0 0
1655  
1656 /* HTML comment, laxist form */
1657 0 ▶ if (size > 5 && data[1] == '!' && data[2] == '-' && data[3] == '-') {
Branch: 4 branches
BranchTrueFalseTotal
if (size > 5 && data[1] == '!' && data[2] == '-' && data[3] == '-') { 0 0 0
if (size > 5 && data[1] == '!' && data[2] == '-' && data[3] == '-') { 0 0 0
if (size > 5 && data[1] == '!' && data[2] == '-' && data[3] == '-') { 0 0 0
if (size > 5 && data[1] == '!' && data[2] == '-' && data[3] == '-') { 0 0 0
1658 0 i = 5;
1659  
1660 0 ▶ while (i < size && !(data[i - 2] == '-' && data[i - 1] == '-' && data[i] == '>'))
Branch: 4 branches
BranchTrueFalseTotal
while (i < size && !(data[i - 2] == '-' && data[i - 1] == '-' && data[i] == '>')) 0 0 0
while (i < size && !(data[i - 2] == '-' && data[i - 1] == '-' && data[i] == '>')) 0 0 0
while (i < size && !(data[i - 2] == '-' && data[i - 1] == '-' && data[i] == '>')) 0 0 0
while (i < size && !(data[i - 2] == '-' && data[i - 1] == '-' && data[i] == '>')) 0 0 0
1661 0 i++;
1662  
1663 0 i++;
1664  
1665 0 ▶ if (i < size)
Branch: 1 branch
BranchTrueFalseTotal
if (i < size) 0 0 0
1666 0 j = is_empty(data + i, size - i);
1667  
1668 0 ▶ if (j) {
Branch: 1 branch
BranchTrueFalseTotal
if (j) { 0 0 0
1669 0 work.size = i + j;
1670 0 ▶ if (do_render && rndr->make.blockhtml)
Branch: 2 branches
BranchTrueFalseTotal
if (do_render && rndr->make.blockhtml) 0 0 0
if (do_render && rndr->make.blockhtml) 0 0 0
1671 0 rndr->make.blockhtml(ob, &work, rndr->make.opaque);
1672 0 return work.size;
1673 }
1674 }
1675  
1676 /* HR, which is the only self-closing block tag considered */
1677 0 ▶ if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) {
Branch: 5 branches
BranchTrueFalseTotal
if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) { 0 0 0
if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) { 0 0 0
if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) { 0 0 0
if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) { 0 0 0
if (size > 4 && (data[1] == 'h' || data[1] == 'H') && (data[2] == 'r' || data[2] == 'R')) { 0 0 0
1678 0 i = 3;
1679 0 ▶ while (i < size && data[i] != '>')
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '>') 0 0 0
while (i < size && data[i] != '>') 0 0 0
1680 0 i += 1;
1681  
1682 0 ▶ if (i + 1 < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 < size) { 0 0 0
1683 0 i += 1;
1684 0 j = is_empty(data + i, size - i);
1685 0 ▶ if (j) {
Branch: 1 branch
BranchTrueFalseTotal
if (j) { 0 0 0
1686 0 work.size = i + j;
1687 0 ▶ if (do_render && rndr->make.blockhtml)
Branch: 2 branches
BranchTrueFalseTotal
if (do_render && rndr->make.blockhtml) 0 0 0
if (do_render && rndr->make.blockhtml) 0 0 0
1688 0 rndr->make.blockhtml(ob, &work, rndr->make.opaque);
1689 0 return work.size;
1690 }
1691 }
1692 }
1693  
1694 /* no special case recognised */
1695 0 return 0;
1696 }
1697  
1698 /* looking for an unindented matching closing tag */
1699 /* followed by a blank line */
1700 0 i = 1;
1701 0 found = 0;
1702  
1703 /* if not found, trying a second pass looking for indented match */
1704 /* but not if tag is "ins" or "del" (following original Markdown.pl) */
1705 0 ▶ if (curtag != INS_TAG && curtag != DEL_TAG) {
Branch: 2 branches
BranchTrueFalseTotal
if (curtag != INS_TAG && curtag != DEL_TAG) { 0 0 0
if (curtag != INS_TAG && curtag != DEL_TAG) { 0 0 0
1706 0 i = 1;
1707 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
1708 0 i++;
1709 0 ▶ while (i < size && !(data[i - 1] == '<' && data[i] == '/'))
Branch: 3 branches
BranchTrueFalseTotal
while (i < size && !(data[i - 1] == '<' && data[i] == '/')) 0 0 0
while (i < size && !(data[i - 1] == '<' && data[i] == '/')) 0 0 0
while (i < size && !(data[i - 1] == '<' && data[i] == '/')) 0 0 0
1710 0 i++;
1711  
1712 0 ▶ if (i + 2 + curtag->size >= size)
Branch: 1 branch
BranchTrueFalseTotal
if (i + 2 + curtag->size >= size) 0 0 0
1713 0 break;
1714  
1715 0 j = htmlblock_end(curtag, rndr, data + i - 1, size - i + 1);
1716  
1717 0 ▶ if (j) {
Branch: 1 branch
BranchTrueFalseTotal
if (j) { 0 0 0
1718 0 i += j - 1;
1719 0 found = 1;
1720 0 break;
1721 }
1722 }
1723 }
1724  
1725 0 ▶ if (!found) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!found) return 0; 0 0 0
1726  
1727 /* the end of the block has been found */
1728 0 work.size = i;
1729 0 ▶ if (do_render && rndr->make.blockhtml)
Branch: 2 branches
BranchTrueFalseTotal
if (do_render && rndr->make.blockhtml) 0 0 0
if (do_render && rndr->make.blockhtml) 0 0 0
1730 0 rndr->make.blockhtml(ob, &work, rndr->make.opaque);
1731  
1732 0 return i;
1733 }
1734  
1735 static void
1736 0 parse_table_row(struct buf *ob, struct render *rndr, char *data, size_t size, size_t columns, int *col_data)
1737 {
1738 0 size_t i = 0, col;
1739 0 struct buf *row_work = 0;
1740  
1741 0 row_work = rndr_newbuf(rndr, BUFFER_SPAN);
1742  
1743 0 ▶ if (i < size && data[i] == '|')
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == '|') 0 0 0
if (i < size && data[i] == '|') 0 0 0
1744 0 i++;
1745  
1746 0 ▶ for (col = 0; col < columns && i < size; ++col) {
Branch: 2 branches
BranchTrueFalseTotal
for (col = 0; col < columns && i < size; ++col) { 0 0 0
for (col = 0; col < columns && i < size; ++col) { 0 0 0
1747 size_t cell_start, cell_end;
1748 struct buf *cell_work;
1749  
1750 0 cell_work = rndr_newbuf(rndr, BUFFER_SPAN);
1751  
1752 0 ▶ while (i < size && isspace(data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && isspace(data[i])) 0 0 0
while (i < size && isspace(data[i])) 0 0 0
1753 0 i++;
1754  
1755 0 cell_start = i;
1756  
1757 0 ▶ while (i < size && data[i] != '|')
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '|') 0 0 0
while (i < size && data[i] != '|') 0 0 0
1758 0 i++;
1759  
1760 0 cell_end = i - 1;
1761  
1762 0 ▶ while (cell_end > cell_start && isspace(data[cell_end]))
Branch: 2 branches
BranchTrueFalseTotal
while (cell_end > cell_start && isspace(data[cell_end])) 0 0 0
while (cell_end > cell_start && isspace(data[cell_end])) 0 0 0
1763 0 cell_end--;
1764  
1765 0 parse_inline(cell_work, rndr, data + cell_start, 1 + cell_end - cell_start);
1766 0 ▶ if (rndr->make.table_cell)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.table_cell) 0 0 0
1767 0 ▶ rndr->make.table_cell(row_work, cell_work, col_data ? col_data[col] : 0, rndr->make.opaque);
Branch: 1 branch
BranchTrueFalseTotal
rndr->make.table_cell(row_work, cell_work, col_data ? col_data[col] : 0, rndr->make.opaque); 0 0 0
1768  
1769 0 rndr_popbuf(rndr, BUFFER_SPAN);
1770 0 i++;
1771 }
1772  
1773 0 ▶ for (; col < columns; ++col) {
Branch: 1 branch
BranchTrueFalseTotal
for (; col < columns; ++col) { 0 0 0
1774 0 struct buf empty_cell = {0, 0, 0, 0, 0};
1775 0 ▶ if (rndr->make.table_cell)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.table_cell) 0 0 0
1776 0 ▶ rndr->make.table_cell(row_work, &empty_cell, col_data ? col_data[col] : 0, rndr->make.opaque);
Branch: 1 branch
BranchTrueFalseTotal
rndr->make.table_cell(row_work, &empty_cell, col_data ? col_data[col] : 0, rndr->make.opaque); 0 0 0
1777 }
1778  
1779 0 ▶ if (rndr->make.table_row)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.table_row) 0 0 0
1780 0 rndr->make.table_row(ob, row_work, rndr->make.opaque);
1781  
1782 0 rndr_popbuf(rndr, BUFFER_SPAN);
1783 0 }
1784  
1785 static size_t
1786 0 parse_table_header(struct buf *ob, struct render *rndr, char *data, size_t size, size_t *columns, int **column_data)
1787 {
1788 int pipes;
1789 0 size_t i = 0, col, header_end, under_end;
1790  
1791 0 pipes = 0;
1792 0 ▶ while (i < size && data[i] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '\n') 0 0 0
while (i < size && data[i] != '\n') 0 0 0
1793 0 ▶ if (data[i++] == '|')
Branch: 1 branch
BranchTrueFalseTotal
if (data[i++] == '|') 0 0 0
1794 0 pipes++;
1795  
1796 0 ▶ if (i == size || pipes == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (i == size || pipes == 0) 0 0 0
if (i == size || pipes == 0) 0 0 0
1797 0 return 0;
1798  
1799 0 header_end = i;
1800  
1801 0 ▶ if (data[0] == '|')
Branch: 1 branch
BranchTrueFalseTotal
if (data[0] == '|') 0 0 0
1802 0 pipes--;
1803  
1804 0 ▶ if (i > 2 && data[i - 1] == '|')
Branch: 2 branches
BranchTrueFalseTotal
if (i > 2 && data[i - 1] == '|') 0 0 0
if (i > 2 && data[i - 1] == '|') 0 0 0
1805 0 pipes--;
1806  
1807 0 *columns = pipes + 1;
1808 0 *column_data = calloc(*columns, sizeof(int));
1809  
1810 /* Parse the header underline */
1811 0 i++;
1812 0 ▶ if (i < size && data[i] == '|')
Branch: 2 branches
BranchTrueFalseTotal
if (i < size && data[i] == '|') 0 0 0
if (i < size && data[i] == '|') 0 0 0
1813 0 i++;
1814  
1815 0 under_end = i;
1816 0 ▶ while (under_end < size && data[under_end] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (under_end < size && data[under_end] != '\n') 0 0 0
while (under_end < size && data[under_end] != '\n') 0 0 0
1817 0 under_end++;
1818  
1819 0 ▶ for (col = 0; col < *columns && i < under_end; ++col) {
Branch: 2 branches
BranchTrueFalseTotal
for (col = 0; col < *columns && i < under_end; ++col) { 0 0 0
for (col = 0; col < *columns && i < under_end; ++col) { 0 0 0
1820 0 size_t dashes = 0;
1821  
1822 0 ▶ while (i < under_end && (data[i] == ' ' || data[i] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (i < under_end && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i < under_end && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i < under_end && (data[i] == ' ' || data[i] == '\t')) 0 0 0
1823 0 i++;
1824  
1825 0 ▶ if (data[i] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == ':') { 0 0 0
1826 0 i++; (*column_data)[col] |= MKD_TABLE_ALIGN_L;
1827 0 dashes++;
1828 }
1829  
1830 0 ▶ while (i < under_end && data[i] == '-') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < under_end && data[i] == '-') { 0 0 0
while (i < under_end && data[i] == '-') { 0 0 0
1831 0 i++; dashes++;
1832 }
1833  
1834 0 ▶ if (i < under_end && data[i] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < under_end && data[i] == ':') { 0 0 0
if (i < under_end && data[i] == ':') { 0 0 0
1835 0 i++; (*column_data)[col] |= MKD_TABLE_ALIGN_R;
1836 0 dashes++;
1837 }
1838  
1839 0 ▶ while (i < under_end && (data[i] == ' ' || data[i] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (i < under_end && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i < under_end && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i < under_end && (data[i] == ' ' || data[i] == '\t')) 0 0 0
1840 0 i++;
1841  
1842 0 ▶ if (i < under_end && data[i] != '|')
Branch: 2 branches
BranchTrueFalseTotal
if (i < under_end && data[i] != '|') 0 0 0
if (i < under_end && data[i] != '|') 0 0 0
1843 0 break;
1844  
1845 0 ▶ if (dashes < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (dashes < 3) 0 0 0
1846 0 break;
1847  
1848 0 i++;
1849 }
1850  
1851 0 ▶ if (col < *columns)
Branch: 1 branch
BranchTrueFalseTotal
if (col < *columns) 0 0 0
1852 0 return 0;
1853  
1854 0 parse_table_row(ob, rndr, data, header_end, *columns, *column_data);
1855 0 return under_end + 1;
1856 }
1857  
1858 static size_t
1859 0 parse_table(struct buf *ob, struct render *rndr, char *data, size_t size)
1860 {
1861 size_t i;
1862  
1863 0 struct buf *header_work = 0;
1864 0 struct buf *body_work = 0;
1865  
1866 size_t columns;
1867 0 int *col_data = NULL;
1868  
1869 0 header_work = rndr_newbuf(rndr, BUFFER_SPAN);
1870 0 body_work = rndr_newbuf(rndr, BUFFER_BLOCK);
1871  
1872 0 i = parse_table_header(header_work, rndr, data, size, &columns, &col_data);
1873 0 ▶ if (i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i > 0) { 0 0 0
1874  
1875 0 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 0 0 0
1876 size_t row_start;
1877 0 int pipes = 0;
1878  
1879 0 row_start = i;
1880  
1881 0 ▶ while (i < size && data[i] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && data[i] != '\n') 0 0 0
while (i < size && data[i] != '\n') 0 0 0
1882 0 ▶ if (data[i++] == '|')
Branch: 1 branch
BranchTrueFalseTotal
if (data[i++] == '|') 0 0 0
1883 0 pipes++;
1884  
1885 0 ▶ if (pipes == 0 || i == size) {
Branch: 2 branches
BranchTrueFalseTotal
if (pipes == 0 || i == size) { 0 0 0
if (pipes == 0 || i == size) { 0 0 0
1886 0 i = row_start;
1887 0 break;
1888 }
1889  
1890 0 parse_table_row(body_work, rndr, data + row_start, i - row_start, columns, col_data);
1891 0 i++;
1892 }
1893  
1894 0 ▶ if (rndr->make.table)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.table) 0 0 0
1895 0 rndr->make.table(ob, header_work, body_work, rndr->make.opaque);
1896 }
1897  
1898 0 free(col_data);
1899 0 rndr_popbuf(rndr, BUFFER_SPAN);
1900 0 rndr_popbuf(rndr, BUFFER_BLOCK);
1901 0 return i;
1902 }
1903  
1904 /* parse_block • parsing of one block, returning next char to parse */
1905 static void
1906 1 parse_block(struct buf *ob, struct render *rndr, char *data, size_t size)
1907 {
1908 size_t beg, end, i;
1909 char *txt_data;
1910 1 beg = 0;
1911  
1912 1 if (rndr->work_bufs[BUFFER_SPAN].size +
1913 1 ▶ rndr->work_bufs[BUFFER_BLOCK].size > (int)rndr->max_nesting)
Branch: 1 branch
BranchTrueFalseTotal
rndr->work_bufs[BUFFER_BLOCK].size > (int)rndr->max_nesting) 0 1 1
1914 0 return;
1915  
1916 5 ▶ while (beg < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (beg < size) { 4 1 5
1917 4 txt_data = data + beg;
1918 4 end = size - beg;
1919  
1920 4 ▶ if (is_atxheader(rndr, txt_data, end))
Branch: 1 branch
BranchTrueFalseTotal
if (is_atxheader(rndr, txt_data, end)) 0 4 4
1921 0 beg += parse_atxheader(ob, rndr, txt_data, end);
1922  
1923 4 ▶ else if (data[beg] == '<' && rndr->make.blockhtml &&
Branch: 1 branch
BranchTrueFalseTotal
else if (data[beg] == '<' && rndr->make.blockhtml && 0 4 4
1924 0 (i = parse_htmlblock(ob, rndr, txt_data, end, 1)) != 0)
1925 0 beg += i;
1926  
1927 4 ▶ else if ((i = is_empty(txt_data, end)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
else if ((i = is_empty(txt_data, end)) != 0) 1 3 4
1928 1 beg += i;
1929  
1930 3 ▶ else if (is_hrule(txt_data, end)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (is_hrule(txt_data, end)) { 0 3 3
1931 0 ▶ if (rndr->make.hrule)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr->make.hrule) 0 0 0
1932 0 rndr->make.hrule(ob, rndr->make.opaque);
1933  
1934 0 ▶ while (beg < size && data[beg] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (beg < size && data[beg] != '\n') 0 0 0
while (beg < size && data[beg] != '\n') 0 0 0
1935 0 beg++;
1936  
1937 0 beg++;
1938 }
1939  
1940 3 else if ((rndr->ext_flags & MKDEXT_FENCED_CODE) != 0 &&
1941 0 (i = parse_fencedcode(ob, rndr, txt_data, end)) != 0)
1942 0 beg += i;
1943  
1944 3 else if ((rndr->ext_flags & MKDEXT_TABLES) != 0 &&
1945 0 (i = parse_table(ob, rndr, txt_data, end)) != 0)
1946 0 beg += i;
1947  
1948 3 ▶ else if (prefix_quote(txt_data, end))
Branch: 1 branch
BranchTrueFalseTotal
else if (prefix_quote(txt_data, end)) 0 3 3
1949 0 beg += parse_blockquote(ob, rndr, txt_data, end);
1950  
1951 3 ▶ else if (prefix_code(txt_data, end))
Branch: 1 branch
BranchTrueFalseTotal
else if (prefix_code(txt_data, end)) 0 3 3
1952 0 beg += parse_blockcode(ob, rndr, txt_data, end);
1953  
1954 3 ▶ else if (prefix_uli(txt_data, end))
Branch: 1 branch
BranchTrueFalseTotal
else if (prefix_uli(txt_data, end)) 1 2 3
1955 1 beg += parse_list(ob, rndr, txt_data, end, 0);
1956  
1957 2 ▶ else if (prefix_oli(txt_data, end))
Branch: 1 branch
BranchTrueFalseTotal
else if (prefix_oli(txt_data, end)) 0 2 2
1958 0 beg += parse_list(ob, rndr, txt_data, end, MKD_LIST_ORDERED);
1959  
1960 else
1961 2 beg += parse_paragraph(ob, rndr, txt_data, end);
1962 }
1963 }
1964  
1965  
1966  
1967 /*********************
1968 * REFERENCE PARSING *
1969 *********************/
1970  
1971 /* is_ref • returns whether a line is a reference or not */
1972 static int
1973 6 is_ref(char *data, size_t beg, size_t end, size_t *last, struct array *refs)
1974 {
1975 /* int n; */
1976 6 size_t i = 0;
1977 size_t id_offset, id_end;
1978 size_t link_offset, link_end;
1979 size_t title_offset, title_end;
1980 size_t line_end;
1981 struct link_ref *lr;
1982 /* struct buf id = { 0, 0, 0, 0, 0 }; / * volatile buf for id search */
1983  
1984 /* up to 3 optional leading spaces */
1985 6 ▶ if (beg + 3 >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (beg + 3 >= end) return 0; 0 6 6
1986 6 ▶ if (data[beg] == ' ') { i = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[beg] == ' ') { i = 1; 0 6 6
1987 0 ▶ if (data[beg + 1] == ' ') { i = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (data[beg + 1] == ' ') { i = 2; 0 0 0
1988 0 ▶ if (data[beg + 2] == ' ') { i = 3;
Branch: 1 branch
BranchTrueFalseTotal
if (data[beg + 2] == ' ') { i = 3; 0 0 0
1989 0 ▶ if (data[beg + 3] == ' ') return 0; } } }
Branch: 1 branch
BranchTrueFalseTotal
if (data[beg + 3] == ' ') return 0; } } } 0 0 0
1990 6 i += beg;
1991  
1992 /* id part: anything but a newline between brackets */
1993 6 ▶ if (data[i] != '[') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] != '[') return 0; 6 0 6
1994 0 i += 1;
1995 0 id_offset = i;
1996 0 ▶ while (i < end && data[i] != '\n' && data[i] != '\r' && data[i] != ']')
Branch: 4 branches
BranchTrueFalseTotal
while (i < end && data[i] != '\n' && data[i] != '\r' && data[i] != ']') 0 0 0
while (i < end && data[i] != '\n' && data[i] != '\r' && data[i] != ']') 0 0 0
while (i < end && data[i] != '\n' && data[i] != '\r' && data[i] != ']') 0 0 0
while (i < end && data[i] != '\n' && data[i] != '\r' && data[i] != ']') 0 0 0
1997 0 i += 1;
1998 0 ▶ if (i >= end || data[i] != ']') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= end || data[i] != ']') return 0; 0 0 0
if (i >= end || data[i] != ']') return 0; 0 0 0
1999 0 id_end = i;
2000  
2001 /* spacer: colon (space | tab)* newline? (space | tab)* */
2002 0 i += 1;
2003 0 ▶ if (i >= end || data[i] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i >= end || data[i] != ':') return 0; 0 0 0
if (i >= end || data[i] != ':') return 0; 0 0 0
2004 0 i += 1;
2005 0 ▶ while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
2006 0 ▶ if (i < end && (data[i] == '\n' || data[i] == '\r')) {
Branch: 3 branches
BranchTrueFalseTotal
if (i < end && (data[i] == '\n' || data[i] == '\r')) { 0 0 0
if (i < end && (data[i] == '\n' || data[i] == '\r')) { 0 0 0
if (i < end && (data[i] == '\n' || data[i] == '\r')) { 0 0 0
2007 0 i += 1;
2008 0 ▶ if (i < end && data[i] == '\r' && data[i - 1] == '\n') i += 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (i < end && data[i] == '\r' && data[i - 1] == '\n') i += 1; } 0 0 0
if (i < end && data[i] == '\r' && data[i - 1] == '\n') i += 1; } 0 0 0
if (i < end && data[i] == '\r' && data[i - 1] == '\n') i += 1; } 0 0 0
2009 0 ▶ while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
2010 0 ▶ if (i >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= end) return 0; 0 0 0
2011  
2012 /* link: whitespace-free sequence, optionally between angle brackets */
2013 0 ▶ if (data[i] == '<') i += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i] == '<') i += 1; 0 0 0
2014 0 link_offset = i;
2015 0 ▶ while (i < end && data[i] != ' ' && data[i] != '\t'
Branch: 2 branches
BranchTrueFalseTotal
while (i < end && data[i] != ' ' && data[i] != '\t' 0 0 0
while (i < end && data[i] != ' ' && data[i] != '\t' 0 0 0
2016 0 ▶ && data[i] != '\n' && data[i] != '\r') i += 1;
Branch: 3 branches
BranchTrueFalseTotal
&& data[i] != '\n' && data[i] != '\r') i += 1; 0 0 0
&& data[i] != '\n' && data[i] != '\r') i += 1; 0 0 0
&& data[i] != '\n' && data[i] != '\r') i += 1; 0 0 0
2017 0 ▶ if (data[i - 1] == '>') link_end = i - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (data[i - 1] == '>') link_end = i - 1; 0 0 0
2018 0 else link_end = i;
2019  
2020 /* optional spacer: (space | tab)* (newline | '\'' | '"' | '(' ) */
2021 0 ▶ while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; 0 0 0
2022 0 ▶ if (i < end && data[i] != '\n' && data[i] != '\r'
Branch: 3 branches
BranchTrueFalseTotal
if (i < end && data[i] != '\n' && data[i] != '\r' 0 0 0
if (i < end && data[i] != '\n' && data[i] != '\r' 0 0 0
if (i < end && data[i] != '\n' && data[i] != '\r' 0 0 0
2023 0 ▶ && data[i] != '\'' && data[i] != '"' && data[i] != '(')
Branch: 3 branches
BranchTrueFalseTotal
&& data[i] != '\'' && data[i] != '"' && data[i] != '(') 0 0 0
&& data[i] != '\'' && data[i] != '"' && data[i] != '(') 0 0 0
&& data[i] != '\'' && data[i] != '"' && data[i] != '(') 0 0 0
2024 0 return 0;
2025 0 line_end = 0;
2026 /* computing end-of-line */
2027 0 ▶ if (i >= end || data[i] == '\r' || data[i] == '\n') line_end = i;
Branch: 3 branches
BranchTrueFalseTotal
if (i >= end || data[i] == '\r' || data[i] == '\n') line_end = i; 0 0 0
if (i >= end || data[i] == '\r' || data[i] == '\n') line_end = i; 0 0 0
if (i >= end || data[i] == '\r' || data[i] == '\n') line_end = i; 0 0 0
2028 0 ▶ if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r')
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r') 0 0 0
if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r') 0 0 0
if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r') 0 0 0
2029 0 line_end = i + 1;
2030  
2031 /* optional (space|tab)* spacer after a newline */
2032 0 ▶ if (line_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (line_end) { 0 0 0
2033 0 i = line_end + 1;
2034 0 ▶ while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; }
Branch: 3 branches
BranchTrueFalseTotal
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; } 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; } 0 0 0
while (i < end && (data[i] == ' ' || data[i] == '\t')) i += 1; } 0 0 0
2035  
2036 /* optional title: any non-newline sequence enclosed in '"()
2037 alone on its line */
2038 0 title_offset = title_end = 0;
2039 0 ▶ if (i + 1 < end
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 < end 0 0 0
2040 0 ▶ && (data[i] == '\'' || data[i] == '"' || data[i] == '(')) {
Branch: 3 branches
BranchTrueFalseTotal
&& (data[i] == '\'' || data[i] == '"' || data[i] == '(')) { 0 0 0
&& (data[i] == '\'' || data[i] == '"' || data[i] == '(')) { 0 0 0
&& (data[i] == '\'' || data[i] == '"' || data[i] == '(')) { 0 0 0
2041 0 i += 1;
2042 0 title_offset = i;
2043 /* looking for EOL */
2044 0 ▶ while (i < end && data[i] != '\n' && data[i] != '\r') i += 1;
Branch: 3 branches
BranchTrueFalseTotal
while (i < end && data[i] != '\n' && data[i] != '\r') i += 1; 0 0 0
while (i < end && data[i] != '\n' && data[i] != '\r') i += 1; 0 0 0
while (i < end && data[i] != '\n' && data[i] != '\r') i += 1; 0 0 0
2045 0 ▶ if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r')
Branch: 3 branches
BranchTrueFalseTotal
if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r') 0 0 0
if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r') 0 0 0
if (i + 1 < end && data[i] == '\n' && data[i + 1] == '\r') 0 0 0
2046 0 title_end = i + 1;
2047 0 else title_end = i;
2048 /* stepping back */
2049 0 i -= 1;
2050 0 ▶ while (i > title_offset && (data[i] == ' ' || data[i] == '\t'))
Branch: 3 branches
BranchTrueFalseTotal
while (i > title_offset && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i > title_offset && (data[i] == ' ' || data[i] == '\t')) 0 0 0
while (i > title_offset && (data[i] == ' ' || data[i] == '\t')) 0 0 0
2051 0 i -= 1;
2052 0 ▶ if (i > title_offset
Branch: 1 branch
BranchTrueFalseTotal
if (i > title_offset 0 0 0
2053 0 ▶ && (data[i] == '\'' || data[i] == '"' || data[i] == ')')) {
Branch: 3 branches
BranchTrueFalseTotal
&& (data[i] == '\'' || data[i] == '"' || data[i] == ')')) { 0 0 0
&& (data[i] == '\'' || data[i] == '"' || data[i] == ')')) { 0 0 0
&& (data[i] == '\'' || data[i] == '"' || data[i] == ')')) { 0 0 0
2054 0 line_end = title_end;
2055 0 title_end = i; } }
2056 0 ▶ if (!line_end) return 0; /* garbage after the link */
Branch: 1 branch
BranchTrueFalseTotal
if (!line_end) return 0; /* garbage after the link */ 0 0 0
2057  
2058 /* a valid ref has been found, filling-in return structures */
2059 0 ▶ if (last) *last = line_end;
Branch: 1 branch
BranchTrueFalseTotal
if (last) *last = line_end; 0 0 0
2060 0 ▶ if (!refs) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!refs) return 1; 0 0 0
2061 0 lr = arr_item(refs, arr_newitem(refs));
2062 0 lr->id = bufnew(id_end - id_offset);
2063 0 bufput(lr->id, data + id_offset, id_end - id_offset);
2064 0 lr->link = bufnew(link_end - link_offset);
2065 0 bufput(lr->link, data + link_offset, link_end - link_offset);
2066 0 ▶ if (title_end > title_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (title_end > title_offset) { 0 0 0
2067 0 lr->title = bufnew(title_end - title_offset);
2068 0 bufput(lr->title, data + title_offset,
2069 title_end - title_offset); }
2070 0 else lr->title = 0;
2071 0 return 1;
2072 }
2073  
2074 6 static void expand_tabs(struct buf *ob, const char *line, size_t size)
2075 {
2076 6 size_t i = 0, tab = 0;
2077  
2078 6 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 6 0 6
2079 6 size_t org = i;
2080  
2081 131 ▶ while (i < size && line[i] != '\t') {
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && line[i] != '\t') { 125 6 131
while (i < size && line[i] != '\t') { 125 0 125
2082 125 i++; tab++;
2083 }
2084  
2085 6 ▶ if (i > org)
Branch: 1 branch
BranchTrueFalseTotal
if (i > org) 6 0 6
2086 6 bufput(ob, line + org, i - org);
2087  
2088 6 ▶ if (i >= size)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) 6 0 6
2089 6 break;
2090  
2091 do {
2092 0 bufputc(ob, ' '); tab++;
2093 0 ▶ } while (tab % 4);
Branch: 1 branch
BranchTrueFalseTotal
} while (tab % 4); 0 0 0
2094  
2095 0 i++;
2096 }
2097 6 }
2098  
2099 /**********************
2100 * EXPORTED FUNCTIONS *
2101 **********************/
2102  
2103 /* markdown • parses the input buffer and renders it into the output buffer */
2104 void
2105 1 ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer, unsigned int extensions) {
2106 struct link_ref *lr;
2107 struct buf *text;
2108 size_t i, beg, end;
2109 struct render rndr;
2110  
2111 /* filling the render structure */
2112 1 ▶ if (!rndrer)
Branch: 1 branch
BranchTrueFalseTotal
if (!rndrer) 0 1 1
2113 0 return;
2114  
2115 1 text = bufnew(64);
2116 1 ▶ if (!text)
Branch: 1 branch
BranchTrueFalseTotal
if (!text) 0 1 1
2117 0 return;
2118  
2119 /* Preallocate enough space for our buffer to avoid expanding while copying */
2120 1 bufgrow(text, ib->size);
2121  
2122 1 memcpy(&rndr.make, rndrer, sizeof(struct mkd_renderer));
2123 1 arr_init(&rndr.refs, sizeof (struct link_ref));
2124 1 parr_init(&rndr.work_bufs[BUFFER_BLOCK]);
2125 1 parr_init(&rndr.work_bufs[BUFFER_SPAN]);
2126  
2127 257 ▶ for (i = 0; i < 256; i += 1)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i += 1) 256 1 257
2128 256 rndr.active_char[i] = 0;
2129  
2130 1 ▶ if (rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) {
Branch: 3 branches
BranchTrueFalseTotal
if (rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) { 0 1 1
if (rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) { 0 0 0
if (rndr.make.emphasis || rndr.make.double_emphasis || rndr.make.triple_emphasis) { 0 0 0
2131 1 rndr.active_char['*'] = MD_CHAR_EMPHASIS;
2132 1 rndr.active_char['_'] = MD_CHAR_EMPHASIS;
2133 1 ▶ if (extensions & MKDEXT_STRIKETHROUGH)
Branch: 1 branch
BranchTrueFalseTotal
if (extensions & MKDEXT_STRIKETHROUGH) 0 1 1
2134 0 rndr.active_char['~'] = MD_CHAR_EMPHASIS;
2135 }
2136  
2137 1 ▶ if (rndr.make.codespan)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr.make.codespan) 1 0 1
2138 1 rndr.active_char['`'] = MD_CHAR_CODESPAN;
2139  
2140 1 ▶ if (rndr.make.linebreak)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr.make.linebreak) 1 0 1
2141 1 rndr.active_char['\n'] = MD_CHAR_LINEBREAK;
2142  
2143 1 ▶ if (rndr.make.image || rndr.make.link)
Branch: 2 branches
BranchTrueFalseTotal
if (rndr.make.image || rndr.make.link) 0 1 1
if (rndr.make.image || rndr.make.link) 0 0 0
2144 1 rndr.active_char['['] = MD_CHAR_LINK;
2145  
2146 1 rndr.active_char['<'] = MD_CHAR_LANGLE;
2147 1 rndr.active_char['\\'] = MD_CHAR_ESCAPE;
2148 1 rndr.active_char['&'] = MD_CHAR_ENTITITY;
2149  
2150 1 ▶ if (extensions & MKDEXT_AUTOLINK) {
Branch: 1 branch
BranchTrueFalseTotal
if (extensions & MKDEXT_AUTOLINK) { 0 1 1
2151 0 rndr.active_char[':'] = MD_CHAR_AUTOLINK_URL;
2152 0 rndr.active_char['@'] = MD_CHAR_AUTOLINK_EMAIL;
2153 0 rndr.active_char['w'] = MD_CHAR_AUTOLINK_WWW;
2154 }
2155  
2156 /* Extension data */
2157 1 rndr.ext_flags = extensions;
2158 1 rndr.max_nesting = 16;
2159  
2160 /* first pass: looking for references, copying everything else */
2161 1 beg = 0;
2162 7 ▶ while (beg < ib->size) /* iterating over lines */
Branch: 1 branch
BranchTrueFalseTotal
while (beg < ib->size) /* iterating over lines */ 6 1 7
2163 6 ▶ if (is_ref(ib->data, beg, ib->size, &end, &rndr.refs))
Branch: 1 branch
BranchTrueFalseTotal
if (is_ref(ib->data, beg, ib->size, &end, &rndr.refs)) 0 6 6
2164 0 beg = end;
2165 else { /* skipping to the next line */
2166 6 end = beg;
2167 131 ▶ while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r')
Branch: 3 branches
BranchTrueFalseTotal
while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') 131 0 131
while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') 125 6 131
while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') 125 0 125
2168 125 end += 1;
2169  
2170 /* adding the line body if present */
2171 6 ▶ if (end > beg)
Branch: 1 branch
BranchTrueFalseTotal
if (end > beg) 6 0 6
2172 6 expand_tabs(text, ib->data + beg, end - beg);
2173  
2174 14 ▶ while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) {
Branch: 3 branches
BranchTrueFalseTotal
while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { 13 1 14
while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { 8 5 13
while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { 0 5 5
2175 /* add one \n per newline */
2176 8 ▶ if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n'))
Branch: 3 branches
BranchTrueFalseTotal
if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) 0 8 8
if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) 0 0 0
if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) 0 0 0
2177 8 bufputc(text, '\n');
2178 8 end += 1;
2179 }
2180  
2181 6 beg = end;
2182 }
2183  
2184 /* sorting the reference array */
2185 1 ▶ if (rndr.refs.size)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr.refs.size) 0 1 1
2186 0 qsort(rndr.refs.base, rndr.refs.size, rndr.refs.unit, cmp_link_ref_sort);
2187  
2188 /* second pass: actual rendering */
2189 1 ▶ if (rndr.make.doc_header)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr.make.doc_header) 0 1 1
2190 0 rndr.make.doc_header(ob, rndr.make.opaque);
2191  
2192 1 ▶ if (text->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (text->size) { 1 0 1
2193 /* adding a final newline if not already present */
2194 1 ▶ if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r')
Branch: 2 branches
BranchTrueFalseTotal
if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') 0 1 1
if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') 0 0 0
2195 0 bufputc(text, '\n');
2196  
2197 1 parse_block(ob, &rndr, text->data, text->size);
2198 }
2199  
2200 1 ▶ if (rndr.make.doc_footer)
Branch: 1 branch
BranchTrueFalseTotal
if (rndr.make.doc_footer) 0 1 1
2201 0 rndr.make.doc_footer(ob, rndr.make.opaque);
2202  
2203 /* clean-up */
2204 1 bufrelease(text);
2205 1 lr = rndr.refs.base;
2206 1 ▶ for (i = 0; i < (size_t)rndr.refs.size; i += 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (size_t)rndr.refs.size; i += 1) { 0 1 1
2207 0 bufrelease(lr[i].id);
2208 0 bufrelease(lr[i].link);
2209 0 bufrelease(lr[i].title);
2210 }
2211  
2212 1 arr_free(&rndr.refs);
2213  
2214 1 ▶ assert(rndr.work_bufs[BUFFER_SPAN].size == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(rndr.work_bufs[BUFFER_SPAN].size == 0); 0 1 1
2215 1 ▶ assert(rndr.work_bufs[BUFFER_BLOCK].size == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(rndr.work_bufs[BUFFER_BLOCK].size == 0); 0 1 1
2216  
2217 3 ▶ for (i = 0; i < (size_t)rndr.work_bufs[BUFFER_SPAN].asize; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (size_t)rndr.work_bufs[BUFFER_SPAN].asize; ++i) 2 1 3
2218 2 bufrelease(rndr.work_bufs[BUFFER_SPAN].item[i]);
2219  
2220 2 ▶ for (i = 0; i < (size_t)rndr.work_bufs[BUFFER_BLOCK].asize; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (size_t)rndr.work_bufs[BUFFER_BLOCK].asize; ++i) 1 1 2
2221 1 bufrelease(rndr.work_bufs[BUFFER_BLOCK].item[i]);
2222  
2223 1 parr_free(&rndr.work_bufs[BUFFER_SPAN]);
2224 1 parr_free(&rndr.work_bufs[BUFFER_BLOCK]);
2225 }
2226  
2227 void
2228 0 ups_version(int *ver_major, int *ver_minor, int *ver_revision)
2229 {
2230 0 *ver_major = UPSKIRT_VER_MAJOR;
2231 0 *ver_minor = UPSKIRT_VER_MINOR;
2232 0 *ver_revision = UPSKIRT_VER_REVISION;
2233 0 }
2234  
2235 /* vim: set filetype=c: */
« html.c ↑ Summary buffer.c »