cmark

My personal build of CMark ✏️

Commit
93843bd2fe8c91fe9e659c5b801f1a7d672dc223
Parent
e384149e683fd6ea950ba9318525563175ca9f47
Author
John MacFarlane <jgm@berkeley.edu>
Date

Added cmark_node_replace(oldnode, newnode).

API change.

I've found in using the API that this is very often wanted.

Diffstat

4 files changed, 45 insertions, 16 deletions

Status File Name N° Changes Insertions Deletions
Modified api_test/main.c 9 8 1
Modified man/man3/cmark.3 38 23 15
Modified src/cmark.h 5 5 0
Modified src/node.c 9 9 0
diff --git a/api_test/main.c b/api_test/main.c
@@ -359,10 +359,17 @@ static void create_tree(test_batch_runner *runner) {
   // 1e3
   OK(runner, cmark_node_previous(emph) == str1, "ins after2 works");
 
+  cmark_node *str4 = cmark_node_new(CMARK_NODE_TEXT);
+  cmark_node_set_literal(str4, "brzz");
+  OK(runner, cmark_node_replace(str1, str4), "replace");
+  INT_EQ(runner, cmark_node_check(doc, NULL), 0, "replace consistent");
+  OK(runner, cmark_node_previous(emph) == str4, "replace works");
+  INT_EQ(runner, cmark_node_replace(p, str4), 0, "replace str for p fails");
+
   cmark_node_unlink(emph);
 
   html = cmark_render_html(doc, CMARK_OPT_DEFAULT);
-  STR_EQ(runner, html, "<p>Hello, !</p>\n", "render_html after shuffling");
+  STR_EQ(runner, html, "<p>brzz!</p>\n", "render_html after shuffling");
   free(html);
 
   cmark_node_free(doc);
diff --git a/man/man3/cmark.3 b/man/man3/cmark.3
@@ -1,4 +1,4 @@
-.TH cmark 3 "December 28, 2015" "LOCAL" "Library Functions Manual"
+.TH cmark 3 "January 10, 2016" "LOCAL" "Library Functions Manual"
 .SH
 NAME
 .PP
@@ -28,8 +28,8 @@ Creating and Destroying Nodes
 
 .PP
 Creates a new node of type \f[I]type\f[]. Note that the node may have
-other required properties, which it is the caller's responsibility to
-assign.
+other required properties, which it is the caller\[cq]s responsibility
+to assign.
 
 .PP
 \fIvoid\f[] \fBcmark_node_free\f[](\fIcmark_node *node\f[])
@@ -286,7 +286,8 @@ Returns 1 if \f[I]node\f[] is a tight list, 0 otherwise.
 \fIint\f[] \fBcmark_node_set_list_tight\f[](\fIcmark_node *node\f[], \fIint tight\f[])
 
 .PP
-Sets the "tightness" of a list. Returns 1 on success, 0 on failure.
+Sets the \[lq]tightness\[rq] of a list. Returns 1 on success, 0 on
+failure.
 
 .PP
 \fIconst char *\f[] \fBcmark_node_get_fence_info\f[](\fIcmark_node *node\f[])
@@ -333,31 +334,31 @@ on failure.
 \fIconst char *\f[] \fBcmark_node_get_on_enter\f[](\fIcmark_node *node\f[])
 
 .PP
-Returns the literal "on enter" text for a custom \f[I]node\f[], or an
-empty string if no on_enter is set.
+Returns the literal \[lq]on enter\[rq] text for a custom \f[I]node\f[],
+or an empty string if no on_enter is set.
 
 .PP
 \fIint\f[] \fBcmark_node_set_on_enter\f[](\fIcmark_node *node\f[], \fIconst char *on_enter\f[])
 
 .PP
-Sets the literal text to render "on enter" for a custom \f[I]node\f[].
-Any children of the node will be rendered after this text. Returns 1 on
-success 0 on failure.
+Sets the literal text to render \[lq]on enter\[rq] for a custom
+\f[I]node\f[]. Any children of the node will be rendered after this
+text. Returns 1 on success 0 on failure.
 
 .PP
 \fIconst char *\f[] \fBcmark_node_get_on_exit\f[](\fIcmark_node *node\f[])
 
 .PP
-Returns the literal "on exit" text for a custom \f[I]node\f[], or an
-empty string if no on_exit is set.
+Returns the literal \[lq]on exit\[rq] text for a custom \f[I]node\f[],
+or an empty string if no on_exit is set.
 
 .PP
 \fIint\f[] \fBcmark_node_set_on_exit\f[](\fIcmark_node *node\f[], \fIconst char *on_exit\f[])
 
 .PP
-Sets the literal text to render "on exit" for a custom \f[I]node\f[].
-Any children of the node will be rendered before this text. Returns 1 on
-success 0 on failure.
+Sets the literal text to render \[lq]on exit\[rq] for a custom
+\f[I]node\f[]. Any children of the node will be rendered before this
+text. Returns 1 on success 0 on failure.
 
 .PP
 \fIint\f[] \fBcmark_node_get_start_line\f[](\fIcmark_node *node\f[])
@@ -408,6 +409,13 @@ Inserts \f[I]sibling\f[] after \f[I]node\f[]. Returns 1 on success, 0 on
 failure.
 
 .PP
+\fIint\f[] \fBcmark_node_replace\f[](\fIcmark_node *oldnode\f[], \fIcmark_node *newnode\f[])
+
+.PP
+Replaces \f[I]oldnode\f[] with \f[I]newnode\f[] and frees the memory
+used by \f[I]oldnode\f[]. Returns 1 on success, 0 on failure.
+
+.PP
 \fIint\f[] \fBcmark_node_prepend_child\f[](\fIcmark_node *node\f[], \fIcmark_node *child\f[])
 
 .PP
@@ -624,7 +632,7 @@ with the replacement character U+FFFD.
 .fi
 
 .PP
-Convert straight quotes to curly, \-\-\- to em dashes, \-\- to en
+Convert straight quotes to curly, \[em] to em dashes, \[en] to en
 dashes.
 
 .SS
diff --git a/src/cmark.h b/src/cmark.h
@@ -387,6 +387,11 @@ CMARK_EXPORT int cmark_node_insert_before(cmark_node *node,
  */
 CMARK_EXPORT int cmark_node_insert_after(cmark_node *node, cmark_node *sibling);
 
+/** Replaces 'oldnode' with 'newnode' and frees the memory used by 'oldnode'.
+ * Returns 1 on success, 0 on failure.
+ */
+CMARK_EXPORT int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode);
+
 /** Adds 'child' to the beginning of the children of 'node'.
  * Returns 1 on success, 0 on failure.
  */
diff --git a/src/node.c b/src/node.c
@@ -730,6 +730,15 @@ int cmark_node_insert_after(cmark_node *node, cmark_node *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);
+  cmark_node_free(oldnode);
+  return 1;
+}
+
 int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
   if (!S_can_contain(node, child)) {
     return 0;