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/>.
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 // if text is markdown hr
72 } else if(CHECK_BIT(bits, IS_HR) &&
73 CHECK_BIT(line->bits, IS_EMPTY)) {
81 slide = next_slide(slide);
86 // if slide ! has line
87 if(!slide->line || !line) {
97 line = next_line(line);
109 line->offset = next_nonblank(text, 0);
111 // adjust line length dynamicaly - excluding markup
113 adjust_line_length(line);
116 text = cstring_init();
119 } else if(c == L'\t') {
121 // expand tab to spaces
122 for (i = 0; i < EXPAND_TABS; i++) {
123 (text->expand)(text, L' ');
126 } else if(c == L'\\') {
129 (text->expand)(text, c);
131 // if !IS_CODE add next char to line
132 // and do not increase line count
133 if(next_nonblank(text, 0) < CODE_INDENT) {
136 (text->expand)(text, c);
139 } else if(iswprint(c) || iswspace(c)) {
142 (text->expand)(text, c);
145 (text->delete)(text);
151 line = deck->slide->line;
152 if(line && line->text->size > 0 && line->text->text[0] == L'%') {
154 // assign header to deck
157 // find first non-header line
158 while(line && line->text->size > 0 && line->text->text[0] == L'%') {
163 // only split header if any non-header line is found
167 line->prev->next = NULL;
170 // remove header lines from slide
171 deck->slide->line = line;
175 deck->slide->lines -= hc;
178 // remove header from deck
187 // combine underlined H1/H2 in single line
188 if((CHECK_BIT(line->bits, IS_H1) ||
189 CHECK_BIT(line->bits, IS_H2)) &&
190 CHECK_BIT(line->bits, IS_EMPTY) &&
192 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
195 // remove line from linked list
196 line->prev->next = line->next;
198 line->next->prev = line->prev;
200 // set bits on previous line
201 if(CHECK_BIT(line->bits, IS_H1)) {
202 SET_BIT(line->prev->bits, IS_H1);
204 SET_BIT(line->prev->bits, IS_H2);
210 // maintain loop condition
215 (tmp->text->delete)(tmp->text);
218 // pass enclosing flag IS_UNORDERED_LIST_3
219 // to nested levels for unordered lists
220 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
222 line_t *list_last_level_3 = line;
225 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
226 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
227 list_last_level_3 = tmp;
232 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
233 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
236 // pass enclosing flag IS_UNORDERED_LIST_2
237 // to nested levels for unordered lists
238 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
240 line_t *list_last_level_2 = line;
243 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
244 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
245 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
246 list_last_level_2 = tmp;
251 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
252 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
255 // pass enclosing flag IS_UNORDERED_LIST_1
256 // to nested levels for unordered lists
257 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
259 line_t *list_last_level_1 = line;
262 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
263 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
264 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
265 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
266 list_last_level_1 = tmp;
271 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
272 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
284 int markdown_analyse(cstring_t *text, int prev) {
286 // static variables can not be redeclaired, but changed outside of a declaration
287 // the program remembers their value on every function calls
288 static int unordered_list_level = 0;
289 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
291 int i = 0; // increment
292 int bits = 0; // markdown bits
293 int offset = 0; // text offset
294 int eol = 0; // end of line
296 int equals = 0, hashes = 0,
297 stars = 0, minus = 0,
298 spaces = 0, other = 0; // special character counts
300 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
302 // return IS_EMPTY on null pointers
303 if(!text || !text->text) {
304 SET_BIT(bits, IS_EMPTY);
308 // count leading spaces
309 offset = next_nonblank(text, 0);
311 // strip trailing spaces
312 for(eol = text->size; eol > offset && iswspace(text->text[eol - 1]); eol--);
314 // IS_UNORDERED_LIST_#
315 if(text->size >= offset + 2 &&
316 (text->text[offset] == L'*' || text->text[offset] == L'-') &&
317 iswspace(text->text[offset + 1])) {
319 // if different from last lines offset
320 if(offset != unordered_list_offset) {
322 // test if offset matches a lower indent level
323 for(i = unordered_list_level; i >= 0; i--) {
324 if(unordered_list_level_offset[i] == offset) {
325 unordered_list_level = i;
329 // if offset doesn't match any previously stored indent level
330 if(i != unordered_list_level) {
331 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
332 // memorize the offset as next bigger indent level
333 unordered_list_level_offset[unordered_list_level] = offset;
337 // if no previous indent level matches, this must be the first line of the list
338 if(unordered_list_level == 0) {
339 unordered_list_level = 1;
340 unordered_list_level_offset[1] = offset;
343 switch(unordered_list_level) {
344 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
345 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
346 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
351 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
352 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
353 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
355 // continue list if indent level is still the same as in previous line
356 if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
357 CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
358 CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
359 offset >= unordered_list_offset) {
361 switch(unordered_list_level) {
362 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
363 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
364 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
368 // this line extends the previous list item
369 SET_BIT(bits, IS_UNORDERED_LIST_EXT);
371 // or reset indent level
373 unordered_list_level = 0;
377 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
378 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
379 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
382 if(offset >= CODE_INDENT &&
383 (CHECK_BIT(prev, IS_EMPTY) ||
384 CHECK_BIT(prev, IS_CODE))) {
385 SET_BIT(bits, IS_CODE);
390 if(text->text[offset] == L'>') {
391 SET_BIT(bits, IS_QUOTE);
395 if(text->size >= offset + 3 &&
396 text->text[offset] == L'-' &&
397 text->text[offset + 1] == L'>' &&
398 iswspace(text->text[offset + 2])) {
399 SET_BIT(bits, IS_CENTER);
402 (text->strip)(text, offset, 3);
405 if(text->size >= offset + 3 &&
406 text->text[eol - 1] == L'-' &&
407 text->text[eol - 2] == L'<' &&
408 iswspace(text->text[eol - 3])) {
411 (text->strip)(text, eol - 3, 3);
413 // adjust end of line
414 for(eol = text->size; eol > offset && iswspace(text->text[eol - 1]); eol--);
419 for(i = offset; i < eol; i++) {
421 if(iswspace(text->text[i])) {
425 switch(text->text[i]) {
426 case L'=': equals++; break;
427 case L'#': hashes++; break;
428 case L'*': stars++; break;
429 case L'-': minus++; break;
430 case L'\\': other++; i++; break;
431 default: other++; break;
438 hashes + stars + minus + spaces + other == 0) {
439 SET_BIT(bits, IS_H1);
441 if(text->text[offset] == L'#' &&
442 iswspace(text->text[offset+1])) {
443 SET_BIT(bits, IS_H1);
444 SET_BIT(bits, IS_H1_ATX);
449 equals + hashes + stars + spaces + other == 0) {
450 SET_BIT(bits, IS_H2);
452 if(text->text[offset] == L'#' &&
453 text->text[offset+1] == L'#' &&
454 iswspace(text->text[offset+2])) {
455 SET_BIT(bits, IS_H2);
456 SET_BIT(bits, IS_H2_ATX);
460 if((minus >= 3 && equals + hashes + stars + other == 0) ||
461 (stars >= 3 && equals + hashes + minus + other == 0)) {
463 SET_BIT(bits, IS_HR);
468 SET_BIT(bits, IS_EMPTY);
476 void markdown_debug(deck_t *deck, int debug) {
478 int sc = 0; // slide count
479 int lc = 0; // line count
485 fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
487 } else if(debug > 1) {
489 // print header to STDERR
491 header = deck->header;
493 header->length > 0 &&
494 header->text->text[0] == L'%') {
496 // skip descriptor word (e.g. %title:)
497 offset = next_blank(header->text, 0) + 1;
499 fwprintf(stderr, L"header: %S\n", &header->text->text[offset]);
500 header = header->next;
505 slide_t *slide = deck->slide;
508 // print slide/line count to STDERR
513 fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines);
515 } else if(debug > 1) {
517 // also print bits and line length
518 fwprintf(stderr, L" slide %i:\n", sc);
523 fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
532 void adjust_line_length(line_t *line) {
534 const static wchar_t *special = L"\\*_`"; // list of interpreted chars
535 const wchar_t *c = &line->text->text[line->offset];
536 cstack_t *stack = cstack_init();
538 // for each char in line
540 // if char is in special char list
541 if(wcschr(special, *c)) {
543 // closing special char (or second backslash)
544 if((stack->top)(stack, *c)) {
548 // treat special as regular char
549 } else if((stack->top)(stack, L'\\')) {
553 // opening special char
555 (stack->push)(stack, *c);
559 // remove backslash from stack
560 if((stack->top)(stack, L'\\'))
566 if(CHECK_BIT(line->bits, IS_H1_ATX))
568 if(CHECK_BIT(line->bits, IS_H2_ATX))
573 (stack->delete)(stack);
576 int next_nonblank(cstring_t *text, int i) {
577 while ((i < text->size) && iswspace((text->text)[i]))
583 int prev_blank(cstring_t *text, int i) {
584 while ((i > 0) && !iswspace((text->text)[i]))
590 int next_blank(cstring_t *text, int i) {
591 while ((i < text->size) && !iswspace((text->text)[i]))
597 int next_word(cstring_t *text, int i) {
598 return next_nonblank(text, next_blank(text, i));