58c7f3ab4a6bec25fde1f07cda1b5888f6a6de9d
[smdp.git] / markdown_io.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "include/cstring.h"
5 #include "include/markdown.h"
6
7 document_t *markdown_load(FILE *input) {
8
9     int c = 0, i = 0, bits = 0;
10
11     document_t *doc;
12     page_t *page;
13     line_t *line;
14     cstring_t *text;
15
16     doc = new_document();
17     page = new_page();
18     doc->page = page;
19     text = cstring_init();
20
21     while ((c = fgetc(input)) != EOF) {
22         if(c == '\n') {
23
24             // markdown analyse
25             bits = markdown_analyse(text);
26
27             // if text is markdown hr
28             if(CHECK_BIT(bits, IS_HR)) {
29
30                 // clear text
31                 (text->reset)(text);
32                 // create next page
33                 page = next_page(page);
34
35             } else {
36
37                 // if page ! has line
38                 if(!page->line) {
39
40                     // create new line
41                     line = new_line();
42                     page->line = line;
43
44                 } else {
45
46                     // create next line
47                     line = next_line(line);
48
49                 }
50
51                 // add text to line
52                 line->text = text;
53
54                 // add bits to line
55                 line->bits = bits;
56
57                 // calc offset
58                 line->offset = next_nonblank(text, 0);
59
60                 // new text
61                 text = cstring_init();
62             }
63
64         } else if(c == '\t') {
65
66             // expand tab to spaces
67             for (i = 0;  i <= 4;  i++)
68                 (text->expand)(text, ' ');
69
70         } else if(isprint(c) || isspace(c) || is_utf8(c)) {
71
72             // add char to line
73             (text->expand)(text, c);
74         }
75     }
76
77     //TODO detect header
78
79     return doc;
80 }
81
82 int markdown_analyse(cstring_t *text) {
83     int i = 0, bits = 0,
84         offset = 0, eol = 0,
85         equals = 0, hashes = 0, stars = 0, minus = 0, plus = 0,
86         spaces = 0, other = 0;
87
88     // count leading spaces
89     offset = next_nonblank(text, 0);
90
91     // IS_CODE
92     if(offset >= 4) {
93         SET_BIT(bits, IS_CODE);
94         return bits;
95     }
96
97     // strip trailing spaces
98     for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--);
99
100     for(i = offset; i < eol; i++) {
101
102         switch(text->text[i]) {
103             case '=': equals++; break;
104             case '#': hashes++; break;
105             case '*': stars++; break;
106             case '-': minus++; break;
107             case '+': plus++; break;
108             case ' ': spaces++; break;
109             default: other++; break;
110         }
111     }
112
113     // IS_HR
114     if((minus >= 3 && equals + hashes + stars + plus == 0) ||
115        (stars >= 3 && equals + hashes + minus + plus == 0)) {
116
117         SET_BIT(bits, IS_HR);
118         return bits;
119     }
120
121     //TODO all the other markdown tags
122
123     return bits;
124 }
125
126 int is_utf8(char ch) {
127     return (ch & 0x80);
128 }
129
130 int next_nonblank(cstring_t *text, int i) {
131     while ((i < text->size) && isspace((text->text)[i]))
132         ++i;
133     return i;
134 };
135