cmark

My personal build of CMark ✏️

Commit
09c365fb0a75c9da46a1319aed1888665d6dc627
Parent
9ab35064d99ebe70e80dba88fa75f22f79751cb5
Author
John MacFarlane <jgm@berkeley.edu>
Date

Added some basic functions for traversing/editing blocks in AST.

Diffstat

2 files changed, 94 insertions, 0 deletions

Status File Name N° Changes Insertions Deletions
Modified src/cmark.c 66 66 0
Modified src/cmark.h 28 28 0
diff --git 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
@@ -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