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 l = 0; // line length
37 int hc = 0; // header count
38 int lc = 0; // line count
39 int sc = 1; // slide count
40 int bits = 0; // markdown bits
42 deck_t *deck = new_deck();
43 slide_t *slide = deck->slide;
46 cstring_t *text = cstring_init();
48 while ((c = fgetc(input)) != EOF) {
50 fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
57 bits = markdown_analyse(text);
59 // if first line in file is markdown hr
60 if(!line && CHECK_BIT(bits, IS_HR)) {
65 // if text is markdown hr
66 } else if(CHECK_BIT(bits, IS_HR) &&
67 CHECK_BIT(line->bits, IS_EMPTY)) {
76 slide = next_slide(slide);
81 // if slide ! has line
92 line = next_line(line);
103 // add length to line
107 line->offset = next_nonblank(text, 0);
110 text = cstring_init();
114 } else if(c == '\t') {
116 // expand tab to spaces
117 for (i = 0; i < EXPAND_TABS; i++) {
118 (text->expand)(text, ' ');
122 } else if(c == '\\') {
125 (text->expand)(text, c);
128 // if !IS_CODE add next char to line
129 // and do not increase line count
130 if(next_nonblank(text, 0) < CODE_INDENT) {
133 (text->expand)(text, c);
137 // if utf-8 char > 1 byte add remaing to line
138 for(i = 0; i < length_utf8(c) - 1; i++) {
140 (text->expand)(text, c);
146 } else if(isprint(c) || isspace((unsigned char) c)) {
149 (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);
171 line = deck->slide->line;
172 if(line && line->text->size > 0 && line->text->text[0] == '%') {
174 // assign header to deck
177 // find first non-header line
178 while(line->text->size > 0 && line->text->text[0] == '%') {
184 line->prev->next = NULL;
187 // remove header lines from slide
188 deck->slide->line = line;
192 deck->slide->lines -= hc;
199 if((CHECK_BIT(line->bits, IS_H1) ||
200 CHECK_BIT(line->bits, IS_H2)) &&
201 CHECK_BIT(line->bits, IS_EMPTY) &&
203 !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
204 // combine underlined H1/H2 in single line
206 // remove line from linked list
207 line->prev->next = line->next;
209 line->next->prev = line->prev;
211 // set bits on previous line
212 if(CHECK_BIT(line->bits, IS_H1)) {
213 SET_BIT(line->prev->bits, IS_H1);
215 SET_BIT(line->prev->bits, IS_H2);
221 // maintain loop condition
226 (tmp->text->delete)(tmp->text);
229 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
231 line_t *list_last_level_3 = line;
234 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
235 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
236 list_last_level_3 = tmp;
241 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
242 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
245 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
247 line_t *list_last_level_2 = line;
250 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
251 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
252 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
253 list_last_level_2 = tmp;
258 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
259 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
262 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
264 line_t *list_last_level_1 = line;
267 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
268 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
269 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
270 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
271 list_last_level_1 = tmp;
276 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
277 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
289 int markdown_analyse(cstring_t *text) {
291 static int unordered_list_level = 0;
292 static int unordered_list_level_offset[] = {-1, -1, -1, -1};
294 int i = 0; // increment
295 int bits = 0; // markdown bits
296 int offset = 0; // text offset
297 int eol = 0; // end of line
299 int equals = 0, hashes = 0,
300 stars = 0, minus = 0,
301 spaces = 0, other = 0; // special character counts
303 const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
305 // return IS_EMPTY on null pointers
306 if(!text || !text->text) {
307 SET_BIT(bits, IS_EMPTY);
311 // count leading spaces
312 offset = next_nonblank(text, 0);
314 // strip trailing spaces
315 for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
317 // IS_UNORDERED_LIST_#
318 if(text->size >= offset + 2 &&
319 (text->text[offset] == '*' || text->text[offset] == '-') &&
320 text->text[offset + 1] == ' ') {
322 for(i = offset; i<eol; i++) {
323 if(text->text[i] != '*' &&
324 text->text[i] != '-' &&
325 text->text[i] != ' ') {
326 if(offset > unordered_list_offset + CODE_INDENT) {
327 SET_BIT(bits, IS_CODE);
328 } else if(offset != unordered_list_offset) {
329 for(i = unordered_list_level; i >= 0; i--) {
330 if(unordered_list_level_offset[i] == offset) {
331 unordered_list_level = i;
335 if(i != unordered_list_level) {
336 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
337 unordered_list_level_offset[unordered_list_level] = offset;
341 if(unordered_list_level == 0) {
342 unordered_list_level = 1;
343 unordered_list_level_offset[1] = offset;
346 switch(unordered_list_level) {
347 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
348 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
349 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
358 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
359 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
360 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
362 unordered_list_level = 0;
365 if(offset >= CODE_INDENT) {
366 SET_BIT(bits, IS_CODE);
371 if(text->text[offset] == '>') {
372 SET_BIT(bits, IS_QUOTE);
376 if(text->size >= offset + 3 &&
377 text->text[offset] == '-' &&
378 text->text[offset + 1] == '>' &&
379 text->text[offset + 2] == ' ') {
380 SET_BIT(bits, IS_CENTER);
383 (text->strip)(text, offset, 3);
386 if(text->size >= offset + 3 &&
387 text->text[eol - 1] == '-' &&
388 text->text[eol - 2] == '<' &&
389 text->text[eol - 3] == ' ') {
392 (text->strip)(text, eol - 3, 3);
394 // adjust end of line
395 for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
400 for(i = offset; i < eol; i++) {
402 if(text->text[i] == ' ') {
406 switch(text->text[i]) {
407 case '=': equals++; break;
408 case '#': hashes++; break;
409 case '*': stars++; break;
410 case '-': minus++; break;
411 case '\\': other++; i++; break;
412 default: other++; break;
419 hashes + stars + minus + spaces + other == 0) ||
420 (text->text[offset] == '#' &&
421 text->text[offset+1] != '#')) {
423 SET_BIT(bits, IS_H1);
428 equals + hashes + stars + spaces + other == 0) ||
429 (text->text[offset] == '#' &&
430 text->text[offset+1] == '#')) {
432 SET_BIT(bits, IS_H2);
436 if((minus >= 3 && equals + hashes + stars + other == 0) ||
437 (stars >= 3 && equals + hashes + minus + other == 0)) {
439 SET_BIT(bits, IS_HR);
444 SET_BIT(bits, IS_EMPTY);
452 void markdown_debug(deck_t *deck, int debug) {
454 int sc = 0; // slide count
455 int lc = 0; // line count
461 fprintf(stderr, "headers: %i\nslides: %i\n", deck->headers, deck->slides);
463 } else if(debug > 1) {
465 // print header to STDERR
467 header = deck->header;
469 header->length > 0 &&
470 header->text->text[0] == '%') {
472 // skip descriptor word (e.g. %title:)
473 offset = next_blank(header->text, 0) + 1;
475 fprintf(stderr, "header: %s\n", &header->text->text[offset]);
476 header = header->next;
481 slide_t *slide = deck->slide;
484 // print slide/line count to STDERR
489 fprintf(stderr, " slide %i: %i lines\n", sc, slide->lines);
491 } else if(debug > 1) {
493 // also print bits and line length
494 fprintf(stderr, " slide %i:\n", sc);
499 fprintf(stderr, " line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
508 bool is_utf8(char ch) {
509 return (ch & 0x80) != 0x00;
512 int length_utf8(char ch) {
514 int i = 0; // increment
524 int next_nonblank(cstring_t *text, int i) {
525 while ((i < text->size) && isspace((unsigned char) (text->text)[i]))
531 int prev_blank(cstring_t *text, int i) {
532 while ((i > 0) && !isspace((unsigned char) (text->text)[i]))
538 int next_blank(cstring_t *text, int i) {
539 while ((i < text->size) && !isspace((unsigned char) (text->text)[i]))
545 int next_word(cstring_t *text, int i) {
546 return next_nonblank(text, next_blank(text, i));