X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fparser.c;h=0898b6075808d1f443d5e609a399a767de1fe541;hb=354cf2e07ccd0d7900f96bffc279093997cbe203;hp=760ef4b49ece5956d83e44188b88fc5be87be302;hpb=5f369c826ac0ca4989892b3691f66d0ac73679f3;p=smdp.git diff --git a/src/parser.c b/src/parser.c index 760ef4b..0898b60 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2,7 +2,7 @@ * Functions necessary to parse a file and transform its content into * a deck of slides containing lines. All based on markdown formating * rules. - * Copyright (C) 2014 Michael Goehler + * Copyright (C) 2018 Michael Goehler * * This file is part of mdp. * @@ -25,13 +25,84 @@ #include #include #include +#include +#include #include #include "parser.h" -deck_t *markdown_load(FILE *input) { - - int c = 0; // char +// char entry translation table +static struct named_character_entity { + wchar_t ucs; + const wchar_t *name; +} named_character_entities[] = { + { L'\x0022', L"quot" }, + { L'\x0026', L"amp" }, + { L'\x0027', L"apos" }, + { L'\x003C', L"lt" }, + { L'\x003E', L"gt" }, + { L'\x00A2', L"cent" }, + { L'\x00A3', L"pound" }, + { L'\x00A5', L"yen" }, + { L'\x00A7', L"sect" }, + { L'\x00A9', L"copy" }, + { L'\x00AA', L"laquo" }, + { L'\x00AE', L"reg" }, + { L'\x00B0', L"deg" }, + { L'\x00B1', L"plusmn" }, + { L'\x00B2', L"sup2" }, + { L'\x00B3', L"sup3" }, + { L'\x00B6', L"para" }, + { L'\x00B9', L"sup1" }, + { L'\x00BB', L"raquo" }, + { L'\x00BC', L"frac14" }, + { L'\x00BD', L"frac12" }, + { L'\x00BE', L"frac34" }, + { L'\x00D7', L"times" }, + { L'\x00F7', L"divide" }, + { L'\x2018', L"lsquo" }, + { L'\x2019', L"rsquo" }, + { L'\x201C', L"ldquo" }, + { L'\x201D', L"rdquo" }, + { L'\x2020', L"dagger" }, + { L'\x2021', L"Dagger" }, + { L'\x2022', L"bull" }, + { L'\x2026', L"hellip" }, + { L'\x2030', L"permil" }, + { L'\x2032', L"prime" }, + { L'\x2033', L"Prime" }, + { L'\x2039', L"lsaquo" }, + { L'\x203A', L"rsaquo" }, + { L'\x20AC', L"euro" }, + { L'\x2122', L"trade" }, + { L'\x2190', L"larr" }, + { L'\x2191', L"uarr" }, + { L'\x2192', L"rarr" }, + { L'\x2193', L"darr" }, + { L'\x2194', L"harr" }, + { L'\x21B5', L"crarr" }, + { L'\x21D0', L"lArr" }, + { L'\x21D1', L"uArr" }, + { L'\x21D2', L"rArr" }, + { L'\x21D3', L"dArr" }, + { L'\x21D4', L"hArr" }, + { L'\x221E', L"infin" }, + { L'\x2261', L"equiv" }, + { L'\x2308', L"lceil" }, + { L'\x2309', L"rceil" }, + { L'\x230A', L"lfloor" }, + { L'\x230B', L"rfloor" }, + { L'\x25CA', L"loz" }, + { L'\x2660', L"spades" }, + { L'\x2663', L"clubs" }, + { L'\x2665', L"hearts" }, + { L'\x2666', L"diams" }, + { L'\0', NULL }, +}; + +deck_t *markdown_load(FILE *input, int noexpand) { + + wchar_t c = L'\0'; // char int i = 0; // increment int hc = 0; // header count int lc = 0; // line count @@ -48,13 +119,13 @@ deck_t *markdown_load(FILE *input) { // initialize bits as empty line SET_BIT(bits, IS_EMPTY); - while ((c = fgetc(input)) != EOF) { + while ((c = fgetwc(input)) != WEOF) { if (ferror(input)) { fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno)); exit(EXIT_FAILURE); } - if(c == '\n') { + if(c == L'\n') { // markdown analyse prev = bits; @@ -66,6 +137,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)) { @@ -79,10 +158,16 @@ deck_t *markdown_load(FILE *input) { slide = next_slide(slide); sc++; + } else if((CHECK_BIT(bits, IS_TILDE_CODE) || + CHECK_BIT(bits, IS_GFM_CODE)) && + CHECK_BIT(bits, IS_EMPTY)) { + // remove tilde code markers + (text->reset)(text); + } else { // if slide ! has line - if(!slide->line) { + if(!slide->line || !line) { // create new line line = new_line(); @@ -106,22 +191,28 @@ deck_t *markdown_load(FILE *input) { // calc offset line->offset = next_nonblank(text, 0); + // expand character entities if enabled + if(line->text->value && + !noexpand && + !CHECK_BIT(line->bits, IS_CODE)) + expand_character_entities(line); + // adjust line length dynamicaly - excluding markup - if(line->text->text) + if(line->text->value) adjust_line_length(line); // new text text = cstring_init(); } - } else if(c == '\t') { + } else if(c == L'\t') { // expand tab to spaces for (i = 0; i < EXPAND_TABS; i++) { - (text->expand)(text, ' '); + (text->expand)(text, L' '); } - } else if(c == '\\') { + } else if(c == L'\\') { // add char to line (text->expand)(text, c); @@ -130,69 +221,81 @@ deck_t *markdown_load(FILE *input) { // 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); - - } 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); - } } } + (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 = NULL; - line->prev = NULL; + // only split header if any non-header line is found + if(line) { + + // split linked list + line->prev->next = NULL; + line->prev = NULL; - // remove header lines from slide - deck->slide->line = line; + // remove header lines from slide + deck->slide->line = line; - // adjust counts - deck->headers += hc; - deck->slide->lines -= hc; + // adjust counts + deck->headers += hc; + deck->slide->lines -= hc; + } else { + + // remove header from deck + deck->header = NULL; + } } slide = deck->slide; while(slide) { line = slide->line; + + // ignore mdpress format attributes + if(line && + slide->lines > 1 && + !CHECK_BIT(line->bits, IS_EMPTY) && + line->text->value[line->offset] == L'=' && + line->text->value[line->offset + 1] == L' ') { + + // remove line from linked list + slide->line = line->next; + line->next->prev = NULL; + + // maintain loop condition + tmp = line; + line = line->next; + + // adjust line count + slide->lines -= 1; + + // delete line + (tmp->text->delete)(tmp->text); + free(tmp); + } + while(line) { // combine underlined H1/H2 in single line if((CHECK_BIT(line->bits, IS_H1) || @@ -297,6 +400,8 @@ int markdown_analyse(cstring_t *text, int prev) { // 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}; + static int num_tilde_characters = 0; + static int num_backticks = 0; int i = 0; // increment int bits = 0; // markdown bits @@ -310,21 +415,78 @@ int markdown_analyse(cstring_t *text, int prev) { const int unordered_list_offset = unordered_list_level_offset[unordered_list_level]; // return IS_EMPTY on null pointers - if(!text || !text->text) { + if(!text || !text->value) { SET_BIT(bits, IS_EMPTY); + + // continue fenced code blocks across empty lines + if(num_tilde_characters > 0) + SET_BIT(bits, IS_CODE); + return bits; } // count leading spaces offset = next_nonblank(text, 0); + // IS_TILDE_CODE + if (wcsncmp(text->value, L"~~~", 3) == 0) { + int tildes_in_line = next_nontilde(text, 0); + if (tildes_in_line >= num_tilde_characters) { + if (num_tilde_characters > 0) { + num_tilde_characters = 0; + } else { + num_tilde_characters = tildes_in_line; + } + SET_BIT(bits, IS_EMPTY); + SET_BIT(bits, IS_TILDE_CODE); + return bits; + } + } + + if (num_tilde_characters > 0) { + SET_BIT(bits, IS_CODE); + SET_BIT(bits, IS_TILDE_CODE); + return bits; + } + + // IS_GFM_CODE + if (wcsncmp(text->value, L"```", 3) == 0) { + int backticks_in_line = next_nonbacktick(text, 0); + if (backticks_in_line >= num_backticks) { + if (num_backticks > 0) { + num_backticks = 0; + } else { + num_backticks = backticks_in_line; + } + SET_BIT(bits, IS_EMPTY); + SET_BIT(bits, IS_GFM_CODE); + return bits; + } + } + + if (num_backticks > 0) { + SET_BIT(bits, IS_CODE); + SET_BIT(bits, IS_GFM_CODE); + return bits; + } + + // 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; + } + // strip trailing spaces - for(eol = text->size; eol > offset && isspace((unsigned char) text->text[eol - 1]); eol--); + for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--); + text->size = eol; // IS_UNORDERED_LIST_# if(text->size >= offset + 2 && - (text->text[offset] == '*' || text->text[offset] == '-') && - text->text[offset + 1] == ' ') { + (text->value[offset] == L'*' || text->value[offset] == L'-') && + iswspace(text->value[offset + 1])) { // if different from last lines offset if(offset != unordered_list_offset) { @@ -391,21 +553,22 @@ int markdown_analyse(cstring_t *text, int prev) { // IS_CODE if(offset >= CODE_INDENT && (CHECK_BIT(prev, IS_EMPTY) || - CHECK_BIT(prev, IS_CODE))) { + CHECK_BIT(prev, IS_CODE) || + CHECK_BIT(prev, IS_STOP))) { SET_BIT(bits, IS_CODE); } else { // IS_QUOTE - if(text->text[offset] == '>') { + if(text->value[offset] == L'>') { SET_BIT(bits, IS_QUOTE); } // IS_CENTER if(text->size >= offset + 3 && - text->text[offset] == '-' && - text->text[offset + 1] == '>' && - text->text[offset + 2] == ' ') { + text->value[offset] == L'-' && + text->value[offset + 1] == L'>' && + iswspace(text->value[offset + 2])) { SET_BIT(bits, IS_CENTER); // remove start tag @@ -413,31 +576,31 @@ int markdown_analyse(cstring_t *text, int prev) { eol -= 3; if(text->size >= offset + 3 && - text->text[eol - 1] == '-' && - text->text[eol - 2] == '<' && - text->text[eol - 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 && isspace((unsigned char) text->text[eol - 1]); eol--); + for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--); } } for(i = offset; i < eol; i++) { - if(text->text[i] == ' ') { + if(iswspace(text->value[i])) { spaces++; } else { - switch(text->text[i]) { - case '=': equals++; break; - case '#': hashes++; break; - case '*': stars++; break; - case '-': minus++; break; - case '\\': other++; i++; break; + 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; } } @@ -445,23 +608,23 @@ int markdown_analyse(cstring_t *text, int prev) { // IS_H1 if(equals > 0 && - hashes + stars + minus + spaces + other == 0) { + hashes + stars + minus + spaces + other == 0) { SET_BIT(bits, IS_H1); } - if(text->text[offset] == '#' && - text->text[offset+1] == ' ') { + 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) { + equals + hashes + stars + spaces + other == 0) { SET_BIT(bits, IS_H2); } - if(text->text[offset] == '#' && - text->text[offset+1] == '#' && - text->text[offset+2] == ' ') { + 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); } @@ -492,7 +655,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) { @@ -501,12 +664,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; } } @@ -520,17 +683,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; } } @@ -539,24 +702,101 @@ void markdown_debug(deck_t *deck, int debug) { } } +void expand_character_entities(line_t *line) +{ + wchar_t *ampersand; + wchar_t *prev, *curr; + + ampersand = NULL; + curr = &line->text->value[0]; + + // for each char in line + for(prev = NULL; *curr; prev = curr++) { + if (*curr == L'&' && (prev == NULL || *prev != L'\\')) { + ampersand = curr; + continue; + } + if (ampersand == NULL) { + continue; + } + if (*curr == L'#') { + if (prev == ampersand) + continue; + ampersand = NULL; + continue; + } + if (iswalpha(*curr) || iswxdigit(*curr)) { + continue; + } + if (*curr == L';') { + int cnt; + wchar_t ucs = L'\0'; + if (ampersand + 1 >= curr || ampersand + 16 < curr) { // what is a good limit? + ampersand = NULL; + continue; + } + if (ampersand[1] == L'#') { // &#nnnn; or &#xhhhh; + if (ampersand + 2 >= curr) { + ampersand = NULL; + continue; + } + if (ampersand[2] != L'x') { // &#nnnn; + cnt = wcsspn(&ersand[2], L"0123456789"); + if (ampersand + 2 + cnt != curr) { + ampersand = NULL; + continue; + } + ucs = wcstoul(&ersand[2], NULL, 10); + } else { // &#xhhhh; + if (ampersand + 3 >= curr) { + ampersand = NULL; + continue; + } + cnt = wcsspn(&ersand[3], L"0123456789abcdefABCDEF"); + if (ampersand + 3 + cnt != curr) { + ampersand = NULL; + continue; + } + ucs = wcstoul(&ersand[3], NULL, 16); + } + } else { // &name; + for (cnt = 0; cnt < sizeof(named_character_entities)/sizeof(named_character_entities[0]); ++cnt) { + if (wcsncmp(named_character_entities[cnt].name, &ersand[1], curr - ampersand - 1)) + continue; + ucs = named_character_entities[cnt].ucs; + break; + } + if (ucs == L'\0') { + ampersand = NULL; + continue; + } + } + *ampersand = ucs; + cstring_strip(line->text, ampersand + 1 - &line->text->value[0], curr - ampersand); + curr = ampersand; + ampersand = NULL; + } + } +} + void adjust_line_length(line_t *line) { int l = 0; - const static char *special = "\\*_`"; // list of interpreted chars - const char *c = &line->text->text[line->offset]; + const static wchar_t *special = L"\\*_`"; // list of interpreted chars + const wchar_t *c = &line->text->value[0]; cstack_t *stack = cstack_init(); // for each char in line for(; *c; c++) { // if char is in special char list - if(strchr(special, *c)) { + if(wcschr(special, *c)) { // closing special char (or second backslash) if((stack->top)(stack, *c)) { - if(*c == '\\') l++; + if(*c == L'\\') l++; (stack->pop)(stack); // treat special as regular char - } else if((stack->top)(stack, '\\')) { + } else if((stack->top)(stack, L'\\')) { l++; (stack->pop)(stack); @@ -567,7 +807,7 @@ void adjust_line_length(line_t *line) { } else { // remove backslash from stack - if((stack->top)(stack, '\\')) + if((stack->top)(stack, L'\\')) (stack->pop)(stack); l++; } @@ -583,38 +823,22 @@ void adjust_line_length(line_t *line) { (stack->delete)(stack); } -bool is_utf8(char ch) { - return (ch & 0x80) != 0x00; -} - -int length_utf8(char ch) { - - int i = 0; // increment - - while(is_utf8(ch)) { - i++; - ch <<= 1; - } - - return i; -} - int next_nonblank(cstring_t *text, int i) { - while ((i < text->size) && isspace((unsigned char) (text->text)[i])) + while ((i < text->size) && iswspace((text->value)[i])) i++; return i; } int prev_blank(cstring_t *text, int i) { - while ((i > 0) && !isspace((unsigned char) (text->text)[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])) + while ((i < text->size) && !iswspace((text->value)[i])) i++; return i; @@ -623,3 +847,18 @@ int next_blank(cstring_t *text, int i) { int next_word(cstring_t *text, int i) { return next_nonblank(text, next_blank(text, i)); } + +int next_nontilde(cstring_t *text, int i) { + while ((i < text->size) && text->value[i] == L'~') + i++; + + return i; +} + +int next_nonbacktick(cstring_t *text, int i) { + while ((i < text->size) && text->value[i] == L'`') + i++; + + return i; +} +