4 #include "include/cstring.h"
5 #include "include/markdown.h"
7 document_t *markdown_load(FILE *input) {
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);
63 // expand tab to spaces
64 for (i = 0; i <= 4; i++)
65 (text->expand)(text, ' ');
67 } else if(isprint(c) || isspace(c) || is_utf8(c)) {
70 (text->expand)(text, c);
79 int markdown_analyse(cstring_t *text) {
82 equals, hashes, stars, minus, plus,
85 // count leading spaces
86 offset = next_nonblank(text, 0);
90 SET_BIT(bits, IS_CODE);
94 // strip trailing spaces
95 for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--);
97 for(i = offset; i < eol; i++) {
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;
111 if((minus >= 3 && equals + hashes + stars + plus == 0) ||
112 (stars >= 3 && equals + hashes + minus + plus == 0)) {
114 SET_BIT(bits, IS_HR);
118 //TODO all the other markdown tags
123 int is_utf8(char ch) {
127 int next_nonblank(cstring_t *text, int i) {
128 while ((i < text->size) && isspace((text->text)[i]))