From c589d7345a311b54d5a642759820a254e129e4ff Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 13 Nov 2014 21:33:40 -0800 Subject: Moved ast-manipulating functions from inlines to ast. --- src/ast.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 102 insertions(+), 18 deletions(-) (limited to 'src/ast.c') diff --git a/src/ast.c b/src/ast.c index 2a9ca8f..6e3f0ee 100644 --- a/src/ast.c +++ b/src/ast.c @@ -3,17 +3,18 @@ #include "buffer.h" #include "ast.h" #include "references.h" +#include "html/houdini.h" // Free a node_block list and any children. -void cmark_free_blocks(node_block *e) +void cmark_free_blocks(cmark_node_block *e) { - node_block * next; + cmark_node_block * next; while (e != NULL) { - free_inlines(e->inline_content); + cmark_free_inlines(e->inline_content); strbuf_free(&e->string_content); - if (e->tag == BLOCK_FENCED_CODE) { + if (e->tag == CMARK_BLOCK_FENCED_CODE) { strbuf_free(&e->as.code.info); - } else if (e->tag == BLOCK_DOCUMENT) { + } else if (e->tag == CMARK_BLOCK_DOCUMENT) { reference_map_free(e->as.document.refmap); } if (e->last_child) { @@ -28,8 +29,8 @@ void cmark_free_blocks(node_block *e) } // Utility function used by free_inlines -static void splice_into_list(node_inl* e, node_inl* children) { - node_inl * tmp; +static void splice_into_list(cmark_node_inl* e, node_inl* children) { + cmark_node_inl * tmp; if (children) { tmp = children; // Find last child @@ -45,28 +46,28 @@ static void splice_into_list(node_inl* e, node_inl* children) { // Free an inline list. Avoid recursion to prevent stack overflows // on deeply nested structures. -extern void free_inlines(node_inl* e) +void cmark_free_inlines(cmark_node_inl* e) { node_inl * next; while (e != NULL) { switch (e->tag){ - case INL_STRING: - case INL_RAW_HTML: - case INL_CODE: - chunk_free(&e->content.literal); + case CMARK_INL_STRING: + case CMARK_INL_RAW_HTML: + case CMARK_INL_CODE: + cmark_chunk_free(&e->content.literal); break; - case INL_LINEBREAK: - case INL_SOFTBREAK: + case CMARK_INL_LINEBREAK: + case CMARK_INL_SOFTBREAK: break; - case INL_LINK: - case INL_IMAGE: + case CMARK_INL_LINK: + case CMARK_INL_IMAGE: free(e->content.linkable.url); free(e->content.linkable.title); splice_into_list(e, e->content.linkable.label); break; - case INL_EMPH: - case INL_STRONG: + case CMARK_INL_EMPH: + case CMARK_INL_STRONG: splice_into_list(e, e->content.inlines); break; default: @@ -79,3 +80,86 @@ extern void free_inlines(node_inl* e) e = next; } } + +inline cmark_node_inl *cmark_make_link(cmark_node_inl *label, unsigned char *url, unsigned char *title) +{ + cmark_node_inl* e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = CMARK_INL_LINK; + e->content.linkable.label = label; + e->content.linkable.url = url; + e->content.linkable.title = title; + e->next = NULL; + } + return e; +} + +unsigned char *clean_autolink(chunk *url, int is_email) +{ + strbuf buf = GH_BUF_INIT; + + chunk_trim(url); + + if (url->len == 0) + return NULL; + + if (is_email) + strbuf_puts(&buf, "mailto:"); + + houdini_unescape_html_f(&buf, url->data, url->len); + return strbuf_detach(&buf); +} + +cmark_node_inl* cmark_make_autolink(cmark_node_inl* label, chunk url, int is_email) +{ + return cmark_make_link(label, clean_autolink(&url, is_email), NULL); +} + +inline cmark_node_inl* cmark_make_inlines(int t, cmark_node_inl* contents) +{ + cmark_node_inl * e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = t; + e->content.inlines = contents; + e->next = NULL; + } + return e; +} + +// Create an inline with a literal string value. +inline cmark_node_inl* cmark_make_literal(int t, cmark_chunk s) +{ + cmark_node_inl * e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = t; + e->content.literal = s; + e->next = NULL; + } + return e; +} + +// Create an inline with no value. +inline cmark_node_inl* cmark_make_simple(int t) +{ + cmark_node_inl* e = calloc(1, sizeof(*e)); + if(e != NULL) { + e->tag = t; + e->next = NULL; + } + return e; +} + +// Append inline list b to the end of inline list a. +// Return pointer to head of new list. +inline cmark_node_inl* cmark_append_inlines(cmark_node_inl* a, cmark_node_inl* b) +{ + if (a == NULL) { // NULL acts like an empty list + return b; + } + cmark_node_inl* cur = a; + while (cur->next) { + cur = cur->next; + } + cur->next = b; + return a; +} -- cgit v1.2.3