static cmark_node *get_first_parent_block(cmark_node *node) { cmark_node *parent = node; while (cmark_node_get_type(parent) > CMARK_NODE_LAST_BLOCK) { parent = cmark_node_parent(parent); } return parent; }
static int S_render_node(cmark_node *node, cmark_event_type ev_type, struct render_state *state, int options) { cmark_node *parent; cmark_node *grandparent; cmark_strbuf *html = state->html; char start_header[] = "<h0"; char end_header[] = "</h0"; bool tight; bool entering = (ev_type == CMARK_EVENT_ENTER); if (state->plain == node) { // back at original node state->plain = NULL; } if (state->plain != NULL) { switch(node->type) { case CMARK_NODE_TEXT: case CMARK_NODE_CODE: case CMARK_NODE_INLINE_HTML: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: case CMARK_NODE_SOFTBREAK: cmark_strbuf_putc(html, ' '); break; default: break; } return 1; } switch (node->type) { case CMARK_NODE_DOCUMENT: break; case CMARK_NODE_BLOCK_QUOTE: if (entering) { cr(html); cmark_strbuf_puts(html, "<blockquote"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { cr(html); cmark_strbuf_puts(html, "</blockquote>\n"); } break; case CMARK_NODE_LIST: { cmark_list_type list_type = node->as.list.list_type; int start = node->as.list.start; if (entering) { cr(html); if (list_type == CMARK_BULLET_LIST) { cmark_strbuf_puts(html, "<ul"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else if (start == 1) { cmark_strbuf_puts(html, "<ol"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { cmark_strbuf_printf(html, "<ol start=\"%d\"", start); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } } else { cmark_strbuf_puts(html, list_type == CMARK_BULLET_LIST ? "</ul>\n" : "</ol>\n"); } break; } case CMARK_NODE_ITEM: if (entering) { cr(html); cmark_strbuf_puts(html, "<li"); S_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { cmark_strbuf_puts(html, "</li>\n"); } break; case CMARK_NODE_HEADER: if (entering) { cr(html); start_header[2] = '0' + node->as.header.level; cmark_strbuf_puts(html, start_header); S_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { end_header[3] = '0' + node->as.header.level; cmark_strbuf_puts(html, end_header); cmark_strbuf_puts(html, ">\n"); } break; case CMARK_NODE_CODE_BLOCK: cr(html); if (!node->as.code.fenced || node->as.code.info.len == 0) { cmark_strbuf_puts(html, "<pre"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code>"); } else { int first_tag = 0; while (first_tag < node->as.code.info.len && node->as.code.info.data[first_tag] != ' ') { first_tag += 1; } cmark_strbuf_puts(html, "<pre"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code class=\"language-"); escape_html(html, node->as.code.info.data, first_tag); cmark_strbuf_puts(html, "\">"); } escape_html(html, node->as.code.literal.data, node->as.code.literal.len); cmark_strbuf_puts(html, "</code></pre>\n"); break; case CMARK_NODE_HTML: cr(html); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_HRULE: cr(html); cmark_strbuf_puts(html, "<hr"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, " />\n"); break; case CMARK_NODE_PARAGRAPH: parent = cmark_node_parent(node); grandparent = cmark_node_parent(parent); if (grandparent != NULL && grandparent->type == CMARK_NODE_LIST) { tight = grandparent->as.list.tight; } else { tight = false; } if (!tight) { if (entering) { cr(html); cmark_strbuf_puts(html, "<p"); S_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { cmark_strbuf_puts(html, "</p>\n"); } } break; case CMARK_NODE_TEXT: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: cmark_strbuf_puts(html, "<br />\n"); break; case CMARK_NODE_SOFTBREAK: if (options & CMARK_OPT_HARDBREAKS) { cmark_strbuf_puts(html, "<br />\n"); } else { cmark_strbuf_putc(html, '\n'); } break; case CMARK_NODE_CODE: cmark_strbuf_puts(html, "<code>"); escape_html(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "</code>"); break; case CMARK_NODE_INLINE_HTML: cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_STRONG: if (entering) { cmark_strbuf_puts(html, "<strong>"); } else { cmark_strbuf_puts(html, "</strong>"); } break; case CMARK_NODE_EMPH: if (entering) { cmark_strbuf_puts(html, "<em>"); } else { cmark_strbuf_puts(html, "</em>"); } break; case CMARK_NODE_LINK: if (entering) { cmark_strbuf_puts(html, "<a href=\""); if (node->as.link.url) escape_href(html, node->as.link.url, -1); if (node->as.link.title) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title, -1); } cmark_strbuf_puts(html, "\">"); } else { cmark_strbuf_puts(html, "</a>"); } break; case CMARK_NODE_IMAGE: if (entering) { cmark_strbuf_puts(html, "<img src=\""); if (node->as.link.url) escape_href(html, node->as.link.url, -1); cmark_strbuf_puts(html, "\" alt=\""); state->plain = node; } else { if (node->as.link.title) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title, -1); } cmark_strbuf_puts(html, "\" />"); } break; default: assert(false); break; } // cmark_strbuf_putc(html, 'x'); return 1; }
static void create_tree(test_batch_runner *runner) { char *html; cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT); cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH); OK(runner, !cmark_node_insert_before(doc, p), "insert before root fails"); OK(runner, !cmark_node_insert_after(doc, p), "insert after root fails"); OK(runner, cmark_node_append_child(doc, p), "append1"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append1 consistent"); OK(runner, cmark_node_parent(p) == doc, "node_parent"); cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH); OK(runner, cmark_node_prepend_child(p, emph), "prepend1"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend1 consistent"); cmark_node *str1 = cmark_node_new(CMARK_NODE_TEXT); cmark_node_set_literal(str1, "Hello, "); OK(runner, cmark_node_prepend_child(p, str1), "prepend2"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend2 consistent"); cmark_node *str3 = cmark_node_new(CMARK_NODE_TEXT); cmark_node_set_literal(str3, "!"); OK(runner, cmark_node_append_child(p, str3), "append2"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append2 consistent"); cmark_node *str2 = cmark_node_new(CMARK_NODE_TEXT); cmark_node_set_literal(str2, "world"); OK(runner, cmark_node_append_child(emph, str2), "append3"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append3 consistent"); html = cmark_render_html(doc, CMARK_OPT_DEFAULT); STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n", "render_html"); free(html); OK(runner, cmark_node_insert_before(str1, str3), "ins before1"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "ins before1 consistent"); // 31e OK(runner, cmark_node_first_child(p) == str3, "ins before1 works"); OK(runner, cmark_node_insert_before(str1, emph), "ins before2"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "ins before2 consistent"); // 3e1 OK(runner, cmark_node_last_child(p) == str1, "ins before2 works"); OK(runner, cmark_node_insert_after(str1, str3), "ins after1"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "ins after1 consistent"); // e13 OK(runner, cmark_node_next(str1) == str3, "ins after1 works"); OK(runner, cmark_node_insert_after(str1, emph), "ins after2"); INT_EQ(runner, cmark_node_check(doc, NULL), 0, "ins after2 consistent"); // 1e3 OK(runner, cmark_node_previous(emph) == str1, "ins after2 works"); cmark_node_unlink(emph); html = cmark_render_html(doc, CMARK_OPT_DEFAULT); STR_EQ(runner, html, "<p>Hello, !</p>\n", "render_html after shuffling"); free(html); cmark_node_free(doc); // TODO: Test that the contents of an unlinked inline are valid // after the parent block was destroyed. This doesn't work so far. cmark_node_free(emph); }
static int S_render_node(cmark_html_renderer *renderer, cmark_node *node, cmark_event_type ev_type, int options) { cmark_node *parent; cmark_node *grandparent; cmark_strbuf *html = renderer->html; cmark_llist *it; cmark_syntax_extension *ext; char start_heading[] = "<h0"; char end_heading[] = "</h0"; bool tight; bool filtered; char buffer[BUFFER_SIZE]; bool entering = (ev_type == CMARK_EVENT_ENTER); if (renderer->plain == node) { // back at original node renderer->plain = NULL; } if (renderer->plain != NULL) { switch (node->type) { case CMARK_NODE_TEXT: case CMARK_NODE_CODE: case CMARK_NODE_HTML_INLINE: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: case CMARK_NODE_SOFTBREAK: cmark_strbuf_putc(html, ' '); break; default: break; } return 1; } if (node->extension && node->extension->html_render_func) { node->extension->html_render_func(node->extension, renderer, node, ev_type, options); return 1; } switch (node->type) { case CMARK_NODE_DOCUMENT: break; case CMARK_NODE_BLOCK_QUOTE: if (entering) { cmark_html_render_cr(html); cmark_strbuf_puts(html, "<blockquote"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { cmark_html_render_cr(html); cmark_strbuf_puts(html, "</blockquote>\n"); } break; case CMARK_NODE_LIST: { cmark_list_type list_type = node->as.list.list_type; int start = node->as.list.start; if (entering) { cmark_html_render_cr(html); if (list_type == CMARK_BULLET_LIST) { cmark_strbuf_puts(html, "<ul"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else if (start == 1) { cmark_strbuf_puts(html, "<ol"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { snprintf(buffer, BUFFER_SIZE, "<ol start=\"%d\"", start); cmark_strbuf_puts(html, buffer); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } } else { cmark_strbuf_puts(html, list_type == CMARK_BULLET_LIST ? "</ul>\n" : "</ol>\n"); } break; } case CMARK_NODE_ITEM: if (entering) { cmark_html_render_cr(html); cmark_strbuf_puts(html, "<li"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { cmark_strbuf_puts(html, "</li>\n"); } break; case CMARK_NODE_HEADING: if (entering) { cmark_html_render_cr(html); start_heading[2] = (char)('0' + node->as.heading.level); cmark_strbuf_puts(html, start_heading); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { end_heading[3] = (char)('0' + node->as.heading.level); cmark_strbuf_puts(html, end_heading); cmark_strbuf_puts(html, ">\n"); } break; case CMARK_NODE_CODE_BLOCK: cmark_html_render_cr(html); if (node->as.code.info.len == 0) { cmark_strbuf_puts(html, "<pre"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code>"); } else { bufsize_t first_tag = 0; while (first_tag < node->as.code.info.len && !cmark_isspace(node->as.code.info.data[first_tag])) { first_tag += 1; } if (options & CMARK_OPT_GITHUB_PRE_LANG) { cmark_strbuf_puts(html, "<pre"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, " lang=\""); escape_html(html, node->as.code.info.data, first_tag); if (first_tag < node->as.code.info.len && (options & CMARK_OPT_FULL_INFO_STRING)) { cmark_strbuf_puts(html, "\" data-meta=\""); escape_html(html, node->as.code.info.data + first_tag + 1, node->as.code.info.len - first_tag - 1); } cmark_strbuf_puts(html, "\"><code>"); } else { cmark_strbuf_puts(html, "<pre"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code class=\"language-"); escape_html(html, node->as.code.info.data, first_tag); if (first_tag < node->as.code.info.len && (options & CMARK_OPT_FULL_INFO_STRING)) { cmark_strbuf_puts(html, "\" data-meta=\""); escape_html(html, node->as.code.info.data + first_tag + 1, node->as.code.info.len - first_tag - 1); } cmark_strbuf_puts(html, "\">"); } } escape_html(html, node->as.code.literal.data, node->as.code.literal.len); cmark_strbuf_puts(html, "</code></pre>\n"); break; case CMARK_NODE_HTML_BLOCK: cmark_html_render_cr(html); if (!(options & CMARK_OPT_UNSAFE)) { cmark_strbuf_puts(html, "<!-- raw HTML omitted -->"); } else if (renderer->filter_extensions) { filter_html_block(renderer, node->as.literal.data, node->as.literal.len); } else { cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); } cmark_html_render_cr(html); break; case CMARK_NODE_CUSTOM_BLOCK: cmark_html_render_cr(html); if (entering) { cmark_strbuf_put(html, node->as.custom.on_enter.data, node->as.custom.on_enter.len); } else { cmark_strbuf_put(html, node->as.custom.on_exit.data, node->as.custom.on_exit.len); } cmark_html_render_cr(html); break; case CMARK_NODE_THEMATIC_BREAK: cmark_html_render_cr(html); cmark_strbuf_puts(html, "<hr"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, " />\n"); break; case CMARK_NODE_PARAGRAPH: parent = cmark_node_parent(node); grandparent = cmark_node_parent(parent); if (grandparent != NULL && grandparent->type == CMARK_NODE_LIST) { tight = grandparent->as.list.tight; } else { tight = false; } if (!tight) { if (entering) { cmark_html_render_cr(html); cmark_strbuf_puts(html, "<p"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { if (parent->type == CMARK_NODE_FOOTNOTE_DEFINITION && node->next == NULL) { cmark_strbuf_putc(html, ' '); S_put_footnote_backref(renderer, html); } cmark_strbuf_puts(html, "</p>\n"); } } break; case CMARK_NODE_TEXT: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: cmark_strbuf_puts(html, "<br />\n"); break; case CMARK_NODE_SOFTBREAK: if (options & CMARK_OPT_HARDBREAKS) { cmark_strbuf_puts(html, "<br />\n"); } else if (options & CMARK_OPT_NOBREAKS) { cmark_strbuf_putc(html, ' '); } else { cmark_strbuf_putc(html, '\n'); } break; case CMARK_NODE_CODE: cmark_strbuf_puts(html, "<code>"); escape_html(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "</code>"); break; case CMARK_NODE_HTML_INLINE: if (!(options & CMARK_OPT_UNSAFE)) { cmark_strbuf_puts(html, "<!-- raw HTML omitted -->"); } else { filtered = false; for (it = renderer->filter_extensions; it; it = it->next) { ext = (cmark_syntax_extension *) it->data; if (!ext->html_filter_func(ext, node->as.literal.data, node->as.literal.len)) { filtered = true; break; } } if (!filtered) { cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); } else { cmark_strbuf_puts(html, "<"); cmark_strbuf_put(html, node->as.literal.data + 1, node->as.literal.len - 1); } } break; case CMARK_NODE_CUSTOM_INLINE: if (entering) { cmark_strbuf_put(html, node->as.custom.on_enter.data, node->as.custom.on_enter.len); } else { cmark_strbuf_put(html, node->as.custom.on_exit.data, node->as.custom.on_exit.len); } break; case CMARK_NODE_STRONG: if (entering) { cmark_strbuf_puts(html, "<strong>"); } else { cmark_strbuf_puts(html, "</strong>"); } break; case CMARK_NODE_EMPH: if (entering) { cmark_strbuf_puts(html, "<em>"); } else { cmark_strbuf_puts(html, "</em>"); } break; case CMARK_NODE_LINK: if (entering) { cmark_strbuf_puts(html, "<a href=\""); if (!(!(options & CMARK_OPT_UNSAFE) && scan_dangerous_url(&node->as.link.url, 0))) { houdini_escape_href(html, node->as.link.url.data, node->as.link.url.len); } if (node->as.link.title.len) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title.data, node->as.link.title.len); } cmark_strbuf_puts(html, "\">"); } else { cmark_strbuf_puts(html, "</a>"); } break; case CMARK_NODE_IMAGE: if (entering) { cmark_strbuf_puts(html, "<img src=\""); if (!(!(options & CMARK_OPT_UNSAFE) && scan_dangerous_url(&node->as.link.url, 0))) { houdini_escape_href(html, node->as.link.url.data, node->as.link.url.len); } cmark_strbuf_puts(html, "\" alt=\""); renderer->plain = node; } else { if (node->as.link.title.len) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title.data, node->as.link.title.len); } cmark_strbuf_puts(html, "\" />"); } break; case CMARK_NODE_FOOTNOTE_DEFINITION: if (entering) { if (renderer->footnote_ix == 0) { cmark_strbuf_puts(html, "<section class=\"footnotes\">\n<ol>\n"); } ++renderer->footnote_ix; cmark_strbuf_puts(html, "<li id=\"fn"); char n[32]; snprintf(n, sizeof(n), "%d", renderer->footnote_ix); cmark_strbuf_puts(html, n); cmark_strbuf_puts(html, "\">\n"); } else { if (S_put_footnote_backref(renderer, html)) { cmark_strbuf_putc(html, '\n'); } cmark_strbuf_puts(html, "</li>\n"); } break; case CMARK_NODE_FOOTNOTE_REFERENCE: if (entering) { cmark_strbuf_puts(html, "<sup class=\"footnote-ref\"><a href=\"#fn"); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "\" id=\"fnref"); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "\">"); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "</a></sup>"); } break; default: assert(false); break; } return 1; }