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);
228 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
230 line_t *list_last_level_3 = line;
233 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
234 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
235 list_last_level_3 = tmp;
240 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
241 SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
243 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
245 line_t *list_last_level_2 = line;
248 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
249 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
250 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
251 list_last_level_2 = tmp;
256 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
257 SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
259 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
261 line_t *list_last_level_1 = line;
264 (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
265 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
266 CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
267 if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
268 list_last_level_1 = tmp;
273 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
274 SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
286 int markdown_analyse(cstring_t *text) {
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 // count leading spaces
303 offset = next_nonblank(text, 0);
305 // strip trailing spaces
306 for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
308 // IS_UNORDERED_LIST_#
309 if(text->size >= offset + 2 &&
310 (text->text[offset] == '*' || text->text[offset] == '-') &&
311 text->text[offset + 1] == ' ') {
313 for(i = offset; i<eol; i++) {
314 if(text->text[i] != '*' &&
315 text->text[i] != '-' &&
316 text->text[i] != ' ') {
317 if(offset > unordered_list_offset + CODE_INDENT) {
318 SET_BIT(bits, IS_CODE);
319 } else if(offset != unordered_list_offset) {
320 for(i = unordered_list_level; i >= 0; i--) {
321 if(unordered_list_level_offset[i] == offset) {
322 unordered_list_level = i;
326 if(i != unordered_list_level) {
327 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
328 unordered_list_level_offset[unordered_list_level] = offset;
332 if(unordered_list_level == 0) {
333 unordered_list_level = 1;
334 unordered_list_level_offset[1] = offset;
337 switch(unordered_list_level) {
338 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
339 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
340 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
349 if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
350 !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
351 !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
353 unordered_list_level = 0;
356 if(offset >= CODE_INDENT) {
357 SET_BIT(bits, IS_CODE);
361 for(i = offset; i < eol; i++) {
363 if(text->text[i] == ' ') {
366 } else if(CHECK_BIT(bits, IS_CODE)) {
370 switch(text->text[i]) {
371 case '=': equals++; break;
372 case '#': hashes++; break;
373 case '*': stars++; break;
374 case '-': minus++; break;
375 case '\\': other++; i++; break;
376 default: other++; break;
383 hashes + stars + minus + spaces + other == 0) ||
386 text->text[offset] == '#' &&
387 text->text[offset+1] != '#')) {
389 SET_BIT(bits, IS_H1);
394 equals + hashes + stars + spaces + other == 0) ||
397 text->text[offset] == '#' &&
398 text->text[offset+1] == '#')) {
400 SET_BIT(bits, IS_H2);
406 text->text[offset] == '>') {
408 SET_BIT(bits, IS_QUOTE);
412 if((minus >= 3 && equals + hashes + stars + other == 0) ||
413 (stars >= 3 && equals + hashes + minus + other == 0)) {
415 SET_BIT(bits, IS_HR);
420 SET_BIT(bits, IS_EMPTY);
428 void markdown_debug(deck_t *deck, int debug) {
430 int sc = 0; // slide count
431 int lc = 0; // line count
437 fprintf(stderr, "headers: %i\nslides: %i\n", deck->headers, deck->slides);
439 } else if(debug > 1) {
441 // print header to STDERR
443 header = deck->header;
445 header->length > 0 &&
446 header->text->text[0] == '%') {
448 // skip descriptor word (e.g. %title:)
449 offset = next_blank(header->text, 0) + 1;
451 fprintf(stderr, "header: %s\n", &header->text->text[offset]);
452 header = header->next;
457 slide_t *slide = deck->slide;
460 // print slide/line count to STDERR
465 fprintf(stderr, " slide %i: %i lines\n", sc, slide->lines);
467 } else if(debug > 1) {
469 // also print bits and line length
470 fprintf(stderr, " slide %i:\n", sc);
475 fprintf(stderr, " line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
484 int is_utf8(char ch) {
488 int length_utf8(char ch) {
490 int i = 0; // increment
500 int next_nonblank(cstring_t *text, int i) {
501 while ((i < text->size) && isspace((unsigned char) (text->text)[i]))
507 int prev_blank(cstring_t *text, int i) {
508 while ((i > 0) && !isspace((unsigned char) (text->text)[i]))
514 int next_blank(cstring_t *text, int i) {
515 while ((i < text->size) && !isspace((unsigned char) (text->text)[i]))
521 int next_word(cstring_t *text, int i) {
522 return next_nonblank(text, next_blank(text, i));