From: Michael Göhler Date: Sun, 17 Aug 2014 20:12:59 +0000 (+0200) Subject: add missing markdown tags to file parser X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=b68b33bdf38847668c349c3df09f9b29f58fc1b1;p=smdp.git add missing markdown tags to file parser --- diff --git a/markdown_io.c b/markdown_io.c index ba2ff7a..7c80695 100644 --- a/markdown_io.c +++ b/markdown_io.c @@ -25,7 +25,8 @@ document_t *markdown_load(FILE *input) { bits = markdown_analyse(text); // if text is markdown hr - if(CHECK_BIT(bits, IS_HR)) { + if(CHECK_BIT(bits, IS_HR) && + CHECK_BIT(line->bits, IS_EMPTY)) { // clear text (text->reset)(text); @@ -94,49 +95,111 @@ document_t *markdown_load(FILE *input) { doc->page->line = line; } + // combine underlined H1/H2 in single line + page = doc->page; + while(page) { + line = page->line; + while(line) { + if((CHECK_BIT(line->bits, IS_H1) || + CHECK_BIT(line->bits, IS_H2)) && + CHECK_BIT(line->bits, IS_EMPTY) && + line->prev && + !CHECK_BIT(line->prev->bits, IS_EMPTY)) { + + // remove line from linked list + line->prev->next = line->next; + line->next->prev = line->prev; + + // set bits on revious line + if(CHECK_BIT(line->bits, IS_H1)) { + SET_BIT(line->prev->bits, IS_H1); + } else { + SET_BIT(line->prev->bits, IS_H2); + } + + // delete line + (line->text->delete)(line->text); + free(line); + } + line = line->next; + } + page = page->next; + } + return doc; } int markdown_analyse(cstring_t *text) { int i = 0, bits = 0, - offset = 0, eol = 0, - equals = 0, hashes = 0, stars = 0, minus = 0, plus = 0, - spaces = 0, other = 0; + offset = 0, eol = 0, + equals = 0, hashes = 0, + stars = 0, minus = 0, + spaces = 0, other = 0; // count leading spaces offset = next_nonblank(text, 0); - // IS_CODE - if(offset >= 4) { - SET_BIT(bits, IS_CODE); - return bits; - } - // strip trailing spaces for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--); for(i = offset; i < eol; i++) { switch(text->text[i]) { - case '=': equals++; break; - case '#': hashes++; break; - case '*': stars++; break; - case '-': minus++; break; - case '+': plus++; break; - case ' ': spaces++; break; - default: other++; break; + case '=': equals++; break; + case '#': hashes++; break; + case '*': stars++; break; + case '-': minus++; break; + case ' ': spaces++; break; + default: other++; break; } } + // IS_H1 + if((equals > 0 && + hashes + stars + minus + spaces + other == 0) || + (text && + text->text && + text->text[offset] == '#' && + text->text[offset+1] != '#')) { + + SET_BIT(bits, IS_H1); + } + + // IS_H2 + if((minus > 0 && + equals + hashes + stars + spaces + other == 0) || + (text && + text->text && + text->text[offset] == '#' && + text->text[offset+1] == '#')) { + + SET_BIT(bits, IS_H2); + } + + // IS_QUOTE + if(text && + text->text && + text->text[offset] == '>') { + + SET_BIT(bits, IS_QUOTE); + } + + // IS_CODE + if(offset >= 4) { + SET_BIT(bits, IS_CODE); + } + // IS_HR - if((minus >= 3 && equals + hashes + stars + plus == 0) || - (stars >= 3 && equals + hashes + minus + plus == 0)) { + if((minus >= 3 && equals + hashes + stars + other == 0) || + (stars >= 3 && equals + hashes + minus + other == 0)) { SET_BIT(bits, IS_HR); - return bits; } - //TODO all the other markdown tags + // IS_EMPTY + if(other == 0) { + SET_BIT(bits, IS_EMPTY); + } return bits; } diff --git a/sample.md b/sample.md index dfb2d1a..e99bc8c 100644 --- a/sample.md +++ b/sample.md @@ -24,3 +24,11 @@ Now with different indentation. function expand_tab { } + +*** + +Another Title +------------- + +And some Text. +