1CommonMark::Iterator(3)User Contributed Perl DocumentatioCnommonMark::Iterator(3)
2
3
4

NAME

6       CommonMark::Iterator - Iterate CommonMark nodes
7

SYNOPSIS

9           use CommonMark qw(:node :event);
10
11           my $iter = $doc->iterator;
12
13           while (my ($ev_type, $node) = $iter->next) {
14               my $node_type = $node->get_type;
15
16               if ($node_type == NODE_PARAGRAPH) {
17                   if ($ev_type == EVENT_ENTER) {
18                       print("<p>");
19                   }
20                   else {
21                       print("</p>\n");
22                   }
23               }
24               elsif ($node_type == NODE_TEXT) {
25                   print($node->get_literal);
26               }
27           }
28

DESCRIPTION

30       "CommonMark::Iterator" provides a convenient way to walk through the
31       nodes in a parse tree.
32
33   Construction
34          my $iterator = $node->iterator;
35
36       Creates an iterator from a node. $node is the root node of the
37       iterator.
38
39   next
40           my $ev_type = $iterator->next;
41           my ($ev_type, $node) = $iterator->next;
42
43       The contents of the iterator are initially undefined. After the first
44       and each subsequent call to "next", the iterator holds a new event type
45       and a new current node. In scalar context, "next" returns the new event
46       type.  In list context, it returns a 2-element list consisting of the
47       new event type and the new current node.
48
49       Event types are:
50
51           CommonMark::EVENT_DONE
52           CommonMark::EVENT_ENTER
53           CommonMark::EVENT_EXIT
54
55       Event types can be imported from CommonMark with tag "event".
56
57           use CommonMark qw(:event);
58
59       The iterator starts by visiting the root node. Every visited node "V"
60       generates the following sequence of events.
61
62       •   Enter the node. The event type is "CommonMark::EVENT_ENTER" and the
63           current node is set to the entered node "V".
64
65       •   Visit all children of the node "V" from first to last applying this
66           sequence of events recursively.
67
68       •   Except for leaf nodes, exit the node. The event type is
69           "CommonMark::EVENT_EXIT" and the current node is set to the
70           original node "V".
71
72       After the root node was exited, the event type is set to
73       "CommonMark::EVENT_DONE" and the current node to "undef". In scalar
74       context, "next" returns "CommonMark::EVENT_DONE". In list context, it
75       returns the empty list.
76
77       For leaf nodes, no exit events are generated. Leaf nodes comprise the
78       node types that never have children:
79
80           CommonMark::NODE_HTML
81           CommonMark::NODE_HRULE
82           CommonMark::NODE_CODE_BLOCK
83           CommonMark::NODE_TEXT
84           CommonMark::NODE_SOFTBREAK
85           CommonMark::NODE_LINEBREAK
86           CommonMark::NODE_CODE
87           CommonMark::NODE_INLINE_HTML
88
89       For other node types, an exit event is generated even if the node has
90       no children.
91
92       It is safe to modify nodes after an exit event, or an enter event for
93       leaf nodes. Otherwise, changes to the tree structure can result in
94       undefined behavior.
95
96   Accessors
97           my $node    = $iter->get_node;
98           my $ev_type = $iter->get_event_type;
99           my $node    = $iter->get_root;
100
101       These accessors return the current node, the current event type, and
102       the root node.
103
105       This software is copyright (C) by Nick Wellnhofer.
106
107       This is free software; you can redistribute it and/or modify it under
108       the same terms as the Perl 5 programming language system itself.
109
110
111
112perl v5.36.0                      2022-07-22           CommonMark::Iterator(3)
Impressum