blob: a3ae4158d8a8173202f1b254717f49777e93f1e0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#include <stdlib.h>
#include "config.h"
#include "node.h"
#include "cmark.h"
#include "iterator.h"
cmark_iter*
cmark_iter_new(cmark_node *root)
{
cmark_iter *iter = (cmark_iter*)malloc(sizeof(cmark_iter));
if (iter == NULL) {
return NULL;
} else {
iter->root = root;
iter->current = root;
iter->event_type = CMARK_EVENT_ENTER;
return iter;
}
}
void
cmark_iter_free(cmark_iter *iter)
{
free(iter);
}
cmark_event_type
cmark_iter_next(cmark_iter *iter)
{
return iter->event_type;
}
int S_is_leaf(cmark_node *node)
{
switch (cmark_node_get_type(node)) {
case CMARK_NODE_HTML:
case CMARK_NODE_HRULE:
case CMARK_NODE_CODE_BLOCK:
case CMARK_NODE_TEXT:
case CMARK_NODE_SOFTBREAK:
case CMARK_NODE_LINEBREAK:
case CMARK_NODE_CODE:
case CMARK_NODE_INLINE_HTML:
return 1;
default:
return 0;
}
}
void
cmark_iter_reset(cmark_iter *iter, cmark_node *current,
cmark_event_type event_type)
{
iter->event_type = event_type;
iter->current = current;
}
cmark_node*
cmark_iter_get_node(cmark_iter *iter)
{
/* we'll return current */
cmark_node *cur = iter->current;
if (cur == NULL || iter->event_type == CMARK_EVENT_DONE) {
return NULL;
}
/* roll forward to next item, setting both fields */
if (iter->event_type == CMARK_EVENT_ENTER && !S_is_leaf(cur)) {
if (cur->first_child == NULL) {
/* stay on this node but exit */
iter->event_type = CMARK_EVENT_EXIT;
} else {
iter->current = cur->first_child;
iter->event_type = CMARK_EVENT_ENTER;
}
} else if (cur == iter->root) {
/* don't move past root */
iter->event_type = CMARK_EVENT_DONE;
iter->current = NULL;
} else if (cur->next) {
iter->event_type = CMARK_EVENT_ENTER;
iter->current = cur->next;
} else if (cur->parent) {
iter->event_type = CMARK_EVENT_EXIT;
iter->current = cur->parent;
} else {
iter->event_type = CMARK_EVENT_DONE;
iter->current = NULL;
}
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);
}
|