Bugfix: set the right bits for unordered list.
[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 <stdio.h>
26 #include <stdlib.h>
27
28 #include "parser.h"
29
30 deck_t *markdown_load(FILE *input) {
31
32     int c = 0;    // char
33     int i = 0;    // increment
34     int l = 0;    // line length
35     int hc = 0;   // header count
36     int lc = 0;   // line count
37     int sc = 0;   // slide count
38     int bits = 0; // markdown bits
39
40     deck_t *deck = new_deck();
41     slide_t *slide = new_slide();
42     line_t *line = NULL;
43     line_t *tmp = NULL;
44     cstring_t *text = cstring_init();
45
46     // assign first slide to deck
47     deck->slide = slide;
48     sc++;
49
50     while ((c = fgetc(input)) != EOF) {
51         if(c == '\n') {
52
53             // markdown analyse
54             bits = markdown_analyse(text);
55
56             // if first line in file is markdown hr
57             if(!line && CHECK_BIT(bits, IS_HR)) {
58
59                 // clear text
60                 (text->reset)(text);
61
62             // if text is markdown hr
63             } else if(CHECK_BIT(bits, IS_HR) &&
64                       CHECK_BIT(line->bits, IS_EMPTY)) {
65
66                 slide->lines = lc;
67
68                 // clear text
69                 (text->reset)(text);
70                 l = 0;
71
72                 // create next slide
73                 slide = next_slide(slide);
74                 sc++;
75
76             } else {
77
78                 // if slide ! has line
79                 if(!slide->line) {
80
81                     // create new line
82                     line = new_line();
83                     slide->line = line;
84                     lc = 1;
85
86                 } else {
87
88                     // create next line
89                     line = next_line(line);
90                     lc++;
91
92                 }
93
94                 // add text to line
95                 line->text = text;
96
97                 // add bits to line
98                 line->bits = bits;
99
100                 // add length to line
101                 line->length = l;
102
103                 // calc offset
104                 line->offset = next_nonblank(text, 0);
105
106                 // new text
107                 text = cstring_init();
108                 l = 0;
109             }
110
111         } else if(c == '\t') {
112
113             // expand tab to spaces
114             for (i = 0;  i < EXPAND_TABS;  i++) {
115                 (text->expand)(text, ' ');
116                 l++;
117             }
118
119         } else if(c == '\\') {
120
121             // add char to line
122             (text->expand)(text, c);
123             l++;
124
125             // if !IS_CODE add next char to line
126             // and do not increase line count
127             if(next_nonblank(text, 0) < CODE_INDENT) {
128
129                 c = fgetc(input);
130                 (text->expand)(text, c);
131
132                 if(is_utf8(c)) {
133
134                     // if utf-8 char > 1 byte add remaing to line
135                     for(i = 0; i < length_utf8(c) - 1; i++) {
136                         c = fgetc(input);
137                         (text->expand)(text, c);
138                     }
139                 }
140
141             }
142
143         } else if(isprint(c) || isspace((unsigned char) c)) {
144
145             // add char to line
146             (text->expand)(text, c);
147             l++;
148
149         } else if(is_utf8(c)) {
150
151             // add char to line
152             (text->expand)(text, c);
153
154             // if utf-8 char > 1 byte add remaing to line
155             for(i = 0; i < length_utf8(c) - 1; i++) {
156                 c = fgetc(input);
157                 (text->expand)(text, c);
158             }
159
160             l++;
161         }
162     }
163
164     slide->lines = lc;
165     deck->slides = sc;
166
167     // detect header
168     line = deck->slide->line;
169     if(line && line->text->size > 0 && line->text->text[0] == '%') {
170
171         // assign header to deck
172         deck->header = line;
173
174         // find first non-header line
175         while(line->text->size > 0 && line->text->text[0] == '%') {
176             hc++;
177             line = line->next;
178         }
179
180         // split linked list
181         line->prev->next = (void*)0;
182         line->prev = (void*)0;
183
184         // remove header lines from slide
185         deck->slide->line = line;
186
187         // adjust counts
188         deck->headers += hc;
189         deck->slide->lines -= hc;
190     }
191
192     slide = deck->slide;
193     while(slide) {
194         line = slide->line;
195         while(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                 // combine underlined H1/H2 in single line
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             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
226                 tmp = line->next;
227                 line_t *list_last_level_3 = line;
228
229                 while(tmp &&
230                       CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
231                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) {
232                         list_last_level_3 = tmp;
233                     }
234                     tmp = tmp->next;
235                 }
236
237                 for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) {
238                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_3);
239                 }
240             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
241                 tmp = line->next;
242                 line_t *list_last_level_2 = line;
243
244                 while(tmp &&
245                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
246                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
247                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) {
248                         list_last_level_2 = tmp;
249                     }
250                     tmp = tmp->next;
251                 }
252
253                 for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) {
254                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_2);
255                 }
256             } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
257                 tmp = line->next;
258                 line_t *list_last_level_1 = line;
259
260                 while(tmp &&
261                       (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) ||
262                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) ||
263                        CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) {
264                     if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) {
265                         list_last_level_1 = tmp;
266                     }
267                     tmp = tmp->next;
268                 }
269                 
270                 for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) {
271                     SET_BIT(tmp->bits, IS_UNORDERED_LIST_1);
272                 }
273             }
274
275             line = line->next;
276         }
277         slide = slide->next;
278     }
279
280     return deck;
281 }
282
283 int markdown_analyse(cstring_t *text) {
284
285     static int unordered_list_level = 0;
286     static int unordered_list_level_offset[] = {-1, -1, -1, -1};
287
288     int i = 0;      // increment
289     int bits = 0;   // markdown bits
290     int offset = 0; // text offset
291     int eol    = 0; // end of line
292
293     int equals = 0, hashes = 0,
294         stars  = 0, minus  = 0,
295         spaces = 0, other  = 0; // special character counts
296
297     const int unordered_list_offset = unordered_list_level_offset[unordered_list_level];
298
299     // count leading spaces
300     offset = next_nonblank(text, 0);
301
302     // strip trailing spaces
303     for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--);
304
305     if(text->size >= offset + 2 &&
306        (text->text[offset] == '*' || text->text[offset] == '-') &&
307        text->text[offset + 1] == ' ') {
308         if(offset > unordered_list_offset + CODE_INDENT) {
309             SET_BIT(bits, IS_CODE);
310         } else if(offset != unordered_list_offset) {
311             for(i = unordered_list_level; i >= 0; i--) {
312                 if(unordered_list_level_offset[i] == offset) {
313                     unordered_list_level = i;
314                     break;
315                 }
316             }
317             if(i != unordered_list_level) {
318                 unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL);
319                 unordered_list_level_offset[unordered_list_level] = offset;
320             }
321         }
322
323         if(unordered_list_level == 0) {
324             unordered_list_level = 1;
325             unordered_list_level_offset[1] = offset;
326         }
327
328         switch(unordered_list_level) {
329             case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break;
330             case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break;
331             case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break;
332             default: break;
333         }
334     } else {
335         unordered_list_level = 0;
336
337         if(offset >= CODE_INDENT) {
338             // IS_CODE
339             SET_BIT(bits, IS_CODE);
340         } else {
341
342             for(i = offset; i < eol; i++) {
343
344                 if(text->text[i] == ' ') {
345                     spaces++;
346
347                 } else if(CHECK_BIT(bits, IS_CODE)) {
348                     other++;
349
350                 } else {
351                     switch(text->text[i]) {
352                         case '=': equals++;  break;
353                         case '#': hashes++;  break;
354                         case '*': stars++;   break;
355                         case '-': minus++;   break;
356                         case '\\': other++; i++; break;
357                         default:  other++;   break;
358                     }
359                 }
360             }
361
362             // IS_H1
363             if((equals > 0 &&
364                 hashes + stars + minus + spaces + other == 0) ||
365                (text &&
366                 text->text &&
367                 text->text[offset] == '#' &&
368                 text->text[offset+1] != '#')) {
369
370                 SET_BIT(bits, IS_H1);
371             }
372
373             // IS_H2
374             if((minus > 0 &&
375                 equals + hashes + stars + spaces + other == 0) ||
376                (text &&
377                 text->text &&
378                 text->text[offset] == '#' &&
379                 text->text[offset+1] == '#')) {
380
381                 SET_BIT(bits, IS_H2);
382             }
383
384             // IS_QUOTE
385             if(text &&
386                text->text &&
387                text->text[offset] == '>') {
388
389                 SET_BIT(bits, IS_QUOTE);
390             }
391
392             // IS_HR
393             if((minus >= 3 && equals + hashes + stars + other == 0) ||
394                (stars >= 3 && equals + hashes + minus + other == 0)) {
395
396                 SET_BIT(bits, IS_HR);
397             }
398
399             // IS_EMPTY
400             if(other == 0) {
401                 SET_BIT(bits, IS_EMPTY);
402             }
403         }
404     }
405
406     return bits;
407 }
408
409 void markdown_debug(deck_t *deck, int debug) {
410
411     int sc = 0; // slide count
412     int lc = 0; // line count
413
414     int offset;
415     line_t *header;
416
417     if(debug == 1) {
418         fprintf(stderr, "headers: %i\nslides: %i\n", deck->headers, deck->slides);
419
420     } else if(debug > 1) {
421
422         // print header to STDERR
423         if(deck->header) {
424             header = deck->header;
425             while(header &&
426                 header->length > 0 &&
427                 header->text->text[0] == '%') {
428
429                 // skip descriptor word (e.g. %title:)
430                 offset = next_blank(header->text, 0) + 1;
431
432                 fprintf(stderr, "header: %s\n", &header->text->text[offset]);
433                 header = header->next;
434             }
435         }
436     }
437
438     slide_t *slide = deck->slide;
439     line_t *line;
440
441     // print slide/line count to STDERR
442     while(slide) {
443         sc++;
444
445         if(debug == 1) {
446             fprintf(stderr, "  slide %i: %i lines\n", sc, slide->lines);
447
448         } else if(debug > 1) {
449
450             // also print bits and line length
451             fprintf(stderr, "  slide %i:\n", sc);
452             line = slide->line;
453             lc = 0;
454             while(line) {
455                 lc++;
456                 fprintf(stderr, "    line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
457                 line = line->next;
458             }
459         }
460
461         slide = slide->next;
462     }
463 }
464
465 int is_utf8(char ch) {
466     return (ch & 0x80);
467 }
468
469 int length_utf8(char ch) {
470
471     int i = 0; // increment
472
473     while(is_utf8(ch)) {
474         i++;
475         ch <<= 1;
476     }
477
478     return i;
479 }
480
481 int next_nonblank(cstring_t *text, int i) {
482     while ((i < text->size) && isspace((unsigned char) (text->text)[i]))
483         i++;
484
485     return i;
486 }
487
488 int next_blank(cstring_t *text, int i) {
489     while ((i < text->size) && !isspace((unsigned char) (text->text)[i]))
490         i++;
491
492     return i;
493 }
494
495 int next_word(cstring_t *text, int i) {
496     return next_nonblank(text, next_blank(text, i));
497 }
498