From: FreeBirdLjj Date: Wed, 24 Sep 2014 17:11:37 +0000 (+0800) Subject: Parse and display list. X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=57d199e001fa71fc1d0e2aa4e7daae975254392c;p=smdp.git Parse and display list. --- diff --git a/src/parser.c b/src/parser.c index 478da5b..9ce43eb 100644 --- a/src/parser.c +++ b/src/parser.c @@ -189,7 +189,6 @@ deck_t *markdown_load(FILE *input) { deck->slide->lines -= hc; } - // combine underlined H1/H2 in single line slide = deck->slide; while(slide) { line = slide->line; @@ -199,6 +198,7 @@ deck_t *markdown_load(FILE *input) { CHECK_BIT(line->bits, IS_EMPTY) && line->prev && !CHECK_BIT(line->prev->bits, IS_EMPTY)) { + // combine underlined H1/H2 in single line // remove line from linked list line->prev->next = line->next; @@ -222,7 +222,56 @@ deck_t *markdown_load(FILE *input) { // delete line (tmp->text->delete)(tmp->text); free(tmp); + } 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); + } + } 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); + } + } 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); + } } + line = line->next; } slide = slide->next; @@ -233,6 +282,9 @@ deck_t *markdown_load(FILE *input) { int markdown_analyse(cstring_t *text) { + 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 int offset = 0; // text offset @@ -242,77 +294,113 @@ 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]; + // 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); - } - - for(i = offset; i < eol; i++) { + if(text->size >= offset + 2 && + (text->text[offset] == '*' || text->text[offset] == '-') && + text->text[offset + 1] == ' ') { + if(offset > unordered_list_offset + CODE_INDENT) { + SET_BIT(bits, IS_CODE); + } else if(offset != unordered_list_offset) { + for(i = unordered_list_level; i >= 0; i--) { + if(unordered_list_level_offset[i] == offset) { + unordered_list_level = i; + break; + } + } + if(i != unordered_list_level) { + unordered_list_level = MIN(unordered_list_level + 1, UNORDERED_LIST_MAX_LEVEL); + unordered_list_level_offset[unordered_list_level] = offset; + } + } - if(text->text[i] == ' ') { - spaces++; + if(unordered_list_level == 0) { + unordered_list_level = 1; + unordered_list_level_offset[1] = offset; + } - } else if(CHECK_BIT(bits, IS_CODE)) { - other++; + 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; + } + } else { + unordered_list_level = 0; + if(offset >= CODE_INDENT) { + // IS_CODE + SET_BIT(bits, IS_CODE); } else { - switch(text->text[i]) { - case '=': equals++; break; - case '#': hashes++; break; - case '*': stars++; break; - case '-': minus++; break; - case '\\': other++; i++; break; - default: other++; break; + + for(i = offset; i < eol; i++) { + + 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; + } + } } - } - } - // IS_H1 - if((equals > 0 && - hashes + stars + minus + spaces + other == 0) || - (text && - text->text && - text->text[offset] == '#' && - text->text[offset+1] != '#')) { + // 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); - } + 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] == '#')) { + // 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); - } + SET_BIT(bits, IS_H2); + } - // IS_QUOTE - if(text && - text->text && - text->text[offset] == '>') { + // IS_QUOTE + if(text && + text->text && + text->text[offset] == '>') { - SET_BIT(bits, IS_QUOTE); - } + SET_BIT(bits, IS_QUOTE); + } - // IS_HR - if((minus >= 3 && equals + hashes + stars + other == 0) || - (stars >= 3 && equals + hashes + minus + other == 0)) { + // IS_HR + if((minus >= 3 && equals + hashes + stars + other == 0) || + (stars >= 3 && equals + hashes + minus + other == 0)) { - SET_BIT(bits, IS_HR); - } + SET_BIT(bits, IS_HR); + } - // IS_EMPTY - if(other == 0) { - SET_BIT(bits, IS_EMPTY); + // IS_EMPTY + if(other == 0) { + SET_BIT(bits, IS_EMPTY); + } + } } return bits; diff --git a/src/viewer.c b/src/viewer.c index 76dc18c..6b551de 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -368,10 +368,40 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo char *c; // char pointer for iteration char *special = "\\*_`"; // list of interpreted chars cstack_t *stack = cstack_init(); - + if(line->text->text) { int offset = 0; // text offset + if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) { + offset = next_nonblank(line->text, 0); + char format_s[15]; + strcpy(&format_s[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? "| " : " "); + strcpy(&format_s[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? "| " : " "); + strcpy(&format_s[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? "+-- %s" : "`-- %s"); + mvwprintw(window, + y, x, + format_s, + &line->text->text[offset + 2]); + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) { + offset = next_nonblank(line->text, 0); + char format_s[11]; + strcpy(&format_s[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? "| " : " "); + strcpy(&format_s[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? "+-- %s" : "`-- %s"); + mvwprintw(window, + y, x, + format_s, + &line->text->text[offset + 2]); + } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) { + offset = next_nonblank(line->text, 0); + char format_s[7]; + strcpy(&format_s[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? "+-- %s" : "`-- %s"); + mvwprintw(window, + y, x, + format_s, + &line->text->text[offset + 2]); + } else + + // IS_CODE if(CHECK_BIT(line->bits, IS_CODE)) {