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