fixed code indent issue, closes #96
[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 if(CHECK_BIT(bits, IS_TILDE_CODE) && CHECK_BIT(bits, IS_EMPTY)) {
93                 // remove tilde code markers
94                 (text->reset)(text);
95
96             } else {
97
98                 // if slide ! has line
99                 if(!slide->line || !line) {
100
101                     // create new line
102                     line = new_line();
103                     slide->line = line;
104                     lc = 1;
105
106                 } else {
107
108                     // create next line
109                     line = next_line(line);
110                     lc++;
111
112                 }
113
114                 // add text to line
115                 line->text = text;
116
117                 // add bits to line
118                 line->bits = bits;
119
120                 // calc offset
121                 line->offset = next_nonblank(text, 0);
122
123                 // adjust line length dynamicaly - excluding markup
124                 if(line->text->value)
125                     adjust_line_length(line);
126
127                 // new text
128                 text = cstring_init();
129             }
130
131         } else if(c == L'\t') {
132
133             // expand tab to spaces
134             for (i = 0;  i < EXPAND_TABS;  i++) {
135                 (text->expand)(text, L' ');
136             }
137
138         } else if(c == L'\\') {
139
140             // add char to line
141             (text->expand)(text, c);
142
143             // if !IS_CODE add next char to line
144             // and do not increase line count
145             if(next_nonblank(text, 0) < CODE_INDENT) {
146
147                 c = fgetwc(input);
148                 (text->expand)(text, c);
149             }
150
151         } else if(iswprint(c) || iswspace(c)) {
152
153             // add char to line
154             (text->expand)(text, c);
155         }
156     }
157     (text->delete)(text);
158
159     slide->lines = lc;
160     deck->slides = sc;
161
162     // detect header
163     line = deck->slide->line;
164     if(line && line->text->size > 0 && line->text->value[0] == L'%') {
165
166         // assign header to deck
167         deck->header = line;
168
169         // find first non-header line
170         while(line && line->text->size > 0 && line->text->value[0] == L'%') {
171             hc++;
172             line = line->next;
173         }
174
175         // only split header if any non-header line is found
176         if(line) {
177
178             // split linked list
179             line->prev->next = NULL;
180             line->prev = NULL;
181
182             // remove header lines from slide
183             deck->slide->line = line;
184
185             // adjust counts
186             deck->headers += hc;
187             deck->slide->lines -= hc;
188         } else {
189
190             // remove header from deck
191             deck->header = NULL;
192         }
193     }
194
195     slide = deck->slide;
196     while(slide) {
197         line = slide->line;
198
199         // ignore mdpress format attributes
200         if(line &&
201            slide->lines > 1 &&
202            !CHECK_BIT(line->bits, IS_EMPTY) &&
203            line->text->value[line->offset] == L'=' &&
204            line->text->value[line->offset + 1] == L' ') {
205
206             // remove line from linked list
207             slide->line = line->next;
208             line->next->prev = NULL;
209
210             // maintain loop condition
211             tmp = line;
212             line = line->next;
213
214             // adjust line count
215             slide->lines -= 1;
216
217             // delete line
218             (tmp->text->delete)(tmp->text);
219             free(tmp);
220         }
221
222         while(line) {
223             // combine underlined H1/H2 in single line
224             if((CHECK_BIT(line->bits, IS_H1) ||
225                 CHECK_BIT(line->bits, IS_H2)) &&
226                CHECK_BIT(line->bits, IS_EMPTY) &&
227                line->prev &&
228                !CHECK_BIT(line->prev->bits, IS_EMPTY)) {
229
230
231                 // remove line from linked list
232                 line->prev->next = line->next;
233                 if(line->next)
234                     line->next->prev = line->prev;
235
236                 // set bits on previous line
237                 if(CHECK_BIT(line->bits, IS_H1)) {
238                     SET_BIT(line->prev->bits, IS_H1);
239                 } else {
240                     SET_BIT(line->prev->bits, IS_H2);
241                 }
242
243                 // adjust line count
244                 slide->lines -= 1;
245
246                 // maintain loop condition
247                 tmp = line;
248                 line = line->prev;
249
250                 // delete line
251                 (tmp->text->delete)(tmp->text);
252                 free(tmp);
253
254             // pass enclosing flag IS_UNORDERED_LIST_3
255             // to nested levels for unordered lists
256             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
257                 tmp = line->next;
258                 line_t *list_last_level_3 = line;
259
260                 while(tmp &&
261                       CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
262                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
263                         list_last_level_3 = tmp;
264                     }
265                     tmp = tmp->next;
266                 }
267
268                 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
269                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
270                 }
271
272             // pass enclosing flag IS_UNORDERED_LIST_2
273             // to nested levels for unordered lists
274             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
275                 tmp = line->next;
276                 line_t *list_last_level_2 = line;
277
278                 while(tmp &&
279                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
280                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
281                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
282                         list_last_level_2 = tmp;
283                     }
284                     tmp = tmp->next;
285                 }
286
287                 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
288                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
289                 }
290
291             // pass enclosing flag IS_UNORDERED_LIST_1
292             // to nested levels for unordered lists
293             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
294                 tmp = line->next;
295                 line_t *list_last_level_1 = line;
296
297                 while(tmp &&
298                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
299                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
300                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
301                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
302                         list_last_level_1 = tmp;
303                     }
304                     tmp = tmp->next;
305                 }
306
307                 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
308                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
309                 }
310             }
311
312             line = line->next;
313         }
314         slide = slide->next;
315     }
316
317     return deck;
318 }
319
320 int markdown_analyse(cstring_t *text, int prev) {
321
322     // static variables can not be redeclaired, but changed outside of a declaration
323     // the program remembers their value on every function calls
324     static int unordered_list_level = 0;
325     static int unordered_list_level_offset[] = {-1, -1, -1, -1};
326     static int num_tilde_characters = 0;
327
328     int i = 0;      // increment
329     int bits = 0;   // markdown bits
330     int offset = 0; // text offset
331     int eol    = 0; // end of line
332
333     int equals = 0, hashes = 0,
334         stars  = 0, minus  = 0,
335         spaces = 0, other  = 0; // special character counts
336
337     const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
338
339     // return IS_EMPTY on null pointers
340     if(!text || !text->value) {
341         SET_BIT(bits, IS_EMPTY);
342         return bits;
343     }
344
345     // count leading spaces
346     offset = next_nonblank(text, 0);
347
348     // IS_TILDE_CODE
349     if (wcsncmp(text->value, L"~~~", 3) == 0) {
350         int tildes_in_line = next_nontilde(text, 0);
351         if (tildes_in_line >= num_tilde_characters) {
352             if (num_tilde_characters > 0) {
353                 num_tilde_characters = 0;
354             } else {
355                 num_tilde_characters = tildes_in_line;
356             }
357             SET_BIT(bits, IS_EMPTY);
358             SET_BIT(bits, IS_TILDE_CODE);
359             return bits;
360         }
361     }
362
363     if (num_tilde_characters > 0) {
364         SET_BIT(bits, IS_CODE);
365         SET_BIT(bits, IS_TILDE_CODE);
366         return bits;
367     }
368
369     // IS_STOP
370     if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
371        (!wcsncmp(&text->value[offset], L"<br>", 4) ||
372         !wcsncmp(&text->value[offset], L"<BR>", 4) ||
373         !wcsncmp(&text->value[offset], L"^", 1))) {
374         SET_BIT(bits, IS_STOP);
375         return bits;
376     }
377
378     // strip trailing spaces
379     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
380
381     // IS_UNORDERED_LIST_#
382     if(text->size >= offset + 2 &&
383        (text->value[offset] == L'*' || text->value[offset] == L'-') &&
384        iswspace(text->value[offset + 1])) {
385
386         // if different from last lines offset
387         if(offset != unordered_list_offset) {
388
389             // test if offset matches a lower indent level
390             for(i = unordered_list_level; i >= 0; i--) {
391                 if(unordered_list_level_offset[i] == offset) {
392                     unordered_list_level = i;
393                     break;
394                 }
395             }
396             // if offset doesn't match any previously stored indent level
397             if(i != unordered_list_level) {
398                 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
399                 // memorize the offset as next bigger indent level
400                 unordered_list_level_offset[unordered_list_level] = offset;
401             }
402         }
403
404         // if no previous indent level matches, this must be the first line of the list
405         if(unordered_list_level == 0) {
406             unordered_list_level = 1;
407             unordered_list_level_offset[1] = offset;
408         }
409
410         switch(unordered_list_level) {
411             case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
412             case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
413             case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
414             default: break;
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         // continue list if indent level is still the same as in previous line
423         if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) ||
424              CHECK_BIT(prev, IS_UNORDERED_LIST_2) ||
425              CHECK_BIT(prev, IS_UNORDERED_LIST_3)) &&
426             offset >= unordered_list_offset) {
427
428             switch(unordered_list_level) {
429                 case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
430                 case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
431                 case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
432                 default: break;
433             }
434
435             // this line extends the previous list item
436             SET_BIT(bits, IS_UNORDERED_LIST_EXT);
437
438         // or reset indent level
439         } else {
440             unordered_list_level = 0;
441         }
442     }
443
444     if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) &&
445        !CHECK_BIT(bits, IS_UNORDERED_LIST_2) &&
446        !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) {
447
448         // IS_CODE
449         if(offset >= CODE_INDENT &&
450            (CHECK_BIT(prev, IS_EMPTY) ||
451             CHECK_BIT(prev, IS_CODE)  ||
452             CHECK_BIT(prev, IS_STOP))) {
453             SET_BIT(bits, IS_CODE);
454
455         } else {
456
457             // IS_QUOTE
458             if(text->value[offset] == L'>') {
459                 SET_BIT(bits, IS_QUOTE);
460             }
461
462             // IS_CENTER
463             if(text->size >= offset + 3 &&
464                text->value[offset] == L'-' &&
465                text->value[offset + 1] == L'>' &&
466                iswspace(text->value[offset + 2])) {
467                 SET_BIT(bits, IS_CENTER);
468
469                 // remove start tag
470                 (text->strip)(text, offset, 3);
471                 eol -= 3;
472
473                 if(text->size >= offset + 3 &&
474                    text->value[eol - 1] == L'-' &&
475                    text->value[eol - 2] == L'<' &&
476                    iswspace(text->value[eol - 3])) {
477
478                     // remove end tags
479                     (text->strip)(text, eol - 3, 3);
480
481                     // adjust end of line
482                     for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
483
484                 }
485             }
486
487             for(i = offset; i < eol; i++) {
488
489                 if(iswspace(text->value[i])) {
490                     spaces++;
491
492                 } else {
493                     switch(text->value[i]) {
494                         case L'=': equals++;  break;
495                         case L'#': hashes++;  break;
496                         case L'*': stars++;   break;
497                         case L'-': minus++;   break;
498                         case L'\\': other++; i++; break;
499                         default:  other++;   break;
500                     }
501                 }
502             }
503
504             // IS_H1
505             if(equals > 0 &&
506                hashes + stars + minus + spaces + other == 0) {
507                 SET_BIT(bits, IS_H1);
508             }
509             if(text->value[offset] == L'#' &&
510                iswspace(text->value[offset+1])) {
511                 SET_BIT(bits, IS_H1);
512                 SET_BIT(bits, IS_H1_ATX);
513             }
514
515             // IS_H2
516             if(minus > 0 &&
517                equals + hashes + stars + spaces + other == 0) {
518                 SET_BIT(bits, IS_H2);
519             }
520             if(text->value[offset] == L'#' &&
521                text->value[offset+1] == L'#' &&
522                iswspace(text->value[offset+2])) {
523                 SET_BIT(bits, IS_H2);
524                 SET_BIT(bits, IS_H2_ATX);
525             }
526
527             // IS_HR
528             if((minus >= 3 && equals + hashes + stars + other == 0) ||
529                (stars >= 3 && equals + hashes + minus + other == 0)) {
530
531                 SET_BIT(bits, IS_HR);
532             }
533
534             // IS_EMPTY
535             if(other == 0) {
536                 SET_BIT(bits, IS_EMPTY);
537             }
538         }
539     }
540
541     return bits;
542 }
543
544 void markdown_debug(deck_t *deck, int debug) {
545
546     int sc = 0; // slide count
547     int lc = 0; // line count
548
549     int offset;
550     line_t *header;
551
552     if(debug == 1) {
553         fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides);
554
555     } else if(debug > 1) {
556
557         // print header to STDERR
558         if(deck->header) {
559             header = deck->header;
560             while(header &&
561                 header->length > 0 &&
562                 header->text->value[0] == L'%') {
563
564                 // skip descriptor word (e.g. %title:)
565                 offset = next_blank(header->text, 0) + 1;
566
567                 fwprintf(stderr, L"header: %S\n", &header->text->value[offset]);
568                 header = header->next;
569             }
570         }
571     }
572
573     slide_t *slide = deck->slide;
574     line_t *line;
575
576     // print slide/line count to STDERR
577     while(slide) {
578         sc++;
579
580         if(debug == 1) {
581             fwprintf(stderr, L"  slide %i: %i lines\n", sc, slide->lines);
582
583         } else if(debug > 1) {
584
585             // also print bits and line length
586             fwprintf(stderr, L"  slide %i:\n", sc);
587             line = slide->line;
588             lc = 0;
589             while(line) {
590                 lc++;
591                 fwprintf(stderr, L"    line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
592                 line = line->next;
593             }
594         }
595
596         slide = slide->next;
597     }
598 }
599
600 void adjust_line_length(line_t *line) {
601     int l = 0;
602     const static wchar_t *special = L"\\*_`"; // list of interpreted chars
603     const wchar_t *c = &line->text->value[0];
604     cstack_t *stack = cstack_init();
605
606     // for each char in line
607     for(; *c; c++) {
608         // if char is in special char list
609         if(wcschr(special, *c)) {
610
611             // closing special char (or second backslash)
612             if((stack->top)(stack, *c)) {
613                 if(*c == L'\\') l++;
614                 (stack->pop)(stack);
615
616             // treat special as regular char
617             } else if((stack->top)(stack, L'\\')) {
618                 l++;
619                 (stack->pop)(stack);
620
621             // opening special char
622             } else {
623                 (stack->push)(stack, *c);
624             }
625
626         } else {
627             // remove backslash from stack
628             if((stack->top)(stack, L'\\'))
629                 (stack->pop)(stack);
630             l++;
631         }
632     }
633
634     if(CHECK_BIT(line->bits, IS_H1_ATX))
635         l -= 2;
636     if(CHECK_BIT(line->bits, IS_H2_ATX))
637         l -= 3;
638
639     line->length = l;
640
641     (stack->delete)(stack);
642 }
643
644 int next_nonblank(cstring_t *text, int i) {
645     while ((i < text->size) && iswspace((text->value)[i]))
646         i++;
647
648     return i;
649 }
650
651 int prev_blank(cstring_t *text, int i) {
652     while ((i > 0) && !iswspace((text->value)[i]))
653         i--;
654
655     return i;
656 }
657
658 int next_blank(cstring_t *text, int i) {
659     while ((i < text->size) && !iswspace((text->value)[i]))
660         i++;
661
662     return i;
663 }
664
665 int next_word(cstring_t *text, int i) {
666     return next_nonblank(text, next_blank(text, i));
667 }
668
669 int next_nontilde(cstring_t *text, int i) {
670     while ((i < text->size) && text->value[i] == L'~')
671         i++;
672
673     return i;
674 }
675