summaryrefslogtreecommitdiff
path: root/src/iterator.c
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-01-07 11:17:24 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2015-01-07 11:23:29 -0800
commit52c591d75433b16cf32f4fae319ccb60b20f6ae7 (patch)
treeaf0e956b72dfbc3883252f48d3f2138b55aca367 /src/iterator.c
parenta29c16c5e283fb50ecd318477072687caf987d4a (diff)
cmark: Add function & option to normalize text nodes.
So, instead of <text>Hi</text> <text>&amp;</text> <text>lo</text> we get <text>Hi&amp;lo</text> * Added exported `cmark_consolidate_text_nodes` function. * Added `CMARK_OPT_NORMALIZE` to options. * Added optional normalization in XML writer. * Added `--normalize` option to command-line program. * Updated man page.
Diffstat (limited to 'src/iterator.c')
-rw-r--r--src/iterator.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/iterator.c b/src/iterator.c
index 6ebc9af..b0ac9d2 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -84,3 +84,34 @@ cmark_iter_get_node(cmark_iter *iter)
return cur;
}
+
+
+void cmark_consolidate_text_nodes(cmark_node *root)
+{
+ cmark_iter *iter = cmark_iter_new(root);
+ cmark_strbuf buf = GH_BUF_INIT;
+ cmark_event_type ev_type;
+ cmark_node *cur, *tmp, *next;
+
+ while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
+ cur = cmark_iter_get_node(iter);
+ if (ev_type == CMARK_EVENT_ENTER &&
+ cur->type == CMARK_NODE_TEXT &&
+ cur->next &&
+ cur->next->type == CMARK_NODE_TEXT) {
+ cmark_strbuf_clear(&buf);
+ cmark_strbuf_puts(&buf, cmark_node_get_literal(cur));
+ tmp = cur->next;
+ while (tmp && tmp->type == CMARK_NODE_TEXT) {
+ cmark_iter_get_node(iter); // advance pointer
+ cmark_strbuf_puts(&buf, cmark_node_get_literal(tmp));
+ next = tmp->next;
+ cmark_node_free(tmp);
+ tmp = next;
+ }
+ cmark_node_set_literal(cur, (char *)cmark_strbuf_detach(&buf));
+ }
+ }
+
+ cmark_iter_free(iter);
+}