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
98 // if slide ! has line
99 if(!slide->line || !line) {
109 line = next_line(line);
121 line->offset = next_nonblank(text, 0);
123 // adjust line length dynamicaly - excluding markup
124 if(line->text->value)
125 adjust_line_length(line);
128 text = cstring_init();
131 } else if(c == L'\t') {
133 // expand tab to spaces
134 for (i = 0; i < EXPAND_TABS; i++) {
135 (text->expand)(text, L' ');
138 } else if(c == L'\\') {
141 (text->expand)(text, c);
143 // if !IS_CODE add next char to line
144 // and do not increase line count
145 if(next_nonblank(text, 0) < CODE_INDENT) {
148 (text->expand)(text, c);
151 } else if(iswprint(c) || iswspace(c)) {
154 (text->expand)(text, c);
157 (text->delete)(text);
163 line = deck->slide->line;
164 if(line && line->text->size > 0 && line->text->value[0] == L'%') {
166 // assign header to deck
169 // find first non-header line
170 while(line && line->text->size > 0 && line->text->value[0] == L'%') {
175 // only split header if any non-header line is found
179 line->prev->next = NULL;
182 // remove header lines from slide
183 deck->slide->line = line;
187 deck->slide->lines -= hc;
190 // remove header from deck
199 // ignore mdpress format attributes
202 !CHECK_BIT(line->bits, IS_EMPTY) &&
203 line->text->value[line->offset] == L'=' &&
204 line->text->value[line->offset + 1] == L' ') {
206 // remove line from linked list
207 slide->line = line->next;
208 line->next->prev = NULL;
210 // maintain loop condition
218 (tmp->text->delete)(tmp->text);
223 // combine underlined H1/H2 in single line
224 if((CHECK_BIT(line->bits, IS_H1) ||
225 CHECK_BIT(line->bits, IS_H2)) &&
226 CHECK_BIT(line->bits, IS_EMPTY) &&
228 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
231 // remove line from linked list
232 line->prev->next = line->next;
234 line->next->prev = line->prev;
236 // set bits on previous line
237 if(CHECK_BIT(line->bits, IS_H1)) {
238 SET_BIT(line->prev->bits, IS_H1);
240 SET_BIT(line->prev->bits, IS_H2);
246 // maintain loop condition
251 (tmp->text->delete)(tmp->text);
254 // pass enclosing flag IS_UNORDERED_LIST_3
255 // to nested levels for unordered lists
256 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
258 line_t *list_last_level_3 = line;
261 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
262 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
263 list_last_level_3 = tmp;
268 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
269 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
272 // pass enclosing flag IS_UNORDERED_LIST_2
273 // to nested levels for unordered lists
274 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
276 line_t *list_last_level_2 = line;
279 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
280 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
281 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
282 list_last_level_2 = tmp;
287 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
288 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
291 // pass enclosing flag IS_UNORDERED_LIST_1
292 // to nested levels for unordered lists
293 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
295 line_t *list_last_level_1 = line;
298 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
299 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
300 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
301 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
302 list_last_level_1 = tmp;
307 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
308 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
320 int markdown_analyse(cstring_t *text, int prev) {
322 // static variables can not be redeclaired, but changed outside of a declaration
323 // the program remembers their value on every function calls
324 static int unordered_list_level = 0;
325 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
326 static int num_tilde_characters = 0;
328 int i = 0; // increment
329 int bits = 0; // markdown bits
330 int offset = 0; // text offset
331 int eol = 0; // end of line
333 int equals = 0, hashes = 0,
334 stars = 0, minus = 0,
335 spaces = 0, other = 0; // special character counts
337 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
339 // return IS_EMPTY on null pointers
340 if(!text || !text->value) {
341 SET_BIT(bits, IS_EMPTY);
345 // count leading spaces
346 offset = next_nonblank(text, 0);
349 if (wcsncmp(text->value, L"~~~", 3) == 0) {
350 int tildes_in_line = next_nontilde(text, 0);
351 if (tildes_in_line >= num_tilde_characters) {
352 if (num_tilde_characters > 0) {
353 num_tilde_characters = 0;
355 num_tilde_characters = tildes_in_line;
357 SET_BIT(bits, IS_EMPTY);
358 SET_BIT(bits, IS_TILDE_CODE);
363 if (num_tilde_characters > 0) {
364 SET_BIT(bits, IS_CODE);
365 SET_BIT(bits, IS_TILDE_CODE);
370 if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
371 (!wcsncmp(&text->value[offset], L"<br>", 4) ||
372 !wcsncmp(&text->value[offset], L"<BR>", 4) ||
373 !wcsncmp(&text->value[offset], L"^", 1))) {
374 SET_BIT(bits, IS_STOP);
378 // strip trailing spaces
379 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
381 // IS_UNORDERED_LIST_#
382 if(text->size >= offset + 2 &&
383 (text->value[offset] == L'*' || text->value[offset] == L'-') &&
384 iswspace(text->value[offset + 1])) {
386 // if different from last lines offset
387 if(offset != unordered_list_offset) {
389 // test if offset matches a lower indent level
390 for(i = unordered_list_level; i >= 0; i--) {
391 if(unordered_list_level_offset[i] == offset) {
392 unordered_list_level = i;
396 // if offset doesn't match any previously stored indent level
397 if(i != unordered_list_level) {
398 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
399 // memorize the offset as next bigger indent level
400 unordered_list_level_offset[unordered_list_level] = offset;
404 // if no previous indent level matches, this must be the first line of the list
405 if(unordered_list_level == 0) {
406 unordered_list_level = 1;
407 unordered_list_level_offset[1] = offset;
410 switch(unordered_list_level) {
411 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
412 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
413 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
418 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
419 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
420 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
422 // continue list if indent level is still the same as in previous line
423 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
424 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
425 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
426 offset >= unordered_list_offset) {
428 switch(unordered_list_level) {
429 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
430 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
431 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
435 // this line extends the previous list item
436 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
438 // or reset indent level
440 unordered_list_level = 0;
444 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
445 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
446 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
449 if(offset >= CODE_INDENT &&
450 (CHECK_BIT(prev, IS_EMPTY) ||
451 CHECK_BIT(prev, IS_CODE) ||
452 CHECK_BIT(prev, IS_STOP))) {
453 SET_BIT(bits, IS_CODE);
458 if(text->value[offset] == L'>') {
459 SET_BIT(bits, IS_QUOTE);
463 if(text->size >= offset + 3 &&
464 text->value[offset] == L'-' &&
465 text->value[offset + 1] == L'>' &&
466 iswspace(text->value[offset + 2])) {
467 SET_BIT(bits, IS_CENTER);
470 (text->strip)(text, offset, 3);
473 if(text->size >= offset + 3 &&
474 text->value[eol - 1] == L'-' &&
475 text->value[eol - 2] == L'<' &&
476 iswspace(text->value[eol - 3])) {
479 (text->strip)(text, eol - 3, 3);
481 // adjust end of line
482 for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
487 for(i = offset; i < eol; i++) {
489 if(iswspace(text->value[i])) {
493 switch(text->value[i]) {
494 case L'=': equals++; break;
495 case L'#': hashes++; break;
496 case L'*': stars++; break;
497 case L'-': minus++; break;
498 case L'\\': other++; i++; break;
499 default: other++; break;
506 hashes + stars + minus + spaces + other == 0) {
507 SET_BIT(bits, IS_H1);
509 if(text->value[offset] == L'#' &&
510 iswspace(text->value[offset+1])) {
511 SET_BIT(bits, IS_H1);
512 SET_BIT(bits, IS_H1_ATX);
517 equals + hashes + stars + spaces + other == 0) {
518 SET_BIT(bits, IS_H2);
520 if(text->value[offset] == L'#' &&
521 text->value[offset+1] == L'#' &&
522 iswspace(text->value[offset+2])) {
523 SET_BIT(bits, IS_H2);
524 SET_BIT(bits, IS_H2_ATX);
528 if((minus >= 3 && equals + hashes + stars + other == 0) ||
529 (stars >= 3 && equals + hashes + minus + other == 0)) {
531 SET_BIT(bits, IS_HR);
536 SET_BIT(bits, IS_EMPTY);
544 void markdown_debug(deck_t *deck, int debug) {
546 int sc = 0; // slide count
547 int lc = 0; // line count
553 fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
555 } else if(debug > 1) {
557 // print header to STDERR
559 header = deck->header;
561 header->length > 0 &&
562 header->text->value[0] == L'%') {
564 // skip descriptor word (e.g. %title:)
565 offset = next_blank(header->text, 0) + 1;
567 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
568 header = header->next;
573 slide_t *slide = deck->slide;
576 // print slide/line count to STDERR
581 fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines);
583 } else if(debug > 1) {
585 // also print bits and line length
586 fwprintf(stderr, L" slide %i:\n", sc);
591 fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
600 void adjust_line_length(line_t *line) {
602 const static wchar_t *special = L"\\*_`"; // list of interpreted chars
603 const wchar_t *c = &line->text->value[line->offset];
604 cstack_t *stack = cstack_init();
606 // for each char in line
608 // if char is in special char list
609 if(wcschr(special, *c)) {
611 // closing special char (or second backslash)
612 if((stack->top)(stack, *c)) {
616 // treat special as regular char
617 } else if((stack->top)(stack, L'\\')) {
621 // opening special char
623 (stack->push)(stack, *c);
627 // remove backslash from stack
628 if((stack->top)(stack, L'\\'))
634 if(CHECK_BIT(line->bits, IS_H1_ATX))
636 if(CHECK_BIT(line->bits, IS_H2_ATX))
641 (stack->delete)(stack);
644 int next_nonblank(cstring_t *text, int i) {
645 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'~')