added support for interupting slides line by line, #45, #75
[smdp.git] / src / parser.c
1 /*
2  * Functions necessary to parse a file and transform its content into
3  * a deck of slides containing lines. All based on markdown formating
4  * rules.
5  * Copyright (C) 2014 Michael Goehler
6  *
7  * This file is part of mdp.
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  */
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <wchar.h>
29 #include <wctype.h>
30 #include <string.h>
31
32 #include "parser.h"
33
34 deck_t *markdown_load(FILE *input) {
35
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
43
44     deck_t *deck = new_deck();
45     slide_t *slide = deck->slide;
46     line_t *line = NULL;
47     line_t *tmp = NULL;
48     cstring_t *text = cstring_init();
49
50     // initialize bits as empty line
51     SET_BIT(bits, IS_EMPTY);
52
53     while ((c = fgetwc(input)) != WEOF) {
54         if (ferror(input)) {
55             fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
56             exit(EXIT_FAILURE);
57         }
58
59         if(c == L'\n') {
60
61             // markdown analyse
62             prev = bits;
63             bits = markdown_analyse(text, prev);
64
65             // if first line in file is markdown hr
66             if(!line && CHECK_BIT(bits, IS_HR)) {
67
68                 // clear text
69                 (text->reset)(text);
70
71             } else if(line && CHECK_BIT(bits, IS_STOP)) {
72
73                 // set stop bit on last line
74                 SET_BIT(line->bits, IS_STOP);
75
76                 // clear text
77                 (text->reset)(text);
78
79             // if text is markdown hr
80             } else if(CHECK_BIT(bits, IS_HR) &&
81                       CHECK_BIT(line->bits, IS_EMPTY)) {
82
83                 slide->lines = lc;
84
85                 // clear text
86                 (text->reset)(text);
87
88                 // create next slide
89                 slide = next_slide(slide);
90                 sc++;
91
92             } else {
93
94                 // if slide ! has line
95                 if(!slide->line || !line) {
96
97                     // create new line
98                     line = new_line();
99                     slide->line = line;
100                     lc = 1;
101
102                 } else {
103
104                     // create next line
105                     line = next_line(line);
106                     lc++;
107
108                 }
109
110                 // add text to line
111                 line->text = text;
112
113                 // add bits to line
114                 line->bits = bits;
115
116                 // calc offset
117                 line->offset = next_nonblank(text, 0);
118
119                 // adjust line length dynamicaly - excluding markup
120                 if(line->text->value)
121                     adjust_line_length(line);
122
123                 // new text
124                 text = cstring_init();
125             }
126
127         } else if(c == L'\t') {
128
129             // expand tab to spaces
130             for (i = 0;  i < EXPAND_TABS;  i++) {
131                 (text->expand)(text, L' ');
132             }
133
134         } else if(c == L'\\') {
135
136             // add char to line
137             (text->expand)(text, c);
138
139             // if !IS_CODE add next char to line
140             // and do not increase line count
141             if(next_nonblank(text, 0) < CODE_INDENT) {
142
143                 c = fgetwc(input);
144                 (text->expand)(text, c);
145             }
146
147         } else if(iswprint(c) || iswspace(c)) {
148
149             // add char to line
150             (text->expand)(text, c);
151         }
152     }
153     (text->delete)(text);
154
155     slide->lines = lc;
156     deck->slides = sc;
157
158     // detect header
159     line = deck->slide->line;
160     if(line && line->text->size > 0 && line->text->value[0] == L'%') {
161
162         // assign header to deck
163         deck->header = line;
164
165         // find first non-header line
166         while(line && line->text->size > 0 && line->text->value[0] == L'%') {
167             hc++;
168             line = line->next;
169         }
170
171         // only split header if any non-header line is found
172         if(line) {
173
174             // split linked list
175             line->prev->next = NULL;
176             line->prev = NULL;
177
178             // remove header lines from slide
179             deck->slide->line = line;
180
181             // adjust counts
182             deck->headers += hc;
183             deck->slide->lines -= hc;
184         } else {
185
186             // remove header from deck
187             deck->header = NULL;
188         }
189     }
190
191     slide = deck->slide;
192     while(slide) {
193         line = slide->line;
194         while(line) {
195             // combine underlined H1/H2 in single line
196             if((CHECK_BIT(line->bits, IS_H1) ||
197                 CHECK_BIT(line->bits, IS_H2)) &&
198                CHECK_BIT(line->bits, IS_EMPTY) &&
199                line->prev &&
200                !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
201
202
203                 // remove line from linked list
204                 line->prev->next = line->next;
205                 if(line->next)
206                     line->next->prev = line->prev;
207
208                 // set bits on previous line
209                 if(CHECK_BIT(line->bits, IS_H1)) {
210                     SET_BIT(line->prev->bits, IS_H1);
211                 } else {
212                     SET_BIT(line->prev->bits, IS_H2);
213                 }
214
215                 // adjust line count
216                 slide->lines -= 1;
217
218                 // maintain loop condition
219                 tmp = line;
220                 line = line->prev;
221
222                 // delete line
223                 (tmp->text->delete)(tmp->text);
224                 free(tmp);
225
226             // pass enclosing flag IS_UNORDERED_LIST_3
227             // to nested levels for unordered lists
228             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
229                 tmp = line->next;
230                 line_t *list_last_level_3 = line;
231
232                 while(tmp &&
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;
236                     }
237                     tmp = tmp->next;
238                 }
239
240                 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
241                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
242                 }
243
244             // pass enclosing flag IS_UNORDERED_LIST_2
245             // to nested levels for unordered lists
246             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
247                 tmp = line->next;
248                 line_t *list_last_level_2 = line;
249
250                 while(tmp &&
251                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
252                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
253                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
254                         list_last_level_2 = tmp;
255                     }
256                     tmp = tmp->next;
257                 }
258
259                 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
260                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
261                 }
262
263             // pass enclosing flag IS_UNORDERED_LIST_1
264             // to nested levels for unordered lists
265             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
266                 tmp = line->next;
267                 line_t *list_last_level_1 = line;
268
269                 while(tmp &&
270                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
271                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
272                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
273                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
274                         list_last_level_1 = tmp;
275                     }
276                     tmp = tmp->next;
277                 }
278
279                 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
280                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
281                 }
282             }
283
284             line = line->next;
285         }
286         slide = slide->next;
287     }
288
289     return deck;
290 }
291
292 int markdown_analyse(cstring_t *text, int prev) {
293
294     // static variables can not be redeclaired, but changed outside of a declaration
295     // the program remembers their value on every function calls
296     static int unordered_list_level = 0;
297     static int unordered_list_level_offset[] = {-1, -1, -1, -1};
298
299     int i = 0;      // increment
300     int bits = 0;   // markdown bits
301     int offset = 0; // text offset
302     int eol    = 0; // end of line
303
304     int equals = 0, hashes = 0,
305         stars  = 0, minus  = 0,
306         spaces = 0, other  = 0; // special character counts
307
308     const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
309
310     // return IS_EMPTY on null pointers
311     if(!text || !text->value) {
312         SET_BIT(bits, IS_EMPTY);
313         return bits;
314     }
315
316     // count leading spaces
317     offset = next_nonblank(text, 0);
318
319     // IS_STOP
320     if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
321        (!wcsncmp(&text->value[offset], L"<br>", 4) ||
322         !wcsncmp(&text->value[offset], L"<BR>", 4) ||
323         !wcsncmp(&text->value[offset], L"^", 1))) {
324         SET_BIT(bits, IS_STOP);
325         return bits;
326     }
327
328     // strip trailing spaces
329     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
330
331     // IS_UNORDERED_LIST_#
332     if(text->size >= offset + 2 &&
333        (text->value[offset] == L'*' || text->value[offset] == L'-') &&
334        iswspace(text->value[offset + 1])) {
335
336         // if different from last lines offset
337         if(offset != unordered_list_offset) {
338
339             // test if offset matches a lower indent level
340             for(i = unordered_list_level; i >= 0; i--) {
341                 if(unordered_list_level_offset[i] == offset) {
342                     unordered_list_level = i;
343                     break;
344                 }
345             }
346             // if offset doesn't match any previously stored indent level
347             if(i != unordered_list_level) {
348                 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
349                 // memorize the offset as next bigger indent level
350                 unordered_list_level_offset[unordered_list_level] = offset;
351             }
352         }
353
354         // if no previous indent level matches, this must be the first line of the list
355         if(unordered_list_level == 0) {
356             unordered_list_level = 1;
357             unordered_list_level_offset[1] = offset;
358         }
359
360         switch(unordered_list_level) {
361             case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
362             case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
363             case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
364             default: break;
365         }
366     }
367
368     if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
369        !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
370        !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
371
372         // continue list if indent level is still the same as in previous line
373         if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
374              CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
375              CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
376             offset >= unordered_list_offset) {
377
378             switch(unordered_list_level) {
379                 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
380                 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
381                 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
382                 default: break;
383             }
384
385             // this line extends the previous list item
386             SET_BIT(bits, IS_UNORDERED_LIST_EXT);
387
388         // or reset indent level
389         } else {
390             unordered_list_level = 0;
391         }
392     }
393
394     if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
395        !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
396        !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
397
398         // IS_CODE
399         if(offset >= CODE_INDENT &&
400            (CHECK_BIT(prev, IS_EMPTY) ||
401             CHECK_BIT(prev, IS_CODE)  ||
402             CHECK_BIT(prev, IS_STOP))) {
403             SET_BIT(bits, IS_CODE);
404
405         } else {
406
407             // IS_QUOTE
408             if(text->value[offset] == L'>') {
409                 SET_BIT(bits, IS_QUOTE);
410             }
411
412             // IS_CENTER
413             if(text->size >= offset + 3 &&
414                text->value[offset] == L'-' &&
415                text->value[offset + 1] == L'>' &&
416                iswspace(text->value[offset + 2])) {
417                 SET_BIT(bits, IS_CENTER);
418
419                 // remove start tag
420                 (text->strip)(text, offset, 3);
421                 eol -= 3;
422
423                 if(text->size >= offset + 3 &&
424                    text->value[eol - 1] == L'-' &&
425                    text->value[eol - 2] == L'<' &&
426                    iswspace(text->value[eol - 3])) {
427
428                     // remove end tags
429                     (text->strip)(text, eol - 3, 3);
430
431                     // adjust end of line
432                     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
433
434                 }
435             }
436
437             for(i = offset; i < eol; i++) {
438
439                 if(iswspace(text->value[i])) {
440                     spaces++;
441
442                 } else {
443                     switch(text->value[i]) {
444                         case L'=': equals++;  break;
445                         case L'#': hashes++;  break;
446                         case L'*': stars++;   break;
447                         case L'-': minus++;   break;
448                         case L'\\': other++; i++; break;
449                         default:  other++;   break;
450                     }
451                 }
452             }
453
454             // IS_H1
455             if(equals > 0 &&
456                hashes + stars + minus + spaces + other == 0) {
457                 SET_BIT(bits, IS_H1);
458             }
459             if(text->value[offset] == L'#' &&
460                iswspace(text->value[offset+1])) {
461                 SET_BIT(bits, IS_H1);
462                 SET_BIT(bits, IS_H1_ATX);
463             }
464
465             // IS_H2
466             if(minus > 0 &&
467                equals + hashes + stars + spaces + other == 0) {
468                 SET_BIT(bits, IS_H2);
469             }
470             if(text->value[offset] == L'#' &&
471                text->value[offset+1] == L'#' &&
472                iswspace(text->value[offset+2])) {
473                 SET_BIT(bits, IS_H2);
474                 SET_BIT(bits, IS_H2_ATX);
475             }
476
477             // IS_HR
478             if((minus >= 3 && equals + hashes + stars + other == 0) ||
479                (stars >= 3 && equals + hashes + minus + other == 0)) {
480
481                 SET_BIT(bits, IS_HR);
482             }
483
484             // IS_EMPTY
485             if(other == 0) {
486                 SET_BIT(bits, IS_EMPTY);
487             }
488         }
489     }
490
491     return bits;
492 }
493
494 void markdown_debug(deck_t *deck, int debug) {
495
496     int sc = 0; // slide count
497     int lc = 0; // line count
498
499     int offset;
500     line_t *header;
501
502     if(debug == 1) {
503         fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
504
505     } else if(debug > 1) {
506
507         // print header to STDERR
508         if(deck->header) {
509             header = deck->header;
510             while(header &&
511                 header->length > 0 &&
512                 header->text->value[0] == L'%') {
513
514                 // skip descriptor word (e.g. %title:)
515                 offset = next_blank(header->text, 0) + 1;
516
517                 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
518                 header = header->next;
519             }
520         }
521     }
522
523     slide_t *slide = deck->slide;
524     line_t *line;
525
526     // print slide/line count to STDERR
527     while(slide) {
528         sc++;
529
530         if(debug == 1) {
531             fwprintf(stderr, L"  slide %i: %i lines\n", sc, slide->lines);
532
533         } else if(debug > 1) {
534
535             // also print bits and line length
536             fwprintf(stderr, L"  slide %i:\n", sc);
537             line = slide->line;
538             lc = 0;
539             while(line) {
540                 lc++;
541                 fwprintf(stderr, L"    line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
542                 line = line->next;
543             }
544         }
545
546         slide = slide->next;
547     }
548 }
549
550 void adjust_line_length(line_t *line) {
551     int l = 0;
552     const static wchar_t *special = L"\\*_`"; // list of interpreted chars
553     const wchar_t *c = &line->text->value[line->offset];
554     cstack_t *stack = cstack_init();
555
556     // for each char in line
557     for(; *c; c++) {
558         // if char is in special char list
559         if(wcschr(special, *c)) {
560
561             // closing special char (or second backslash)
562             if((stack->top)(stack, *c)) {
563                 if(*c == L'\\') l++;
564                 (stack->pop)(stack);
565
566             // treat special as regular char
567             } else if((stack->top)(stack, L'\\')) {
568                 l++;
569                 (stack->pop)(stack);
570
571             // opening special char
572             } else {
573                 (stack->push)(stack, *c);
574             }
575
576         } else {
577             // remove backslash from stack
578             if((stack->top)(stack, L'\\'))
579                 (stack->pop)(stack);
580             l++;
581         }
582     }
583
584     if(CHECK_BIT(line->bits, IS_H1_ATX))
585         l -= 2;
586     if(CHECK_BIT(line->bits, IS_H2_ATX))
587         l -= 3;
588
589     line->length = l;
590
591     (stack->delete)(stack);
592 }
593
594 int next_nonblank(cstring_t *text, int i) {
595     while ((i < text->size) && iswspace((text->value)[i]))
596         i++;
597
598     return i;
599 }
600
601 int prev_blank(cstring_t *text, int i) {
602     while ((i > 0) && !iswspace((text->value)[i]))
603         i--;
604
605     return i;
606 }
607
608 int next_blank(cstring_t *text, int i) {
609     while ((i < text->size) && !iswspace((text->value)[i]))
610         i++;
611
612     return i;
613 }
614
615 int next_word(cstring_t *text, int i) {
616     return next_nonblank(text, next_blank(text, i));
617 }