summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-11-16 22:35:03 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2014-11-16 22:35:03 -0800
commit09c365fb0a75c9da46a1319aed1888665d6dc627 (patch)
tree32200fe4a9827ff8b185622ef485a69b7f62b4ae
parent9ab35064d99ebe70e80dba88fa75f22f79751cb5 (diff)
Added some basic functions for traversing/editing blocks in AST.
-rw-r--r--src/cmark.c66
-rw-r--r--src/cmark.h28
2 files changed, 94 insertions, 0 deletions
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