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 deck_t *markdown_load(FILE *input) {
36 wchar_t c = L'\0'; // char
37 int i = 0; // increment
38 int hc = 0; // header count
39 int lc = 0; // line count
40 int sc = 1; // slide count
41 int bits = 0; // markdown bits
42 int prev = 0; // markdown bits of previous line
44 deck_t *deck = new_deck();
45 slide_t *slide = deck->slide;
48 cstring_t *text = cstring_init();
50 // initialize bits as empty line
51 SET_BIT(bits, IS_EMPTY);
53 while ((c = fgetwc(input)) != WEOF) {
55 fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
63 bits = markdown_analyse(text, prev);
65 // if first line in file is markdown hr
66 if(!line && CHECK_BIT(bits, IS_HR)) {
71 } else if(line && CHECK_BIT(bits, IS_STOP)) {
73 // set stop bit on last line
74 SET_BIT(line->bits, IS_STOP);
79 // if text is markdown hr
80 } else if(CHECK_BIT(bits, IS_HR) &&
81 CHECK_BIT(line->bits, IS_EMPTY)) {
89 slide = next_slide(slide);
92 } else if(CHECK_BIT(bits, IS_TILDE_CODE) &&
93 CHECK_BIT(bits, IS_EMPTY)) {
94 // remove tilde code markers
99 // if slide ! has line
100 if(!slide->line || !line) {
110 line = next_line(line);
122 line->offset = next_nonblank(text, 0);
124 // expand character entities if enabled
125 if(line->text->value)
126 expand_character_entities(line);
128 // adjust line length dynamicaly - excluding markup
129 if(line->text->value)
130 adjust_line_length(line);
133 text = cstring_init();
136 } else if(c == L'\t') {
138 // expand tab to spaces
139 for (i = 0; i < EXPAND_TABS; i++) {
140 (text->expand)(text, L' ');
143 } else if(c == L'\\') {
146 (text->expand)(text, c);
148 // if !IS_CODE add next char to line
149 // and do not increase line count
150 if(next_nonblank(text, 0) < CODE_INDENT) {
153 (text->expand)(text, c);
156 } else if(iswprint(c) || iswspace(c)) {
159 (text->expand)(text, c);
162 (text->delete)(text);
168 line = deck->slide->line;
169 if(line && line->text->size > 0 && line->text->value[0] == L'%') {
171 // assign header to deck
174 // find first non-header line
175 while(line && line->text->size > 0 && line->text->value[0] == L'%') {
180 // only split header if any non-header line is found
184 line->prev->next = NULL;
187 // remove header lines from slide
188 deck->slide->line = line;
192 deck->slide->lines -= hc;
195 // remove header from deck
204 // ignore mdpress format attributes
207 !CHECK_BIT(line->bits, IS_EMPTY) &&
208 line->text->value[line->offset] == L'=' &&
209 line->text->value[line->offset + 1] == L' ') {
211 // remove line from linked list
212 slide->line = line->next;
213 line->next->prev = NULL;
215 // maintain loop condition
223 (tmp->text->delete)(tmp->text);
228 // combine underlined H1/H2 in single line
229 if((CHECK_BIT(line->bits, IS_H1) ||
230 CHECK_BIT(line->bits, IS_H2)) &&
231 CHECK_BIT(line->bits, IS_EMPTY) &&
233 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
236 // remove line from linked list
237 line->prev->next = line->next;
239 line->next->prev = line->prev;
241 // set bits on previous line
242 if(CHECK_BIT(line->bits, IS_H1)) {
243 SET_BIT(line->prev->bits, IS_H1);
245 SET_BIT(line->prev->bits, IS_H2);
251 // maintain loop condition
256 (tmp->text->delete)(tmp->text);
259 // pass enclosing flag IS_UNORDERED_LIST_3
260 // to nested levels for unordered lists
261 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
263 line_t *list_last_level_3 = line;
266 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
267 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
268 list_last_level_3 = tmp;
273 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
274 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
277 // pass enclosing flag IS_UNORDERED_LIST_2
278 // to nested levels for unordered lists
279 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
281 line_t *list_last_level_2 = line;
284 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
285 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
286 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
287 list_last_level_2 = tmp;
292 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
293 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
296 // pass enclosing flag IS_UNORDERED_LIST_1
297 // to nested levels for unordered lists
298 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
300 line_t *list_last_level_1 = line;
303 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
304 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
305 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
306 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
307 list_last_level_1 = tmp;
312 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
313 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
325 int markdown_analyse(cstring_t *text, int prev) {
327 // static variables can not be redeclaired, but changed outside of a declaration
328 // the program remembers their value on every function calls
329 static int unordered_list_level = 0;
330 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
331 static int num_tilde_characters = 0;
333 int i = 0; // increment
334 int bits = 0; // markdown bits
335 int offset = 0; // text offset
336 int eol = 0; // end of line
338 int equals = 0, hashes = 0,
339 stars = 0, minus = 0,
340 spaces = 0, other = 0; // special character counts
342 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
344 // return IS_EMPTY on null pointers
345 if(!text || !text->value) {
346 SET_BIT(bits, IS_EMPTY);
348 // continue fenced code blocks across empty lines
349 if(num_tilde_characters > 0)
350 SET_BIT(bits, IS_CODE);
355 // count leading spaces
356 offset = next_nonblank(text, 0);
359 if (wcsncmp(text->value, L"~~~", 3) == 0) {
360 int tildes_in_line = next_nontilde(text, 0);
361 if (tildes_in_line >= num_tilde_characters) {
362 if (num_tilde_characters > 0) {
363 num_tilde_characters = 0;
365 num_tilde_characters = tildes_in_line;
367 SET_BIT(bits, IS_EMPTY);
368 SET_BIT(bits, IS_TILDE_CODE);
373 if (num_tilde_characters > 0) {
374 SET_BIT(bits, IS_CODE);
375 SET_BIT(bits, IS_TILDE_CODE);
380 if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
381 (!wcsncmp(&text->value[offset], L"<br>", 4) ||
382 !wcsncmp(&text->value[offset], L"<BR>", 4) ||
383 !wcsncmp(&text->value[offset], L"^", 1))) {
384 SET_BIT(bits, IS_STOP);
388 // strip trailing spaces
389 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
391 // IS_UNORDERED_LIST_#
392 if(text->size >= offset + 2 &&
393 (text->value[offset] == L'*' || text->value[offset] == L'-') &&
394 iswspace(text->value[offset + 1])) {
396 // if different from last lines offset
397 if(offset != unordered_list_offset) {
399 // test if offset matches a lower indent level
400 for(i = unordered_list_level; i >= 0; i--) {
401 if(unordered_list_level_offset[i] == offset) {
402 unordered_list_level = i;
406 // if offset doesn't match any previously stored indent level
407 if(i != unordered_list_level) {
408 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
409 // memorize the offset as next bigger indent level
410 unordered_list_level_offset[unordered_list_level] = offset;
414 // if no previous indent level matches, this must be the first line of the list
415 if(unordered_list_level == 0) {
416 unordered_list_level = 1;
417 unordered_list_level_offset[1] = offset;
420 switch(unordered_list_level) {
421 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
422 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
423 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
428 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
429 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
430 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
432 // continue list if indent level is still the same as in previous line
433 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
434 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
435 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
436 offset >= unordered_list_offset) {
438 switch(unordered_list_level) {
439 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
440 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
441 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
445 // this line extends the previous list item
446 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
448 // or reset indent level
450 unordered_list_level = 0;
454 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
455 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
456 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
459 if(offset >= CODE_INDENT &&
460 (CHECK_BIT(prev, IS_EMPTY) ||
461 CHECK_BIT(prev, IS_CODE) ||
462 CHECK_BIT(prev, IS_STOP))) {
463 SET_BIT(bits, IS_CODE);
468 if(text->value[offset] == L'>') {
469 SET_BIT(bits, IS_QUOTE);
473 if(text->size >= offset + 3 &&
474 text->value[offset] == L'-' &&
475 text->value[offset + 1] == L'>' &&
476 iswspace(text->value[offset + 2])) {
477 SET_BIT(bits, IS_CENTER);
480 (text->strip)(text, offset, 3);
483 if(text->size >= offset + 3 &&
484 text->value[eol - 1] == L'-' &&
485 text->value[eol - 2] == L'<' &&
486 iswspace(text->value[eol - 3])) {
489 (text->strip)(text, eol - 3, 3);
491 // adjust end of line
492 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
497 for(i = offset; i < eol; i++) {
499 if(iswspace(text->value[i])) {
503 switch(text->value[i]) {
504 case L'=': equals++; break;
505 case L'#': hashes++; break;
506 case L'*': stars++; break;
507 case L'-': minus++; break;
508 case L'\\': other++; i++; break;
509 default: other++; break;
516 hashes + stars + minus + spaces + other == 0) {
517 SET_BIT(bits, IS_H1);
519 if(text->value[offset] == L'#' &&
520 iswspace(text->value[offset+1])) {
521 SET_BIT(bits, IS_H1);
522 SET_BIT(bits, IS_H1_ATX);
527 equals + hashes + stars + spaces + other == 0) {
528 SET_BIT(bits, IS_H2);
530 if(text->value[offset] == L'#' &&
531 text->value[offset+1] == L'#' &&
532 iswspace(text->value[offset+2])) {
533 SET_BIT(bits, IS_H2);
534 SET_BIT(bits, IS_H2_ATX);
538 if((minus >= 3 && equals + hashes + stars + other == 0) ||
539 (stars >= 3 && equals + hashes + minus + other == 0)) {
541 SET_BIT(bits, IS_HR);
546 SET_BIT(bits, IS_EMPTY);
554 void markdown_debug(deck_t *deck, int debug) {
556 int sc = 0; // slide count
557 int lc = 0; // line count
563 fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
565 } else if(debug > 1) {
567 // print header to STDERR
569 header = deck->header;
571 header->length > 0 &&
572 header->text->value[0] == L'%') {
574 // skip descriptor word (e.g. %title:)
575 offset = next_blank(header->text, 0) + 1;
577 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
578 header = header->next;
583 slide_t *slide = deck->slide;
586 // print slide/line count to STDERR
591 fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines);
593 } else if(debug > 1) {
595 // also print bits and line length
596 fwprintf(stderr, L" slide %i:\n", sc);
601 fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
610 static int enable_character_entities = 0;
611 static struct named_character_entity {
614 } named_character_entities[] = {
615 { L'\x0022', L"quot" },
616 { L'\x0026', L"amp" },
617 { L'\x0027', L"apos" },
618 { L'\x003C', L"lt" },
619 { L'\x003E', L"gt" },
620 { L'\x00A2', L"cent" },
621 { L'\x00A3', L"pound" },
622 { L'\x00A5', L"yen" },
623 { L'\x00A7', L"sect" },
624 { L'\x00A9', L"copy" },
625 { L'\x00AA', L"laquo" },
626 { L'\x00AE', L"reg" },
627 { L'\x00B0', L"deg" },
628 { L'\x00B1', L"plusmn" },
629 { L'\x00B2', L"sup2" },
630 { L'\x00B3', L"sup3" },
631 { L'\x00B6', L"para" },
632 { L'\x00B9', L"sup1" },
633 { L'\x00BB', L"raquo" },
634 { L'\x00BC', L"frac14" },
635 { L'\x00BD', L"frac12" },
636 { L'\x00BE', L"frac34" },
637 { L'\x00D7', L"times" },
638 { L'\x00F7', L"divide" },
639 { L'\x2018', L"lsquo" },
640 { L'\x2019', L"rsquo" },
641 { L'\x201C', L"ldquo" },
642 { L'\x201D', L"rdquo" },
643 { L'\x2020', L"dagger" },
644 { L'\x2021', L"Dagger" },
645 { L'\x2022', L"bull" },
646 { L'\x2026', L"hellip" },
647 { L'\x2030', L"permil" },
648 { L'\x2032', L"prime" },
649 { L'\x2033', L"Prime" },
650 { L'\x2039', L"lsaquo" },
651 { L'\x203A', L"rsaquo" },
652 { L'\x20AC', L"euro" },
653 { L'\x2122', L"trade" },
654 { L'\x2190', L"larr" },
655 { L'\x2191', L"uarr" },
656 { L'\x2192', L"rarr" },
657 { L'\x2193', L"darr" },
658 { L'\x2194', L"harr" },
659 { L'\x21B5', L"crarr" },
660 { L'\x21D0', L"lArr" },
661 { L'\x21D1', L"uArr" },
662 { L'\x21D2', L"rArr" },
663 { L'\x21D3', L"dArr" },
664 { L'\x21D4', L"hArr" },
665 { L'\x221E', L"infin" },
666 { L'\x2261', L"equiv" },
667 { L'\x2308', L"lceil" },
668 { L'\x2309', L"rceil" },
669 { L'\x230A', L"lfloor" },
670 { L'\x230B', L"rfloor" },
671 { L'\x25CA', L"loz" },
672 { L'\x2660', L"spades" },
673 { L'\x2663', L"clubs" },
674 { L'\x2665', L"hearts" },
675 { L'\x2666', L"diams" },
680 export MDP_ENABLE_CHARENT=1
682 void setup_character_entities(void)
684 char *str = getenv("MDP_ENABLE_CHARENT");
686 enable_character_entities = 0;
687 else if (str[0] == '\0')
688 enable_character_entities = 1;
690 enable_character_entities = atoi(str);
693 void expand_character_entities(line_t *line)
696 wchar_t *prev, *curr;
698 if (!enable_character_entities)
702 curr = &line->text->value[0];
704 // for each char in line
705 for(prev = NULL; *curr; prev = curr++) {
706 if (*curr == L'&' && (prev == NULL || *prev != L'\\')) {
710 if (ampersand == NULL) {
714 if (prev == ampersand)
718 if (iswalpha(*curr) || iswxdigit(*curr)) {
724 if (ampersand + 1 >= curr || ampersand + 16 < curr) // what is a good limit?
726 if (ampersand[1] == L'#') { // &#nnnn; or &#xhhhh;
727 if (ampersand + 2 >= curr)
729 if (ampersand[2] != L'x') { // &#nnnn;
730 cnt = wcsspn(&ersand[2], L"0123456789");
731 if (ampersand + 2 + cnt != curr)
733 ucs = wcstoul(&ersand[2], NULL, 10);
735 if (ampersand + 3 >= curr)
737 cnt = wcsspn(&ersand[3], L"0123456789abcdefABCDEF");
738 if (ampersand + 3 + cnt != curr)
740 ucs = wcstoul(&ersand[3], NULL, 16);
743 for (cnt = 0; cnt < sizeof(named_character_entities)/sizeof(named_character_entities[0]); ++cnt) {
744 if (wcsncmp(named_character_entities[cnt].name, &ersand[1], curr - ampersand - 1))
746 ucs = named_character_entities[cnt].ucs;
753 cstring_strip(line->text, ampersand + 1 - &line->text->value[0], curr - ampersand);
762 void adjust_line_length(line_t *line) {
764 const static wchar_t *special = L"\\*_`"; // list of interpreted chars
765 const wchar_t *c = &line->text->value[0];
766 cstack_t *stack = cstack_init();
768 // for each char in line
770 // if char is in special char list
771 if(wcschr(special, *c)) {
773 // closing special char (or second backslash)
774 if((stack->top)(stack, *c)) {
778 // treat special as regular char
779 } else if((stack->top)(stack, L'\\')) {
783 // opening special char
785 (stack->push)(stack, *c);
789 // remove backslash from stack
790 if((stack->top)(stack, L'\\'))
796 if(CHECK_BIT(line->bits, IS_H1_ATX))
798 if(CHECK_BIT(line->bits, IS_H2_ATX))
803 (stack->delete)(stack);
806 int next_nonblank(cstring_t *text, int i) {
807 while ((i < text->size) && iswspace((text->value)[i]))
813 int prev_blank(cstring_t *text, int i) {
814 while ((i > 0) && !iswspace((text->value)[i]))
820 int next_blank(cstring_t *text, int i) {
821 while ((i < text->size) && !iswspace((text->value)[i]))
827 int next_word(cstring_t *text, int i) {
828 return next_nonblank(text, next_blank(text, i));
831 int next_nontilde(cstring_t *text, int i) {
832 while ((i < text->size) && text->value[i] == L'~')