diff options
-rw-r--r-- | src/blocks.c | 110 | ||||
-rw-r--r-- | src/html/html.c | 26 | ||||
-rw-r--r-- | src/print.c | 24 | ||||
-rw-r--r-- | src/stmd.h | 43 |
4 files changed, 100 insertions, 103 deletions
diff --git a/src/blocks.c b/src/blocks.c index d74ceb2..f671b5e 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -38,7 +38,7 @@ static node_block* make_block(int tag, int start_line, int start_column) // Create a root document node_block. extern node_block* make_document() { - node_block * e = make_block(document, 1, 1); + node_block * e = make_block(BLOCK_DOCUMENT, 1, 1); reference * map = NULL; reference ** refmap; refmap = (reference**) malloc(sizeof(reference*)); @@ -68,18 +68,18 @@ bool is_blank(strbuf *s, int offset) static inline bool can_contain(int parent_type, int child_type) { - return ( parent_type == document || - parent_type == block_quote || - parent_type == list_item || - (parent_type == list && child_type == list_item) ); + return ( parent_type == BLOCK_DOCUMENT || + parent_type == BLOCK_BQUOTE || + parent_type == BLOCK_LIST_ITEM || + (parent_type == BLOCK_LIST && child_type == BLOCK_LIST_ITEM) ); } static inline bool accepts_lines(int block_type) { - return (block_type == paragraph || - block_type == atx_header || - block_type == indented_code || - block_type == fenced_code); + return (block_type == BLOCK_PARAGRAPH || + block_type == BLOCK_ATX_HEADER || + block_type == BLOCK_INDENTED_CODE || + block_type == BLOCK_FENCED_CODE); } static void add_line(node_block* node_block, chunk *ch, int offset) @@ -116,7 +116,7 @@ static bool ends_with_blank_line(node_block* node_block) if (node_block->last_line_blank) { return true; } - if ((node_block->tag == list || node_block->tag == list_item) && node_block->last_child) { + if ((node_block->tag == BLOCK_LIST || node_block->tag == BLOCK_LIST_ITEM) && node_block->last_child) { return ends_with_blank_line(node_block->last_child); } else { return false; @@ -128,8 +128,8 @@ static int break_out_of_lists(node_block ** bptr, int line_number) { node_block * container = *bptr; node_block * b = container->top; - // find first containing list: - while (b && b->tag != list) { + // find first containing BLOCK_LIST: + while (b && b->tag != BLOCK_LIST) { b = b->last_child; } if (b) { @@ -162,7 +162,7 @@ static void finalize(node_block* b, int line_number) } switch (b->tag) { - case paragraph: + case BLOCK_PARAGRAPH: pos = 0; while (strbuf_at(&b->string_content, 0) == '[' && (pos = parse_reference(&b->string_content, b->top->attributes.refmap))) { @@ -170,16 +170,16 @@ static void finalize(node_block* b, int line_number) strbuf_drop(&b->string_content, pos); } if (is_blank(&b->string_content, 0)) { - b->tag = reference_def; + b->tag = BLOCK_REFERENCE_DEF; } break; - case indented_code: + case BLOCK_INDENTED_CODE: remove_trailing_blank_lines(&b->string_content); strbuf_putc(&b->string_content, '\n'); break; - case fenced_code: + case BLOCK_FENCED_CODE: // first line of contents becomes info firstlinelen = strbuf_strchr(&b->string_content, '\n', 0); @@ -196,7 +196,7 @@ static void finalize(node_block* b, int line_number) unescape_buffer(&b->attributes.fenced_code_data.info); break; - case list: // determine tight/loose status + case BLOCK_LIST: // determine tight/loose status b->attributes.list_data.tight = true; // tight by default item = b->children; @@ -266,9 +266,9 @@ extern void free_blocks(node_block* e) next = e->next; free_inlines(e->inline_content); strbuf_free(&e->string_content); - if (e->tag == fenced_code) { + if (e->tag == BLOCK_FENCED_CODE) { strbuf_free(&e->attributes.fenced_code_data.info); - } else if (e->tag == document) { + } else if (e->tag == BLOCK_DOCUMENT) { free_reference_map(e->attributes.refmap); } free_blocks(e->children); @@ -282,9 +282,9 @@ extern void free_blocks(node_block* e) void process_inlines(node_block* cur, reference** refmap) { switch (cur->tag) { - case paragraph: - case atx_header: - case setext_header: + case BLOCK_PARAGRAPH: + case BLOCK_ATX_HEADER: + case BLOCK_SETEXT_HEADER: cur->inline_content = parse_inlines(&cur->string_content, refmap); // MEM // strbuf_free(&cur->string_content); @@ -507,7 +507,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) indent = first_nonspace - offset; blank = peek_at(&input, first_nonspace) == '\n'; - if (container->tag == block_quote) { + if (container->tag == BLOCK_BQUOTE) { matched = indent <= 3 && peek_at(&input, first_nonspace) == '>'; if (matched) { offset = first_nonspace + 1; @@ -517,7 +517,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) all_matched = false; } - } else if (container->tag == list_item) { + } else if (container->tag == BLOCK_LIST_ITEM) { if (indent >= container->attributes.list_data.marker_offset + container->attributes.list_data.padding) { @@ -529,7 +529,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) all_matched = false; } - } else if (container->tag == indented_code) { + } else if (container->tag == BLOCK_INDENTED_CODE) { if (indent >= CODE_INDENT) { offset += CODE_INDENT; @@ -539,13 +539,13 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) all_matched = false; } - } else if (container->tag == atx_header || - container->tag == setext_header) { + } else if (container->tag == BLOCK_ATX_HEADER || + container->tag == BLOCK_SETEXT_HEADER) { // a header can never contain more than one line all_matched = false; - } else if (container->tag == fenced_code) { + } else if (container->tag == BLOCK_FENCED_CODE) { // skip optional spaces of fence offset i = container->attributes.fenced_code_data.fence_offset; @@ -554,13 +554,13 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) i--; } - } else if (container->tag == html_block) { + } else if (container->tag == BLOCK_HTML) { if (blank) { all_matched = false; } - } else if (container->tag == paragraph) { + } else if (container->tag == BLOCK_PARAGRAPH) { if (blank) { container->last_line_blank = true; @@ -583,8 +583,8 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) } // unless last matched container is code node_block, try new container starts: - while (container->tag != fenced_code && container->tag != indented_code && - container->tag != html_block) { + while (container->tag != BLOCK_FENCED_CODE && container->tag != BLOCK_INDENTED_CODE && + container->tag != BLOCK_HTML) { first_nonspace = offset; while (peek_at(&input, first_nonspace) == ' ') @@ -594,9 +594,9 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) blank = peek_at(&input, first_nonspace) == '\n'; if (indent >= CODE_INDENT) { - if (cur->tag != paragraph && !blank) { + if (cur->tag != BLOCK_PARAGRAPH && !blank) { offset += CODE_INDENT; - container = add_child(container, indented_code, line_number, offset + 1); + container = add_child(container, BLOCK_INDENTED_CODE, line_number, offset + 1); } else { // indent > 4 in lazy line break; } @@ -607,12 +607,12 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) // optional following character if (peek_at(&input, offset) == ' ') offset++; - container = add_child(container, block_quote, line_number, offset + 1); + container = add_child(container, BLOCK_BQUOTE, line_number, offset + 1); } else if ((matched = scan_atx_header_start(&input, first_nonspace))) { offset = first_nonspace + matched; - container = add_child(container, atx_header, line_number, offset + 1); + container = add_child(container, BLOCK_ATX_HEADER, line_number, offset + 1); int hashpos = chunk_strchr(&input, '#', first_nonspace); int level = 0; @@ -625,7 +625,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) } else if ((matched = scan_open_code_fence(&input, first_nonspace))) { - container = add_child(container, fenced_code, line_number, first_nonspace + 1); + container = add_child(container, BLOCK_FENCED_CODE, line_number, first_nonspace + 1); container->attributes.fenced_code_data.fence_char = peek_at(&input, first_nonspace); container->attributes.fenced_code_data.fence_length = matched; container->attributes.fenced_code_data.fence_offset = first_nonspace - offset; @@ -633,24 +633,24 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) } else if ((matched = scan_html_block_tag(&input, first_nonspace))) { - container = add_child(container, html_block, line_number, first_nonspace + 1); + container = add_child(container, BLOCK_HTML, line_number, first_nonspace + 1); // note, we don't adjust offset because the tag is part of the text - } else if (container->tag == paragraph && + } else if (container->tag == BLOCK_PARAGRAPH && (lev = scan_setext_header_line(&input, first_nonspace)) && // check that there is only one line in the paragraph: strbuf_strrchr(&container->string_content, '\n', strbuf_len(&container->string_content) - 2) < 0) { - container->tag = setext_header; + container->tag = BLOCK_SETEXT_HEADER; container->attributes.header_level = lev; offset = input.len - 1; - } else if (!(container->tag == paragraph && !all_matched) && + } else if (!(container->tag == BLOCK_PARAGRAPH && !all_matched) && (matched = scan_hrule(&input, first_nonspace))) { // it's only now that we know the line is not part of a setext header: - container = add_child(container, hrule, line_number, first_nonspace + 1); + container = add_child(container, BLOCK_HRULE, line_number, first_nonspace + 1); finalize(container, line_number); container = container->parent; offset = input.len - 1; @@ -679,15 +679,15 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) data->marker_offset = indent; - if (container->tag != list || + if (container->tag != BLOCK_LIST || !lists_match(container->attributes.list_data, *data)) { - container = add_child(container, list, line_number, + container = add_child(container, BLOCK_LIST, line_number, first_nonspace + 1); container->attributes.list_data = *data; } // add the list item - container = add_child(container, list_item, line_number, + container = add_child(container, BLOCK_LIST_ITEM, line_number, first_nonspace + 1); /* TODO: static */ container->attributes.list_data = *data; @@ -718,9 +718,9 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) // lists or breaking out of lists. we also don't set last_line_blank // on an empty list item. container->last_line_blank = (blank && - container->tag != block_quote && - container->tag != fenced_code && - !(container->tag == list_item && + container->tag != BLOCK_BQUOTE && + container->tag != BLOCK_FENCED_CODE && + !(container->tag == BLOCK_LIST_ITEM && container->children == NULL && container->start_line == line_number)); @@ -733,7 +733,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) if (cur != last_matched_container && container == last_matched_container && !blank && - cur->tag == paragraph && + cur->tag == BLOCK_PARAGRAPH && strbuf_len(&cur->string_content) > 0) { add_line(cur, &input, offset); @@ -747,11 +747,11 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) assert(cur != NULL); } - if (container->tag == indented_code) { + if (container->tag == BLOCK_INDENTED_CODE) { add_line(container, &input, offset); - } else if (container->tag == fenced_code) { + } else if (container->tag == BLOCK_FENCED_CODE) { matched = 0; if (indent <= 3 && @@ -769,7 +769,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) add_line(container, &input, offset); } - } else if (container->tag == html_block) { + } else if (container->tag == BLOCK_HTML) { add_line(container, &input, offset); @@ -777,7 +777,7 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) // ??? do nothing - } else if (container->tag == atx_header) { + } else if (container->tag == BLOCK_ATX_HEADER) { chop_trailing_hashtags(&input); add_line(container, &input, first_nonspace); @@ -788,10 +788,10 @@ static void incorporate_line(strbuf *line, int line_number, node_block** curptr) add_line(container, &input, first_nonspace); - } else if (container->tag != hrule && container->tag != setext_header) { + } else if (container->tag != BLOCK_HRULE && container->tag != BLOCK_SETEXT_HEADER) { // create paragraph container for line - container = add_child(container, paragraph, line_number, first_nonspace + 1); + container = add_child(container, BLOCK_PARAGRAPH, line_number, first_nonspace + 1); add_line(container, &input, first_nonspace); } else { diff --git a/src/html/html.c b/src/html/html.c index 6041fde..758ec80 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -39,11 +39,11 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) while(b != NULL) { switch(b->tag) { - case document: + case BLOCK_DOCUMENT: blocks_to_html(html, b->children, false); break; - case paragraph: + case BLOCK_PARAGRAPH: if (tight) { inlines_to_html(html, b->inline_content); } else { @@ -54,14 +54,14 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) } break; - case block_quote: + case BLOCK_BQUOTE: cr(html); strbuf_puts(html, "<blockquote>\n"); blocks_to_html(html, b->children, false); strbuf_puts(html, "</blockquote>\n"); break; - case list_item: + case BLOCK_LIST_ITEM: cr(html); strbuf_puts(html, "<li>"); blocks_to_html(html, b->children, tight); @@ -69,7 +69,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_puts(html, "</li>\n"); break; - case list: + case BLOCK_LIST: // make sure a list starts at the beginning of the line: cr(html); data = &(b->attributes.list_data); @@ -87,21 +87,21 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_putc(html, '\n'); break; - case atx_header: - case setext_header: + case BLOCK_ATX_HEADER: + case BLOCK_SETEXT_HEADER: cr(html); strbuf_printf(html, "<h%d>", b->attributes.header_level); inlines_to_html(html, b->inline_content); strbuf_printf(html, "</h%d>\n", b->attributes.header_level); break; - case indented_code: - case fenced_code: + case BLOCK_INDENTED_CODE: + case BLOCK_FENCED_CODE: cr(html); strbuf_puts(html, "<pre"); - if (b->tag == fenced_code) { + if (b->tag == BLOCK_FENCED_CODE) { strbuf *info = &b->attributes.fenced_code_data.info; if (strbuf_len(info) > 0) { @@ -121,15 +121,15 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_puts(html, "</code></pre>\n"); break; - case html_block: + case BLOCK_HTML: strbuf_put(html, b->string_content.ptr, b->string_content.size); break; - case hrule: + case BLOCK_HRULE: strbuf_puts(html, "<hr />\n"); break; - case reference_def: + case BLOCK_REFERENCE_DEF: break; default: diff --git a/src/print.c b/src/print.c index 069d299..0ff86fa 100644 --- a/src/print.c +++ b/src/print.c @@ -45,20 +45,20 @@ extern void print_blocks(node_block* b, int indent) } switch(b->tag) { - case document: + case BLOCK_DOCUMENT: printf("document\n"); print_blocks(b->children, indent + 2); break; - case block_quote: + case BLOCK_BQUOTE: printf("block_quote\n"); print_blocks(b->children, indent + 2); break; - case list_item: + case BLOCK_LIST_ITEM: data = &(b->attributes.list_data); printf("list_item\n"); print_blocks(b->children, indent + 2); break; - case list: + case BLOCK_LIST: data = &(b->attributes.list_data); if (data->list_type == ordered) { printf("list (type=ordered tight=%s start=%d delim=%s)\n", @@ -72,27 +72,27 @@ extern void print_blocks(node_block* b, int indent) } print_blocks(b->children, indent + 2); break; - case atx_header: + case BLOCK_ATX_HEADER: printf("atx_header (level=%d)\n", b->attributes.header_level); print_inlines(b->inline_content, indent + 2); break; - case setext_header: + case BLOCK_SETEXT_HEADER: printf("setext_header (level=%d)\n", b->attributes.header_level); print_inlines(b->inline_content, indent + 2); break; - case paragraph: + case BLOCK_PARAGRAPH: printf("paragraph\n"); print_inlines(b->inline_content, indent + 2); break; - case hrule: + case BLOCK_HRULE: printf("hrule\n"); break; - case indented_code: + case BLOCK_INDENTED_CODE: printf("indented_code "); print_str(b->string_content.ptr, -1); putchar('\n'); break; - case fenced_code: + case BLOCK_FENCED_CODE: printf("fenced_code length=%d info=", b->attributes.fenced_code_data.fence_length); print_str(b->attributes.fenced_code_data.info.ptr, -1); @@ -100,12 +100,12 @@ extern void print_blocks(node_block* b, int indent) print_str(b->string_content.ptr, -1); putchar('\n'); break; - case html_block: + case BLOCK_HTML: printf("html_block "); print_str(b->string_content.ptr, -1); putchar('\n'); break; - case reference_def: + case BLOCK_REFERENCE_DEF: printf("reference_def\n"); break; default: @@ -67,30 +67,29 @@ struct FencedCodeData { strbuf info; }; -struct node_block { - enum { - document, - block_quote, - list, - list_item, - fenced_code, - indented_code, - html_block, - paragraph, - atx_header, - setext_header, - hrule, - reference_def +typedef struct Block { + enum { BLOCK_DOCUMENT, + BLOCK_BQUOTE, + BLOCK_LIST, + BLOCK_LIST_ITEM, + BLOCK_FENCED_CODE, + BLOCK_INDENTED_CODE, + BLOCK_HTML, + BLOCK_PARAGRAPH, + BLOCK_ATX_HEADER, + BLOCK_SETEXT_HEADER, + BLOCK_HRULE, + BLOCK_REFERENCE_DEF } tag; int start_line; int start_column; int end_line; bool open; bool last_line_blank; - struct node_block* children; - struct node_block* last_child; - struct node_block* parent; - struct node_block* top; + struct Block* children; + struct Block* last_child; + struct Block* parent; + struct Block* top; strbuf string_content; node_inl* inline_content; union { @@ -99,11 +98,9 @@ struct node_block { int header_level; reference** refmap; } attributes; - struct node_block * next; - struct node_block * prev; -}; - -typedef struct node_block node_block; + struct Block * next; + struct Block * prev; +} node_block; node_inl* parse_inlines(strbuf *input, reference** refmap); void free_inlines(node_inl* e); |