4 package Pod::Simple::SimpleTree;
8 use vars qw( $ATTR_PAD @ISA $VERSION $SORT_ATTRS);
11 @ISA = ('Pod::Simple');
12 *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG;
15 __PACKAGE__->_accessorize(
16 'root', # root of the tree
19 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 sub _handle_element_start { # self, tagname, attrhash
22 DEBUG > 2 and print "Handling $_[1] start-event\n";
23 my $x = [$_[1], $_[2]];
24 if($_[0]{'_currpos'}) {
25 push @{ $_[0]{'_currpos'}[0] }, $x; # insert in parent's child-list
26 unshift @{ $_[0]{'_currpos'} }, $x; # prefix to stack
28 DEBUG and print " And oo, it gets to be root!\n";
29 $_[0]{'_currpos'} = [ $_[0]{'root'} = $x ];
30 # first event! set to stack, and set as root.
32 DEBUG > 3 and print "Stack is now: ",
33 join(">", map $_->[0], @{$_[0]{'_currpos'}}), "\n";
37 sub _handle_element_end { # self, tagname
38 DEBUG > 2 and print "Handling $_[1] end-event\n";
39 shift @{$_[0]{'_currpos'}};
40 DEBUG > 3 and print "Stack is now: ",
41 join(">", map $_->[0], @{$_[0]{'_currpos'}}), "\n";
45 sub _handle_text { # self, text
46 DEBUG > 2 and print "Handling $_[1] text-event\n";
47 push @{ $_[0]{'_currpos'}[0] }, $_[1];
52 # A bit of evil from the black box... please avert your eyes, kind souls.
53 sub _traverse_treelet_bit {
54 DEBUG > 2 and print "Handling $_[1] paragraph event\n";
56 push @{ $self->{'_currpos'}[0] }, [@_];
59 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65 Pod::Simple::SimpleTree -- parse Pod into a simple parse tree
75 % perl -MPod::Simple::SimpleTree -MData::Dumper -e \
76 "print Dumper(Pod::Simple::SimpleTree->new->parse_file(shift)->root)" \
81 { 'start_line' => 1 },
84 { 'start_line' => 1 },
89 { 'start_line' => 3 },
102 This class is of interest to people writing a Pod processor/formatter.
104 This class takes Pod and parses it, returning a parse tree made just
105 of arrayrefs, and hashrefs, and strings.
107 This is a subclass of L<Pod::Simple> and inherits all its methods.
109 This class is inspired by XML::Parser's "Tree" parsing-style, although
110 it doesn't use exactly the same LoL format.
114 At the end of the parse, call C<< $parser->root >> to get the
119 Every element node in the parse tree is represented by an arrayref of
120 the form: C<[ I<elementname>, \%attributes, I<...subnodes...> ]>.
121 See the example tree dump in the Synopsis, above.
123 Every text node in the tree is represented by a simple (non-ref)
124 string scalar. So you can test C<ref($node)> to see whather you have
125 an element node or just a text node.
127 The top node in the tree is C<[ 'Document', \%attributes,
137 L<The "Tree" subsubsection in XML::Parser|XML::Parser/"Tree">
139 =head1 COPYRIGHT AND DISCLAIMERS
141 Copyright (c) 2002 Sean M. Burke. All rights reserved.
143 This library is free software; you can redistribute it and/or modify it
144 under the same terms as Perl itself.
146 This program is distributed in the hope that it will be useful, but
147 without any warranty; without even the implied warranty of
148 merchantability or fitness for a particular purpose.
152 Sean M. Burke C<sburke@cpan.org>