diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-11-22 10:28:16 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-11-22 10:28:16 -0800 |
commit | 36f6ab248ab8391bb6dbaab97e23010a9fb82ce8 (patch) | |
tree | 1414de21ed1017974e7ff30b6b0d4327cbfd0dd7 /src | |
parent | aebf7308ef00a8c2150200dc641d3eeee80a8303 (diff) | |
parent | b2dac901288a3431fdadd9bbefb7875596a13e4d (diff) |
Merge pull request #210 from nwellnhof/nwellnhof_141122
C API fixes, additional tests, stackless rendering
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/blocks.c | 18 | ||||
-rw-r--r-- | src/cmark.c | 4 | ||||
-rw-r--r-- | src/cmark.h | 8 | ||||
-rw-r--r-- | src/html/html.c | 469 | ||||
-rw-r--r-- | src/main.c | 4 | ||||
-rw-r--r-- | src/node.c | 85 | ||||
-rw-r--r-- | src/node.h | 4 |
8 files changed, 310 insertions, 284 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dbe53a5..71f45a7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,7 +64,7 @@ set_target_properties(${PROGRAM} PROPERTIES COMPILE_FLAGS -DCMARK_STATIC_DEFINE) # Check integrity of node structure when compiled as debug: -set(CMAKE_C_FLAGS_DEBUG "-DCMARK_DEBUG_NODES") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DCMARK_DEBUG_NODES") if (${CMAKE_MAJOR_VERSION} GREATER 1 AND ${CMAKE_MINOR_VERSION} GREATER 8) set(CMAKE_C_VISIBILITY_PRESET hidden) diff --git a/src/blocks.c b/src/blocks.c index 58162b5..a30560f 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -416,13 +416,13 @@ static cmark_node *finalize_document(cmark_doc_parser *parser) extern cmark_node *cmark_parse_file(FILE *f) { - unsigned char buffer[4096]; + char buffer[4096]; cmark_doc_parser *parser = cmark_new_doc_parser(); size_t offset; cmark_node *document; - while (fgets((char *)buffer, sizeof(buffer), f)) { - offset = strlen((char *)buffer); + while (fgets(buffer, sizeof(buffer), f)) { + offset = strlen(buffer); cmark_process_line(parser, buffer, offset); } @@ -431,16 +431,16 @@ extern cmark_node *cmark_parse_file(FILE *f) return document; } -extern cmark_node *cmark_parse_document(const unsigned char *buffer, size_t len) +extern cmark_node *cmark_parse_document(const char *buffer, size_t len) { int linenum = 1; - const unsigned char *end = buffer + len; + const char *end = buffer + len; size_t offset; cmark_doc_parser *parser = cmark_new_doc_parser(); cmark_node *document; while (buffer < end) { - const unsigned char *eol = (unsigned char *)memchr(buffer, '\n', end - buffer); + const char *eol = memchr(buffer, '\n', end - buffer); offset = eol ? (eol - buffer) + 1 : eol - buffer; cmark_process_line(parser, buffer, offset); buffer += offset; @@ -470,7 +470,7 @@ static void chop_trailing_hashtags(chunk *ch) } } -void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer, +void cmark_process_line(cmark_doc_parser *parser, const char *buffer, size_t bytes) { cmark_node* last_matched_container; @@ -487,7 +487,7 @@ void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer, int indent; chunk input; - utf8proc_detab(parser->curline, buffer, bytes); + utf8proc_detab(parser->curline, (unsigned char *)buffer, bytes); // Add a newline to the end if not present: // TODO this breaks abstraction: @@ -818,7 +818,7 @@ cmark_node *cmark_finish(cmark_doc_parser *parser) finalize_document(parser); strbuf_free(parser->curline); #if CMARK_DEBUG_NODES - if (cmark_node_check(parser->root)) { + if (cmark_node_check(parser->root, stderr)) { abort(); } #endif diff --git a/src/cmark.c b/src/cmark.c index b20b84b..273a37b 100644 --- a/src/cmark.c +++ b/src/cmark.c @@ -7,10 +7,10 @@ #include "cmark.h" #include "buffer.h" -unsigned char *cmark_markdown_to_html(unsigned char *text, int len) +char *cmark_markdown_to_html(const char *text, int len) { cmark_node *blocks; - unsigned char *result; + char *result; blocks = cmark_parse_document(text, len); diff --git a/src/cmark.h b/src/cmark.h index a74fe93..6131024 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -176,10 +176,10 @@ CMARK_EXPORT cmark_node *cmark_finish(cmark_doc_parser *parser); CMARK_EXPORT -void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer, size_t bytes); +void cmark_process_line(cmark_doc_parser *parser, const char *buffer, size_t bytes); CMARK_EXPORT -cmark_node *cmark_parse_document(const unsigned char *buffer, size_t len); +cmark_node *cmark_parse_document(const char *buffer, size_t len); CMARK_EXPORT cmark_node *cmark_parse_file(FILE *f); @@ -190,10 +190,10 @@ CMARK_EXPORT void cmark_debug_print(cmark_node *root); CMARK_EXPORT -unsigned char *cmark_render_html(cmark_node *root); +char *cmark_render_html(cmark_node *root); CMARK_EXPORT -unsigned char *cmark_markdown_to_html(unsigned char *text, int len); +char *cmark_markdown_to_html(const char *text, int len); #ifndef CMARK_NO_SHORT_NAMES #define NODE_DOCUMENT CMARK_NODE_DOCUMENT diff --git a/src/html/html.c b/src/html/html.c index 76d488a..4fa79b1 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -7,58 +7,12 @@ #include "cmark.h" #include "node.h" #include "buffer.h" -#include "debug.h" #include "html/houdini.h" -typedef struct RenderStack { - struct RenderStack *previous; - const char* literal; - cmark_node* next_sibling; - bool tight; - bool trim; -} render_stack; +// Functions to convert cmark_nodes to HTML strings. -static void free_render_stack(render_stack * rstack) -{ - render_stack * tempstack; - while (rstack) { - tempstack = rstack; - rstack = rstack->previous; - free(tempstack); - } -} - -static render_stack* push_render_stack(render_stack* rstack, - cmark_node* node, - const char* literal) -{ - render_stack* newstack; - newstack = (render_stack*)malloc(sizeof(render_stack)); - if (newstack == NULL) { - return NULL; - } - newstack->previous = rstack; - newstack->next_sibling = node; - newstack->literal = literal; - newstack->tight = false; - newstack->trim = false; - return newstack; -} - -static render_stack* pop_render_stack(render_stack* rstack) -{ - render_stack* top = rstack; - - if (rstack == NULL) { - return NULL; - } - rstack = rstack->previous; - top->previous = NULL; - free_render_stack(top); - return rstack; -} - -// Functions to convert cmark_node and inline lists to HTML strings. +static bool +finish_node(strbuf *html, cmark_node *node, bool tight); static void escape_html(strbuf *dest, const unsigned char *source, int length) { @@ -82,65 +36,163 @@ static inline void cr(strbuf *html) strbuf_putc(html, '\n'); } -// Convert an inline list to HTML. Returns 0 on success, and sets result. -static void inlines_to_plain_html(strbuf *html, cmark_node* ils) +// Convert the inline children of a node to a plain string. +static void inlines_to_plain_html(strbuf *html, cmark_node* node) { - cmark_node* children; - bool visit_children; - render_stack* rstack = NULL; + cmark_node* cur = node->first_child; - while(ils != NULL) { - visit_children = false; - switch(ils->type) { + if (cur == NULL) { + return; + } + + while (true) { + switch(cur->type) { case NODE_STRING: case NODE_INLINE_CODE: case NODE_INLINE_HTML: - escape_html(html, ils->as.literal.data, ils->as.literal.len); + escape_html(html, cur->as.literal.data, cur->as.literal.len); break; case NODE_LINEBREAK: case NODE_SOFTBREAK: - strbuf_putc(html, '\n'); + strbuf_putc(html, ' '); break; - case NODE_LINK: - case NODE_IMAGE: - case NODE_STRONG: - case NODE_EMPH: - children = ils->first_child; - visit_children = true; - rstack = push_render_stack(rstack, ils->next, ""); - break; default: break; } - if (visit_children) { - ils = children; - } else { - ils = ils->next; + + if (cur->first_child) { + cur = cur->first_child; + continue; } - while (ils == NULL && rstack != NULL) { - strbuf_puts(html, rstack->literal); - ils = rstack->next_sibling; - rstack = pop_render_stack(rstack); + + next_sibling: + if (cur->next) { + cur = cur->next; + continue; } + cur = cur->parent; + if (cur == node) { + break; + } + goto next_sibling; } - - free_render_stack(rstack); } -// Convert an inline list to HTML. Returns 0 on success, and sets result. -static void inlines_to_html(strbuf *html, cmark_node* ils) +// Convert a cmark_node to HTML. +static void node_to_html(strbuf *html, cmark_node *node) { + cmark_node *cur; + char start_header[] = "<h0>"; + bool tight = false; bool visit_children; - render_stack* rstack = NULL; - while(ils != NULL) { - visit_children = false; - switch(ils->type) { + if (node == NULL) { + return; + } + + cur = node; + while (true) { + // Only NODE_IMAGE wants to skip its children. + visit_children = true; + + switch(cur->type) { + case NODE_DOCUMENT: + break; + + case NODE_PARAGRAPH: + if (!tight) { + cr(html); + strbuf_puts(html, "<p>"); + } + break; + + case NODE_BQUOTE: + cr(html); + strbuf_puts(html, "<blockquote>\n"); + // BQUOTE doesn't use any of the 'as' structs, + // so the 'list' member can be used to store the + // current value of 'tight'. + cur->as.list.tight = tight; + tight = false; + break; + + case NODE_LIST_ITEM: + cr(html); + strbuf_puts(html, "<li>"); + break; + + case NODE_LIST: { + cmark_list *list = &cur->as.list; + bool tmp; + + // make sure a list starts at the beginning of the line: + cr(html); + + if (list->list_type == CMARK_BULLET_LIST) { + strbuf_puts(html, "<ul>\n"); + } + else if (list->start == 1) { + strbuf_puts(html, "<ol>\n"); + } + else { + strbuf_printf(html, "<ol start=\"%d\">\n", + list->start); + } + + // Store the current value of 'tight' by swapping. + tmp = list->tight; + list->tight = tight; + tight = tmp; + break; + } + + case NODE_ATX_HEADER: + case NODE_SETEXT_HEADER: + cr(html); + start_header[2] = '0' + cur->as.header.level; + strbuf_puts(html, start_header); + break; + + case NODE_INDENTED_CODE: + case NODE_FENCED_CODE: { + strbuf *info = &cur->as.code.info; + cr(html); + + if (cur->type != NODE_FENCED_CODE + || strbuf_len(info) == 0) { + strbuf_puts(html, "<pre><code>"); + } + else { + int first_tag = strbuf_strchr(info, ' ', 0); + if (first_tag < 0) + first_tag = strbuf_len(info); + + strbuf_puts(html, + "<pre><code class=\"language-"); + escape_html(html, info->ptr, first_tag); + strbuf_puts(html, "\">"); + } + + escape_html(html, cur->string_content.ptr, cur->string_content.size); + break; + } + + case NODE_HTML: + strbuf_put(html, cur->string_content.ptr, cur->string_content.size); + break; + + case NODE_HRULE: + strbuf_puts(html, "<hr />\n"); + break; + + case NODE_REFERENCE_DEF: + break; + case NODE_STRING: - escape_html(html, ils->as.literal.data, ils->as.literal.len); + escape_html(html, cur->as.literal.data, cur->as.literal.len); break; case NODE_LINEBREAK: @@ -153,218 +205,155 @@ static void inlines_to_html(strbuf *html, cmark_node* ils) case NODE_INLINE_CODE: strbuf_puts(html, "<code>"); - escape_html(html, ils->as.literal.data, ils->as.literal.len); - strbuf_puts(html, "</code>"); + escape_html(html, cur->as.literal.data, cur->as.literal.len); break; case NODE_INLINE_HTML: strbuf_put(html, - ils->as.literal.data, - ils->as.literal.len); + cur->as.literal.data, + cur->as.literal.len); break; case NODE_LINK: strbuf_puts(html, "<a href=\""); - if (ils->as.link.url) - escape_href(html, ils->as.link.url, -1); + if (cur->as.link.url) + escape_href(html, cur->as.link.url, -1); - if (ils->as.link.title) { + if (cur->as.link.title) { strbuf_puts(html, "\" title=\""); - escape_html(html, ils->as.link.title, -1); + escape_html(html, cur->as.link.title, -1); } strbuf_puts(html, "\">"); - visit_children = true; - rstack = push_render_stack(rstack, ils->next, "</a>"); break; case NODE_IMAGE: strbuf_puts(html, "<img src=\""); - if (ils->as.link.url) - escape_href(html, ils->as.link.url, -1); + if (cur->as.link.url) + escape_href(html, cur->as.link.url, -1); strbuf_puts(html, "\" alt=\""); - inlines_to_plain_html(html, ils->first_child); + inlines_to_plain_html(html, cur); - if (ils->as.link.title) { + if (cur->as.link.title) { strbuf_puts(html, "\" title=\""); - escape_html(html, ils->as.link.title, -1); + escape_html(html, cur->as.link.title, -1); } strbuf_puts(html, "\" />"); + visit_children = false; break; case NODE_STRONG: strbuf_puts(html, "<strong>"); - visit_children = true; - rstack = push_render_stack(rstack, ils->next, "</strong>"); break; case NODE_EMPH: strbuf_puts(html, "<em>"); - visit_children = true; - rstack = push_render_stack(rstack, ils->next, "</em>"); break; + default: - break; + assert(false); } - if (visit_children) { - ils = ils->first_child; - } else { - ils = ils->next; + + if (visit_children && cur->first_child) { + cur = cur->first_child; + continue; + } + + next_sibling: + tight = finish_node(html, cur, tight); + if (cur == node) { + break; } - while (ils == NULL && rstack != NULL) { - strbuf_puts(html, rstack->literal); - ils = rstack->next_sibling; - rstack = pop_render_stack(rstack); + if (cur->next) { + cur = cur->next; + continue; } + cur = cur->parent; + goto next_sibling; } - - free_render_stack(rstack); } -// Convert a cmark_node list to HTML. Returns 0 on success, and sets result. -static void blocks_to_html(strbuf *html, cmark_node *b) +// Returns the restored value of 'tight'. +static bool +finish_node(strbuf *html, cmark_node *node, bool tight) { - cmark_list *data; - render_stack* rstack = NULL; - bool visit_children = false; - bool tight = false; - - while(b != NULL) { - visit_children = false; - switch(b->type) { - case NODE_DOCUMENT: - rstack = push_render_stack(rstack, b->next, ""); - rstack->tight = false; - rstack->trim = false; - visit_children = true; - break; - - case NODE_PARAGRAPH: - if (tight) { - inlines_to_html(html, b->first_child); - } else { - cr(html); - strbuf_puts(html, "<p>"); - inlines_to_html(html, b->first_child); - strbuf_puts(html, "</p>\n"); - } - break; + char end_header[] = "</h0>\n"; - case NODE_BQUOTE: - cr(html); - strbuf_puts(html, "<blockquote>\n"); - rstack = push_render_stack(rstack, b->next, "</blockquote>\n"); - rstack->tight = tight; - rstack->trim = false; - tight = false; - visit_children = true; - break; - - case NODE_LIST_ITEM: - cr(html); - strbuf_puts(html, "<li>"); - rstack = push_render_stack(rstack, b->next, "</li>\n"); - rstack->tight = tight; - rstack->trim = true; - visit_children = true; - break; - - case NODE_LIST: - // make sure a list starts at the beginning of the line: - cr(html); - data = &(b->as.list); - - if (data->start > 1) { - strbuf_printf(html, "<%s start=\"%d\">\n", - data->list_type == CMARK_BULLET_LIST ? "ul" : "ol", - data->start); - } else { - strbuf_puts(html, data->list_type == CMARK_BULLET_LIST ? "<ul>\n" : "<ol>\n"); - } - - rstack = push_render_stack(rstack, b->next, - data->list_type == CMARK_BULLET_LIST ? - "\n</ul>\n" : "\n</ol>\n"); - rstack->tight = tight; - rstack->trim = false; - tight = data->tight; - visit_children = true; - break; - - case NODE_ATX_HEADER: - case NODE_SETEXT_HEADER: - cr(html); - strbuf_printf(html, "<h%d>", b->as.header.level); - inlines_to_html(html, b->first_child); - strbuf_printf(html, "</h%d>\n", b->as.header.level); - break; - - case NODE_INDENTED_CODE: - case NODE_FENCED_CODE: - cr(html); - - strbuf_puts(html, "<pre><code"); + switch (node->type) { + case NODE_PARAGRAPH: + if (!tight) { + strbuf_puts(html, "</p>\n"); + } + break; + + case NODE_BQUOTE: { + cmark_list *list = &node->as.list; + strbuf_puts(html, "</blockquote>\n"); + // Restore old 'tight' value. + tight = list->tight; + list->tight = false; + break; + } - if (b->type == NODE_FENCED_CODE) { - strbuf *info = &b->as.code.info; + case NODE_LIST_ITEM: + strbuf_puts(html, "</li>\n"); + break; + + case NODE_LIST: { + cmark_list *list = &node->as.list; + bool tmp; + strbuf_puts(html, + list->list_type == CMARK_BULLET_LIST ? + "</ul>\n" : "</ol>\n"); + // Restore old 'tight' value. + tmp = tight; + tight = list->tight; + list->tight = tmp; + break; + } - if (strbuf_len(info) > 0) { - int first_tag = strbuf_strchr(info, ' ', 0); - if (first_tag < 0) - first_tag = strbuf_len(info); + case NODE_ATX_HEADER: + case NODE_SETEXT_HEADER: + end_header[3] = '0' + node->as.header.level; + strbuf_puts(html, end_header); + break; - strbuf_puts(html, " class=\"language-"); - escape_html(html, info->ptr, first_tag); - strbuf_putc(html, '"'); - } - } + case NODE_INDENTED_CODE: + case NODE_FENCED_CODE: + strbuf_puts(html, "</code></pre>\n"); + break; - strbuf_putc(html, '>'); - escape_html(html, b->string_content.ptr, b->string_content.size); - strbuf_puts(html, "</code></pre>\n"); - break; + case NODE_INLINE_CODE: + strbuf_puts(html, "</code>"); + break; - case NODE_HTML: - strbuf_put(html, b->string_content.ptr, b->string_content.size); - break; + case NODE_LINK: + strbuf_puts(html, "</a>"); + break; - case NODE_HRULE: - strbuf_puts(html, "<hr />\n"); - break; + case NODE_STRONG: + strbuf_puts(html, "</strong>"); + break; - case NODE_REFERENCE_DEF: - break; + case NODE_EMPH: + strbuf_puts(html, "</em>"); + break; - default: - assert(false); - } - if (visit_children) { - b = b->first_child; - } else { - b = b->next; - } - while (b == NULL && rstack != NULL) { - strbuf_puts(html, rstack->literal); - if (rstack->trim) { - strbuf_rtrim(html); - } - tight = rstack->tight; - b = rstack->next_sibling; - rstack = pop_render_stack(rstack); - } + default: + break; } - free_render_stack(rstack); + return tight; } -unsigned char *cmark_render_html(cmark_node *root) +char *cmark_render_html(cmark_node *root) { - unsigned char *result; + char *result; strbuf html = GH_BUF_INIT; - blocks_to_html(&html, root); - result = strbuf_detach(&html); + node_to_html(&html, root); + result = (char *)strbuf_detach(&html); strbuf_free(&html); return result; } @@ -17,7 +17,7 @@ void print_usage() static void print_document(cmark_node *document, bool ast) { - unsigned char *result; + char *result; if (ast) { cmark_debug_print(document); } else { @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) int i, numfps = 0; bool ast = false; int *files; - unsigned char buffer[4096]; + char buffer[4096]; cmark_doc_parser *parser; size_t offset; cmark_node *document; @@ -11,6 +11,25 @@ cmark_node* cmark_node_new(cmark_node_type type) { cmark_node *node = (cmark_node *)calloc(1, sizeof(*node)); node->type = type; + + switch (node->type) { + case CMARK_NODE_ATX_HEADER: + case CMARK_NODE_SETEXT_HEADER: + node->as.header.level = 1; + break; + + case CMARK_NODE_LIST: { + cmark_list *list = &node->as.list; + list->list_type = CMARK_BULLET_LIST; + list->start = 1; + list->tight = false; + break; + } + + default: + break; + } + return node; } @@ -348,6 +367,17 @@ S_is_inline(cmark_node *node) { static bool S_can_contain(cmark_node *node, cmark_node *child) { + cmark_node *cur; + + // Verify that child is not an ancestor of node or equal to node. + cur = node; + do { + if (cur == child) { + return false; + } + cur = cur->parent; + } while (cur != NULL); + if (child->type == CMARK_NODE_DOCUMENT) { return false; } @@ -528,58 +558,63 @@ cmark_node_append_child(cmark_node *node, cmark_node *child) } static void -S_print_error(cmark_node *node, const char *elem) +S_print_error(FILE *out, cmark_node *node, const char *elem) { - fprintf(stderr, "Invalid '%s' in node type %s at %d:%d\n", elem, + if (out == NULL) { + return; + } + fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem, S_type_string(node), node->start_line, node->start_column); } int -cmark_node_check(cmark_node *node) +cmark_node_check(cmark_node *node, FILE *out) { - cmark_node *cur = node; + cmark_node *cur; int errors = 0; - while (cur) { + if (!node) { + return 0; + } + + cur = node; + while (true) { if (cur->first_child) { if (cur->first_child->parent != cur) { - S_print_error(cur->first_child, "parent"); + S_print_error(out, cur->first_child, "parent"); cur->first_child->parent = cur; ++errors; } cur = cur->first_child; + continue; } - else if (cur->next) { + + next_sibling: + if (cur == node) { + break; + } + if (cur->next) { if (cur->next->prev != cur) { - S_print_error(cur->next, "prev"); + S_print_error(out, cur->next, "prev"); cur->next->prev = cur; ++errors; } if (cur->next->parent != cur->parent) { - S_print_error(cur->next, "parent"); + S_print_error(out, cur->next, "parent"); cur->next->parent = cur->parent; ++errors; } cur = cur->next; + continue; } - else { - if (cur->parent->last_child != cur) { - S_print_error(cur->parent, "last_child"); - cur->parent->last_child = cur; - ++errors; - } - - cmark_node *ancestor = cur->parent; - cur = NULL; - while (ancestor != node->parent) { - if (ancestor->next) { - cur = ancestor->next; - break; - } - ancestor = ancestor->parent; - } + if (cur->parent->last_child != cur) { + S_print_error(out, cur->parent, "last_child"); + cur->parent->last_child = cur; + ++errors; } + cur = cur->parent; + goto next_sibling; } return errors; @@ -5,6 +5,8 @@ extern "C" { #endif +#include <stdio.h> + #include "cmark.h" #include "buffer.h" #include "chunk.h" @@ -62,7 +64,7 @@ struct cmark_node { }; CMARK_EXPORT int -cmark_node_check(cmark_node *node); +cmark_node_check(cmark_node *node, FILE *out); #ifdef __cplusplus } |