refactoring: only function prototypes belong in header files
[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, i, bits;
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
61         } else if('\t') {
62
63             // expand tab to spaces
64             for (i = 0;  i <= 4;  i++)
65                 (text->expand)(text, ' ');
66
67         } else if(isprint(c) || isspace(c) || is_utf8(c)) {
68
69             // add char to line
70             (text->expand)(text, c);
71         }
72     }
73
74     //TODO detect header
75
76     return doc;
77 }
78
79 int markdown_analyse(cstring_t *text) {
80     int c, i, bits,
81         offset, eol,
82         equals, hashes, stars, minus, plus,
83         spaces, other;
84
85     // count leading spaces
86     offset = next_nonblank(text, 0);
87
88     // IS_CODE
89     if(offset >= 4) {
90         SET_BIT(bits, IS_CODE);
91         return bits;
92     }
93
94     // strip trailing spaces
95     for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--);
96
97     for(i = offset; i < eol; i++) {
98
99         switch(text->text[i]) {
100             case '=': equals++; break;
101             case '#': hashes++; break;
102             case '*': stars++; break;
103             case '-': minus++; break;
104             case '+': plus++; break;
105             case ' ': spaces++; break;
106             default: other++; break;
107         }
108     }
109
110     // IS_HR
111     if((minus >= 3 && equals + hashes + stars + plus == 0) ||
112        (stars >= 3 && equals + hashes + minus + plus == 0)) {
113
114         SET_BIT(bits, IS_HR);
115         return bits;
116     }
117
118     //TODO all the other markdown tags
119
120     return bits;
121 }
122
123 int is_utf8(char ch) {
124     return (ch & 0x80);
125 }
126
127 int next_nonblank(cstring_t *text, int i) {
128     while ((i < text->size) && isspace((text->text)[i]))
129         ++i;
130     return i;
131 };
132