From 09c365fb0a75c9da46a1319aed1888665d6dc627 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 16 Nov 2014 22:35:03 -0800 Subject: Added some basic functions for traversing/editing blocks in AST. --- src/cmark.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cmark.h | 28 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) (limited to 'src') diff --git a/src/cmark.c b/src/cmark.c index e7f6899..768d462 100644 --- a/src/cmark.c +++ b/src/cmark.c @@ -7,6 +7,72 @@ #include "buffer.h" #include "ast.h" +// AST traversal and manipulation functions + +cmark_node_block *cmark_block_next(cmark_node_block *current) +{ + return current->next; +} + +cmark_node_block *cmark_block_previous(cmark_node_block *current) +{ + return current->prev; +} + +cmark_node_block *cmark_block_parent(cmark_node_block *current) +{ + return current->parent; +} + +cmark_node_block *cmark_block_children(cmark_node_block *current) +{ + return current->children; +} + +void cmark_block_delete(cmark_node_block *current) +{ + if (current->prev) { + current->prev->next = current->next; + } + if (current->next) { + current->next->prev = current->prev; + } + current->next = NULL; + cmark_free_blocks(current); +} + +void cmark_block_insert_before(cmark_node_block *new, cmark_node_block *current) +{ + // Find last node in new: + cmark_node_block *new_last = new; + while (new_last->next) { + new_last = new_last->next; + } + new_last->next = current; + current->prev = new_last; + if (current->prev) { + current->prev->next = new; + new->prev = current->prev; + } +} + +void cmark_block_insert_after(cmark_node_block *current, cmark_node_block *new) +{ + // Find last node in new: + cmark_node_block *new_last = new; + while (new_last->next) { + new_last = new_last->next; + } + if (current->next) { + new_last->next = current->next; + current->next->prev = new_last; + } + current->next = new; + new->prev = current; +} + +/* * */ + unsigned char *cmark_markdown_to_html(unsigned char *text, int len) { node_block *blocks; diff --git a/src/cmark.h b/src/cmark.h index 0b0196a..faa0cc0 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -87,6 +87,27 @@ void cmark_free_blocks(cmark_node_block *e); CMARK_EXPORT void cmark_free_inlines(cmark_node_inl* e); +CMARK_EXPORT +cmark_node_block *cmark_block_next(cmark_node_block *current); + +CMARK_EXPORT +cmark_node_block *cmark_block_previous(cmark_node_block *current); + +CMARK_EXPORT +cmark_node_block *cmark_block_parent(cmark_node_block *current); + +CMARK_EXPORT +cmark_node_block *cmark_block_children(cmark_node_block *current); + +CMARK_EXPORT +void cmark_block_delete(cmark_node_block *current); + +CMARK_EXPORT +void cmark_block_insert_before(cmark_node_block *new, cmark_node_block *current); + +CMARK_EXPORT +void cmark_block_insert_after(cmark_node_block *current, cmark_node_block *new); + #ifndef CMARK_NO_SHORT_NAMES #define node_inl cmark_node_inl #define INL_STRING CMARK_INL_STRING @@ -124,6 +145,13 @@ void cmark_free_inlines(cmark_node_inl* e); #define free_doc_parser cmark_free_doc_parser #define process_line cmark_process_line #define finish cmark_finish + #define block_next cmark_block_next + #define block_previous cmark_block_previous + #define block_parent cmark_block_parent + #define block_children cmark_block_children + #define block_delete cmark_block_delete + #define block_insert_before cmark_block_insert_before + #define block_insert_after cmark_block_insert_after #endif #ifdef __cplusplus -- cgit v1.2.3