Debian Codegraph @

md2html/md2html.c

raw
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 <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "md4c-html.h"
32 #include "cmdline.h"
33
34
35
36 /* Global options. */
37 static unsigned parser_flags = 0;
38 #ifndef MD4C_USE_ASCII
39 static unsigned renderer_flags = MD_HTML_FLAG_DEBUG | MD_HTML_FLAG_SKIP_UTF8_BOM;
40 #else
41 static unsigned renderer_flags = MD_HTML_FLAG_DEBUG;
42 #endif
43 static int want_fullhtml = 0;
44 static int want_xhtml = 0;
45 static int want_stat = 0;
46 static int want_replay_fuzz = 0;
47
48 static const char* html_title = NULL;
49 static const char* css_path = NULL;
50
51
52 /*********************************
53 *** Simple grow-able buffer ***
54 *********************************/
55
56 /* We render to a memory buffer instead of directly outputting the rendered
57 * documents, as this allows using this utility for evaluating performance
58 * of MD4C (--stat option). This allows us to measure just time of the parser,
59 * without the I/O.
60 */
61
62 struct membuffer {
63 char* data;
64 size_t asize;
65 size_t size;
66 };
67
68 static void
69 membuf_init(struct membuffer* buf, MD_SIZE new_asize)
70 {
71 buf->size = 0;
72 buf->asize = new_asize;
73 buf->data = malloc(buf->asize);
74 if(buf->data == NULL) {
75 fprintf(stderr, "membuf_init: malloc() failed.\n");
76 exit(1);
77 }
78 }
79
80 static void
81 membuf_fini(struct membuffer* buf)
82 {
83 if(buf->data)
84 free(buf->data);
85 }
86
87 static void
88 membuf_grow(struct membuffer* buf, size_t new_asize)
89 {
90 buf->data = realloc(buf->data, new_asize);
91 if(buf->data == NULL) {
92 fprintf(stderr, "membuf_grow: realloc() failed.\n");
93 exit(1);
94 }
95 buf->asize = new_asize;
96 }
97
98 static void
99 membuf_append(struct membuffer* buf, const char* data, MD_SIZE size)
100 {
101 if(buf->asize < buf->size + size)
102 membuf_grow(buf, buf->size + buf->size / 2 + size);
103 memcpy(buf->data + buf->size, data, size);
104 buf->size += size;
105 }
106
107
108 /**********************
109 *** Main program ***
110 **********************/
111
112 static void
113 process_output(const MD_CHAR* text, MD_SIZE size, void* userdata)
114 {
115 membuf_append((struct membuffer*) userdata, text, size);
116 }
117
118 static int
119 process_file(const char* in_path, FILE* in, FILE* out)
120 {
121 size_t n;
122 struct membuffer buf_in = {0};
123 struct membuffer buf_out = {0};
124 int ret = -1;
125 clock_t t0, t1;
126 unsigned p_flags = parser_flags;
127 unsigned r_flags = renderer_flags;
128
129 membuf_init(&buf_in, 32 * 1024);
130
131 /* Read the input file into a buffer. */
132 while(1) {
133 if(buf_in.size >= buf_in.asize)
134 membuf_grow(&buf_in, buf_in.asize + buf_in.asize / 2);
135
136 n = fread(buf_in.data + buf_in.size, 1, buf_in.asize - buf_in.size, in);
137 if(n == 0)
138 break;
139 buf_in.size += n;
140 }
141
142 /* Input size is good estimation of output size. Add some more reserve to
143 * deal with the HTML header/footer and tags. */
144 membuf_init(&buf_out, (MD_SIZE)(buf_in.size + buf_in.size/8 + 64));
145
146 /* Special mode for reproduce test case found with fuzzing a tool.
147 * We assume file format as produced by test/fuzzers/fuzz-mdhtml.c. */
148 if(want_replay_fuzz) {
149 if(buf_in.size < 2 * sizeof(unsigned)) {
150 fprintf(stderr, "File %s isn't valid fuzz test case.\n", in_path);
151 ret = -1;
152 goto out;
153 }
154
155 /* Override parser and renderer flags with those form the test case. */
156 p_flags = ((unsigned*)buf_in.data)[0];
157 r_flags = ((unsigned*)buf_in.data)[1];
158
159 /* And get rid of them from the text input to the parser. */
160 memmove(buf_in.data, buf_in.data + 2 * sizeof(unsigned),
161 buf_in.size - 2 * sizeof(unsigned));
162 buf_in.size -= 2 * sizeof(unsigned);
163
164 /* Zero the tail we have moved the contents from.
165 * It helps in debugging if make it actually a zero-terminated string. */
166 memset(buf_in.data + buf_in.size, 0, 2 * sizeof(unsigned));
167 }
168
169 /* Parse the document. This shall call our callbacks provided via the
170 * md_renderer_t structure. */
171 t0 = clock();
172
173 ret = md_html(buf_in.data, (MD_SIZE)buf_in.size, process_output,
174 (void*) &buf_out, p_flags, r_flags);
175
176 t1 = clock();
177 if(ret != 0) {
178 fprintf(stderr, "Parsing failed.\n");
179 goto out;
180 }
181
182 /* Write down the document in the HTML format. */
183 if(want_fullhtml) {
184 if(want_xhtml) {
185 fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
186 fprintf(out, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
187 "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
188 fprintf(out, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
189 } else {
190 fprintf(out, "<!DOCTYPE html>\n");
191 fprintf(out, "<html>\n");
192 }
193 fprintf(out, "<head>\n");
194 fprintf(out, "<title>%s</title>\n", html_title ? html_title : "");
195 fprintf(out, "<meta name=\"generator\" content=\"md2html\"%s>\n", want_xhtml ? " /" : "");
196 #if !defined MD4C_USE_ASCII && !defined MD4C_USE_UTF16
197 fprintf(out, "<meta charset=\"UTF-8\"%s>\n", want_xhtml ? " /" : "");
198 #endif
199 if(css_path != NULL) {
200 fprintf(out, "<link rel=\"stylesheet\" href=\"%s\"%s>\n", css_path, want_xhtml ? " /" : "");
201 }
202 fprintf(out, "</head>\n");
203 fprintf(out, "<body>\n");
204 }
205
206 fwrite(buf_out.data, 1, buf_out.size, out);
207
208 if(want_fullhtml) {
209 fprintf(out, "</body>\n");
210 fprintf(out, "</html>\n");
211 }
212
213 if(want_stat) {
214 if(t0 != (clock_t)-1 && t1 != (clock_t)-1) {
215 double elapsed = (double)(t1 - t0) / CLOCKS_PER_SEC;
216 if (elapsed < 1)
217 fprintf(stderr, "Time spent on parsing: %7.2f ms.\n", elapsed*1e3);
218 else
219 fprintf(stderr, "Time spent on parsing: %6.3f s.\n", elapsed);
220 }
221 }
222
223 /* Success if we have reached here. */
224 ret = 0;
225
226 out:
227 membuf_fini(&buf_in);
228 membuf_fini(&buf_out);
229
230 return ret;
231 }
232
233
234 static const CMDLINE_OPTION cmdline_options[] = {
235 { 'o', "output", 'o', CMDLINE_OPTFLAG_REQUIREDARG },
236 { 'f', "full-html", 'f', 0 },
237 { 'x', "xhtml", 'x', 0 },
238 { 's', "stat", 's', 0 },
239 { 'h', "help", 'h', 0 },
240 { 'v', "version", 'v', 0 },
241
242 { 0, "html-title", '1', CMDLINE_OPTFLAG_REQUIREDARG },
243 { 0, "html-css", '2', CMDLINE_OPTFLAG_REQUIREDARG },
244
245 { 0, "commonmark", 'c', 0 },
246 { 0, "github", 'g', 0 },
247
248 { 0, "fcollapse-whitespace", 'W', 0 },
249 { 0, "flatex-math", 'L', 0 },
250 { 0, "fpermissive-atx-headers", 'A', 0 },
251 { 0, "fpermissive-autolinks", 'V', 0 },
252 { 0, "fhard-soft-breaks", 'B', 0 },
253 { 0, "fpermissive-email-autolinks", '@', 0 },
254 { 0, "fpermissive-url-autolinks", 'U', 0 },
255 { 0, "fpermissive-www-autolinks", '.', 0 },
256 { 0, "fstrikethrough", 'S', 0 },
257 { 0, "ftables", 'T', 0 },
258 { 0, "ftasklists", 'X', 0 },
259 { 0, "funderline", '_', 0 },
260 { 0, "fverbatim-entities", 'E', 0 },
261 { 0, "fwiki-links", 'K', 0 },
262
263 { 0, "fno-html-blocks", 'F', 0 },
264 { 0, "fno-html-spans", 'G', 0 },
265 { 0, "fno-html", 'H', 0 },
266 { 0, "fno-indented-code", 'I', 0 },
267
268 /* Undocumented option for replaying test cases from fuzzers. */
269 { 0, "replay-fuzz", 'r', 0 },
270
271 { 0, NULL, 0, 0 }
272 };
273
274 static void
275 usage(void)
276 {
277 printf(
278 "Usage: md2html [OPTION]... [FILE]\n"
279 "Convert input FILE (or standard input) in Markdown format to HTML.\n"
280 "\n"
281 "General options:\n"
282 " -o --output=FILE Output file (default is standard output)\n"
283 " -f, --full-html Generate full HTML document, including header\n"
284 " -x, --xhtml Generate XHTML instead of HTML\n"
285 " -s, --stat Measure time of input parsing\n"
286 " -h, --help Display this help and exit\n"
287 " -v, --version Display version and exit\n"
288 "\n"
289 "Markdown dialect options:\n"
290 "(note these are equivalent to some combinations of the flags below)\n"
291 " --commonmark CommonMark (this is default)\n"
292 " --github Github Flavored Markdown\n"
293 "\n"
294 "Markdown extension options:\n"
295 " --fcollapse-whitespace\n"
296 " Collapse non-trivial whitespace\n"
297 " --flatex-math Enable LaTeX style mathematics spans\n"
298 " --fpermissive-atx-headers\n"
299 " Allow ATX headers without delimiting space\n"
300 " --fpermissive-url-autolinks\n"
301 " Allow URL autolinks without '<', '>'\n"
302 " --fpermissive-www-autolinks\n"
303 " Allow WWW autolinks without any scheme (e.g. 'www.example.com')\n"
304 " --fpermissive-email-autolinks \n"
305 " Allow e-mail autolinks without '<', '>' and 'mailto:'\n"
306 " --fpermissive-autolinks\n"
307 " Same as --fpermissive-url-autolinks --fpermissive-www-autolinks\n"
308 " --fpermissive-email-autolinks\n"
309 " --fhard-soft-breaks\n"
310 " Force all soft breaks to act as hard breaks\n"
311 " --fstrikethrough Enable strike-through spans\n"
312 " --ftables Enable tables\n"
313 " --ftasklists Enable task lists\n"
314 " --funderline Enable underline spans\n"
315 " --fwiki-links Enable wiki links\n"
316 "\n"
317 "Markdown suppression options:\n"
318 " --fno-html-blocks\n"
319 " Disable raw HTML blocks\n"
320 " --fno-html-spans\n"
321 " Disable raw HTML spans\n"
322 " --fno-html Same as --fno-html-blocks --fno-html-spans\n"
323 " --fno-indented-code\n"
324 " Disable indented code blocks\n"
325 "\n"
326 "HTML generator options:\n"
327 " --fverbatim-entities\n"
328 " Do not translate entities\n"
329 " --html-title=TITLE Sets the title of the document\n"
330 " --html-css=URL In full HTML or XHTML mode add a css link\n"
331 "\n"
332 );
333 }
334
335 static void
336 version(void)
337 {
338 printf("%d.%d.%d\n", MD_VERSION_MAJOR, MD_VERSION_MINOR, MD_VERSION_RELEASE);
339 }
340
341 static const char* input_path = NULL;
342 static const char* output_path = NULL;
343
344 static int
345 cmdline_callback(int opt, char const* value, void* data)
346 {
347 (void) data; /* unused parameter */
348
349 switch(opt) {
350 case 0:
351 if(input_path) {
352 fprintf(stderr, "Too many arguments. Only one input file can be specified.\n");
353 fprintf(stderr, "Use --help for more info.\n");
354 exit(1);
355 }
356 input_path = value;
357 break;
358
359 case 'o': output_path = value; break;
360 case 'f': want_fullhtml = 1; break;
361 case 'x': want_xhtml = 1; renderer_flags |= MD_HTML_FLAG_XHTML; break;
362 case 's': want_stat = 1; break;
363 case 'r': want_replay_fuzz = 1; break;
364 case 'h': usage(); exit(0); break;
365 case 'v': version(); exit(0); break;
366
367 case '1': html_title = value; break;
368 case '2': css_path = value; break;
369
370 case 'c': parser_flags |= MD_DIALECT_COMMONMARK; break;
371 case 'g': parser_flags |= MD_DIALECT_GITHUB; break;
372
373 case 'E': renderer_flags |= MD_HTML_FLAG_VERBATIM_ENTITIES; break;
374 case 'A': parser_flags |= MD_FLAG_PERMISSIVEATXHEADERS; break;
375 case 'I': parser_flags |= MD_FLAG_NOINDENTEDCODEBLOCKS; break;
376 case 'F': parser_flags |= MD_FLAG_NOHTMLBLOCKS; break;
377 case 'G': parser_flags |= MD_FLAG_NOHTMLSPANS; break;
378 case 'H': parser_flags |= MD_FLAG_NOHTML; break;
379 case 'W': parser_flags |= MD_FLAG_COLLAPSEWHITESPACE; break;
380 case 'U': parser_flags |= MD_FLAG_PERMISSIVEURLAUTOLINKS; break;
381 case '.': parser_flags |= MD_FLAG_PERMISSIVEWWWAUTOLINKS; break;
382 case '@': parser_flags |= MD_FLAG_PERMISSIVEEMAILAUTOLINKS; break;
383 case 'V': parser_flags |= MD_FLAG_PERMISSIVEAUTOLINKS; break;
384 case 'T': parser_flags |= MD_FLAG_TABLES; break;
385 case 'S': parser_flags |= MD_FLAG_STRIKETHROUGH; break;
386 case 'L': parser_flags |= MD_FLAG_LATEXMATHSPANS; break;
387 case 'K': parser_flags |= MD_FLAG_WIKILINKS; break;
388 case 'X': parser_flags |= MD_FLAG_TASKLISTS; break;
389 case '_': parser_flags |= MD_FLAG_UNDERLINE; break;
390 case 'B': parser_flags |= MD_FLAG_HARD_SOFT_BREAKS; break;
391
392 default:
393 fprintf(stderr, "Illegal option: %s\n", value);
394 fprintf(stderr, "Use --help for more info.\n");
395 exit(1);
396 break;
397 }
398
399 return 0;
400 }
401
402 int
403 main(int argc, char** argv)
404 {
405 FILE* in = stdin;
406 FILE* out = stdout;
407 int ret = 0;
408
409 if(cmdline_read(cmdline_options, argc, argv, cmdline_callback, NULL) != 0) {
410 usage();
411 exit(1);
412 }
413
414 if(input_path != NULL && strcmp(input_path, "-") != 0) {
415 in = fopen(input_path, "rb");
416 if(in == NULL) {
417 fprintf(stderr, "Cannot open %s.\n", input_path);
418 exit(1);
419 }
420 }
421 if(output_path != NULL && strcmp(output_path, "-") != 0) {
422 out = fopen(output_path, "wt");
423 if(out == NULL) {
424 fprintf(stderr, "Cannot open %s.\n", output_path);
425 exit(1);
426 }
427 }
428
429 ret = process_file((input_path != NULL) ? input_path : "<stdin>", in, out);
430 if(in != stdin)
431 fclose(in);
432 if(out != stdout)
433 fclose(out);
434
435 return ret;
436 }