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) 2014 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/>.
32 deck_t *markdown_load(FILE *input) {
35 int i = 0; // increment
36 int hc = 0; // header count
37 int lc = 0; // line count
38 int sc = 1; // slide count
39 int bits = 0; // markdown bits
40 int prev = 0; // markdown bits of previous line
42 deck_t *deck = new_deck();
43 slide_t *slide = deck->slide;
46 cstring_t *text = cstring_init();
48 // initialize bits as empty line
49 SET_BIT(bits, IS_EMPTY);
51 while ((c = fgetc(input)) != EOF) {
53 fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
61 bits = markdown_analyse(text, prev);
63 // if first line in file is markdown hr
64 if(!line && CHECK_BIT(bits, IS_HR)) {
69 // if text is markdown hr
70 } else if(CHECK_BIT(bits, IS_HR) &&
71 CHECK_BIT(line->bits, IS_EMPTY)) {
79 slide = next_slide(slide);
84 // if slide ! has line
95 line = next_line(line);
107 line->offset = next_nonblank(text, 0);
109 // adjust line length dynamicaly - excluding markup
111 adjust_line_length(line);
114 text = cstring_init();
117 } else if(c == '\t') {
119 // expand tab to spaces
120 for (i = 0; i < EXPAND_TABS; i++) {
121 (text->expand)(text, ' ');
124 } else if(c == '\\') {
127 (text->expand)(text, c);
129 // if !IS_CODE add next char to line
130 // and do not increase line count
131 if(next_nonblank(text, 0) < CODE_INDENT) {
134 (text->expand)(text, c);
138 // if utf-8 char > 1 byte add remaing to line
139 for(i = 0; i < length_utf8(c) - 1; i++) {
141 (text->expand)(text, c);
147 } else if(isprint(c) || isspace((unsigned char) c)) {
150 (text->expand)(text, c);
152 } else if(is_utf8(c)) {
155 (text->expand)(text, c);
157 // if utf-8 char > 1 byte add remaing to line
158 for(i = 0; i < length_utf8(c) - 1; i++) {
160 (text->expand)(text, c);
164 (text->delete)(text);
170 line = deck->slide->line;
171 if(line && line->text->size > 0 && line->text->text[0] == '%') {
173 // assign header to deck
176 // find first non-header line
177 while(line && line->text->size > 0 && line->text->text[0] == '%') {
182 // only split header if any non-header line is found
186 line->prev->next = NULL;
189 // remove header lines from slide
190 deck->slide->line = line;
194 deck->slide->lines -= hc;
202 // combine underlined H1/H2 in single line
203 if((CHECK_BIT(line->bits, IS_H1) ||
204 CHECK_BIT(line->bits, IS_H2)) &&
205 CHECK_BIT(line->bits, IS_EMPTY) &&
207 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
210 // remove line from linked list
211 line->prev->next = line->next;
213 line->next->prev = line->prev;
215 // set bits on previous line
216 if(CHECK_BIT(line->bits, IS_H1)) {
217 SET_BIT(line->prev->bits, IS_H1);
219 SET_BIT(line->prev->bits, IS_H2);
225 // maintain loop condition
230 (tmp->text->delete)(tmp->text);
233 // pass enclosing flag IS_UNORDERED_LIST_3
234 // to nested levels for unordered lists
235 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
237 line_t *list_last_level_3 = line;
240 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
241 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
242 list_last_level_3 = tmp;
247 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
248 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
251 // pass enclosing flag IS_UNORDERED_LIST_2
252 // to nested levels for unordered lists
253 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
255 line_t *list_last_level_2 = line;
258 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
259 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
260 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
261 list_last_level_2 = tmp;
266 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
267 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
270 // pass enclosing flag IS_UNORDERED_LIST_1
271 // to nested levels for unordered lists
272 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
274 line_t *list_last_level_1 = line;
277 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
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_1)) {
281 list_last_level_1 = tmp;
286 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
287 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
299 int markdown_analyse(cstring_t *text, int prev) {
301 // static variables can not be redeclaired, but changed outside of a declaration
302 // the program remembers their value on every function calls
303 static int unordered_list_level = 0;
304 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
306 int i = 0; // increment
307 int bits = 0; // markdown bits
308 int offset = 0; // text offset
309 int eol = 0; // end of line
311 int equals = 0, hashes = 0,
312 stars = 0, minus = 0,
313 spaces = 0, other = 0; // special character counts
315 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
317 // return IS_EMPTY on null pointers
318 if(!text || !text->text) {
319 SET_BIT(bits, IS_EMPTY);
323 // count leading spaces
324 offset = next_nonblank(text, 0);
326 // strip trailing spaces
327 for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
329 // IS_UNORDERED_LIST_#
330 if(text->size >= offset + 2 &&
331 (text->text[offset] == '*' || text->text[offset] == '-') &&
332 text->text[offset + 1] == ' ') {
334 // if different from last lines offset
335 if(offset != unordered_list_offset) {
337 // test if offset matches a lower indent level
338 for(i = unordered_list_level; i >= 0; i--) {
339 if(unordered_list_level_offset[i] == offset) {
340 unordered_list_level = i;
344 // if offset doesn't match any previously stored indent level
345 if(i != unordered_list_level) {
346 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
347 // memorize the offset as next bigger indent level
348 unordered_list_level_offset[unordered_list_level] = offset;
352 // if no previous indent level matches, this must be the first line of the list
353 if(unordered_list_level == 0) {
354 unordered_list_level = 1;
355 unordered_list_level_offset[1] = offset;
358 switch(unordered_list_level) {
359 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
360 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
361 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
366 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
367 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
368 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
370 // continue list if indent level is still the same as in previous line
371 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
372 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
373 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
374 offset >= unordered_list_offset) {
376 switch(unordered_list_level) {
377 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
378 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
379 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
383 // this line extends the previous list item
384 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
386 // or reset indent level
388 unordered_list_level = 0;
392 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
393 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
394 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
397 if(offset >= CODE_INDENT &&
398 (CHECK_BIT(prev, IS_EMPTY) ||
399 CHECK_BIT(prev, IS_CODE))) {
400 SET_BIT(bits, IS_CODE);
405 if(text->text[offset] == '>') {
406 SET_BIT(bits, IS_QUOTE);
410 if(text->size >= offset + 3 &&
411 text->text[offset] == '-' &&
412 text->text[offset + 1] == '>' &&
413 text->text[offset + 2] == ' ') {
414 SET_BIT(bits, IS_CENTER);
417 (text->strip)(text, offset, 3);
420 if(text->size >= offset + 3 &&
421 text->text[eol - 1] == '-' &&
422 text->text[eol - 2] == '<' &&
423 text->text[eol - 3] == ' ') {
426 (text->strip)(text, eol - 3, 3);
428 // adjust end of line
429 for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
434 for(i = offset; i < eol; i++) {
436 if(text->text[i] == ' ') {
440 switch(text->text[i]) {
441 case '=': equals++; break;
442 case '#': hashes++; break;
443 case '*': stars++; break;
444 case '-': minus++; break;
445 case '\\': other++; i++; break;
446 default: other++; break;
453 hashes + stars + minus + spaces + other == 0) {
454 SET_BIT(bits, IS_H1);
456 if(text->text[offset] == '#' &&
457 text->text[offset+1] == ' ') {
458 SET_BIT(bits, IS_H1);
459 SET_BIT(bits, IS_H1_ATX);
464 equals + hashes + stars + spaces + other == 0) {
465 SET_BIT(bits, IS_H2);
467 if(text->text[offset] == '#' &&
468 text->text[offset+1] == '#' &&
469 text->text[offset+2] == ' ') {
470 SET_BIT(bits, IS_H2);
471 SET_BIT(bits, IS_H2_ATX);
475 if((minus >= 3 && equals + hashes + stars + other == 0) ||
476 (stars >= 3 && equals + hashes + minus + other == 0)) {
478 SET_BIT(bits, IS_HR);
483 SET_BIT(bits, IS_EMPTY);
491 void markdown_debug(deck_t *deck, int debug) {
493 int sc = 0; // slide count
494 int lc = 0; // line count
500 fprintf(stderr, "headers: %i\nslides: %i\n", deck->headers, deck->slides);
502 } else if(debug > 1) {
504 // print header to STDERR
506 header = deck->header;
508 header->length > 0 &&
509 header->text->text[0] == '%') {
511 // skip descriptor word (e.g. %title:)
512 offset = next_blank(header->text, 0) + 1;
514 fprintf(stderr, "header: %s\n", &header->text->text[offset]);
515 header = header->next;
520 slide_t *slide = deck->slide;
523 // print slide/line count to STDERR
528 fprintf(stderr, " slide %i: %i lines\n", sc, slide->lines);
530 } else if(debug > 1) {
532 // also print bits and line length
533 fprintf(stderr, " slide %i:\n", sc);
538 fprintf(stderr, " line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
547 void adjust_line_length(line_t *line) {
549 const static char *special = "\\*_`"; // list of interpreted chars
550 const char *c = &line->text->text[line->offset];
551 cstack_t *stack = cstack_init();
553 // for each char in line
555 // if char is in special char list
556 if(strchr(special, *c)) {
558 // closing special char (or second backslash)
559 if((stack->top)(stack, *c)) {
563 // treat special as regular char
564 } else if((stack->top)(stack, '\\')) {
568 // opening special char
570 (stack->push)(stack, *c);
574 // remove backslash from stack
575 if((stack->top)(stack, '\\'))
581 if(CHECK_BIT(line->bits, IS_H1_ATX))
583 if(CHECK_BIT(line->bits, IS_H2_ATX))
588 (stack->delete)(stack);
591 bool is_utf8(char ch) {
592 return (ch & 0x80) != 0x00;
595 int length_utf8(char ch) {
597 int i = 0; // increment
607 int next_nonblank(cstring_t *text, int i) {
608 while ((i < text->size) && isspace((unsigned char) (text->text)[i]))
614 int prev_blank(cstring_t *text, int i) {
615 while ((i > 0) && !isspace((unsigned char) (text->text)[i]))
621 int next_blank(cstring_t *text, int i) {
622 while ((i < text->size) && !isspace((unsigned char) (text->text)[i]))
628 int next_word(cstring_t *text, int i) {
629 return next_nonblank(text, next_blank(text, i));