ignore mdpress format attributes, closes #88
[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) 2015 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
195         // ignore mdpress format attributes
196         if(line &&
197            slide->lines > 1 &&
198            !CHECK_BIT(line->bits, IS_EMPTY) &&
199            line->text->value[line->offset] == L'=' &&
200            line->text->value[line->offset + 1] == L' ') {
201
202             // remove line from linked list
203             slide->line = line->next;
204             line->next->prev = NULL;
205
206             // maintain loop condition
207             tmp = line;
208             line = line->next;
209
210             // adjust line count
211             slide->lines -= 1;
212
213             // delete line
214             (tmp->text->delete)(tmp->text);
215             free(tmp);
216         }
217
218         while(line) {
219             // combine underlined H1/H2 in single line
220             if((CHECK_BIT(line->bits, IS_H1) ||
221                 CHECK_BIT(line->bits, IS_H2)) &&
222                CHECK_BIT(line->bits, IS_EMPTY) &&
223                line->prev &&
224                !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
225
226
227                 // remove line from linked list
228                 line->prev->next = line->next;
229                 if(line->next)
230                     line->next->prev = line->prev;
231
232                 // set bits on previous line
233                 if(CHECK_BIT(line->bits, IS_H1)) {
234                     SET_BIT(line->prev->bits, IS_H1);
235                 } else {
236                     SET_BIT(line->prev->bits, IS_H2);
237                 }
238
239                 // adjust line count
240                 slide->lines -= 1;
241
242                 // maintain loop condition
243                 tmp = line;
244                 line = line->prev;
245
246                 // delete line
247                 (tmp->text->delete)(tmp->text);
248                 free(tmp);
249
250             // pass enclosing flag IS_UNORDERED_LIST_3
251             // to nested levels for unordered lists
252             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
253                 tmp = line->next;
254                 line_t *list_last_level_3 = line;
255
256                 while(tmp &&
257                       CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
258                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
259                         list_last_level_3 = tmp;
260                     }
261                     tmp = tmp->next;
262                 }
263
264                 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
265                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
266                 }
267
268             // pass enclosing flag IS_UNORDERED_LIST_2
269             // to nested levels for unordered lists
270             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
271                 tmp = line->next;
272                 line_t *list_last_level_2 = line;
273
274                 while(tmp &&
275                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
276                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
277                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
278                         list_last_level_2 = tmp;
279                     }
280                     tmp = tmp->next;
281                 }
282
283                 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
284                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
285                 }
286
287             // pass enclosing flag IS_UNORDERED_LIST_1
288             // to nested levels for unordered lists
289             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
290                 tmp = line->next;
291                 line_t *list_last_level_1 = line;
292
293                 while(tmp &&
294                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
295                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
296                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
297                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
298                         list_last_level_1 = tmp;
299                     }
300                     tmp = tmp->next;
301                 }
302
303                 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
304                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
305                 }
306             }
307
308             line = line->next;
309         }
310         slide = slide->next;
311     }
312
313     return deck;
314 }
315
316 int markdown_analyse(cstring_t *text, int prev) {
317
318     // static variables can not be redeclaired, but changed outside of a declaration
319     // the program remembers their value on every function calls
320     static int unordered_list_level = 0;
321     static int unordered_list_level_offset[] = {-1, -1, -1, -1};
322
323     int i = 0;      // increment
324     int bits = 0;   // markdown bits
325     int offset = 0; // text offset
326     int eol    = 0; // end of line
327
328     int equals = 0, hashes = 0,
329         stars  = 0, minus  = 0,
330         spaces = 0, other  = 0; // special character counts
331
332     const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
333
334     // return IS_EMPTY on null pointers
335     if(!text || !text->value) {
336         SET_BIT(bits, IS_EMPTY);
337         return bits;
338     }
339
340     // count leading spaces
341     offset = next_nonblank(text, 0);
342
343     // IS_STOP
344     if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
345        (!wcsncmp(&text->value[offset], L"<br>", 4) ||
346         !wcsncmp(&text->value[offset], L"<BR>", 4) ||
347         !wcsncmp(&text->value[offset], L"^", 1))) {
348         SET_BIT(bits, IS_STOP);
349         return bits;
350     }
351
352     // strip trailing spaces
353     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
354
355     // IS_UNORDERED_LIST_#
356     if(text->size >= offset + 2 &&
357        (text->value[offset] == L'*' || text->value[offset] == L'-') &&
358        iswspace(text->value[offset + 1])) {
359
360         // if different from last lines offset
361         if(offset != unordered_list_offset) {
362
363             // test if offset matches a lower indent level
364             for(i = unordered_list_level; i >= 0; i--) {
365                 if(unordered_list_level_offset[i] == offset) {
366                     unordered_list_level = i;
367                     break;
368                 }
369             }
370             // if offset doesn't match any previously stored indent level
371             if(i != unordered_list_level) {
372                 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
373                 // memorize the offset as next bigger indent level
374                 unordered_list_level_offset[unordered_list_level] = offset;
375             }
376         }
377
378         // if no previous indent level matches, this must be the first line of the list
379         if(unordered_list_level == 0) {
380             unordered_list_level = 1;
381             unordered_list_level_offset[1] = offset;
382         }
383
384         switch(unordered_list_level) {
385             case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
386             case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
387             case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
388             default: break;
389         }
390     }
391
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)) {
395
396         // continue list if indent level is still the same as in previous line
397         if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
398              CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
399              CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
400             offset >= unordered_list_offset) {
401
402             switch(unordered_list_level) {
403                 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
404                 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
405                 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
406                 default: break;
407             }
408
409             // this line extends the previous list item
410             SET_BIT(bits, IS_UNORDERED_LIST_EXT);
411
412         // or reset indent level
413         } else {
414             unordered_list_level = 0;
415         }
416     }
417
418     if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
419        !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
420        !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
421
422         // IS_CODE
423         if(offset >= CODE_INDENT &&
424            (CHECK_BIT(prev, IS_EMPTY) ||
425             CHECK_BIT(prev, IS_CODE)  ||
426             CHECK_BIT(prev, IS_STOP))) {
427             SET_BIT(bits, IS_CODE);
428
429         } else {
430
431             // IS_QUOTE
432             if(text->value[offset] == L'>') {
433                 SET_BIT(bits, IS_QUOTE);
434             }
435
436             // IS_CENTER
437             if(text->size >= offset + 3 &&
438                text->value[offset] == L'-' &&
439                text->value[offset + 1] == L'>' &&
440                iswspace(text->value[offset + 2])) {
441                 SET_BIT(bits, IS_CENTER);
442
443                 // remove start tag
444                 (text->strip)(text, offset, 3);
445                 eol -= 3;
446
447                 if(text->size >= offset + 3 &&
448                    text->value[eol - 1] == L'-' &&
449                    text->value[eol - 2] == L'<' &&
450                    iswspace(text->value[eol - 3])) {
451
452                     // remove end tags
453                     (text->strip)(text, eol - 3, 3);
454
455                     // adjust end of line
456                     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
457
458                 }
459             }
460
461             for(i = offset; i < eol; i++) {
462
463                 if(iswspace(text->value[i])) {
464                     spaces++;
465
466                 } else {
467                     switch(text->value[i]) {
468                         case L'=': equals++;  break;
469                         case L'#': hashes++;  break;
470                         case L'*': stars++;   break;
471                         case L'-': minus++;   break;
472                         case L'\\': other++; i++; break;
473                         default:  other++;   break;
474                     }
475                 }
476             }
477
478             // IS_H1
479             if(equals > 0 &&
480                hashes + stars + minus + spaces + other == 0) {
481                 SET_BIT(bits, IS_H1);
482             }
483             if(text->value[offset] == L'#' &&
484                iswspace(text->value[offset+1])) {
485                 SET_BIT(bits, IS_H1);
486                 SET_BIT(bits, IS_H1_ATX);
487             }
488
489             // IS_H2
490             if(minus > 0 &&
491                equals + hashes + stars + spaces + other == 0) {
492                 SET_BIT(bits, IS_H2);
493             }
494             if(text->value[offset] == L'#' &&
495                text->value[offset+1] == L'#' &&
496                iswspace(text->value[offset+2])) {
497                 SET_BIT(bits, IS_H2);
498                 SET_BIT(bits, IS_H2_ATX);
499             }
500
501             // IS_HR
502             if((minus >= 3 && equals + hashes + stars + other == 0) ||
503                (stars >= 3 && equals + hashes + minus + other == 0)) {
504
505                 SET_BIT(bits, IS_HR);
506             }
507
508             // IS_EMPTY
509             if(other == 0) {
510                 SET_BIT(bits, IS_EMPTY);
511             }
512         }
513     }
514
515     return bits;
516 }
517
518 void markdown_debug(deck_t *deck, int debug) {
519
520     int sc = 0; // slide count
521     int lc = 0; // line count
522
523     int offset;
524     line_t *header;
525
526     if(debug == 1) {
527         fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
528
529     } else if(debug > 1) {
530
531         // print header to STDERR
532         if(deck->header) {
533             header = deck->header;
534             while(header &&
535                 header->length > 0 &&
536                 header->text->value[0] == L'%') {
537
538                 // skip descriptor word (e.g. %title:)
539                 offset = next_blank(header->text, 0) + 1;
540
541                 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
542                 header = header->next;
543             }
544         }
545     }
546
547     slide_t *slide = deck->slide;
548     line_t *line;
549
550     // print slide/line count to STDERR
551     while(slide) {
552         sc++;
553
554         if(debug == 1) {
555             fwprintf(stderr, L"  slide %i: %i lines\n", sc, slide->lines);
556
557         } else if(debug > 1) {
558
559             // also print bits and line length
560             fwprintf(stderr, L"  slide %i:\n", sc);
561             line = slide->line;
562             lc = 0;
563             while(line) {
564                 lc++;
565                 fwprintf(stderr, L"    line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
566                 line = line->next;
567             }
568         }
569
570         slide = slide->next;
571     }
572 }
573
574 void adjust_line_length(line_t *line) {
575     int l = 0;
576     const static wchar_t *special = L"\\*_`"; // list of interpreted chars
577     const wchar_t *c = &line->text->value[line->offset];
578     cstack_t *stack = cstack_init();
579
580     // for each char in line
581     for(; *c; c++) {
582         // if char is in special char list
583         if(wcschr(special, *c)) {
584
585             // closing special char (or second backslash)
586             if((stack->top)(stack, *c)) {
587                 if(*c == L'\\') l++;
588                 (stack->pop)(stack);
589
590             // treat special as regular char
591             } else if((stack->top)(stack, L'\\')) {
592                 l++;
593                 (stack->pop)(stack);
594
595             // opening special char
596             } else {
597                 (stack->push)(stack, *c);
598             }
599
600         } else {
601             // remove backslash from stack
602             if((stack->top)(stack, L'\\'))
603                 (stack->pop)(stack);
604             l++;
605         }
606     }
607
608     if(CHECK_BIT(line->bits, IS_H1_ATX))
609         l -= 2;
610     if(CHECK_BIT(line->bits, IS_H2_ATX))
611         l -= 3;
612
613     line->length = l;
614
615     (stack->delete)(stack);
616 }
617
618 int next_nonblank(cstring_t *text, int i) {
619     while ((i < text->size) && iswspace((text->value)[i]))
620         i++;
621
622     return i;
623 }
624
625 int prev_blank(cstring_t *text, int i) {
626     while ((i > 0) && !iswspace((text->value)[i]))
627         i--;
628
629     return i;
630 }
631
632 int next_blank(cstring_t *text, int i) {
633     while ((i < text->size) && !iswspace((text->value)[i]))
634         i++;
635
636     return i;
637 }
638
639 int next_word(cstring_t *text, int i) {
640     return next_nonblank(text, next_blank(text, i));
641 }