html.c

filter: Statementstmt 20.4% 67 / 327 Branchbran 14.0% 36 / 256 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 17.6% 103 / 583 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« array.c ↑ Summary markdown.c »
1 /*
2 * Copyright (c) 2009, Natacha Porté
3 * Copyright (c) 2011, Vicent Marti
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17  
18 #include "markdown.h"
19 #include "html.h"
20  
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <ctype.h>
25  
26 struct html_renderopt {
27 struct {
28 int header_count;
29 int current_level;
30 } toc_data;
31  
32 unsigned int flags;
33 const char *close_tag;
34 };
35  
36 static inline void
37 6 put_scaped_char(struct buf *ob, char c)
38 {
39 6 switch (c) {
40 0 case '<': BUFPUTSL(ob, "&lt;"); break;
41 0 case '>': BUFPUTSL(ob, "&gt;"); break;
42 0 case '&': BUFPUTSL(ob, "&amp;"); break;
43 6 case '"': BUFPUTSL(ob, "&quot;"); break;
44 0 default: bufputc(ob, c); break;
45 }
46 6 }
47  
48 /* upshtml_escape • copy the buffer entity-escaping '<', '>', '&' and '"' */
49 void
50 7 upshtml_escape(struct buf *ob, const char *src, size_t size)
51 {
52 7 size_t i = 0, org;
53 13 ▶ while (i < size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < size) { 12 1 13
54 /* copying directly unescaped characters */
55 12 org = i;
56 129 ▶ while (i < size && src[i] != '<' && src[i] != '>'
Branch: 2 branches
BranchTrueFalseTotal
while (i < size && src[i] != '<' && src[i] != '>' 117 0 117
while (i < size && src[i] != '<' && src[i] != '>' 117 0 117
57 240 ▶ && src[i] != '&' && src[i] != '"')
Branch: 3 branches
BranchTrueFalseTotal
&& src[i] != '&' && src[i] != '"') 117 6 123
&& src[i] != '&' && src[i] != '"') 117 0 117
&& src[i] != '&' && src[i] != '"') 111 6 117
58 111 i += 1;
59 12 ▶ if (i > org) bufput(ob, src + org, i - org);
Branch: 1 branch
BranchTrueFalseTotal
if (i > org) bufput(ob, src + org, i - org); 12 0 12
60  
61 /* escaping */
62 12 ▶ if (i >= size) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= size) break; 6 6 12
63  
64 6 put_scaped_char(ob, src[i]);
65 6 i++;
66 }
67 7 }
68  
69 static int
70 0 is_html_tag(struct buf *tag, const char *tagname)
71 {
72 0 size_t i = 0;
73  
74 0 ▶ if (i < tag->size && tag->data[0] != '<')
Branch: 2 branches
BranchTrueFalseTotal
if (i < tag->size && tag->data[0] != '<') 0 0 0
if (i < tag->size && tag->data[0] != '<') 0 0 0
75 0 return 0;
76  
77 0 i++;
78  
79 0 ▶ while (i < tag->size && isspace(tag->data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < tag->size && isspace(tag->data[i])) 0 0 0
while (i < tag->size && isspace(tag->data[i])) 0 0 0
80 0 i++;
81  
82 0 ▶ if (i < tag->size && tag->data[i] == '/')
Branch: 2 branches
BranchTrueFalseTotal
if (i < tag->size && tag->data[i] == '/') 0 0 0
if (i < tag->size && tag->data[i] == '/') 0 0 0
83 0 i++;
84  
85 0 ▶ while (i < tag->size && isspace(tag->data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < tag->size && isspace(tag->data[i])) 0 0 0
while (i < tag->size && isspace(tag->data[i])) 0 0 0
86 0 i++;
87  
88 0 ▶ for (; i < tag->size; ++i, ++tagname) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < tag->size; ++i, ++tagname) { 0 0 0
89 0 ▶ if (*tagname == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (*tagname == 0) 0 0 0
90 0 break;
91  
92 0 ▶ if (tag->data[i] != *tagname)
Branch: 1 branch
BranchTrueFalseTotal
if (tag->data[i] != *tagname) 0 0 0
93 0 return 0;
94 }
95  
96 0 ▶ if (i == tag->size)
Branch: 1 branch
BranchTrueFalseTotal
if (i == tag->size) 0 0 0
97 0 return 0;
98  
99 0 ▶ return (isspace(tag->data[i]) || tag->data[i] == '>');
Branch: 2 branches
BranchTrueFalseTotal
return (isspace(tag->data[i]) || tag->data[i] == '>'); 0 0 0
return (isspace(tag->data[i]) || tag->data[i] == '>'); 0 0 0
100 }
101  
102 /********************
103 * GENERIC RENDERER *
104 ********************/
105 static int
106 0 rndr_autolink(struct buf *ob, struct buf *link, enum mkd_autolink type, void *opaque)
107 {
108 0 struct html_renderopt *options = opaque;
109  
110 0 ▶ if (!link || !link->size)
Branch: 2 branches
BranchTrueFalseTotal
if (!link || !link->size) 0 0 0
if (!link || !link->size) 0 0 0
111 0 return 0;
112  
113 0 if ((options->flags & HTML_SAFELINK) != 0 &&
114 0 ▶ !is_safe_link(link->data, link->size) &&
Branch: 1 branch
BranchTrueFalseTotal
!is_safe_link(link->data, link->size) && 0 0 0
115 type != MKDA_EMAIL)
116 0 return 0;
117  
118 0 BUFPUTSL(ob, "<a href=\"");
119 0 ▶ if (type == MKDA_EMAIL)
Branch: 1 branch
BranchTrueFalseTotal
if (type == MKDA_EMAIL) 0 0 0
120 0 BUFPUTSL(ob, "mailto:");
121 0 bufput(ob, link->data, link->size);
122 0 BUFPUTSL(ob, "\">");
123  
124 /*
125 * Pretty printing: if we get an email address as
126 * an actual URI, e.g. `mailto:foo@bar.com`, we don't
127 * want to print the `mailto:` prefix
128 */
129 0 ▶ if (bufprefix(link, "mailto:") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bufprefix(link, "mailto:") == 0) { 0 0 0
130 0 upshtml_escape(ob, link->data + 7, link->size - 7);
131 } else {
132 0 upshtml_escape(ob, link->data, link->size);
133 }
134  
135 0 BUFPUTSL(ob, "</a>");
136  
137 0 return 1;
138 }
139  
140 static void
141 0 rndr_blockcode(struct buf *ob, struct buf *text, struct buf *lang, void *opaque)
142 {
143 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
144  
145 0 ▶ if (lang && lang->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (lang && lang->size) { 0 0 0
if (lang && lang->size) { 0 0 0
146 size_t i, cls;
147 0 BUFPUTSL(ob, "<pre><code class=\"");
148  
149 0 ▶ for (i = 0, cls = 0; i < lang->size; ++i, ++cls) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, cls = 0; i < lang->size; ++i, ++cls) { 0 0 0
150 0 ▶ while (i < lang->size && isspace(lang->data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < lang->size && isspace(lang->data[i])) 0 0 0
while (i < lang->size && isspace(lang->data[i])) 0 0 0
151 0 i++;
152  
153 0 ▶ if (i < lang->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < lang->size) { 0 0 0
154 0 size_t org = i;
155 0 ▶ while (i < lang->size && !isspace(lang->data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < lang->size && !isspace(lang->data[i])) 0 0 0
while (i < lang->size && !isspace(lang->data[i])) 0 0 0
156 0 i++;
157  
158 0 ▶ if (lang->data[org] == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (lang->data[org] == '.') 0 0 0
159 0 org++;
160  
161 0 ▶ if (cls) bufputc(ob, ' ');
Branch: 1 branch
BranchTrueFalseTotal
if (cls) bufputc(ob, ' '); 0 0 0
162 0 upshtml_escape(ob, lang->data + org, i - org);
163 }
164 }
165  
166 0 BUFPUTSL(ob, "\">");
167 } else
168 0 BUFPUTSL(ob, "<pre><code>");
169  
170 0 ▶ if (text)
Branch: 1 branch
BranchTrueFalseTotal
if (text) 0 0 0
171 0 upshtml_escape(ob, text->data, text->size);
172  
173 0 BUFPUTSL(ob, "</code></pre>\n");
174 0 }
175  
176 /*
177 * GitHub style code block:
178 *
179 * <pre lang="LANG"><code>
180 * ...
181 * </pre></code>
182 *
183 * Unlike other parsers, we store the language identifier in the <pre>,
184 * and don't let the user generate custom classes.
185 *
186 * The language identifier in the <pre> block gets postprocessed and all
187 * the code inside gets syntax highlighted with Pygments. This is much safer
188 * than letting the user specify a CSS class for highlighting.
189 *
190 * Note that we only generate HTML for the first specifier.
191 * E.g.
192 * ~~~~ {.python .numbered} => <pre lang="python"><code>
193 */
194 static void
195 0 rndr_blockcode_github(struct buf *ob, struct buf *text, struct buf *lang, void *opaque)
196 {
197 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
198  
199 0 ▶ if (lang && lang->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (lang && lang->size) { 0 0 0
if (lang && lang->size) { 0 0 0
200 0 size_t i = 0;
201 0 BUFPUTSL(ob, "<pre lang=\"");
202  
203 0 ▶ while (i < lang->size && !isspace(lang->data[i]))
Branch: 2 branches
BranchTrueFalseTotal
while (i < lang->size && !isspace(lang->data[i])) 0 0 0
while (i < lang->size && !isspace(lang->data[i])) 0 0 0
204 0 i++;
205  
206 0 ▶ if (lang->data[0] == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (lang->data[0] == '.') 0 0 0
207 0 upshtml_escape(ob, lang->data + 1, i - 1);
208 else
209 0 upshtml_escape(ob, lang->data, i);
210  
211 0 BUFPUTSL(ob, "\"><code>");
212 } else
213 0 BUFPUTSL(ob, "<pre><code>");
214  
215 0 ▶ if (text)
Branch: 1 branch
BranchTrueFalseTotal
if (text) 0 0 0
216 0 upshtml_escape(ob, text->data, text->size);
217  
218 0 BUFPUTSL(ob, "</code></pre>\n");
219 0 }
220  
221 static void
222 0 rndr_blockquote(struct buf *ob, struct buf *text, void *opaque)
223 {
224 0 BUFPUTSL(ob, "<blockquote>\n");
225 0 ▶ if (text) bufput(ob, text->data, text->size);
Branch: 1 branch
BranchTrueFalseTotal
if (text) bufput(ob, text->data, text->size); 0 0 0
226 0 BUFPUTSL(ob, "</blockquote>");
227 0 }
228  
229 static int
230 0 rndr_codespan(struct buf *ob, struct buf *text, void *opaque)
231 {
232 0 BUFPUTSL(ob, "<code>");
233 0 ▶ if (text) upshtml_escape(ob, text->data, text->size);
Branch: 1 branch
BranchTrueFalseTotal
if (text) upshtml_escape(ob, text->data, text->size); 0 0 0
234 0 BUFPUTSL(ob, "</code>");
235 0 return 1;
236 }
237  
238 static int
239 0 rndr_strikethrough(struct buf *ob, struct buf *text, void *opaque)
240 {
241 0 ▶ if (!text || !text->size)
Branch: 2 branches
BranchTrueFalseTotal
if (!text || !text->size) 0 0 0
if (!text || !text->size) 0 0 0
242 0 return 0;
243  
244 0 BUFPUTSL(ob, "<del>");
245 0 bufput(ob, text->data, text->size);
246 0 BUFPUTSL(ob, "</del>");
247 0 return 1;
248 }
249  
250 static int
251 0 rndr_double_emphasis(struct buf *ob, struct buf *text, void *opaque)
252 {
253 0 ▶ if (!text || !text->size)
Branch: 2 branches
BranchTrueFalseTotal
if (!text || !text->size) 0 0 0
if (!text || !text->size) 0 0 0
254 0 return 0;
255  
256 0 BUFPUTSL(ob, "<strong>");
257 0 bufput(ob, text->data, text->size);
258 0 BUFPUTSL(ob, "</strong>");
259  
260 0 return 1;
261 }
262  
263 static int
264 0 rndr_emphasis(struct buf *ob, struct buf *text, void *opaque)
265 {
266 0 ▶ if (!text || !text->size) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!text || !text->size) return 0; 0 0 0
if (!text || !text->size) return 0; 0 0 0
267 0 BUFPUTSL(ob, "<em>");
268 0 ▶ if (text) bufput(ob, text->data, text->size);
Branch: 1 branch
BranchTrueFalseTotal
if (text) bufput(ob, text->data, text->size); 0 0 0
269 0 BUFPUTSL(ob, "</em>");
270 0 return 1;
271 }
272  
273 static void
274 1 rndr_header(struct buf *ob, struct buf *text, int level, void *opaque)
275 {
276 1 struct html_renderopt *options = opaque;
277
278 1 ▶ if (ob->size)
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) 0 1 1
279 0 bufputc(ob, '\n');
280  
281 1 ▶ if (options->flags & HTML_TOC)
Branch: 1 branch
BranchTrueFalseTotal
if (options->flags & HTML_TOC) 0 1 1
282 0 bufprintf(ob, "<h%d id=\"toc_%d\">", level, options->toc_data.header_count++);
283 else
284 1 bufprintf(ob, "<h%d>", level);
285  
286 1 ▶ if (text) bufput(ob, text->data, text->size);
Branch: 1 branch
BranchTrueFalseTotal
if (text) bufput(ob, text->data, text->size); 1 0 1
287 1 bufprintf(ob, "</h%d>\n", level);
288 1 }
289  
290 static int
291 0 rndr_link(struct buf *ob, struct buf *link, struct buf *title, struct buf *content, void *opaque)
292 {
293 0 struct html_renderopt *options = opaque;
294
295 0 ▶ if ((options->flags & HTML_SAFELINK) != 0 && !is_safe_link(link->data, link->size))
Branch: 2 branches
BranchTrueFalseTotal
if ((options->flags & HTML_SAFELINK) != 0 && !is_safe_link(link->data, link->size)) 0 0 0
if ((options->flags & HTML_SAFELINK) != 0 && !is_safe_link(link->data, link->size)) 0 0 0
296 0 return 0;
297  
298 0 BUFPUTSL(ob, "<a href=\"");
299 0 ▶ if (link && link->size) bufput(ob, link->data, link->size);
Branch: 2 branches
BranchTrueFalseTotal
if (link && link->size) bufput(ob, link->data, link->size); 0 0 0
if (link && link->size) bufput(ob, link->data, link->size); 0 0 0
300 0 ▶ if (title && title->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (title && title->size) { 0 0 0
if (title && title->size) { 0 0 0
301 0 BUFPUTSL(ob, "\" title=\"");
302 0 upshtml_escape(ob, title->data, title->size); }
303 0 BUFPUTSL(ob, "\">");
304 0 ▶ if (content && content->size) bufput(ob, content->data, content->size);
Branch: 2 branches
BranchTrueFalseTotal
if (content && content->size) bufput(ob, content->data, content->size); 0 0 0
if (content && content->size) bufput(ob, content->data, content->size); 0 0 0
305 0 BUFPUTSL(ob, "</a>");
306 0 return 1;
307 }
308  
309 static void
310 1 rndr_list(struct buf *ob, struct buf *text, int flags, void *opaque)
311 {
312 1 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 1 0 1
313 1 ▶ bufput(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5);
Branch: 1 branch
BranchTrueFalseTotal
bufput(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5); 0 1 1
314 1 ▶ if (text) bufput(ob, text->data, text->size);
Branch: 1 branch
BranchTrueFalseTotal
if (text) bufput(ob, text->data, text->size); 1 0 1
315 1 ▶ bufput(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6);
Branch: 1 branch
BranchTrueFalseTotal
bufput(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6); 0 1 1
316 1 }
317  
318 static void
319 2 rndr_listitem(struct buf *ob, struct buf *text, int flags, void *opaque)
320 {
321 2 BUFPUTSL(ob, "<li>");
322 2 ▶ if (text) {
Branch: 1 branch
BranchTrueFalseTotal
if (text) { 2 0 2
323 4 ▶ while (text->size && text->data[text->size - 1] == '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (text->size && text->data[text->size - 1] == '\n') 4 0 4
while (text->size && text->data[text->size - 1] == '\n') 2 2 4
324 2 text->size -= 1;
325 2 bufput(ob, text->data, text->size); }
326 2 BUFPUTSL(ob, "</li>\n");
327 2 }
328  
329 static void
330 1 rndr_paragraph(struct buf *ob, struct buf *text, void *opaque)
331 {
332 1 struct html_renderopt *options = opaque;
333 1 size_t i = 0;
334  
335 1 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 1 0 1
336  
337 1 ▶ if (!text || !text->size)
Branch: 2 branches
BranchTrueFalseTotal
if (!text || !text->size) 1 0 1
if (!text || !text->size) 0 1 1
338 0 return;
339  
340 1 ▶ while (i < text->size && isspace(text->data[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < text->size && isspace(text->data[i])) i++; 1 0 1
while (i < text->size && isspace(text->data[i])) i++; 0 1 1
341  
342 1 ▶ if (i == text->size)
Branch: 1 branch
BranchTrueFalseTotal
if (i == text->size) 0 1 1
343 0 return;
344  
345 1 BUFPUTSL(ob, "<p>");
346 1 ▶ if (options->flags & HTML_HARD_WRAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (options->flags & HTML_HARD_WRAP) { 0 1 1
347 size_t org;
348 0 ▶ while (i < text->size) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < text->size) { 0 0 0
349 0 org = i;
350 0 ▶ while (i < text->size && text->data[i] != '\n')
Branch: 2 branches
BranchTrueFalseTotal
while (i < text->size && text->data[i] != '\n') 0 0 0
while (i < text->size && text->data[i] != '\n') 0 0 0
351 0 i++;
352  
353 0 ▶ if (i > org)
Branch: 1 branch
BranchTrueFalseTotal
if (i > org) 0 0 0
354 0 bufput(ob, text->data + org, i - org);
355  
356 0 ▶ if (i >= text->size)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= text->size) 0 0 0
357 0 break;
358  
359 0 BUFPUTSL(ob, "<br");
360 0 bufputs(ob, options->close_tag);
361 0 i++;
362 }
363 } else {
364 1 bufput(ob, &text->data[i], text->size - i);
365 }
366 1 BUFPUTSL(ob, "</p>\n");
367 }
368  
369 static void
370 0 rndr_raw_block(struct buf *ob, struct buf *text, void *opaque)
371 {
372 size_t org, sz;
373 0 ▶ if (!text) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!text) return; 0 0 0
374 0 sz = text->size;
375 0 ▶ while (sz > 0 && text->data[sz - 1] == '\n') sz -= 1;
Branch: 2 branches
BranchTrueFalseTotal
while (sz > 0 && text->data[sz - 1] == '\n') sz -= 1; 0 0 0
while (sz > 0 && text->data[sz - 1] == '\n') sz -= 1; 0 0 0
376 0 org = 0;
377 0 ▶ while (org < sz && text->data[org] == '\n') org += 1;
Branch: 2 branches
BranchTrueFalseTotal
while (org < sz && text->data[org] == '\n') org += 1; 0 0 0
while (org < sz && text->data[org] == '\n') org += 1; 0 0 0
378 0 ▶ if (org >= sz) return;
Branch: 1 branch
BranchTrueFalseTotal
if (org >= sz) return; 0 0 0
379 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
380 0 bufput(ob, text->data + org, sz - org);
381 0 bufputc(ob, '\n');
382 }
383  
384 static int
385 0 rndr_triple_emphasis(struct buf *ob, struct buf *text, void *opaque)
386 {
387 0 ▶ if (!text || !text->size) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!text || !text->size) return 0; 0 0 0
if (!text || !text->size) return 0; 0 0 0
388 0 BUFPUTSL(ob, "<strong><em>");
389 0 bufput(ob, text->data, text->size);
390 0 BUFPUTSL(ob, "</em></strong>");
391 0 return 1;
392 }
393  
394 static void
395 0 rndr_hrule(struct buf *ob, void *opaque)
396 {
397 0 struct html_renderopt *options = opaque;
398 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
399 0 BUFPUTSL(ob, "<hr");
400 0 bufputs(ob, options->close_tag);
401 0 }
402  
403 static int
404 0 rndr_image(struct buf *ob, struct buf *link, struct buf *title, struct buf *alt, void *opaque)
405 {
406 0 struct html_renderopt *options = opaque;
407 0 ▶ if (!link || !link->size) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!link || !link->size) return 0; 0 0 0
if (!link || !link->size) return 0; 0 0 0
408 0 BUFPUTSL(ob, "<img src=\"");
409 0 upshtml_escape(ob, link->data, link->size);
410 0 BUFPUTSL(ob, "\" alt=\"");
411 0 ▶ if (alt && alt->size)
Branch: 2 branches
BranchTrueFalseTotal
if (alt && alt->size) 0 0 0
if (alt && alt->size) 0 0 0
412 0 upshtml_escape(ob, alt->data, alt->size);
413 0 ▶ if (title && title->size) {
Branch: 2 branches
BranchTrueFalseTotal
if (title && title->size) { 0 0 0
if (title && title->size) { 0 0 0
414 0 BUFPUTSL(ob, "\" title=\"");
415 0 upshtml_escape(ob, title->data, title->size); }
416  
417 0 bufputc(ob, '"');
418 0 bufputs(ob, options->close_tag);
419 0 return 1;
420 }
421  
422 static int
423 0 rndr_linebreak(struct buf *ob, void *opaque)
424 {
425 0 struct html_renderopt *options = opaque;
426 0 BUFPUTSL(ob, "<br");
427 0 bufputs(ob, options->close_tag);
428 0 return 1;
429 }
430  
431 static int
432 0 rndr_raw_html(struct buf *ob, struct buf *text, void *opaque)
433 {
434 0 struct html_renderopt *options = opaque;
435  
436 0 ▶ if ((options->flags & HTML_SKIP_HTML) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((options->flags & HTML_SKIP_HTML) != 0) 0 0 0
437 0 return 1;
438  
439 0 ▶ if ((options->flags & HTML_SKIP_STYLE) != 0 && is_html_tag(text, "style"))
Branch: 2 branches
BranchTrueFalseTotal
if ((options->flags & HTML_SKIP_STYLE) != 0 && is_html_tag(text, "style")) 0 0 0
if ((options->flags & HTML_SKIP_STYLE) != 0 && is_html_tag(text, "style")) 0 0 0
440 0 return 1;
441  
442 0 ▶ if ((options->flags & HTML_SKIP_LINKS) != 0 && is_html_tag(text, "a"))
Branch: 2 branches
BranchTrueFalseTotal
if ((options->flags & HTML_SKIP_LINKS) != 0 && is_html_tag(text, "a")) 0 0 0
if ((options->flags & HTML_SKIP_LINKS) != 0 && is_html_tag(text, "a")) 0 0 0
443 0 return 1;
444  
445 0 ▶ if ((options->flags & HTML_SKIP_IMAGES) != 0 && is_html_tag(text, "img"))
Branch: 2 branches
BranchTrueFalseTotal
if ((options->flags & HTML_SKIP_IMAGES) != 0 && is_html_tag(text, "img")) 0 0 0
if ((options->flags & HTML_SKIP_IMAGES) != 0 && is_html_tag(text, "img")) 0 0 0
446 0 return 1;
447  
448 0 bufput(ob, text->data, text->size);
449 0 return 1;
450 }
451  
452 static void
453 0 rndr_table(struct buf *ob, struct buf *header, struct buf *body, void *opaque)
454 {
455 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
456 0 BUFPUTSL(ob, "<table><thead>\n");
457 0 ▶ if (header)
Branch: 1 branch
BranchTrueFalseTotal
if (header) 0 0 0
458 0 bufput(ob, header->data, header->size);
459 0 BUFPUTSL(ob, "\n</thead><tbody>\n");
460 0 ▶ if (body)
Branch: 1 branch
BranchTrueFalseTotal
if (body) 0 0 0
461 0 bufput(ob, body->data, body->size);
462 0 BUFPUTSL(ob, "\n</tbody></table>");
463 0 }
464  
465 static void
466 0 rndr_tablerow(struct buf *ob, struct buf *text, void *opaque)
467 {
468 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
469 0 BUFPUTSL(ob, "<tr>\n");
470 0 ▶ if (text)
Branch: 1 branch
BranchTrueFalseTotal
if (text) 0 0 0
471 0 bufput(ob, text->data, text->size);
472 0 BUFPUTSL(ob, "\n</tr>");
473 0 }
474  
475 static void
476 0 rndr_tablecell(struct buf *ob, struct buf *text, int align, void *opaque)
477 {
478 0 ▶ if (ob->size) bufputc(ob, '\n');
Branch: 1 branch
BranchTrueFalseTotal
if (ob->size) bufputc(ob, '\n'); 0 0 0
479 0 switch (align) {
480 0 case MKD_TABLE_ALIGN_L:
481 0 BUFPUTSL(ob, "<td align=\"left\">");
482 0 break;
483  
484 0 case MKD_TABLE_ALIGN_R:
485 0 BUFPUTSL(ob, "<td align=\"right\">");
486 0 break;
487  
488 0 case MKD_TABLE_ALIGN_CENTER:
489 0 BUFPUTSL(ob, "<td align=\"center\">");
490 0 break;
491  
492 0 default:
493 0 BUFPUTSL(ob, "<td>");
494 0 break;
495 }
496  
497 0 ▶ if (text)
Branch: 1 branch
BranchTrueFalseTotal
if (text) 0 0 0
498 0 bufput(ob, text->data, text->size);
499 0 BUFPUTSL(ob, "</td>");
500 0 }
501  
502 static void
503 7 rndr_normal_text(struct buf *ob, struct buf *text, void *opaque)
504 {
505 7 ▶ if (text)
Branch: 1 branch
BranchTrueFalseTotal
if (text) 7 0 7
506 7 upshtml_escape(ob, text->data, text->size);
507 7 }
508  
509 static void
510 0 toc_header(struct buf *ob, struct buf *text, int level, void *opaque)
511 {
512 0 struct html_renderopt *options = opaque;
513  
514 0 ▶ while (level > options->toc_data.current_level) {
Branch: 1 branch
BranchTrueFalseTotal
while (level > options->toc_data.current_level) { 0 0 0
515 0 ▶ if (options->toc_data.current_level > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (options->toc_data.current_level > 0) 0 0 0
516 0 BUFPUTSL(ob, "<li>");
517 0 BUFPUTSL(ob, "<ul>\n");
518 0 options->toc_data.current_level++;
519 }
520  
521 0 ▶ while (level < options->toc_data.current_level) {
Branch: 1 branch
BranchTrueFalseTotal
while (level < options->toc_data.current_level) { 0 0 0
522 0 BUFPUTSL(ob, "</ul>");
523 0 ▶ if (options->toc_data.current_level > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (options->toc_data.current_level > 1) 0 0 0
524 0 BUFPUTSL(ob, "</li>\n");
525 0 options->toc_data.current_level--;
526 }
527  
528 0 bufprintf(ob, "<li><a href=\"#toc_%d\">", options->toc_data.header_count++);
529 0 ▶ if (text)
Branch: 1 branch
BranchTrueFalseTotal
if (text) 0 0 0
530 0 bufput(ob, text->data, text->size);
531 0 BUFPUTSL(ob, "</a></li>\n");
532 0 }
533  
534 static void
535 0 toc_finalize(struct buf *ob, void *opaque)
536 {
537 0 struct html_renderopt *options = opaque;
538  
539 0 ▶ while (options->toc_data.current_level > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (options->toc_data.current_level > 1) { 0 0 0
540 0 BUFPUTSL(ob, "</ul></li>\n");
541 0 options->toc_data.current_level--;
542 }
543  
544 0 ▶ if (options->toc_data.current_level)
Branch: 1 branch
BranchTrueFalseTotal
if (options->toc_data.current_level) 0 0 0
545 0 BUFPUTSL(ob, "</ul>\n");
546 0 }
547  
548 void
549 0 upshtml_toc_renderer(struct mkd_renderer *renderer)
550 {
551 static const struct mkd_renderer toc_render = {
552 NULL,
553 NULL,
554 NULL,
555 toc_header,
556 NULL,
557 NULL,
558 NULL,
559 NULL,
560 NULL,
561 NULL,
562 NULL,
563  
564 NULL,
565 rndr_codespan,
566 rndr_double_emphasis,
567 rndr_emphasis,
568 NULL,
569 NULL,
570 NULL,
571 NULL,
572 rndr_triple_emphasis,
573 rndr_strikethrough,
574  
575 NULL,
576 NULL,
577  
578 NULL,
579 toc_finalize,
580  
581 NULL
582 };
583  
584 struct html_renderopt *options;
585 0 options = calloc(1, sizeof(struct html_renderopt));
586 0 options->flags = HTML_TOC;
587  
588 0 memcpy(renderer, &toc_render, sizeof(struct mkd_renderer));
589 0 renderer->opaque = options;
590 0 }
591  
592 void
593 1 upshtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags)
594 {
595 static const char *xhtml_close = "/>\n";
596 static const char *html_close = ">\n";
597  
598 static const struct mkd_renderer renderer_default = {
599 rndr_blockcode,
600 rndr_blockquote,
601 rndr_raw_block,
602 rndr_header,
603 rndr_hrule,
604 rndr_list,
605 rndr_listitem,
606 rndr_paragraph,
607 rndr_table,
608 rndr_tablerow,
609 rndr_tablecell,
610  
611 rndr_autolink,
612 rndr_codespan,
613 rndr_double_emphasis,
614 rndr_emphasis,
615 rndr_image,
616 rndr_linebreak,
617 rndr_link,
618 rndr_raw_html,
619 rndr_triple_emphasis,
620 rndr_strikethrough,
621  
622 NULL,
623 rndr_normal_text,
624  
625 NULL,
626 NULL,
627  
628 NULL
629 };
630  
631 struct html_renderopt *options;
632 1 options = calloc(1, sizeof(struct html_renderopt));
633 1 options->flags = render_flags;
634 1 ▶ options->close_tag = (render_flags & HTML_USE_XHTML) ? xhtml_close : html_close;
Branch: 1 branch
BranchTrueFalseTotal
options->close_tag = (render_flags & HTML_USE_XHTML) ? xhtml_close : html_close; 0 1 1
635  
636 1 memcpy(renderer, &renderer_default, sizeof(struct mkd_renderer));
637 1 renderer->opaque = options;
638  
639 1 ▶ if (render_flags & HTML_SKIP_IMAGES)
Branch: 1 branch
BranchTrueFalseTotal
if (render_flags & HTML_SKIP_IMAGES) 0 1 1
640 0 renderer->image = NULL;
641  
642 1 ▶ if (render_flags & HTML_SKIP_LINKS) {
Branch: 1 branch
BranchTrueFalseTotal
if (render_flags & HTML_SKIP_LINKS) { 0 1 1
643 0 renderer->link = NULL;
644 0 renderer->autolink = NULL;
645 }
646  
647 1 ▶ if (render_flags & HTML_SKIP_HTML)
Branch: 1 branch
BranchTrueFalseTotal
if (render_flags & HTML_SKIP_HTML) 0 1 1
648 0 renderer->blockhtml = NULL;
649  
650 1 ▶ if (render_flags & HTML_GITHUB_BLOCKCODE)
Branch: 1 branch
BranchTrueFalseTotal
if (render_flags & HTML_GITHUB_BLOCKCODE) 0 1 1
651 0 renderer->blockcode = rndr_blockcode_github;
652 1 }
653  
654 void
655 1 upshtml_free_renderer(struct mkd_renderer *renderer)
656 {
657 1 free(renderer->opaque);
658 1 }
659  
« array.c ↑ Summary markdown.c »