diff options
Diffstat (limited to 'src/node.c')
-rw-r--r-- | src/node.c | 185 |
1 files changed, 110 insertions, 75 deletions
@@ -8,7 +8,8 @@ static void S_node_unlink(cmark_node *node); cmark_node* -cmark_node_new(cmark_node_type type) { +cmark_node_new(cmark_node_type type) +{ cmark_node *node = (cmark_node *)calloc(1, sizeof(*node)); node->type = type; @@ -39,7 +40,7 @@ void S_free_nodes(cmark_node *e) cmark_node *next; while (e != NULL) { cmark_strbuf_free(&e->string_content); - switch (e->type){ + switch (e->type) { case NODE_CODE_BLOCK: cmark_chunk_free(&e->as.code.info); cmark_chunk_free(&e->as.code.literal); @@ -70,7 +71,8 @@ void S_free_nodes(cmark_node *e) } void -cmark_node_free(cmark_node *node) { +cmark_node_free(cmark_node *node) +{ S_node_unlink(node); node->next = NULL; S_free_nodes(node); @@ -94,25 +96,44 @@ cmark_node_get_type_string(cmark_node *node) } switch (node->type) { - case CMARK_NODE_NONE: return "none"; - case CMARK_NODE_DOCUMENT: return "document"; - case CMARK_NODE_BLOCK_QUOTE: return "block_quote"; - case CMARK_NODE_LIST: return "list"; - case CMARK_NODE_ITEM: return "item"; - case CMARK_NODE_CODE_BLOCK: return "code_block"; - case CMARK_NODE_HTML: return "html"; - case CMARK_NODE_PARAGRAPH: return "paragraph"; - case CMARK_NODE_HEADER: return "header"; - case CMARK_NODE_HRULE: return "hrule"; - case CMARK_NODE_TEXT: return "text"; - case CMARK_NODE_SOFTBREAK: return "softbreak"; - case CMARK_NODE_LINEBREAK: return "linebreak"; - case CMARK_NODE_CODE: return "code"; - case CMARK_NODE_INLINE_HTML: return "inline_html"; - case CMARK_NODE_EMPH: return "emph"; - case CMARK_NODE_STRONG: return "strong"; - case CMARK_NODE_LINK: return "link"; - case CMARK_NODE_IMAGE: return "image"; + case CMARK_NODE_NONE: + return "none"; + case CMARK_NODE_DOCUMENT: + return "document"; + case CMARK_NODE_BLOCK_QUOTE: + return "block_quote"; + case CMARK_NODE_LIST: + return "list"; + case CMARK_NODE_ITEM: + return "item"; + case CMARK_NODE_CODE_BLOCK: + return "code_block"; + case CMARK_NODE_HTML: + return "html"; + case CMARK_NODE_PARAGRAPH: + return "paragraph"; + case CMARK_NODE_HEADER: + return "header"; + case CMARK_NODE_HRULE: + return "hrule"; + case CMARK_NODE_TEXT: + return "text"; + case CMARK_NODE_SOFTBREAK: + return "softbreak"; + case CMARK_NODE_LINEBREAK: + return "linebreak"; + case CMARK_NODE_CODE: + return "code"; + case CMARK_NODE_INLINE_HTML: + return "inline_html"; + case CMARK_NODE_EMPH: + return "emph"; + case CMARK_NODE_STRONG: + return "strong"; + case CMARK_NODE_LINK: + return "link"; + case CMARK_NODE_IMAGE: + return "image"; } return "<unknown>"; @@ -169,7 +190,8 @@ cmark_node_last_child(cmark_node *node) } static char* -S_strdup(const char *str) { +S_strdup(const char *str) +{ size_t size = strlen(str) + 1; char *dup = (char *)malloc(size); memcpy(dup, str, size); @@ -177,7 +199,8 @@ S_strdup(const char *str) { } const char* -cmark_node_get_literal(cmark_node *node) { +cmark_node_get_literal(cmark_node *node) +{ if (node == NULL) { return NULL; } @@ -200,7 +223,8 @@ cmark_node_get_literal(cmark_node *node) { } int -cmark_node_set_literal(cmark_node *node, const char *content) { +cmark_node_set_literal(cmark_node *node, const char *content) +{ if (node == NULL) { return 0; } @@ -225,7 +249,8 @@ cmark_node_set_literal(cmark_node *node, const char *content) { } int -cmark_node_get_header_level(cmark_node *node) { +cmark_node_get_header_level(cmark_node *node) +{ if (node == NULL) { return 0; } @@ -242,7 +267,8 @@ cmark_node_get_header_level(cmark_node *node) { } int -cmark_node_set_header_level(cmark_node *node, int level) { +cmark_node_set_header_level(cmark_node *node, int level) +{ if (node == NULL || level < 1 || level > 6) { return 0; } @@ -260,21 +286,22 @@ cmark_node_set_header_level(cmark_node *node, int level) { } cmark_list_type -cmark_node_get_list_type(cmark_node *node) { +cmark_node_get_list_type(cmark_node *node) +{ if (node == NULL) { return CMARK_NO_LIST; } if (node->type == CMARK_NODE_LIST) { return node->as.list.list_type; - } - else { + } else { return CMARK_NO_LIST; } } int -cmark_node_set_list_type(cmark_node *node, cmark_list_type type) { +cmark_node_set_list_type(cmark_node *node, cmark_list_type type) +{ if (!(type == CMARK_BULLET_LIST || type == CMARK_ORDERED_LIST)) { return 0; } @@ -286,28 +313,28 @@ cmark_node_set_list_type(cmark_node *node, cmark_list_type type) { if (node->type == CMARK_NODE_LIST) { node->as.list.list_type = type; return 1; - } - else { + } else { return 0; } } cmark_delim_type -cmark_node_get_list_delim(cmark_node *node) { +cmark_node_get_list_delim(cmark_node *node) +{ if (node == NULL) { return CMARK_NO_DELIM; } if (node->type == CMARK_NODE_LIST) { return node->as.list.delimiter; - } - else { + } else { return CMARK_NO_DELIM; } } int -cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) { +cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) +{ if (!(delim == CMARK_PERIOD_DELIM || delim == CMARK_PAREN_DELIM)) { return 0; } @@ -319,28 +346,28 @@ cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) { if (node->type == CMARK_NODE_LIST) { node->as.list.delimiter = delim; return 1; - } - else { + } else { return 0; } } int -cmark_node_get_list_start(cmark_node *node) { +cmark_node_get_list_start(cmark_node *node) +{ if (node == NULL) { return 0; } if (node->type == CMARK_NODE_LIST) { return node->as.list.start; - } - else { + } else { return 0; } } int -cmark_node_set_list_start(cmark_node *node, int start) { +cmark_node_set_list_start(cmark_node *node, int start) +{ if (node == NULL || start < 0) { return 0; } @@ -348,28 +375,28 @@ cmark_node_set_list_start(cmark_node *node, int start) { if (node->type == CMARK_NODE_LIST) { node->as.list.start = start; return 1; - } - else { + } else { return 0; } } int -cmark_node_get_list_tight(cmark_node *node) { +cmark_node_get_list_tight(cmark_node *node) +{ if (node == NULL) { return 0; } if (node->type == CMARK_NODE_LIST) { return node->as.list.tight; - } - else { + } else { return 0; } } int -cmark_node_set_list_tight(cmark_node *node, int tight) { +cmark_node_set_list_tight(cmark_node *node, int tight) +{ if (node == NULL) { return 0; } @@ -377,28 +404,28 @@ cmark_node_set_list_tight(cmark_node *node, int tight) { if (node->type == CMARK_NODE_LIST) { node->as.list.tight = tight; return 1; - } - else { + } else { return 0; } } const char* -cmark_node_get_fence_info(cmark_node *node) { +cmark_node_get_fence_info(cmark_node *node) +{ if (node == NULL) { return NULL; } if (node->type == NODE_CODE_BLOCK) { return cmark_chunk_to_cstr(&node->as.code.info); - } - else { + } else { return NULL; } } int -cmark_node_set_fence_info(cmark_node *node, const char *info) { +cmark_node_set_fence_info(cmark_node *node, const char *info) +{ if (node == NULL) { return 0; } @@ -406,14 +433,14 @@ cmark_node_set_fence_info(cmark_node *node, const char *info) { if (node->type == NODE_CODE_BLOCK) { cmark_chunk_set_cstr(&node->as.code.info, info); return 1; - } - else { + } else { return 0; } } const char* -cmark_node_get_url(cmark_node *node) { +cmark_node_get_url(cmark_node *node) +{ if (node == NULL) { return NULL; } @@ -430,7 +457,8 @@ cmark_node_get_url(cmark_node *node) { } int -cmark_node_set_url(cmark_node *node, const char *url) { +cmark_node_set_url(cmark_node *node, const char *url) +{ if (node == NULL) { return 0; } @@ -449,7 +477,8 @@ cmark_node_set_url(cmark_node *node, const char *url) { } const char* -cmark_node_get_title(cmark_node *node) { +cmark_node_get_title(cmark_node *node) +{ if (node == NULL) { return NULL; } @@ -466,7 +495,8 @@ cmark_node_get_title(cmark_node *node) { } int -cmark_node_set_title(cmark_node *node, const char *title) { +cmark_node_set_title(cmark_node *node, const char *title) +{ if (node == NULL) { return 0; } @@ -485,7 +515,8 @@ cmark_node_set_title(cmark_node *node, const char *title) { } int -cmark_node_get_start_line(cmark_node *node) { +cmark_node_get_start_line(cmark_node *node) +{ if (node == NULL) { return 0; } @@ -493,7 +524,8 @@ cmark_node_get_start_line(cmark_node *node) { } int -cmark_node_get_start_column(cmark_node *node) { +cmark_node_get_start_column(cmark_node *node) +{ if (node == NULL) { return 0; } @@ -501,7 +533,8 @@ cmark_node_get_start_column(cmark_node *node) { } int -cmark_node_get_end_line(cmark_node *node) { +cmark_node_get_end_line(cmark_node *node) +{ if (node == NULL) { return 0; } @@ -509,7 +542,8 @@ cmark_node_get_end_line(cmark_node *node) { } int -cmark_node_get_end_column(cmark_node *node) { +cmark_node_get_end_column(cmark_node *node) +{ if (node == NULL) { return 0; } @@ -517,7 +551,8 @@ cmark_node_get_end_column(cmark_node *node) { } static inline bool -S_is_block(cmark_node *node) { +S_is_block(cmark_node *node) +{ if (node == NULL) { return false; } @@ -526,7 +561,8 @@ S_is_block(cmark_node *node) { } static inline bool -S_is_inline(cmark_node *node) { +S_is_inline(cmark_node *node) +{ if (node == NULL) { return false; } @@ -609,7 +645,8 @@ S_node_unlink(cmark_node *node) } void -cmark_node_unlink(cmark_node *node) { +cmark_node_unlink(cmark_node *node) +{ S_node_unlink(node); node->next = NULL; @@ -706,8 +743,7 @@ cmark_node_prepend_child(cmark_node *node, cmark_node *child) if (old_first_child) { old_first_child->prev = child; - } - else { + } else { // Also set last_child if node previously had no children. node->last_child = child; } @@ -733,8 +769,7 @@ cmark_node_append_child(cmark_node *node, cmark_node *child) if (old_last_child) { old_last_child->next = child; - } - else { + } else { // Also set first_child if node previously had no children. node->first_child = child; } @@ -749,8 +784,8 @@ S_print_error(FILE *out, cmark_node *node, const char *elem) return; } fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem, - cmark_node_get_type_string(node), node->start_line, - node->start_column); + cmark_node_get_type_string(node), node->start_line, + node->start_column); } int @@ -780,7 +815,7 @@ cmark_node_check(cmark_node *node, FILE *out) continue; } - next_sibling: +next_sibling: if (cur == node) { break; } |