blob: a9e6d20a82f0aa9b61a6bad3fe400e0f09b74d4c (
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
|
#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_INLINE_CODE:
case CMARK_NODE_INLINE_HTML:
return 1;
default:
return 0;
}
}
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;
}
|