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
|
=head1 NAME
CommonMark::Iterator - Iterate CommonMark nodes
=head1 SYNOPSIS
use CommonMark qw(:node :event);
my $iter = $doc->iterator;
while (my ($ev_type, $node) = $iter->next) {
my $node_type = $node->get_type;
if ($node_type == NODE_PARAGRAPH) {
if ($ev_type == EVENT_ENTER) {
print("<p>");
}
else {
print("</p>\n");
}
}
elsif ($node_type == NODE_TEXT) {
print($node->get_literal);
}
}
=head1 DESCRIPTION
C<CommonMark::Iterator> provides a convenient way to walk through the nodes
in a parse tree.
=head2 Construction
my $iterator = $node->iterator;
Creates an iterator from a node. C<$node> is the root node of the iterator.
=head2 next
my $ev_type = $iterator->next;
my ($ev_type, $node) = $iterator->next;
The contents of the iterator are initially undefined. After the first and
each subsequent call to C<next>, the iterator holds a new event type and a
new current node. In scalar context, C<next> returns the new event type.
In list context, it returns a 2-element list consisting of the new event
type and the new current node.
Event types are:
CommonMark::EVENT_DONE
CommonMark::EVENT_ENTER
CommonMark::EVENT_EXIT
Event types can be imported from L<CommonMark> with tag C<event>.
use CommonMark qw(:event);
The iterator starts by visiting the root node. Every visited node C<V>
generates the following sequence of events.
=over
=item *
Enter the node. The event type is C<CommonMark::EVENT_ENTER> and the current
node is set to the entered node C<V>.
=item *
Visit all children of the node C<V> from first to last applying this sequence
of events recursively.
=item *
Except for leaf nodes, exit the node. The event type is
C<CommonMark::EVENT_EXIT> and the current node is set to the original node
C<V>.
=back
After the root node was exited, the event type is set to
C<CommonMark::EVENT_DONE> and the current node to C<undef>. In scalar
context, C<next> returns C<CommonMark::EVENT_DONE>. In list context, it
returns the empty list.
For leaf nodes, no exit events are generated. Leaf nodes comprise the node
types that never have children:
CommonMark::NODE_HTML
CommonMark::NODE_HRULE
CommonMark::NODE_CODE_BLOCK
CommonMark::NODE_TEXT
CommonMark::NODE_SOFTBREAK
CommonMark::NODE_LINEBREAK
CommonMark::NODE_CODE
CommonMark::NODE_INLINE_HTML
For other node types, an exit event is generated even if the node has no
children.
It is safe to modify nodes after an exit event, or an enter event for leaf
nodes. Otherwise, changes to the tree structure can result in undefined
behavior.
=head2 Accessors
my $node = $iter->get_node;
my $ev_type = $iter->get_event_type;
my $node = $iter->get_root;
These accessors return the current node, the current event type, and the
root node.
=head1 COPYRIGHT
This software is copyright (C) by Nick Wellnhofer.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|