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) 2016 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_EMPTY)) {
163 // remove tilde code markers
168 // if slide ! has line
169 if(!slide->line || !line) {
179 line = next_line(line);
191 line->offset = next_nonblank(text, 0);
193 // expand character entities if enabled
194 if(line->text->value &&
196 !CHECK_BIT(line->bits, IS_CODE))
197 expand_character_entities(line);
199 // adjust line length dynamicaly - excluding markup
200 if(line->text->value)
201 adjust_line_length(line);
204 text = cstring_init();
207 } else if(c == L'\t') {
209 // expand tab to spaces
210 for (i = 0; i < EXPAND_TABS; i++) {
211 (text->expand)(text, L' ');
214 } else if(c == L'\\') {
217 (text->expand)(text, c);
219 // if !IS_CODE add next char to line
220 // and do not increase line count
221 if(next_nonblank(text, 0) < CODE_INDENT) {
224 (text->expand)(text, c);
227 } else if(iswprint(c) || iswspace(c)) {
230 (text->expand)(text, c);
233 (text->delete)(text);
239 line = deck->slide->line;
240 if(line && line->text->size > 0 && line->text->value[0] == L'%') {
242 // assign header to deck
245 // find first non-header line
246 while(line && line->text->size > 0 && line->text->value[0] == L'%') {
251 // only split header if any non-header line is found
255 line->prev->next = NULL;
258 // remove header lines from slide
259 deck->slide->line = line;
263 deck->slide->lines -= hc;
266 // remove header from deck
275 // ignore mdpress format attributes
278 !CHECK_BIT(line->bits, IS_EMPTY) &&
279 line->text->value[line->offset] == L'=' &&
280 line->text->value[line->offset + 1] == L' ') {
282 // remove line from linked list
283 slide->line = line->next;
284 line->next->prev = NULL;
286 // maintain loop condition
294 (tmp->text->delete)(tmp->text);
299 // combine underlined H1/H2 in single line
300 if((CHECK_BIT(line->bits, IS_H1) ||
301 CHECK_BIT(line->bits, IS_H2)) &&
302 CHECK_BIT(line->bits, IS_EMPTY) &&
304 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
307 // remove line from linked list
308 line->prev->next = line->next;
310 line->next->prev = line->prev;
312 // set bits on previous line
313 if(CHECK_BIT(line->bits, IS_H1)) {
314 SET_BIT(line->prev->bits, IS_H1);
316 SET_BIT(line->prev->bits, IS_H2);
322 // maintain loop condition
327 (tmp->text->delete)(tmp->text);
330 // pass enclosing flag IS_UNORDERED_LIST_3
331 // to nested levels for unordered lists
332 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
334 line_t *list_last_level_3 = line;
337 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
338 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
339 list_last_level_3 = tmp;
344 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
345 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
348 // pass enclosing flag IS_UNORDERED_LIST_2
349 // to nested levels for unordered lists
350 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
352 line_t *list_last_level_2 = line;
355 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
356 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
357 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
358 list_last_level_2 = tmp;
363 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
364 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
367 // pass enclosing flag IS_UNORDERED_LIST_1
368 // to nested levels for unordered lists
369 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
371 line_t *list_last_level_1 = line;
374 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
375 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
376 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
377 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
378 list_last_level_1 = tmp;
383 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
384 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
396 int markdown_analyse(cstring_t *text, int prev) {
398 // static variables can not be redeclaired, but changed outside of a declaration
399 // the program remembers their value on every function calls
400 static int unordered_list_level = 0;
401 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
402 static int num_tilde_characters = 0;
404 int i = 0; // increment
405 int bits = 0; // markdown bits
406 int offset = 0; // text offset
407 int eol = 0; // end of line
409 int equals = 0, hashes = 0,
410 stars = 0, minus = 0,
411 spaces = 0, other = 0; // special character counts
413 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
415 // return IS_EMPTY on null pointers
416 if(!text || !text->value) {
417 SET_BIT(bits, IS_EMPTY);
419 // continue fenced code blocks across empty lines
420 if(num_tilde_characters > 0)
421 SET_BIT(bits, IS_CODE);
426 // count leading spaces
427 offset = next_nonblank(text, 0);
430 if (wcsncmp(text->value, L"~~~", 3) == 0) {
431 int tildes_in_line = next_nontilde(text, 0);
432 if (tildes_in_line >= num_tilde_characters) {
433 if (num_tilde_characters > 0) {
434 num_tilde_characters = 0;
436 num_tilde_characters = tildes_in_line;
438 SET_BIT(bits, IS_EMPTY);
439 SET_BIT(bits, IS_TILDE_CODE);
444 if (num_tilde_characters > 0) {
445 SET_BIT(bits, IS_CODE);
446 SET_BIT(bits, IS_TILDE_CODE);
451 if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
452 (!wcsncmp(&text->value[offset], L"<br>", 4) ||
453 !wcsncmp(&text->value[offset], L"<BR>", 4) ||
454 !wcsncmp(&text->value[offset], L"^", 1))) {
455 SET_BIT(bits, IS_STOP);
459 // strip trailing spaces
460 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
462 // IS_UNORDERED_LIST_#
463 if(text->size >= offset + 2 &&
464 (text->value[offset] == L'*' || text->value[offset] == L'-') &&
465 iswspace(text->value[offset + 1])) {
467 // if different from last lines offset
468 if(offset != unordered_list_offset) {
470 // test if offset matches a lower indent level
471 for(i = unordered_list_level; i >= 0; i--) {
472 if(unordered_list_level_offset[i] == offset) {
473 unordered_list_level = i;
477 // if offset doesn't match any previously stored indent level
478 if(i != unordered_list_level) {
479 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
480 // memorize the offset as next bigger indent level
481 unordered_list_level_offset[unordered_list_level] = offset;
485 // if no previous indent level matches, this must be the first line of the list
486 if(unordered_list_level == 0) {
487 unordered_list_level = 1;
488 unordered_list_level_offset[1] = offset;
491 switch(unordered_list_level) {
492 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
493 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
494 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
499 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
500 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
501 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
503 // continue list if indent level is still the same as in previous line
504 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
505 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
506 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
507 offset >= unordered_list_offset) {
509 switch(unordered_list_level) {
510 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
511 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
512 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
516 // this line extends the previous list item
517 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
519 // or reset indent level
521 unordered_list_level = 0;
525 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
526 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
527 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
530 if(offset >= CODE_INDENT &&
531 (CHECK_BIT(prev, IS_EMPTY) ||
532 CHECK_BIT(prev, IS_CODE) ||
533 CHECK_BIT(prev, IS_STOP))) {
534 SET_BIT(bits, IS_CODE);
539 if(text->value[offset] == L'>') {
540 SET_BIT(bits, IS_QUOTE);
544 if(text->size >= offset + 3 &&
545 text->value[offset] == L'-' &&
546 text->value[offset + 1] == L'>' &&
547 iswspace(text->value[offset + 2])) {
548 SET_BIT(bits, IS_CENTER);
551 (text->strip)(text, offset, 3);
554 if(text->size >= offset + 3 &&
555 text->value[eol - 1] == L'-' &&
556 text->value[eol - 2] == L'<' &&
557 iswspace(text->value[eol - 3])) {
560 (text->strip)(text, eol - 3, 3);
562 // adjust end of line
563 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
568 for(i = offset; i < eol; i++) {
570 if(iswspace(text->value[i])) {
574 switch(text->value[i]) {
575 case L'=': equals++; break;
576 case L'#': hashes++; break;
577 case L'*': stars++; break;
578 case L'-': minus++; break;
579 case L'\\': other++; i++; break;
580 default: other++; break;
587 hashes + stars + minus + spaces + other == 0) {
588 SET_BIT(bits, IS_H1);
590 if(text->value[offset] == L'#' &&
591 iswspace(text->value[offset+1])) {
592 SET_BIT(bits, IS_H1);
593 SET_BIT(bits, IS_H1_ATX);
598 equals + hashes + stars + spaces + other == 0) {
599 SET_BIT(bits, IS_H2);
601 if(text->value[offset] == L'#' &&
602 text->value[offset+1] == L'#' &&
603 iswspace(text->value[offset+2])) {
604 SET_BIT(bits, IS_H2);
605 SET_BIT(bits, IS_H2_ATX);
609 if((minus >= 3 && equals + hashes + stars + other == 0) ||
610 (stars >= 3 && equals + hashes + minus + other == 0)) {
612 SET_BIT(bits, IS_HR);
617 SET_BIT(bits, IS_EMPTY);
625 void markdown_debug(deck_t *deck, int debug) {
627 int sc = 0; // slide count
628 int lc = 0; // line count
634 fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
636 } else if(debug > 1) {
638 // print header to STDERR
640 header = deck->header;
642 header->length > 0 &&
643 header->text->value[0] == L'%') {
645 // skip descriptor word (e.g. %title:)
646 offset = next_blank(header->text, 0) + 1;
648 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
649 header = header->next;
654 slide_t *slide = deck->slide;
657 // print slide/line count to STDERR
662 fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines);
664 } else if(debug > 1) {
666 // also print bits and line length
667 fwprintf(stderr, L" slide %i:\n", sc);
672 fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
681 void expand_character_entities(line_t *line)
684 wchar_t *prev, *curr;
687 curr = &line->text->value[0];
689 // for each char in line
690 for(prev = NULL; *curr; prev = curr++) {
691 if (*curr == L'&' && (prev == NULL || *prev != L'\\')) {
695 if (ampersand == NULL) {
699 if (prev == ampersand)
703 if (iswalpha(*curr) || iswxdigit(*curr)) {
709 if (ampersand + 1 >= curr || ampersand + 16 < curr) // what is a good limit?
711 if (ampersand[1] == L'#') { // &#nnnn; or &#xhhhh;
712 if (ampersand + 2 >= curr)
714 if (ampersand[2] != L'x') { // &#nnnn;
715 cnt = wcsspn(&ersand[2], L"0123456789");
716 if (ampersand + 2 + cnt != curr)
718 ucs = wcstoul(&ersand[2], NULL, 10);
720 if (ampersand + 3 >= curr)
722 cnt = wcsspn(&ersand[3], L"0123456789abcdefABCDEF");
723 if (ampersand + 3 + cnt != curr)
725 ucs = wcstoul(&ersand[3], NULL, 16);
728 for (cnt = 0; cnt < sizeof(named_character_entities)/sizeof(named_character_entities[0]); ++cnt) {
729 if (wcsncmp(named_character_entities[cnt].name, &ersand[1], curr - ampersand - 1))
731 ucs = named_character_entities[cnt].ucs;
738 cstring_strip(line->text, ampersand + 1 - &line->text->value[0], curr - ampersand);
747 void adjust_line_length(line_t *line) {
749 const static wchar_t *special = L"\\*_`"; // list of interpreted chars
750 const wchar_t *c = &line->text->value[0];
751 cstack_t *stack = cstack_init();
753 // for each char in line
755 // if char is in special char list
756 if(wcschr(special, *c)) {
758 // closing special char (or second backslash)
759 if((stack->top)(stack, *c)) {
763 // treat special as regular char
764 } else if((stack->top)(stack, L'\\')) {
768 // opening special char
770 (stack->push)(stack, *c);
774 // remove backslash from stack
775 if((stack->top)(stack, L'\\'))
781 if(CHECK_BIT(line->bits, IS_H1_ATX))
783 if(CHECK_BIT(line->bits, IS_H2_ATX))
788 (stack->delete)(stack);
791 int next_nonblank(cstring_t *text, int i) {
792 while ((i < text->size) && iswspace((text->value)[i]))
798 int prev_blank(cstring_t *text, int i) {
799 while ((i > 0) && !iswspace((text->value)[i]))
805 int next_blank(cstring_t *text, int i) {
806 while ((i < text->size) && !iswspace((text->value)[i]))
812 int next_word(cstring_t *text, int i) {
813 return next_nonblank(text, next_blank(text, i));
816 int next_nontilde(cstring_t *text, int i) {
817 while ((i < text->size) && text->value[i] == L'~')