4 #include "include/cstring.h"
5 #include "include/markdown.h"
7 document_t *markdown_load(FILE *input) {
9 int c = 0, i = 0, bits = 0;
19 text = cstring_init();
21 while ((c = fgetc(input)) != EOF) {
25 bits = markdown_analyse(text);
27 // if text is markdown hr
28 if(CHECK_BIT(bits, IS_HR)) {
33 page = next_page(page);
47 line = next_line(line);
58 line->offset = next_nonblank(text, 0);
61 text = cstring_init();
64 } else if(c == '\t') {
66 // expand tab to spaces
67 for (i = 0; i <= 4; i++)
68 (text->expand)(text, ' ');
70 } else if(isprint(c) || isspace(c) || is_utf8(c)) {
73 (text->expand)(text, c);
82 int markdown_analyse(cstring_t *text) {
85 equals = 0, hashes = 0, stars = 0, minus = 0, plus = 0,
86 spaces = 0, other = 0;
88 // count leading spaces
89 offset = next_nonblank(text, 0);
93 SET_BIT(bits, IS_CODE);
97 // strip trailing spaces
98 for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--);
100 for(i = offset; i < eol; i++) {
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;
114 if((minus >= 3 && equals + hashes + stars + plus == 0) ||
115 (stars >= 3 && equals + hashes + minus + plus == 0)) {
117 SET_BIT(bits, IS_HR);
121 //TODO all the other markdown tags
126 int is_utf8(char ch) {
130 int next_nonblank(cstring_t *text, int i) {
131 while ((i < text->size) && isspace((text->text)[i]))