123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858 |
- #include <stdlib.h>
- #include <string.h>
- #include "config.h"
- #include "node.h"
- static void S_node_unlink(cmark_node *node);
- #define NODE_MEM(node) cmark_node_mem(node)
- static CMARK_INLINE bool S_is_block(cmark_node *node) {
- if (node == NULL) {
- return false;
- }
- return node->type >= CMARK_NODE_FIRST_BLOCK &&
- node->type <= CMARK_NODE_LAST_BLOCK;
- }
- static CMARK_INLINE bool S_is_inline(cmark_node *node) {
- if (node == NULL) {
- return false;
- }
- return node->type >= CMARK_NODE_FIRST_INLINE &&
- node->type <= CMARK_NODE_LAST_INLINE;
- }
- static bool S_can_contain(cmark_node *node, cmark_node *child) {
- cmark_node *cur;
- if (node == NULL || child == NULL) {
- return false;
- }
- // 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;
- }
- switch (node->type) {
- case CMARK_NODE_DOCUMENT:
- case CMARK_NODE_BLOCK_QUOTE:
- case CMARK_NODE_ITEM:
- return S_is_block(child) && child->type != CMARK_NODE_ITEM;
- case CMARK_NODE_LIST:
- return child->type == CMARK_NODE_ITEM;
- case CMARK_NODE_CUSTOM_BLOCK:
- return true;
- case CMARK_NODE_PARAGRAPH:
- case CMARK_NODE_HEADING:
- case CMARK_NODE_EMPH:
- case CMARK_NODE_STRONG:
- case CMARK_NODE_LINK:
- case CMARK_NODE_IMAGE:
- case CMARK_NODE_CUSTOM_INLINE:
- return S_is_inline(child);
- default:
- break;
- }
- return false;
- }
- cmark_node *cmark_node_new_with_mem(cmark_node_type type, cmark_mem *mem) {
- cmark_node *node = (cmark_node *)mem->calloc(1, sizeof(*node));
- cmark_strbuf_init(mem, &node->content, 0);
- node->type = (uint16_t)type;
- switch (node->type) {
- case CMARK_NODE_HEADING:
- node->as.heading.level = 1;
- break;
- case CMARK_NODE_LIST: {
- cmark_list *list = &node->as.list;
- list->list_type = CMARK_BULLET_LIST;
- list->start = 0;
- list->tight = false;
- break;
- }
- default:
- break;
- }
- return node;
- }
- cmark_node *cmark_node_new(cmark_node_type type) {
- extern cmark_mem DEFAULT_MEM_ALLOCATOR;
- return cmark_node_new_with_mem(type, &DEFAULT_MEM_ALLOCATOR);
- }
- // Free a cmark_node list and any children.
- static void S_free_nodes(cmark_node *e) {
- cmark_node *next;
- while (e != NULL) {
- cmark_strbuf_free(&e->content);
- switch (e->type) {
- case CMARK_NODE_CODE_BLOCK:
- cmark_chunk_free(NODE_MEM(e), &e->as.code.info);
- cmark_chunk_free(NODE_MEM(e), &e->as.code.literal);
- break;
- case CMARK_NODE_TEXT:
- case CMARK_NODE_HTML_INLINE:
- case CMARK_NODE_CODE:
- case CMARK_NODE_HTML_BLOCK:
- cmark_chunk_free(NODE_MEM(e), &e->as.literal);
- break;
- case CMARK_NODE_LINK:
- case CMARK_NODE_IMAGE:
- cmark_chunk_free(NODE_MEM(e), &e->as.link.url);
- cmark_chunk_free(NODE_MEM(e), &e->as.link.title);
- break;
- case CMARK_NODE_CUSTOM_BLOCK:
- case CMARK_NODE_CUSTOM_INLINE:
- cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_enter);
- cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_exit);
- break;
- default:
- break;
- }
- if (e->last_child) {
- // Splice children into list
- e->last_child->next = e->next;
- e->next = e->first_child;
- }
- next = e->next;
- NODE_MEM(e)->free(e);
- e = next;
- }
- }
- void cmark_node_free(cmark_node *node) {
- S_node_unlink(node);
- node->next = NULL;
- S_free_nodes(node);
- }
- cmark_node_type cmark_node_get_type(cmark_node *node) {
- if (node == NULL) {
- return CMARK_NODE_NONE;
- } else {
- return (cmark_node_type)node->type;
- }
- }
- const char *cmark_node_get_type_string(cmark_node *node) {
- if (node == NULL) {
- return "NONE";
- }
- 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_BLOCK:
- return "html_block";
- case CMARK_NODE_CUSTOM_BLOCK:
- return "custom_block";
- case CMARK_NODE_PARAGRAPH:
- return "paragraph";
- case CMARK_NODE_HEADING:
- return "heading";
- case CMARK_NODE_THEMATIC_BREAK:
- return "thematic_break";
- 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_HTML_INLINE:
- return "html_inline";
- case CMARK_NODE_CUSTOM_INLINE:
- return "custom_inline";
- 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>";
- }
- cmark_node *cmark_node_next(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- } else {
- return node->next;
- }
- }
- cmark_node *cmark_node_previous(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- } else {
- return node->prev;
- }
- }
- cmark_node *cmark_node_parent(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- } else {
- return node->parent;
- }
- }
- cmark_node *cmark_node_first_child(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- } else {
- return node->first_child;
- }
- }
- cmark_node *cmark_node_last_child(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- } else {
- return node->last_child;
- }
- }
- void *cmark_node_get_user_data(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- } else {
- return node->user_data;
- }
- }
- int cmark_node_set_user_data(cmark_node *node, void *user_data) {
- if (node == NULL) {
- return 0;
- }
- node->user_data = user_data;
- return 1;
- }
- const char *cmark_node_get_literal(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- }
- switch (node->type) {
- case CMARK_NODE_HTML_BLOCK:
- case CMARK_NODE_TEXT:
- case CMARK_NODE_HTML_INLINE:
- case CMARK_NODE_CODE:
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal);
- case CMARK_NODE_CODE_BLOCK:
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.literal);
- default:
- break;
- }
- return NULL;
- }
- int cmark_node_set_literal(cmark_node *node, const char *content) {
- if (node == NULL) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_HTML_BLOCK:
- case CMARK_NODE_TEXT:
- case CMARK_NODE_HTML_INLINE:
- case CMARK_NODE_CODE:
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.literal, content);
- return 1;
- case CMARK_NODE_CODE_BLOCK:
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.literal, content);
- return 1;
- default:
- break;
- }
- return 0;
- }
- int cmark_node_get_heading_level(cmark_node *node) {
- if (node == NULL) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_HEADING:
- return node->as.heading.level;
- default:
- break;
- }
- return 0;
- }
- int cmark_node_set_heading_level(cmark_node *node, int level) {
- if (node == NULL || level < 1 || level > 6) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_HEADING:
- node->as.heading.level = level;
- return 1;
- default:
- break;
- }
- return 0;
- }
- cmark_list_type 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 {
- return CMARK_NO_LIST;
- }
- }
- int cmark_node_set_list_type(cmark_node *node, cmark_list_type type) {
- if (!(type == CMARK_BULLET_LIST || type == CMARK_ORDERED_LIST)) {
- return 0;
- }
- if (node == NULL) {
- return 0;
- }
- if (node->type == CMARK_NODE_LIST) {
- node->as.list.list_type = type;
- return 1;
- } else {
- return 0;
- }
- }
- cmark_delim_type 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 {
- return CMARK_NO_DELIM;
- }
- }
- int cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) {
- if (!(delim == CMARK_PERIOD_DELIM || delim == CMARK_PAREN_DELIM)) {
- return 0;
- }
- if (node == NULL) {
- return 0;
- }
- if (node->type == CMARK_NODE_LIST) {
- node->as.list.delimiter = delim;
- return 1;
- } else {
- return 0;
- }
- }
- int 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 {
- return 0;
- }
- }
- int cmark_node_set_list_start(cmark_node *node, int start) {
- if (node == NULL || start < 0) {
- return 0;
- }
- if (node->type == CMARK_NODE_LIST) {
- node->as.list.start = start;
- return 1;
- } else {
- return 0;
- }
- }
- int 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 {
- return 0;
- }
- }
- int cmark_node_set_list_tight(cmark_node *node, int tight) {
- if (node == NULL) {
- return 0;
- }
- if (node->type == CMARK_NODE_LIST) {
- node->as.list.tight = tight == 1;
- return 1;
- } else {
- return 0;
- }
- }
- const char *cmark_node_get_fence_info(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- }
- if (node->type == CMARK_NODE_CODE_BLOCK) {
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.info);
- } else {
- return NULL;
- }
- }
- int cmark_node_set_fence_info(cmark_node *node, const char *info) {
- if (node == NULL) {
- return 0;
- }
- if (node->type == CMARK_NODE_CODE_BLOCK) {
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.info, info);
- return 1;
- } else {
- return 0;
- }
- }
- const char *cmark_node_get_url(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- }
- switch (node->type) {
- case CMARK_NODE_LINK:
- case CMARK_NODE_IMAGE:
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.url);
- default:
- break;
- }
- return NULL;
- }
- int cmark_node_set_url(cmark_node *node, const char *url) {
- if (node == NULL) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_LINK:
- case CMARK_NODE_IMAGE:
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.url, url);
- return 1;
- default:
- break;
- }
- return 0;
- }
- const char *cmark_node_get_title(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- }
- switch (node->type) {
- case CMARK_NODE_LINK:
- case CMARK_NODE_IMAGE:
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.title);
- default:
- break;
- }
- return NULL;
- }
- int cmark_node_set_title(cmark_node *node, const char *title) {
- if (node == NULL) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_LINK:
- case CMARK_NODE_IMAGE:
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.title, title);
- return 1;
- default:
- break;
- }
- return 0;
- }
- const char *cmark_node_get_on_enter(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- }
- switch (node->type) {
- case CMARK_NODE_CUSTOM_INLINE:
- case CMARK_NODE_CUSTOM_BLOCK:
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_enter);
- default:
- break;
- }
- return NULL;
- }
- int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) {
- if (node == NULL) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_CUSTOM_INLINE:
- case CMARK_NODE_CUSTOM_BLOCK:
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter);
- return 1;
- default:
- break;
- }
- return 0;
- }
- const char *cmark_node_get_on_exit(cmark_node *node) {
- if (node == NULL) {
- return NULL;
- }
- switch (node->type) {
- case CMARK_NODE_CUSTOM_INLINE:
- case CMARK_NODE_CUSTOM_BLOCK:
- return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_exit);
- default:
- break;
- }
- return NULL;
- }
- int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) {
- if (node == NULL) {
- return 0;
- }
- switch (node->type) {
- case CMARK_NODE_CUSTOM_INLINE:
- case CMARK_NODE_CUSTOM_BLOCK:
- cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit);
- return 1;
- default:
- break;
- }
- return 0;
- }
- int cmark_node_get_start_line(cmark_node *node) {
- if (node == NULL) {
- return 0;
- }
- return node->start_line;
- }
- int cmark_node_get_start_column(cmark_node *node) {
- if (node == NULL) {
- return 0;
- }
- return node->start_column;
- }
- int cmark_node_get_end_line(cmark_node *node) {
- if (node == NULL) {
- return 0;
- }
- return node->end_line;
- }
- int cmark_node_get_end_column(cmark_node *node) {
- if (node == NULL) {
- return 0;
- }
- return node->end_column;
- }
- // Unlink a node without adjusting its next, prev, and parent pointers.
- static void S_node_unlink(cmark_node *node) {
- if (node == NULL) {
- return;
- }
- if (node->prev) {
- node->prev->next = node->next;
- }
- if (node->next) {
- node->next->prev = node->prev;
- }
- // Adjust first_child and last_child of parent.
- cmark_node *parent = node->parent;
- if (parent) {
- if (parent->first_child == node) {
- parent->first_child = node->next;
- }
- if (parent->last_child == node) {
- parent->last_child = node->prev;
- }
- }
- }
- void cmark_node_unlink(cmark_node *node) {
- S_node_unlink(node);
- node->next = NULL;
- node->prev = NULL;
- node->parent = NULL;
- }
- int cmark_node_insert_before(cmark_node *node, cmark_node *sibling) {
- if (node == NULL || sibling == NULL) {
- return 0;
- }
- if (!node->parent || !S_can_contain(node->parent, sibling)) {
- return 0;
- }
- S_node_unlink(sibling);
- cmark_node *old_prev = node->prev;
- // Insert 'sibling' between 'old_prev' and 'node'.
- if (old_prev) {
- old_prev->next = sibling;
- }
- sibling->prev = old_prev;
- sibling->next = node;
- node->prev = sibling;
- // Set new parent.
- cmark_node *parent = node->parent;
- sibling->parent = parent;
- // Adjust first_child of parent if inserted as first child.
- if (parent && !old_prev) {
- parent->first_child = sibling;
- }
- return 1;
- }
- int cmark_node_insert_after(cmark_node *node, cmark_node *sibling) {
- if (node == NULL || sibling == NULL) {
- return 0;
- }
- if (!node->parent || !S_can_contain(node->parent, sibling)) {
- return 0;
- }
- S_node_unlink(sibling);
- cmark_node *old_next = node->next;
- // Insert 'sibling' between 'node' and 'old_next'.
- if (old_next) {
- old_next->prev = sibling;
- }
- sibling->next = old_next;
- sibling->prev = node;
- node->next = sibling;
- // Set new parent.
- cmark_node *parent = node->parent;
- sibling->parent = parent;
- // Adjust last_child of parent if inserted as last child.
- if (parent && !old_next) {
- parent->last_child = sibling;
- }
- return 1;
- }
- int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode) {
- if (!cmark_node_insert_before(oldnode, newnode)) {
- return 0;
- }
- cmark_node_unlink(oldnode);
- return 1;
- }
- int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
- if (!S_can_contain(node, child)) {
- return 0;
- }
- S_node_unlink(child);
- cmark_node *old_first_child = node->first_child;
- child->next = old_first_child;
- child->prev = NULL;
- child->parent = node;
- node->first_child = child;
- if (old_first_child) {
- old_first_child->prev = child;
- } else {
- // Also set last_child if node previously had no children.
- node->last_child = child;
- }
- return 1;
- }
- int cmark_node_append_child(cmark_node *node, cmark_node *child) {
- if (!S_can_contain(node, child)) {
- return 0;
- }
- S_node_unlink(child);
- cmark_node *old_last_child = node->last_child;
- child->next = NULL;
- child->prev = old_last_child;
- child->parent = node;
- node->last_child = child;
- if (old_last_child) {
- old_last_child->next = child;
- } else {
- // Also set first_child if node previously had no children.
- node->first_child = child;
- }
- return 1;
- }
- static void S_print_error(FILE *out, cmark_node *node, const char *elem) {
- if (out == NULL) {
- 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);
- }
- int cmark_node_check(cmark_node *node, FILE *out) {
- cmark_node *cur;
- int errors = 0;
- if (!node) {
- return 0;
- }
- cur = node;
- for (;;) {
- if (cur->first_child) {
- if (cur->first_child->prev != NULL) {
- S_print_error(out, cur->first_child, "prev");
- cur->first_child->prev = NULL;
- ++errors;
- }
- if (cur->first_child->parent != cur) {
- S_print_error(out, cur->first_child, "parent");
- cur->first_child->parent = cur;
- ++errors;
- }
- cur = cur->first_child;
- continue;
- }
- next_sibling:
- if (cur == node) {
- break;
- }
- if (cur->next) {
- if (cur->next->prev != cur) {
- S_print_error(out, cur->next, "prev");
- cur->next->prev = cur;
- ++errors;
- }
- if (cur->next->parent != cur->parent) {
- S_print_error(out, cur->next, "parent");
- cur->next->parent = cur->parent;
- ++errors;
- }
- cur = cur->next;
- continue;
- }
- 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;
- }
|