X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fparser.c;h=5ca7ea26a211c2fc223a23a7706bb59e6a31303d;hb=680d0478fc66d6d25b1f258be0114d6c915ec0a0;hp=0852e80dafe01d152f92e694f153b23470b4b514;hpb=6f9f33a6a5c7d677b4f4464218c01580a391cf08;p=smdp.git diff --git a/src/parser.c b/src/parser.c index 0852e80..5ca7ea2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -22,36 +22,45 @@ */ #include +#include #include #include +#include +#include +#include #include "parser.h" deck_t *markdown_load(FILE *input) { - int c = 0; // char + wchar_t c = L'\0'; // char int i = 0; // increment - int l = 0; // line length int hc = 0; // header count int lc = 0; // line count - int sc = 0; // slide count + int sc = 1; // slide count int bits = 0; // markdown bits + int prev = 0; // markdown bits of previous line deck_t *deck = new_deck(); - slide_t *slide = new_slide(); + slide_t *slide = deck->slide; line_t *line = NULL; line_t *tmp = NULL; cstring_t *text = cstring_init(); - // assign first slide to deck - deck->slide = slide; - sc++; + // initialize bits as empty line + SET_BIT(bits, IS_EMPTY); + + while ((c = fgetwc(input)) != WEOF) { + if (ferror(input)) { + fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } - while ((c = fgetc(input)) != EOF) { - if(c == '\n') { + if(c == L'\n') { // markdown analyse - bits = markdown_analyse(text); + prev = bits; + bits = markdown_analyse(text, prev); // if first line in file is markdown hr if(!line && CHECK_BIT(bits, IS_HR)) { @@ -59,6 +68,14 @@ deck_t *markdown_load(FILE *input) { // clear text (text->reset)(text); + } else if(line && CHECK_BIT(bits, IS_STOP)) { + + // set stop bit on last line + SET_BIT(line->bits, IS_STOP); + + // clear text + (text->reset)(text); + // if text is markdown hr } else if(CHECK_BIT(bits, IS_HR) && CHECK_BIT(line->bits, IS_EMPTY)) { @@ -67,7 +84,6 @@ deck_t *markdown_load(FILE *input) { // clear text (text->reset)(text); - l = 0; // create next slide slide = next_slide(slide); @@ -76,7 +92,7 @@ deck_t *markdown_load(FILE *input) { } else { // if slide ! has line - if(!slide->line) { + if(!slide->line || !line) { // create new line line = new_line(); @@ -97,109 +113,93 @@ deck_t *markdown_load(FILE *input) { // add bits to line line->bits = bits; - // add length to line - line->length = l; - // calc offset line->offset = next_nonblank(text, 0); + // adjust line length dynamicaly - excluding markup + if(line->text->value) + adjust_line_length(line); + // new text text = cstring_init(); - l = 0; } - } else if(c == '\t') { + } else if(c == L'\t') { // expand tab to spaces for (i = 0; i < EXPAND_TABS; i++) { - (text->expand)(text, ' '); - l++; + (text->expand)(text, L' '); } - } else if(c == '\\') { + } else if(c == L'\\') { // 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); + c = fgetwc(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((unsigned char) c)) { - - // add char to line - (text->expand)(text, c); - l++; - - } else if(is_utf8(c)) { + } else if(iswprint(c) || iswspace(c)) { // add char to line (text->expand)(text, 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); - } - - l++; } } + (text->delete)(text); slide->lines = lc; deck->slides = sc; // detect header line = deck->slide->line; - if(line && line->text->size > 0 && line->text->text[0] == '%') { + if(line && line->text->size > 0 && line->text->value[0] == L'%') { // assign header to deck deck->header = line; // find first non-header line - while(line->text->size > 0 && line->text->text[0] == '%') { + while(line && line->text->size > 0 && line->text->value[0] == L'%') { hc++; line = line->next; } - // split linked list - line->prev->next = (void*)0; - line->prev = (void*)0; + // only split header if any non-header line is found + if(line) { - // remove header lines from slide - deck->slide->line = line; + // split linked list + line->prev->next = NULL; + line->prev = NULL; + + // remove header lines from slide + deck->slide->line = line; + + // adjust counts + deck->headers += hc; + deck->slide->lines -= hc; + } else { - // adjust counts - deck->headers += hc; - deck->slide->lines -= hc; + // remove header from deck + deck->header = NULL; + } } - // combine underlined H1/H2 in single line slide = deck->slide; while(slide) { line = slide->line; while(line) { + // combine underlined H1/H2 in single 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; if(line->next) @@ -222,7 +222,65 @@ deck_t *markdown_load(FILE *input) { // delete line (tmp->text->delete)(tmp->text); free(tmp); + + // pass enclosing flag IS_UNORDERED_LIST_3 + // to nested levels for unordered lists + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) { + tmp = line->next; + line_t *list_last_level_3 = line; + + while(tmp && + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) { + if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3)) { + list_last_level_3 = tmp; + } + tmp = tmp->next; + } + + for(tmp = line; tmp != list_last_level_3; tmp = tmp->next) { + SET_BIT(tmp->bits, IS_UNORDERED_LIST_3); + } + + // pass enclosing flag IS_UNORDERED_LIST_2 + // to nested levels for unordered lists + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) { + tmp = line->next; + line_t *list_last_level_2 = line; + + while(tmp && + (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) || + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) { + if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2)) { + list_last_level_2 = tmp; + } + tmp = tmp->next; + } + + for(tmp = line; tmp != list_last_level_2; tmp = tmp->next) { + SET_BIT(tmp->bits, IS_UNORDERED_LIST_2); + } + + // pass enclosing flag IS_UNORDERED_LIST_1 + // to nested levels for unordered lists + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) { + tmp = line->next; + line_t *list_last_level_1 = line; + + while(tmp && + (CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1) || + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_2) || + CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_3))) { + if(CHECK_BIT(tmp->bits, IS_UNORDERED_LIST_1)) { + list_last_level_1 = tmp; + } + tmp = tmp->next; + } + + for(tmp = line; tmp != list_last_level_1; tmp = tmp->next) { + SET_BIT(tmp->bits, IS_UNORDERED_LIST_1); + } } + line = line->next; } slide = slide->next; @@ -231,7 +289,12 @@ deck_t *markdown_load(FILE *input) { return deck; } -int markdown_analyse(cstring_t *text) { +int markdown_analyse(cstring_t *text, int prev) { + + // static variables can not be redeclaired, but changed outside of a declaration + // the program remembers their value on every function calls + static int unordered_list_level = 0; + static int unordered_list_level_offset[] = {-1, -1, -1, -1}; int i = 0; // increment int bits = 0; // markdown bits @@ -242,77 +305,187 @@ int markdown_analyse(cstring_t *text) { stars = 0, minus = 0, spaces = 0, other = 0; // special character counts + const int unordered_list_offset = unordered_list_level_offset[unordered_list_level]; + + // return IS_EMPTY on null pointers + if(!text || !text->value) { + SET_BIT(bits, IS_EMPTY); + return bits; + } + // count leading spaces offset = next_nonblank(text, 0); - // strip trailing spaces - for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--); - - // IS_CODE - if(offset >= CODE_INDENT) { - SET_BIT(bits, IS_CODE); + // IS_STOP + if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) && + (!wcsncmp(&text->value[offset], L"
", 4) || + !wcsncmp(&text->value[offset], L"
", 4) || + !wcsncmp(&text->value[offset], L"^", 1))) { + SET_BIT(bits, IS_STOP); + return bits; } - for(i = offset; i < eol; i++) { + // strip trailing spaces + for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--); - if(text->text[i] == ' ') { - spaces++; + // IS_UNORDERED_LIST_# + if(text->size >= offset + 2 && + (text->value[offset] == L'*' || text->value[offset] == L'-') && + iswspace(text->value[offset + 1])) { - } else if(CHECK_BIT(bits, IS_CODE)) { - other++; + // if different from last lines offset + if(offset != unordered_list_offset) { - } else { - switch(text->text[i]) { - case '=': equals++; break; - case '#': hashes++; break; - case '*': stars++; break; - case '-': minus++; break; - case '\\': other++; i++; break; - default: other++; break; + // test if offset matches a lower indent level + for(i = unordered_list_level; i >= 0; i--) { + if(unordered_list_level_offset[i] == offset) { + unordered_list_level = i; + break; + } + } + // if offset doesn't match any previously stored indent level + if(i != unordered_list_level) { + unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL); + // memorize the offset as next bigger indent level + unordered_list_level_offset[unordered_list_level] = offset; } } - } - // IS_H1 - if((equals > 0 && - hashes + stars + minus + spaces + other == 0) || - (text && - text->text && - text->text[offset] == '#' && - text->text[offset+1] != '#')) { + // if no previous indent level matches, this must be the first line of the list + if(unordered_list_level == 0) { + unordered_list_level = 1; + unordered_list_level_offset[1] = offset; + } - SET_BIT(bits, IS_H1); + switch(unordered_list_level) { + case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break; + case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break; + case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break; + default: break; + } } - // IS_H2 - if((minus > 0 && - equals + hashes + stars + spaces + other == 0) || - (text && - text->text && - text->text[offset] == '#' && - text->text[offset+1] == '#')) { + if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_2) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) { + + // continue list if indent level is still the same as in previous line + if ((CHECK_BIT(prev, IS_UNORDERED_LIST_1) || + CHECK_BIT(prev, IS_UNORDERED_LIST_2) || + CHECK_BIT(prev, IS_UNORDERED_LIST_3)) && + offset >= unordered_list_offset) { + + switch(unordered_list_level) { + case 1: SET_BIT(bits, IS_UNORDERED_LIST_1); break; + case 2: SET_BIT(bits, IS_UNORDERED_LIST_2); break; + case 3: SET_BIT(bits, IS_UNORDERED_LIST_3); break; + default: break; + } + + // this line extends the previous list item + SET_BIT(bits, IS_UNORDERED_LIST_EXT); - SET_BIT(bits, IS_H2); + // or reset indent level + } else { + unordered_list_level = 0; + } } - // IS_QUOTE - if(text && - text->text && - text->text[offset] == '>') { + if(!CHECK_BIT(bits, IS_UNORDERED_LIST_1) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_2) && + !CHECK_BIT(bits, IS_UNORDERED_LIST_3)) { - SET_BIT(bits, IS_QUOTE); - } + // IS_CODE + if(offset >= CODE_INDENT && + (CHECK_BIT(prev, IS_EMPTY) || + CHECK_BIT(prev, IS_CODE) || + CHECK_BIT(prev, IS_STOP))) { + SET_BIT(bits, IS_CODE); - // IS_HR - if((minus >= 3 && equals + hashes + stars + other == 0) || - (stars >= 3 && equals + hashes + minus + other == 0)) { + } else { - SET_BIT(bits, IS_HR); - } + // IS_QUOTE + if(text->value[offset] == L'>') { + SET_BIT(bits, IS_QUOTE); + } - // IS_EMPTY - if(other == 0) { - SET_BIT(bits, IS_EMPTY); + // IS_CENTER + if(text->size >= offset + 3 && + text->value[offset] == L'-' && + text->value[offset + 1] == L'>' && + iswspace(text->value[offset + 2])) { + SET_BIT(bits, IS_CENTER); + + // remove start tag + (text->strip)(text, offset, 3); + eol -= 3; + + if(text->size >= offset + 3 && + text->value[eol - 1] == L'-' && + text->value[eol - 2] == L'<' && + iswspace(text->value[eol - 3])) { + + // remove end tags + (text->strip)(text, eol - 3, 3); + + // adjust end of line + for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--); + + } + } + + for(i = offset; i < eol; i++) { + + if(iswspace(text->value[i])) { + spaces++; + + } else { + switch(text->value[i]) { + case L'=': equals++; break; + case L'#': hashes++; break; + case L'*': stars++; break; + case L'-': minus++; break; + case L'\\': other++; i++; break; + default: other++; break; + } + } + } + + // IS_H1 + if(equals > 0 && + hashes + stars + minus + spaces + other == 0) { + SET_BIT(bits, IS_H1); + } + if(text->value[offset] == L'#' && + iswspace(text->value[offset+1])) { + SET_BIT(bits, IS_H1); + SET_BIT(bits, IS_H1_ATX); + } + + // IS_H2 + if(minus > 0 && + equals + hashes + stars + spaces + other == 0) { + SET_BIT(bits, IS_H2); + } + if(text->value[offset] == L'#' && + text->value[offset+1] == L'#' && + iswspace(text->value[offset+2])) { + SET_BIT(bits, IS_H2); + SET_BIT(bits, IS_H2_ATX); + } + + // IS_HR + if((minus >= 3 && equals + hashes + stars + other == 0) || + (stars >= 3 && equals + hashes + minus + other == 0)) { + + SET_BIT(bits, IS_HR); + } + + // IS_EMPTY + if(other == 0) { + SET_BIT(bits, IS_EMPTY); + } + } } return bits; @@ -327,7 +500,7 @@ void markdown_debug(deck_t *deck, int debug) { line_t *header; if(debug == 1) { - fprintf(stderr, "headers: %i\nslides: %i\n", deck->headers, deck->slides); + fwprintf(stderr, L"headers: %i\nslides: %i\n", deck->headers, deck->slides); } else if(debug > 1) { @@ -336,12 +509,12 @@ void markdown_debug(deck_t *deck, int debug) { header = deck->header; while(header && header->length > 0 && - header->text->text[0] == '%') { + header->text->value[0] == L'%') { // skip descriptor word (e.g. %title:) offset = next_blank(header->text, 0) + 1; - fprintf(stderr, "header: %s\n", &header->text->text[offset]); + fwprintf(stderr, L"header: %S\n", &header->text->value[offset]); header = header->next; } } @@ -355,17 +528,17 @@ void markdown_debug(deck_t *deck, int debug) { sc++; if(debug == 1) { - fprintf(stderr, " slide %i: %i lines\n", sc, slide->lines); + fwprintf(stderr, L" slide %i: %i lines\n", sc, slide->lines); } else if(debug > 1) { // also print bits and line length - fprintf(stderr, " slide %i:\n", sc); + fwprintf(stderr, L" slide %i:\n", sc); line = slide->line; lc = 0; while(line) { lc++; - fprintf(stderr, " line %i: bits = %i, length = %i\n", lc, line->bits, line->length); + fwprintf(stderr, L" line %i: bits = %i, length = %i\n", lc, line->bits, line->length); line = line->next; } } @@ -374,32 +547,67 @@ void markdown_debug(deck_t *deck, int debug) { } } -int is_utf8(char ch) { - return (ch & 0x80); -} +void adjust_line_length(line_t *line) { + int l = 0; + const static wchar_t *special = L"\\*_`"; // list of interpreted chars + const wchar_t *c = &line->text->value[line->offset]; + cstack_t *stack = cstack_init(); -int length_utf8(char ch) { + // for each char in line + for(; *c; c++) { + // if char is in special char list + if(wcschr(special, *c)) { - int i = 0; // increment + // closing special char (or second backslash) + if((stack->top)(stack, *c)) { + if(*c == L'\\') l++; + (stack->pop)(stack); - while(ch & 0x80) { - i++; - ch <<= 1; + // treat special as regular char + } else if((stack->top)(stack, L'\\')) { + l++; + (stack->pop)(stack); + + // opening special char + } else { + (stack->push)(stack, *c); + } + + } else { + // remove backslash from stack + if((stack->top)(stack, L'\\')) + (stack->pop)(stack); + l++; + } } - return i; + if(CHECK_BIT(line->bits, IS_H1_ATX)) + l -= 2; + if(CHECK_BIT(line->bits, IS_H2_ATX)) + l -= 3; + + line->length = l; + + (stack->delete)(stack); } int next_nonblank(cstring_t *text, int i) { - while ((i < text->size) && isspace((unsigned char) (text->text)[i])) - ++i; + while ((i < text->size) && iswspace((text->value)[i])) + i++; + + return i; +} + +int prev_blank(cstring_t *text, int i) { + while ((i > 0) && !iswspace((text->value)[i])) + i--; return i; } int next_blank(cstring_t *text, int i) { - while ((i < text->size) && !isspace((unsigned char) (text->text)[i])) - ++i; + while ((i < text->size) && !iswspace((text->value)[i])) + i++; return i; } @@ -407,4 +615,3 @@ int next_blank(cstring_t *text, int i) { int next_word(cstring_t *text, int i) { return next_nonblank(text, next_blank(text, i)); } -