/work/workdir/UnpackedTarball/md4c/src/md4c-html.c
Line | Count | Source |
1 | | /* |
2 | | * MD4C: Markdown parser for C |
3 | | * (http://github.com/mity/md4c) |
4 | | * |
5 | | * Copyright (c) 2016-2024 Martin Mitáš |
6 | | * |
7 | | * Permission is hereby granted, free of charge, to any person obtaining a |
8 | | * copy of this software and associated documentation files (the "Software"), |
9 | | * to deal in the Software without restriction, including without limitation |
10 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
11 | | * and/or sell copies of the Software, and to permit persons to whom the |
12 | | * Software is furnished to do so, subject to the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be included in |
15 | | * all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
18 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
22 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
23 | | * IN THE SOFTWARE. |
24 | | */ |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <string.h> |
28 | | |
29 | | #include "md4c-html.h" |
30 | | #include "entity.h" |
31 | | |
32 | | |
33 | | #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199409L |
34 | | /* C89/90 or old compilers in general may not understand "inline". */ |
35 | | #if defined __GNUC__ |
36 | | #define inline __inline__ |
37 | | #elif defined _MSC_VER |
38 | | #define inline __inline |
39 | | #else |
40 | | #define inline |
41 | | #endif |
42 | | #endif |
43 | | |
44 | | #ifdef _WIN32 |
45 | | #define snprintf _snprintf |
46 | | #endif |
47 | | |
48 | | |
49 | | |
50 | | typedef struct MD_HTML_tag MD_HTML; |
51 | | struct MD_HTML_tag { |
52 | | void (*process_output)(const MD_CHAR*, MD_SIZE, void*); |
53 | | void* userdata; |
54 | | unsigned flags; |
55 | | int image_nesting_level; |
56 | | char escape_map[256]; |
57 | | }; |
58 | | |
59 | 0 | #define NEED_HTML_ESC_FLAG 0x1 |
60 | 0 | #define NEED_URL_ESC_FLAG 0x2 |
61 | | |
62 | | |
63 | | /***************************************** |
64 | | *** HTML rendering helper functions *** |
65 | | *****************************************/ |
66 | | |
67 | 0 | #define ISDIGIT(ch) ('0' <= (ch) && (ch) <= '9') |
68 | 0 | #define ISLOWER(ch) ('a' <= (ch) && (ch) <= 'z') |
69 | 0 | #define ISUPPER(ch) ('A' <= (ch) && (ch) <= 'Z') |
70 | 0 | #define ISALNUM(ch) (ISLOWER(ch) || ISUPPER(ch) || ISDIGIT(ch)) |
71 | | |
72 | | |
73 | | static inline void |
74 | | render_verbatim(MD_HTML* r, const MD_CHAR* text, MD_SIZE size) |
75 | 0 | { |
76 | 0 | r->process_output(text, size, r->userdata); |
77 | 0 | } |
78 | | |
79 | | /* Keep this as a macro. Most compiler should then be smart enough to replace |
80 | | * the strlen() call with a compile-time constant if the string is a C literal. */ |
81 | | #define RENDER_VERBATIM(r, verbatim) \ |
82 | 0 | render_verbatim((r), (verbatim), (MD_SIZE) (strlen(verbatim))) |
83 | | |
84 | | |
85 | | static void |
86 | | render_html_escaped(MD_HTML* r, const MD_CHAR* data, MD_SIZE size) |
87 | 0 | { |
88 | 0 | MD_OFFSET beg = 0; |
89 | 0 | MD_OFFSET off = 0; |
90 | | |
91 | | /* Some characters need to be escaped in normal HTML text. */ |
92 | 0 | #define NEED_HTML_ESC(ch) (r->escape_map[(unsigned char)(ch)] & NEED_HTML_ESC_FLAG) |
93 | |
|
94 | 0 | while(1) { |
95 | | /* Optimization: Use some loop unrolling. */ |
96 | 0 | while(off + 3 < size && !NEED_HTML_ESC(data[off+0]) && !NEED_HTML_ESC(data[off+1]) |
97 | 0 | && !NEED_HTML_ESC(data[off+2]) && !NEED_HTML_ESC(data[off+3])) |
98 | 0 | off += 4; |
99 | 0 | while(off < size && !NEED_HTML_ESC(data[off])) |
100 | 0 | off++; |
101 | |
|
102 | 0 | if(off > beg) |
103 | 0 | render_verbatim(r, data + beg, off - beg); |
104 | |
|
105 | 0 | if(off < size) { |
106 | 0 | switch(data[off]) { |
107 | 0 | case '&': RENDER_VERBATIM(r, "&"); break; |
108 | 0 | case '<': RENDER_VERBATIM(r, "<"); break; |
109 | 0 | case '>': RENDER_VERBATIM(r, ">"); break; |
110 | 0 | case '"': RENDER_VERBATIM(r, """); break; |
111 | 0 | } |
112 | 0 | off++; |
113 | 0 | } else { |
114 | 0 | break; |
115 | 0 | } |
116 | 0 | beg = off; |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | | static void |
121 | | render_url_escaped(MD_HTML* r, const MD_CHAR* data, MD_SIZE size) |
122 | 0 | { |
123 | 0 | static const MD_CHAR hex_chars[] = "0123456789ABCDEF"; |
124 | 0 | MD_OFFSET beg = 0; |
125 | 0 | MD_OFFSET off = 0; |
126 | | |
127 | | /* Some characters need to be escaped in URL attributes. */ |
128 | 0 | #define NEED_URL_ESC(ch) (r->escape_map[(unsigned char)(ch)] & NEED_URL_ESC_FLAG) |
129 | |
|
130 | 0 | while(1) { |
131 | 0 | while(off < size && !NEED_URL_ESC(data[off])) |
132 | 0 | off++; |
133 | 0 | if(off > beg) |
134 | 0 | render_verbatim(r, data + beg, off - beg); |
135 | |
|
136 | 0 | if(off < size) { |
137 | 0 | char hex[3]; |
138 | |
|
139 | 0 | switch(data[off]) { |
140 | 0 | case '&': RENDER_VERBATIM(r, "&"); break; |
141 | 0 | default: |
142 | 0 | hex[0] = '%'; |
143 | 0 | hex[1] = hex_chars[((unsigned)data[off] >> 4) & 0xf]; |
144 | 0 | hex[2] = hex_chars[((unsigned)data[off] >> 0) & 0xf]; |
145 | 0 | render_verbatim(r, hex, 3); |
146 | 0 | break; |
147 | 0 | } |
148 | 0 | off++; |
149 | 0 | } else { |
150 | 0 | break; |
151 | 0 | } |
152 | | |
153 | 0 | beg = off; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | static unsigned |
158 | | hex_val(char ch) |
159 | 0 | { |
160 | 0 | if('0' <= ch && ch <= '9') |
161 | 0 | return ch - '0'; |
162 | 0 | if('A' <= ch && ch <= 'Z') |
163 | 0 | return ch - 'A' + 10; |
164 | 0 | else |
165 | 0 | return ch - 'a' + 10; |
166 | 0 | } |
167 | | |
168 | | static void |
169 | | render_utf8_codepoint(MD_HTML* r, unsigned codepoint, |
170 | | void (*fn_append)(MD_HTML*, const MD_CHAR*, MD_SIZE)) |
171 | 0 | { |
172 | 0 | static const MD_CHAR utf8_replacement_char[] = { (char)0xef, (char)0xbf, (char)0xbd }; |
173 | |
|
174 | 0 | unsigned char utf8[4]; |
175 | 0 | size_t n; |
176 | |
|
177 | 0 | if(codepoint <= 0x7f) { |
178 | 0 | n = 1; |
179 | 0 | utf8[0] = codepoint; |
180 | 0 | } else if(codepoint <= 0x7ff) { |
181 | 0 | n = 2; |
182 | 0 | utf8[0] = 0xc0 | ((codepoint >> 6) & 0x1f); |
183 | 0 | utf8[1] = 0x80 + ((codepoint >> 0) & 0x3f); |
184 | 0 | } else if(codepoint <= 0xffff) { |
185 | 0 | n = 3; |
186 | 0 | utf8[0] = 0xe0 | ((codepoint >> 12) & 0xf); |
187 | 0 | utf8[1] = 0x80 + ((codepoint >> 6) & 0x3f); |
188 | 0 | utf8[2] = 0x80 + ((codepoint >> 0) & 0x3f); |
189 | 0 | } else { |
190 | 0 | n = 4; |
191 | 0 | utf8[0] = 0xf0 | ((codepoint >> 18) & 0x7); |
192 | 0 | utf8[1] = 0x80 + ((codepoint >> 12) & 0x3f); |
193 | 0 | utf8[2] = 0x80 + ((codepoint >> 6) & 0x3f); |
194 | 0 | utf8[3] = 0x80 + ((codepoint >> 0) & 0x3f); |
195 | 0 | } |
196 | |
|
197 | 0 | if(0 < codepoint && codepoint <= 0x10ffff) |
198 | 0 | fn_append(r, (char*)utf8, (MD_SIZE)n); |
199 | 0 | else |
200 | 0 | fn_append(r, utf8_replacement_char, 3); |
201 | 0 | } |
202 | | |
203 | | /* Translate entity to its UTF-8 equivalent, or output the verbatim one |
204 | | * if such entity is unknown (or if the translation is disabled). */ |
205 | | static void |
206 | | render_entity(MD_HTML* r, const MD_CHAR* text, MD_SIZE size, |
207 | | void (*fn_append)(MD_HTML*, const MD_CHAR*, MD_SIZE)) |
208 | 0 | { |
209 | 0 | if(r->flags & MD_HTML_FLAG_VERBATIM_ENTITIES) { |
210 | 0 | render_verbatim(r, text, size); |
211 | 0 | return; |
212 | 0 | } |
213 | | |
214 | | /* We assume UTF-8 output is what is desired. */ |
215 | 0 | if(size > 3 && text[1] == '#') { |
216 | 0 | unsigned codepoint = 0; |
217 | |
|
218 | 0 | if(text[2] == 'x' || text[2] == 'X') { |
219 | | /* Hexadecimal entity (e.g. "�")). */ |
220 | 0 | MD_SIZE i; |
221 | 0 | for(i = 3; i < size-1; i++) |
222 | 0 | codepoint = 16 * codepoint + hex_val(text[i]); |
223 | 0 | } else { |
224 | | /* Decimal entity (e.g. "&1234;") */ |
225 | 0 | MD_SIZE i; |
226 | 0 | for(i = 2; i < size-1; i++) |
227 | 0 | codepoint = 10 * codepoint + (text[i] - '0'); |
228 | 0 | } |
229 | |
|
230 | 0 | render_utf8_codepoint(r, codepoint, fn_append); |
231 | 0 | return; |
232 | 0 | } else { |
233 | | /* Named entity (e.g. " "). */ |
234 | 0 | const ENTITY* ent; |
235 | |
|
236 | 0 | ent = entity_lookup(text, size); |
237 | 0 | if(ent != NULL) { |
238 | 0 | render_utf8_codepoint(r, ent->codepoints[0], fn_append); |
239 | 0 | if(ent->codepoints[1]) |
240 | 0 | render_utf8_codepoint(r, ent->codepoints[1], fn_append); |
241 | 0 | return; |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 0 | fn_append(r, text, size); |
246 | 0 | } |
247 | | |
248 | | static void |
249 | | render_attribute(MD_HTML* r, const MD_ATTRIBUTE* attr, |
250 | | void (*fn_append)(MD_HTML*, const MD_CHAR*, MD_SIZE)) |
251 | 0 | { |
252 | 0 | int i; |
253 | |
|
254 | 0 | for(i = 0; attr->substr_offsets[i] < attr->size; i++) { |
255 | 0 | MD_TEXTTYPE type = attr->substr_types[i]; |
256 | 0 | MD_OFFSET off = attr->substr_offsets[i]; |
257 | 0 | MD_SIZE size = attr->substr_offsets[i+1] - off; |
258 | 0 | const MD_CHAR* text = attr->text + off; |
259 | |
|
260 | 0 | switch(type) { |
261 | 0 | case MD_TEXT_NULLCHAR: render_utf8_codepoint(r, 0x0000, render_verbatim); break; |
262 | 0 | case MD_TEXT_ENTITY: render_entity(r, text, size, fn_append); break; |
263 | 0 | default: fn_append(r, text, size); break; |
264 | 0 | } |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | | |
269 | | static void |
270 | | render_open_ol_block(MD_HTML* r, const MD_BLOCK_OL_DETAIL* det) |
271 | 0 | { |
272 | 0 | char buf[64]; |
273 | |
|
274 | 0 | if(det->start == 1) { |
275 | 0 | RENDER_VERBATIM(r, "<ol>\n"); |
276 | 0 | return; |
277 | 0 | } |
278 | | |
279 | 0 | snprintf(buf, sizeof(buf), "<ol start=\"%u\">\n", det->start); |
280 | 0 | RENDER_VERBATIM(r, buf); |
281 | 0 | } |
282 | | |
283 | | static void |
284 | | render_open_li_block(MD_HTML* r, const MD_BLOCK_LI_DETAIL* det) |
285 | 0 | { |
286 | 0 | if(det->is_task) { |
287 | 0 | RENDER_VERBATIM(r, "<li class=\"task-list-item\">" |
288 | 0 | "<input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled"); |
289 | 0 | if(r->flags & MD_HTML_FLAG_XHTML) RENDER_VERBATIM(r, "=\"true\""); |
290 | 0 | if(det->task_mark == 'x' || det->task_mark == 'X') { |
291 | 0 | RENDER_VERBATIM(r, " checked"); |
292 | 0 | if(r->flags & MD_HTML_FLAG_XHTML) RENDER_VERBATIM(r, "=\"true\""); |
293 | 0 | } |
294 | 0 | RENDER_VERBATIM(r, (r->flags & MD_HTML_FLAG_XHTML) ? " />" : ">"); |
295 | 0 | } else { |
296 | 0 | RENDER_VERBATIM(r, "<li>"); |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | static void |
301 | | render_open_code_block(MD_HTML* r, const MD_BLOCK_CODE_DETAIL* det) |
302 | 0 | { |
303 | 0 | RENDER_VERBATIM(r, "<pre><code"); |
304 | | |
305 | | /* If known, output the HTML 5 attribute class="language-LANGNAME". */ |
306 | 0 | if(det->lang.text != NULL) { |
307 | 0 | RENDER_VERBATIM(r, " class=\""); |
308 | 0 | if(det->lang.size < 9 || strncmp(det->lang.text, "language-", 9) != 0) { |
309 | 0 | RENDER_VERBATIM(r, "language-"); |
310 | 0 | } |
311 | 0 | render_attribute(r, &det->lang, render_html_escaped); |
312 | 0 | RENDER_VERBATIM(r, "\""); |
313 | 0 | } |
314 | |
|
315 | 0 | RENDER_VERBATIM(r, ">"); |
316 | 0 | } |
317 | | |
318 | | static void |
319 | | render_open_td_block(MD_HTML* r, const MD_CHAR* cell_type, const MD_BLOCK_TD_DETAIL* det) |
320 | 0 | { |
321 | 0 | RENDER_VERBATIM(r, "<"); |
322 | 0 | RENDER_VERBATIM(r, cell_type); |
323 | |
|
324 | 0 | switch(det->align) { |
325 | 0 | case MD_ALIGN_LEFT: RENDER_VERBATIM(r, " align=\"left\">"); break; |
326 | 0 | case MD_ALIGN_CENTER: RENDER_VERBATIM(r, " align=\"center\">"); break; |
327 | 0 | case MD_ALIGN_RIGHT: RENDER_VERBATIM(r, " align=\"right\">"); break; |
328 | 0 | default: RENDER_VERBATIM(r, ">"); break; |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | | static void |
333 | | render_open_a_span(MD_HTML* r, const MD_SPAN_A_DETAIL* det) |
334 | 0 | { |
335 | 0 | RENDER_VERBATIM(r, "<a href=\""); |
336 | 0 | render_attribute(r, &det->href, render_url_escaped); |
337 | |
|
338 | 0 | if(det->title.text != NULL) { |
339 | 0 | RENDER_VERBATIM(r, "\" title=\""); |
340 | 0 | render_attribute(r, &det->title, render_html_escaped); |
341 | 0 | } |
342 | |
|
343 | 0 | RENDER_VERBATIM(r, "\">"); |
344 | 0 | } |
345 | | |
346 | | static void |
347 | | render_open_img_span(MD_HTML* r, const MD_SPAN_IMG_DETAIL* det) |
348 | 0 | { |
349 | 0 | RENDER_VERBATIM(r, "<img src=\""); |
350 | 0 | render_attribute(r, &det->src, render_url_escaped); |
351 | |
|
352 | 0 | RENDER_VERBATIM(r, "\" alt=\""); |
353 | 0 | } |
354 | | |
355 | | static void |
356 | | render_close_img_span(MD_HTML* r, const MD_SPAN_IMG_DETAIL* det) |
357 | 0 | { |
358 | 0 | if(det->title.text != NULL) { |
359 | 0 | RENDER_VERBATIM(r, "\" title=\""); |
360 | 0 | render_attribute(r, &det->title, render_html_escaped); |
361 | 0 | } |
362 | |
|
363 | 0 | RENDER_VERBATIM(r, (r->flags & MD_HTML_FLAG_XHTML) ? "\" />" : "\">"); |
364 | 0 | } |
365 | | |
366 | | static void |
367 | | render_open_wikilink_span(MD_HTML* r, const MD_SPAN_WIKILINK_DETAIL* det) |
368 | 0 | { |
369 | 0 | RENDER_VERBATIM(r, "<x-wikilink data-target=\""); |
370 | 0 | render_attribute(r, &det->target, render_html_escaped); |
371 | |
|
372 | 0 | RENDER_VERBATIM(r, "\">"); |
373 | 0 | } |
374 | | |
375 | | |
376 | | /************************************** |
377 | | *** HTML renderer implementation *** |
378 | | **************************************/ |
379 | | |
380 | | static int |
381 | | enter_block_callback(MD_BLOCKTYPE type, void* detail, void* userdata) |
382 | 0 | { |
383 | 0 | static const MD_CHAR* head[6] = { "<h1>", "<h2>", "<h3>", "<h4>", "<h5>", "<h6>" }; |
384 | 0 | MD_HTML* r = (MD_HTML*) userdata; |
385 | |
|
386 | 0 | switch(type) { |
387 | 0 | case MD_BLOCK_DOC: /* noop */ break; |
388 | 0 | case MD_BLOCK_QUOTE: RENDER_VERBATIM(r, "<blockquote>\n"); break; |
389 | 0 | case MD_BLOCK_UL: RENDER_VERBATIM(r, "<ul>\n"); break; |
390 | 0 | case MD_BLOCK_OL: render_open_ol_block(r, (const MD_BLOCK_OL_DETAIL*)detail); break; |
391 | 0 | case MD_BLOCK_LI: render_open_li_block(r, (const MD_BLOCK_LI_DETAIL*)detail); break; |
392 | 0 | case MD_BLOCK_HR: RENDER_VERBATIM(r, (r->flags & MD_HTML_FLAG_XHTML) ? "<hr />\n" : "<hr>\n"); break; |
393 | 0 | case MD_BLOCK_H: RENDER_VERBATIM(r, head[((MD_BLOCK_H_DETAIL*)detail)->level - 1]); break; |
394 | 0 | case MD_BLOCK_CODE: render_open_code_block(r, (const MD_BLOCK_CODE_DETAIL*) detail); break; |
395 | 0 | case MD_BLOCK_HTML: /* noop */ break; |
396 | 0 | case MD_BLOCK_P: RENDER_VERBATIM(r, "<p>"); break; |
397 | 0 | case MD_BLOCK_TABLE: RENDER_VERBATIM(r, "<table>\n"); break; |
398 | 0 | case MD_BLOCK_THEAD: RENDER_VERBATIM(r, "<thead>\n"); break; |
399 | 0 | case MD_BLOCK_TBODY: RENDER_VERBATIM(r, "<tbody>\n"); break; |
400 | 0 | case MD_BLOCK_TR: RENDER_VERBATIM(r, "<tr>\n"); break; |
401 | 0 | case MD_BLOCK_TH: render_open_td_block(r, "th", (MD_BLOCK_TD_DETAIL*)detail); break; |
402 | 0 | case MD_BLOCK_TD: render_open_td_block(r, "td", (MD_BLOCK_TD_DETAIL*)detail); break; |
403 | 0 | } |
404 | | |
405 | 0 | return 0; |
406 | 0 | } |
407 | | |
408 | | static int |
409 | | leave_block_callback(MD_BLOCKTYPE type, void* detail, void* userdata) |
410 | 0 | { |
411 | 0 | static const MD_CHAR* head[6] = { "</h1>\n", "</h2>\n", "</h3>\n", "</h4>\n", "</h5>\n", "</h6>\n" }; |
412 | 0 | MD_HTML* r = (MD_HTML*) userdata; |
413 | |
|
414 | 0 | switch(type) { |
415 | 0 | case MD_BLOCK_DOC: /*noop*/ break; |
416 | 0 | case MD_BLOCK_QUOTE: RENDER_VERBATIM(r, "</blockquote>\n"); break; |
417 | 0 | case MD_BLOCK_UL: RENDER_VERBATIM(r, "</ul>\n"); break; |
418 | 0 | case MD_BLOCK_OL: RENDER_VERBATIM(r, "</ol>\n"); break; |
419 | 0 | case MD_BLOCK_LI: RENDER_VERBATIM(r, "</li>\n"); break; |
420 | 0 | case MD_BLOCK_HR: /*noop*/ break; |
421 | 0 | case MD_BLOCK_H: RENDER_VERBATIM(r, head[((MD_BLOCK_H_DETAIL*)detail)->level - 1]); break; |
422 | 0 | case MD_BLOCK_CODE: RENDER_VERBATIM(r, "</code></pre>\n"); break; |
423 | 0 | case MD_BLOCK_HTML: /* noop */ break; |
424 | 0 | case MD_BLOCK_P: RENDER_VERBATIM(r, "</p>\n"); break; |
425 | 0 | case MD_BLOCK_TABLE: RENDER_VERBATIM(r, "</table>\n"); break; |
426 | 0 | case MD_BLOCK_THEAD: RENDER_VERBATIM(r, "</thead>\n"); break; |
427 | 0 | case MD_BLOCK_TBODY: RENDER_VERBATIM(r, "</tbody>\n"); break; |
428 | 0 | case MD_BLOCK_TR: RENDER_VERBATIM(r, "</tr>\n"); break; |
429 | 0 | case MD_BLOCK_TH: RENDER_VERBATIM(r, "</th>\n"); break; |
430 | 0 | case MD_BLOCK_TD: RENDER_VERBATIM(r, "</td>\n"); break; |
431 | 0 | } |
432 | | |
433 | 0 | return 0; |
434 | 0 | } |
435 | | |
436 | | static int |
437 | | enter_span_callback(MD_SPANTYPE type, void* detail, void* userdata) |
438 | 0 | { |
439 | 0 | MD_HTML* r = (MD_HTML*) userdata; |
440 | 0 | int inside_img = (r->image_nesting_level > 0); |
441 | | |
442 | | /* We are inside a Markdown image label. Markdown allows to use any emphasis |
443 | | * and other rich contents in that context similarly as in any link label. |
444 | | * |
445 | | * However, unlike in the case of links (where that contents becomescontents |
446 | | * of the <a>...</a> tag), in the case of images the contents is supposed to |
447 | | * fall into the attribute alt: <img alt="...">. |
448 | | * |
449 | | * In that context we naturally cannot output nested HTML tags. So lets |
450 | | * suppress them and only output the plain text (i.e. what falls into text() |
451 | | * callback). |
452 | | * |
453 | | * CommonMark specification declares this a recommended practice for HTML |
454 | | * output. |
455 | | */ |
456 | 0 | if(type == MD_SPAN_IMG) |
457 | 0 | r->image_nesting_level++; |
458 | 0 | if(inside_img) |
459 | 0 | return 0; |
460 | | |
461 | 0 | switch(type) { |
462 | 0 | case MD_SPAN_EM: RENDER_VERBATIM(r, "<em>"); break; |
463 | 0 | case MD_SPAN_STRONG: RENDER_VERBATIM(r, "<strong>"); break; |
464 | 0 | case MD_SPAN_U: RENDER_VERBATIM(r, "<u>"); break; |
465 | 0 | case MD_SPAN_A: render_open_a_span(r, (MD_SPAN_A_DETAIL*) detail); break; |
466 | 0 | case MD_SPAN_IMG: render_open_img_span(r, (MD_SPAN_IMG_DETAIL*) detail); break; |
467 | 0 | case MD_SPAN_CODE: RENDER_VERBATIM(r, "<code>"); break; |
468 | 0 | case MD_SPAN_DEL: RENDER_VERBATIM(r, "<del>"); break; |
469 | 0 | case MD_SPAN_LATEXMATH: RENDER_VERBATIM(r, "<x-equation>"); break; |
470 | 0 | case MD_SPAN_LATEXMATH_DISPLAY: RENDER_VERBATIM(r, "<x-equation type=\"display\">"); break; |
471 | 0 | case MD_SPAN_WIKILINK: render_open_wikilink_span(r, (MD_SPAN_WIKILINK_DETAIL*) detail); break; |
472 | 0 | } |
473 | | |
474 | 0 | return 0; |
475 | 0 | } |
476 | | |
477 | | static int |
478 | | leave_span_callback(MD_SPANTYPE type, void* detail, void* userdata) |
479 | 0 | { |
480 | 0 | MD_HTML* r = (MD_HTML*) userdata; |
481 | |
|
482 | 0 | if(type == MD_SPAN_IMG) |
483 | 0 | r->image_nesting_level--; |
484 | 0 | if(r->image_nesting_level > 0) |
485 | 0 | return 0; |
486 | | |
487 | 0 | switch(type) { |
488 | 0 | case MD_SPAN_EM: RENDER_VERBATIM(r, "</em>"); break; |
489 | 0 | case MD_SPAN_STRONG: RENDER_VERBATIM(r, "</strong>"); break; |
490 | 0 | case MD_SPAN_U: RENDER_VERBATIM(r, "</u>"); break; |
491 | 0 | case MD_SPAN_A: RENDER_VERBATIM(r, "</a>"); break; |
492 | 0 | case MD_SPAN_IMG: render_close_img_span(r, (MD_SPAN_IMG_DETAIL*) detail); break; |
493 | 0 | case MD_SPAN_CODE: RENDER_VERBATIM(r, "</code>"); break; |
494 | 0 | case MD_SPAN_DEL: RENDER_VERBATIM(r, "</del>"); break; |
495 | 0 | case MD_SPAN_LATEXMATH: /*fall through*/ |
496 | 0 | case MD_SPAN_LATEXMATH_DISPLAY: RENDER_VERBATIM(r, "</x-equation>"); break; |
497 | 0 | case MD_SPAN_WIKILINK: RENDER_VERBATIM(r, "</x-wikilink>"); break; |
498 | 0 | } |
499 | | |
500 | 0 | return 0; |
501 | 0 | } |
502 | | |
503 | | static int |
504 | | text_callback(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdata) |
505 | 0 | { |
506 | 0 | MD_HTML* r = (MD_HTML*) userdata; |
507 | |
|
508 | 0 | switch(type) { |
509 | 0 | case MD_TEXT_NULLCHAR: render_utf8_codepoint(r, 0x0000, render_verbatim); break; |
510 | 0 | case MD_TEXT_BR: RENDER_VERBATIM(r, (r->image_nesting_level == 0 |
511 | 0 | ? ((r->flags & MD_HTML_FLAG_XHTML) ? "<br />\n" : "<br>\n") |
512 | 0 | : " ")); |
513 | 0 | break; |
514 | 0 | case MD_TEXT_SOFTBR: RENDER_VERBATIM(r, (r->image_nesting_level == 0 ? "\n" : " ")); break; |
515 | 0 | case MD_TEXT_HTML: render_verbatim(r, text, size); break; |
516 | 0 | case MD_TEXT_ENTITY: render_entity(r, text, size, render_html_escaped); break; |
517 | 0 | default: render_html_escaped(r, text, size); break; |
518 | 0 | } |
519 | | |
520 | 0 | return 0; |
521 | 0 | } |
522 | | |
523 | | static void |
524 | | debug_log_callback(const char* msg, void* userdata) |
525 | 0 | { |
526 | 0 | MD_HTML* r = (MD_HTML*) userdata; |
527 | 0 | if(r->flags & MD_HTML_FLAG_DEBUG) |
528 | 0 | fprintf(stderr, "MD4C: %s\n", msg); |
529 | 0 | } |
530 | | |
531 | | int |
532 | | md_html(const MD_CHAR* input, MD_SIZE input_size, |
533 | | void (*process_output)(const MD_CHAR*, MD_SIZE, void*), |
534 | | void* userdata, unsigned parser_flags, unsigned renderer_flags) |
535 | 0 | { |
536 | 0 | MD_HTML render = { process_output, userdata, renderer_flags, 0, { 0 } }; |
537 | 0 | int i; |
538 | |
|
539 | 0 | MD_PARSER parser = { |
540 | 0 | 0, |
541 | 0 | parser_flags, |
542 | 0 | enter_block_callback, |
543 | 0 | leave_block_callback, |
544 | 0 | enter_span_callback, |
545 | 0 | leave_span_callback, |
546 | 0 | text_callback, |
547 | 0 | debug_log_callback, |
548 | 0 | NULL |
549 | 0 | }; |
550 | | |
551 | | /* Build map of characters which need escaping. */ |
552 | 0 | for(i = 0; i < 256; i++) { |
553 | 0 | unsigned char ch = (unsigned char) i; |
554 | |
|
555 | 0 | if(strchr("\"&<>", ch) != NULL) |
556 | 0 | render.escape_map[i] |= NEED_HTML_ESC_FLAG; |
557 | |
|
558 | 0 | if(!ISALNUM(ch) && strchr("~-_.+!*(),%#@?=;:/,+$", ch) == NULL) |
559 | 0 | render.escape_map[i] |= NEED_URL_ESC_FLAG; |
560 | 0 | } |
561 | | |
562 | | /* Consider skipping UTF-8 byte order mark (BOM). */ |
563 | 0 | if(renderer_flags & MD_HTML_FLAG_SKIP_UTF8_BOM && sizeof(MD_CHAR) == 1) { |
564 | 0 | static const MD_CHAR bom[3] = { (char)0xef, (char)0xbb, (char)0xbf }; |
565 | 0 | if(input_size >= sizeof(bom) && memcmp(input, bom, sizeof(bom)) == 0) { |
566 | 0 | input += sizeof(bom); |
567 | 0 | input_size -= sizeof(bom); |
568 | 0 | } |
569 | 0 | } |
570 | |
|
571 | 0 | return md_parse(input, input_size, &parser, (void*) &render); |
572 | 0 | } |
573 | | |