From: Michael Göhler Date: Mon, 25 Aug 2014 22:09:09 +0000 (+0200) Subject: calculate length of lines including backslashes correctly X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=32639324a5ed0b1678d1066680ed6d2e469ae271;p=smdp.git calculate length of lines including backslashes correctly --- diff --git a/include/parser.h b/include/parser.h index 89e1a5d..f7e6847 100644 --- a/include/parser.h +++ b/include/parser.h @@ -3,6 +3,9 @@ #include "markdown.h" +#define EXPAND_TABS 4 +#define CODE_INDENT 4 + deck_t *markdown_load(FILE *input); int markdown_analyse(cstring_t *text); void markdown_debug(deck_t *deck, int debug); diff --git a/parser.c b/parser.c index 4121964..9931b02 100644 --- a/parser.c +++ b/parser.c @@ -80,11 +80,35 @@ deck_t *markdown_load(FILE *input) { } else if(c == '\t') { // expand tab to spaces - for (i = 0; i <= 4; i++) { + for (i = 0; i <= EXPAND_TABS; i++) { (text->expand)(text, ' '); l++; } + } else if(c == '\\') { + + // add char to line + (text->expand)(text, c); + l++; + + // if !IS_CODE add next char to line + // and do not increase line count + if(next_nonblank(text, 0) < CODE_INDENT) { + + c = fgetc(input); + (text->expand)(text, c); + + if(is_utf8(c)) { + + // if utf-8 char > 1 byte add remaing to line + for(i = 0; i < length_utf8(c) - 1; i++) { + c = fgetc(input); + (text->expand)(text, c); + } + } + + } + } else if(isprint(c) || isspace(c)) { // add char to line @@ -101,6 +125,7 @@ deck_t *markdown_load(FILE *input) { c = fgetc(input); (text->expand)(text, c); } + l++; } } @@ -187,15 +212,28 @@ int markdown_analyse(cstring_t *text) { // strip trailing spaces for(eol = text->size; eol > offset && isspace(text->text[eol - 1]); eol--); + // IS_CODE + if(offset >= CODE_INDENT) { + SET_BIT(bits, IS_CODE); + } + for(i = offset; i < eol; i++) { - switch(text->text[i]) { - case '=': equals++; break; - case '#': hashes++; break; - case '*': stars++; break; - case '-': minus++; break; - case ' ': spaces++; break; - default: other++; break; + if(text->text[i] == ' ') { + spaces++; + + } else if(CHECK_BIT(bits, IS_CODE)) { + other++; + + } else { + switch(text->text[i]) { + case '=': equals++; break; + case '#': hashes++; break; + case '*': stars++; break; + case '-': minus++; break; + case '\\': other++; i++; break; + default: other++; break; + } } } @@ -229,11 +267,6 @@ int markdown_analyse(cstring_t *text) { SET_BIT(bits, IS_QUOTE); } - // IS_CODE - if(offset >= 4) { - SET_BIT(bits, IS_CODE); - } - // IS_HR if((minus >= 3 && equals + hashes + stars + other == 0) || (stars >= 3 && equals + hashes + minus + other == 0)) { diff --git a/sample.md b/sample.md index e99bc8c..e35b9e8 100644 --- a/sample.md +++ b/sample.md @@ -7,7 +7,12 @@ Title This is the first page. --------------------------------------------------------------------------------- +------------------------------------ + +These are 3 stars: \*\*\* +And two backslashes: \\\\ + +--- # Page 2