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) 2015 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) && CHECK_BIT(bits, IS_EMPTY)) {
93 // remove tilde code markers
97 // if slide ! has line
98 if(!slide->line || !line) {
108 line = next_line(line);
120 line->offset = next_nonblank(text, 0);
122 // adjust line length dynamicaly - excluding markup
123 if(line->text->value)
124 adjust_line_length(line);
127 text = cstring_init();
130 } else if(c == L'\t') {
132 // expand tab to spaces
133 for (i = 0; i < EXPAND_TABS; i++) {
134 (text->expand)(text, L' ');
137 } else if(c == L'\\') {
140 (text->expand)(text, c);
142 // if !IS_CODE add next char to line
143 // and do not increase line count
144 if(next_nonblank(text, 0) < CODE_INDENT) {
147 (text->expand)(text, c);
150 } else if(iswprint(c) || iswspace(c)) {
153 (text->expand)(text, c);
156 (text->delete)(text);
162 line = deck->slide->line;
163 if(line && line->text->size > 0 && line->text->value[0] == L'%') {
165 // assign header to deck
168 // find first non-header line
169 while(line && line->text->size > 0 && line->text->value[0] == L'%') {
174 // only split header if any non-header line is found
178 line->prev->next = NULL;
181 // remove header lines from slide
182 deck->slide->line = line;
186 deck->slide->lines -= hc;
189 // remove header from deck
198 // ignore mdpress format attributes
201 !CHECK_BIT(line->bits, IS_EMPTY) &&
202 line->text->value[line->offset] == L'=' &&
203 line->text->value[line->offset + 1] == L' ') {
205 // remove line from linked list
206 slide->line = line->next;
207 line->next->prev = NULL;
209 // maintain loop condition
217 (tmp->text->delete)(tmp->text);
222 // combine underlined H1/H2 in single line
223 if((CHECK_BIT(line->bits, IS_H1) ||
224 CHECK_BIT(line->bits, IS_H2)) &&
225 CHECK_BIT(line->bits, IS_EMPTY) &&
227 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
230 // remove line from linked list
231 line->prev->next = line->next;
233 line->next->prev = line->prev;
235 // set bits on previous line
236 if(CHECK_BIT(line->bits, IS_H1)) {
237 SET_BIT(line->prev->bits, IS_H1);
239 SET_BIT(line->prev->bits, IS_H2);
245 // maintain loop condition
250 (tmp->text->delete)(tmp->text);
253 // pass enclosing flag IS_UNORDERED_LIST_3
254 // to nested levels for unordered lists
255 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
257 line_t *list_last_level_3 = line;
260 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
261 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
262 list_last_level_3 = tmp;
267 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
268 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
271 // pass enclosing flag IS_UNORDERED_LIST_2
272 // to nested levels for unordered lists
273 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
275 line_t *list_last_level_2 = line;
278 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
279 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
280 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
281 list_last_level_2 = tmp;
286 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
287 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
290 // pass enclosing flag IS_UNORDERED_LIST_1
291 // to nested levels for unordered lists
292 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
294 line_t *list_last_level_1 = line;
297 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
298 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
299 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
300 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
301 list_last_level_1 = tmp;
306 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
307 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
319 int markdown_analyse(cstring_t *text, int prev) {
321 // static variables can not be redeclaired, but changed outside of a declaration
322 // the program remembers their value on every function calls
323 static int unordered_list_level = 0;
324 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
325 static int num_tilde_characters = 0;
327 int i = 0; // increment
328 int bits = 0; // markdown bits
329 int offset = 0; // text offset
330 int eol = 0; // end of line
332 int equals = 0, hashes = 0,
333 stars = 0, minus = 0,
334 spaces = 0, other = 0; // special character counts
336 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
338 // return IS_EMPTY on null pointers
339 if(!text || !text->value) {
340 SET_BIT(bits, IS_EMPTY);
344 // count leading spaces
345 offset = next_nonblank(text, 0);
348 if (wcsncmp(text->value, L"~~~", 3) == 0) {
349 int tildes_in_line = next_nontilde(text, 0);
350 if (tildes_in_line >= num_tilde_characters) {
351 if (num_tilde_characters > 0) {
352 num_tilde_characters = 0;
354 num_tilde_characters = tildes_in_line;
356 SET_BIT(bits, IS_EMPTY);
357 SET_BIT(bits, IS_TILDE_CODE);
362 if (num_tilde_characters > 0) {
363 SET_BIT(bits, IS_CODE);
364 SET_BIT(bits, IS_TILDE_CODE);
369 if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
370 (!wcsncmp(&text->value[offset], L"<br>", 4) ||
371 !wcsncmp(&text->value[offset], L"<BR>", 4) ||
372 !wcsncmp(&text->value[offset], L"^", 1))) {
373 SET_BIT(bits, IS_STOP);
377 // strip trailing spaces
378 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
380 // IS_UNORDERED_LIST_#
381 if(text->size >= offset + 2 &&
382 (text->value[offset] == L'*' || text->value[offset] == L'-') &&
383 iswspace(text->value[offset + 1])) {
385 // if different from last lines offset
386 if(offset != unordered_list_offset) {
388 // test if offset matches a lower indent level
389 for(i = unordered_list_level; i >= 0; i--) {
390 if(unordered_list_level_offset[i] == offset) {
391 unordered_list_level = i;
395 // if offset doesn't match any previously stored indent level
396 if(i != unordered_list_level) {
397 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
398 // memorize the offset as next bigger indent level
399 unordered_list_level_offset[unordered_list_level] = offset;
403 // if no previous indent level matches, this must be the first line of the list
404 if(unordered_list_level == 0) {
405 unordered_list_level = 1;
406 unordered_list_level_offset[1] = offset;
409 switch(unordered_list_level) {
410 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
411 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
412 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
417 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
418 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
419 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
421 // continue list if indent level is still the same as in previous line
422 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
423 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
424 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
425 offset >= unordered_list_offset) {
427 switch(unordered_list_level) {
428 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
429 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
430 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
434 // this line extends the previous list item
435 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
437 // or reset indent level
439 unordered_list_level = 0;
443 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
444 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
445 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
448 if(offset >= CODE_INDENT &&
449 (CHECK_BIT(prev, IS_EMPTY) ||
450 CHECK_BIT(prev, IS_CODE) ||
451 CHECK_BIT(prev, IS_STOP))) {
452 SET_BIT(bits, IS_CODE);
457 if(text->value[offset] == L'>') {
458 SET_BIT(bits, IS_QUOTE);
462 if(text->size >= offset + 3 &&
463 text->value[offset] == L'-' &&
464 text->value[offset + 1] == L'>' &&
465 iswspace(text->value[offset + 2])) {
466 SET_BIT(bits, IS_CENTER);
469 (text->strip)(text, offset, 3);
472 if(text->size >= offset + 3 &&
473 text->value[eol - 1] == L'-' &&
474 text->value[eol - 2] == L'<' &&
475 iswspace(text->value[eol - 3])) {
478 (text->strip)(text, eol - 3, 3);
480 // adjust end of line
481 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
486 for(i = offset; i < eol; i++) {
488 if(iswspace(text->value[i])) {
492 switch(text->value[i]) {
493 case L'=': equals++; break;
494 case L'#': hashes++; break;
495 case L'*': stars++; break;
496 case L'-': minus++; break;
497 case L'\\': other++; i++; break;
498 default: other++; break;
505 hashes + stars + minus + spaces + other == 0) {
506 SET_BIT(bits, IS_H1);
508 if(text->value[offset] == L'#' &&
509 iswspace(text->value[offset+1])) {
510 SET_BIT(bits, IS_H1);
511 SET_BIT(bits, IS_H1_ATX);
516 equals + hashes + stars + spaces + other == 0) {
517 SET_BIT(bits, IS_H2);
519 if(text->value[offset] == L'#' &&
520 text->value[offset+1] == L'#' &&
521 iswspace(text->value[offset+2])) {
522 SET_BIT(bits, IS_H2);
523 SET_BIT(bits, IS_H2_ATX);
527 if((minus >= 3 && equals + hashes + stars + other == 0) ||
528 (stars >= 3 && equals + hashes + minus + other == 0)) {
530 SET_BIT(bits, IS_HR);
535 SET_BIT(bits, IS_EMPTY);
543 void markdown_debug(deck_t *deck, int debug) {
545 int sc = 0; // slide count
546 int lc = 0; // line count
552 fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
554 } else if(debug > 1) {
556 // print header to STDERR
558 header = deck->header;
560 header->length > 0 &&
561 header->text->value[0] == L'%') {
563 // skip descriptor word (e.g. %title:)
564 offset = next_blank(header->text, 0) + 1;
566 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
567 header = header->next;
572 slide_t *slide = deck->slide;
575 // print slide/line count to STDERR
580 fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines);
582 } else if(debug > 1) {
584 // also print bits and line length
585 fwprintf(stderr, L" slide %i:\n", sc);
590 fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
599 void adjust_line_length(line_t *line) {
601 const static wchar_t *special = L"\\*_`"; // list of interpreted chars
602 const wchar_t *c = &line->text->value[line->offset];
603 cstack_t *stack = cstack_init();
605 // for each char in line
607 // if char is in special char list
608 if(wcschr(special, *c)) {
610 // closing special char (or second backslash)
611 if((stack->top)(stack, *c)) {
615 // treat special as regular char
616 } else if((stack->top)(stack, L'\\')) {
620 // opening special char
622 (stack->push)(stack, *c);
626 // remove backslash from stack
627 if((stack->top)(stack, L'\\'))
633 if(CHECK_BIT(line->bits, IS_H1_ATX))
635 if(CHECK_BIT(line->bits, IS_H2_ATX))
640 (stack->delete)(stack);
643 int next_nonblank(cstring_t *text, int i) {
644 while ((i < text->size) && iswspace((text->value)[i]))
651 int prev_blank(cstring_t *text, int i) {
652 while ((i > 0) && !iswspace((text->value)[i]))
658 int next_blank(cstring_t *text, int i) {
659 while ((i < text->size) && !iswspace((text->value)[i]))
665 int next_word(cstring_t *text, int i) {
666 return next_nonblank(text, next_blank(text, i));
669 int next_nontilde(cstring_t *text, int i) {
670 while ((i < text->size) && text->value[i] == L'~')