2 * Functions necessary to parse a file and transform its content into
3 * a deck of slides containing lines. All based on markdown formating
5 * Copyright (C) 2018 Michael Goehler
7 * This file is part of mdp.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 // char entry translation table
35 static struct named_character_entity {
38 } named_character_entities[] = {
39 { L'\x0022', L"quot" },
40 { L'\x0026', L"amp" },
41 { L'\x0027', L"apos" },
44 { L'\x00A2', L"cent" },
45 { L'\x00A3', L"pound" },
46 { L'\x00A5', L"yen" },
47 { L'\x00A7', L"sect" },
48 { L'\x00A9', L"copy" },
49 { L'\x00AA', L"laquo" },
50 { L'\x00AE', L"reg" },
51 { L'\x00B0', L"deg" },
52 { L'\x00B1', L"plusmn" },
53 { L'\x00B2', L"sup2" },
54 { L'\x00B3', L"sup3" },
55 { L'\x00B6', L"para" },
56 { L'\x00B9', L"sup1" },
57 { L'\x00BB', L"raquo" },
58 { L'\x00BC', L"frac14" },
59 { L'\x00BD', L"frac12" },
60 { L'\x00BE', L"frac34" },
61 { L'\x00D7', L"times" },
62 { L'\x00F7', L"divide" },
63 { L'\x2018', L"lsquo" },
64 { L'\x2019', L"rsquo" },
65 { L'\x201C', L"ldquo" },
66 { L'\x201D', L"rdquo" },
67 { L'\x2020', L"dagger" },
68 { L'\x2021', L"Dagger" },
69 { L'\x2022', L"bull" },
70 { L'\x2026', L"hellip" },
71 { L'\x2030', L"permil" },
72 { L'\x2032', L"prime" },
73 { L'\x2033', L"Prime" },
74 { L'\x2039', L"lsaquo" },
75 { L'\x203A', L"rsaquo" },
76 { L'\x20AC', L"euro" },
77 { L'\x2122', L"trade" },
78 { L'\x2190', L"larr" },
79 { L'\x2191', L"uarr" },
80 { L'\x2192', L"rarr" },
81 { L'\x2193', L"darr" },
82 { L'\x2194', L"harr" },
83 { L'\x21B5', L"crarr" },
84 { L'\x21D0', L"lArr" },
85 { L'\x21D1', L"uArr" },
86 { L'\x21D2', L"rArr" },
87 { L'\x21D3', L"dArr" },
88 { L'\x21D4', L"hArr" },
89 { L'\x221E', L"infin" },
90 { L'\x2261', L"equiv" },
91 { L'\x2308', L"lceil" },
92 { L'\x2309', L"rceil" },
93 { L'\x230A', L"lfloor" },
94 { L'\x230B', L"rfloor" },
95 { L'\x25CA', L"loz" },
96 { L'\x2660', L"spades" },
97 { L'\x2663', L"clubs" },
98 { L'\x2665', L"hearts" },
99 { L'\x2666', L"diams" },
103 deck_t *markdown_load(FILE *input, int noexpand) {
105 wchar_t c = L'\0'; // char
106 int i = 0; // increment
107 int hc = 0; // header count
108 int lc = 0; // line count
109 int sc = 1; // slide count
110 int bits = 0; // markdown bits
111 int prev = 0; // markdown bits of previous line
113 deck_t *deck = new_deck();
114 slide_t *slide = deck->slide;
117 cstring_t *text = cstring_init();
119 // initialize bits as empty line
120 SET_BIT(bits, IS_EMPTY);
122 while ((c = fgetwc(input)) != WEOF) {
124 fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
132 bits = markdown_analyse(text, prev);
134 // if first line in file is markdown hr
135 if(!line && CHECK_BIT(bits, IS_HR)) {
140 } else if(line && CHECK_BIT(bits, IS_STOP)) {
142 // set stop bit on last line
143 SET_BIT(line->bits, IS_STOP);
148 // if text is markdown hr
149 } else if(CHECK_BIT(bits, IS_HR) &&
150 CHECK_BIT(line->bits, IS_EMPTY)) {
158 slide = next_slide(slide);
161 } else if((CHECK_BIT(bits, IS_TILDE_CODE) ||
162 CHECK_BIT(bits, IS_GFM_CODE)) &&
163 CHECK_BIT(bits, IS_EMPTY)) {
164 // remove tilde code markers
169 // if slide ! has line
170 if(!slide->line || !line) {
180 line = next_line(line);
192 line->offset = next_nonblank(text, 0);
194 // expand character entities if enabled
195 if(line->text->value &&
197 !CHECK_BIT(line->bits, IS_CODE))
198 expand_character_entities(line);
200 // adjust line length dynamicaly - excluding markup
201 if(line->text->value)
202 adjust_line_length(line);
205 text = cstring_init();
208 } else if(c == L'\t') {
210 // expand tab to spaces
211 for (i = 0; i < EXPAND_TABS; i++) {
212 (text->expand)(text, L' ');
215 } else if(c == L'\\') {
218 (text->expand)(text, c);
220 // if !IS_CODE add next char to line
221 // and do not increase line count
222 if(next_nonblank(text, 0) < CODE_INDENT) {
225 (text->expand)(text, c);
228 } else if(iswprint(c) || iswspace(c)) {
231 (text->expand)(text, c);
234 (text->delete)(text);
240 line = deck->slide->line;
241 if(line && line->text->size > 0 && line->text->value[0] == L'%') {
243 // assign header to deck
246 // find first non-header line
247 while(line && line->text->size > 0 && line->text->value[0] == L'%') {
252 // only split header if any non-header line is found
256 line->prev->next = NULL;
259 // remove header lines from slide
260 deck->slide->line = line;
264 deck->slide->lines -= hc;
267 // remove header from deck
276 // ignore mdpress format attributes
279 !CHECK_BIT(line->bits, IS_EMPTY) &&
280 line->text->value[line->offset] == L'=' &&
281 line->text->value[line->offset + 1] == L' ') {
283 // remove line from linked list
284 slide->line = line->next;
285 line->next->prev = NULL;
287 // maintain loop condition
295 (tmp->text->delete)(tmp->text);
300 // combine underlined H1/H2 in single line
301 if((CHECK_BIT(line->bits, IS_H1) ||
302 CHECK_BIT(line->bits, IS_H2)) &&
303 CHECK_BIT(line->bits, IS_EMPTY) &&
305 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
308 // remove line from linked list
309 line->prev->next = line->next;
311 line->next->prev = line->prev;
313 // set bits on previous line
314 if(CHECK_BIT(line->bits, IS_H1)) {
315 SET_BIT(line->prev->bits, IS_H1);
317 SET_BIT(line->prev->bits, IS_H2);
323 // maintain loop condition
328 (tmp->text->delete)(tmp->text);
331 // pass enclosing flag IS_UNORDERED_LIST_3
332 // to nested levels for unordered lists
333 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
335 line_t *list_last_level_3 = line;
338 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
339 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
340 list_last_level_3 = tmp;
345 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
346 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
349 // pass enclosing flag IS_UNORDERED_LIST_2
350 // to nested levels for unordered lists
351 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
353 line_t *list_last_level_2 = line;
356 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
357 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
358 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
359 list_last_level_2 = tmp;
364 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
365 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
368 // pass enclosing flag IS_UNORDERED_LIST_1
369 // to nested levels for unordered lists
370 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
372 line_t *list_last_level_1 = line;
375 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
376 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
377 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
378 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
379 list_last_level_1 = tmp;
384 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
385 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
397 int markdown_analyse(cstring_t *text, int prev) {
399 // static variables can not be redeclaired, but changed outside of a declaration
400 // the program remembers their value on every function calls
401 static int unordered_list_level = 0;
402 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
403 static int num_tilde_characters = 0;
404 static int num_backticks = 0;
406 int i = 0; // increment
407 int bits = 0; // markdown bits
408 int offset = 0; // text offset
409 int eol = 0; // end of line
411 int equals = 0, hashes = 0,
412 stars = 0, minus = 0,
413 spaces = 0, other = 0; // special character counts
415 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
417 // return IS_EMPTY on null pointers
418 if(!text || !text->value) {
419 SET_BIT(bits, IS_EMPTY);
421 // continue fenced code blocks across empty lines
422 if(num_tilde_characters > 0)
423 SET_BIT(bits, IS_CODE);
428 // count leading spaces
429 offset = next_nonblank(text, 0);
432 if (wcsncmp(text->value, L"~~~", 3) == 0) {
433 int tildes_in_line = next_nontilde(text, 0);
434 if (tildes_in_line >= num_tilde_characters) {
435 if (num_tilde_characters > 0) {
436 num_tilde_characters = 0;
438 num_tilde_characters = tildes_in_line;
440 SET_BIT(bits, IS_EMPTY);
441 SET_BIT(bits, IS_TILDE_CODE);
446 if (num_tilde_characters > 0) {
447 SET_BIT(bits, IS_CODE);
448 SET_BIT(bits, IS_TILDE_CODE);
453 if (wcsncmp(text->value, L"```", 3) == 0) {
454 int backticks_in_line = next_nonbacktick(text, 0);
455 if (backticks_in_line >= num_backticks) {
456 if (num_backticks > 0) {
459 num_backticks = backticks_in_line;
461 SET_BIT(bits, IS_EMPTY);
462 SET_BIT(bits, IS_GFM_CODE);
467 if (num_backticks > 0) {
468 SET_BIT(bits, IS_CODE);
469 SET_BIT(bits, IS_GFM_CODE);
474 if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
475 (!wcsncmp(&text->value[offset], L"<br>", 4) ||
476 !wcsncmp(&text->value[offset], L"<BR>", 4) ||
477 !wcsncmp(&text->value[offset], L"^", 1))) {
478 SET_BIT(bits, IS_STOP);
482 // strip trailing spaces
483 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
486 // IS_UNORDERED_LIST_#
487 if(text->size >= offset + 2 &&
488 (text->value[offset] == L'*' || text->value[offset] == L'-') &&
489 iswspace(text->value[offset + 1])) {
491 // if different from last lines offset
492 if(offset != unordered_list_offset) {
494 // test if offset matches a lower indent level
495 for(i = unordered_list_level; i >= 0; i--) {
496 if(unordered_list_level_offset[i] == offset) {
497 unordered_list_level = i;
501 // if offset doesn't match any previously stored indent level
502 if(i != unordered_list_level) {
503 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
504 // memorize the offset as next bigger indent level
505 unordered_list_level_offset[unordered_list_level] = offset;
509 // if no previous indent level matches, this must be the first line of the list
510 if(unordered_list_level == 0) {
511 unordered_list_level = 1;
512 unordered_list_level_offset[1] = offset;
515 switch(unordered_list_level) {
516 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
517 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
518 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
523 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
524 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
525 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
527 // continue list if indent level is still the same as in previous line
528 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
529 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
530 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
531 offset >= unordered_list_offset) {
533 switch(unordered_list_level) {
534 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
535 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
536 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
540 // this line extends the previous list item
541 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
543 // or reset indent level
545 unordered_list_level = 0;
549 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
550 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
551 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
554 if(offset >= CODE_INDENT &&
555 (CHECK_BIT(prev, IS_EMPTY) ||
556 CHECK_BIT(prev, IS_CODE) ||
557 CHECK_BIT(prev, IS_STOP))) {
558 SET_BIT(bits, IS_CODE);
563 if(text->value[offset] == L'>') {
564 SET_BIT(bits, IS_QUOTE);
568 if(text->size >= offset + 3 &&
569 text->value[offset] == L'-' &&
570 text->value[offset + 1] == L'>' &&
571 iswspace(text->value[offset + 2])) {
572 SET_BIT(bits, IS_CENTER);
575 (text->strip)(text, offset, 3);
578 if(text->size >= offset + 3 &&
579 text->value[eol - 1] == L'-' &&
580 text->value[eol - 2] == L'<' &&
581 iswspace(text->value[eol - 3])) {
584 (text->strip)(text, eol - 3, 3);
586 // adjust end of line
587 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
592 for(i = offset; i < eol; i++) {
594 if(iswspace(text->value[i])) {
598 switch(text->value[i]) {
599 case L'=': equals++; break;
600 case L'#': hashes++; break;
601 case L'*': stars++; break;
602 case L'-': minus++; break;
603 case L'\\': other++; i++; break;
604 default: other++; break;
611 hashes + stars + minus + spaces + other == 0) {
612 SET_BIT(bits, IS_H1);
614 if(text->value[offset] == L'#' &&
615 iswspace(text->value[offset+1])) {
616 SET_BIT(bits, IS_H1);
617 SET_BIT(bits, IS_H1_ATX);
622 equals + hashes + stars + spaces + other == 0) {
623 SET_BIT(bits, IS_H2);
625 if(text->value[offset] == L'#' &&
626 text->value[offset+1] == L'#' &&
627 iswspace(text->value[offset+2])) {
628 SET_BIT(bits, IS_H2);
629 SET_BIT(bits, IS_H2_ATX);
633 if((minus >= 3 && equals + hashes + stars + other == 0) ||
634 (stars >= 3 && equals + hashes + minus + other == 0)) {
636 SET_BIT(bits, IS_HR);
641 SET_BIT(bits, IS_EMPTY);
649 void markdown_debug(deck_t *deck, int debug) {
651 int sc = 0; // slide count
652 int lc = 0; // line count
658 fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
660 } else if(debug > 1) {
662 // print header to STDERR
664 header = deck->header;
666 header->length > 0 &&
667 header->text->value[0] == L'%') {
669 // skip descriptor word (e.g. %title:)
670 offset = next_blank(header->text, 0) + 1;
672 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
673 header = header->next;
678 slide_t *slide = deck->slide;
681 // print slide/line count to STDERR
686 fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines);
688 } else if(debug > 1) {
690 // also print bits and line length
691 fwprintf(stderr, L" slide %i:\n", sc);
696 fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
705 void expand_character_entities(line_t *line)
708 wchar_t *prev, *curr;
711 curr = &line->text->value[0];
713 // for each char in line
714 for(prev = NULL; *curr; prev = curr++) {
715 if (*curr == L'&' && (prev == NULL || *prev != L'\\')) {
719 if (ampersand == NULL) {
723 if (prev == ampersand)
728 if (iswalpha(*curr) || iswxdigit(*curr)) {
734 if (ampersand + 1 >= curr || ampersand + 16 < curr) { // what is a good limit?
738 if (ampersand[1] == L'#') { // &#nnnn; or &#xhhhh;
739 if (ampersand + 2 >= curr) {
743 if (ampersand[2] != L'x') { // &#nnnn;
744 cnt = wcsspn(&ersand[2], L"0123456789");
745 if (ampersand + 2 + cnt != curr) {
749 ucs = wcstoul(&ersand[2], NULL, 10);
751 if (ampersand + 3 >= curr) {
755 cnt = wcsspn(&ersand[3], L"0123456789abcdefABCDEF");
756 if (ampersand + 3 + cnt != curr) {
760 ucs = wcstoul(&ersand[3], NULL, 16);
763 for (cnt = 0; cnt < sizeof(named_character_entities)/sizeof(named_character_entities[0]); ++cnt) {
764 if (wcsncmp(named_character_entities[cnt].name, &ersand[1], curr - ampersand - 1))
766 ucs = named_character_entities[cnt].ucs;
775 cstring_strip(line->text, ampersand + 1 - &line->text->value[0], curr - ampersand);
782 void adjust_line_length(line_t *line) {
784 const static wchar_t *special = L"\\*_`"; // list of interpreted chars
785 const wchar_t *c = &line->text->value[0];
786 cstack_t *stack = cstack_init();
788 // for each char in line
790 // if char is in special char list
791 if(wcschr(special, *c)) {
793 // closing special char (or second backslash)
794 if((stack->top)(stack, *c)) {
798 // treat special as regular char
799 } else if((stack->top)(stack, L'\\')) {
803 // opening special char
805 (stack->push)(stack, *c);
809 // remove backslash from stack
810 if((stack->top)(stack, L'\\'))
816 if(CHECK_BIT(line->bits, IS_H1_ATX))
818 if(CHECK_BIT(line->bits, IS_H2_ATX))
823 (stack->delete)(stack);
826 int next_nonblank(cstring_t *text, int i) {
827 while ((i < text->size) && iswspace((text->value)[i]))
833 int prev_blank(cstring_t *text, int i) {
834 while ((i > 0) && !iswspace((text->value)[i]))
840 int next_blank(cstring_t *text, int i) {
841 while ((i < text->size) && !iswspace((text->value)[i]))
847 int next_word(cstring_t *text, int i) {
848 return next_nonblank(text, next_blank(text, i));
851 int next_nontilde(cstring_t *text, int i) {
852 while ((i < text->size) && text->value[i] == L'~')
858 int next_nonbacktick(cstring_t *text, int i) {
859 while ((i < text->size) && text->value[i] == L'`')