1 2 3require 5; 4package Pod::Simple::SimpleTree; 5use strict; 6use Carp (); 7use Pod::Simple (); 8use vars qw( $ATTR_PAD @ISA $VERSION $SORT_ATTRS); 9$VERSION = '3.14'; 10BEGIN { 11 @ISA = ('Pod::Simple'); 12 *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG; 13} 14 15__PACKAGE__->_accessorize( 16 'root', # root of the tree 17); 18 19#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 21sub _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 27 } else { 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. 31 } 32 DEBUG > 3 and print "Stack is now: ", 33 join(">", map $_->[0], @{$_[0]{'_currpos'}}), "\n"; 34 return; 35} 36 37sub _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"; 42 return; 43} 44 45sub _handle_text { # self, text 46 DEBUG > 2 and print "Handling $_[1] text-event\n"; 47 push @{ $_[0]{'_currpos'}[0] }, $_[1]; 48 return; 49} 50 51 52# A bit of evil from the black box... please avert your eyes, kind souls. 53sub _traverse_treelet_bit { 54 DEBUG > 2 and print "Handling $_[1] paragraph event\n"; 55 my $self = shift; 56 push @{ $self->{'_currpos'}[0] }, [@_]; 57 return; 58} 59#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 601; 61__END__ 62 63=head1 NAME 64 65Pod::Simple::SimpleTree -- parse Pod into a simple parse tree 66 67=head1 SYNOPSIS 68 69 % cat ptest.pod 70 71 =head1 PIE 72 73 I like B<pie>! 74 75 % perl -MPod::Simple::SimpleTree -MData::Dumper -e \ 76 "print Dumper(Pod::Simple::SimpleTree->new->parse_file(shift)->root)" \ 77 ptest.pod 78 79 $VAR1 = [ 80 'Document', 81 { 'start_line' => 1 }, 82 [ 83 'head1', 84 { 'start_line' => 1 }, 85 'PIE' 86 ], 87 [ 88 'Para', 89 { 'start_line' => 3 }, 90 'I like ', 91 [ 92 'B', 93 {}, 94 'pie' 95 ], 96 '!' 97 ] 98 ]; 99 100=head1 DESCRIPTION 101 102This class is of interest to people writing a Pod processor/formatter. 103 104This class takes Pod and parses it, returning a parse tree made just 105of arrayrefs, and hashrefs, and strings. 106 107This is a subclass of L<Pod::Simple> and inherits all its methods. 108 109This class is inspired by XML::Parser's "Tree" parsing-style, although 110it doesn't use exactly the same LoL format. 111 112=head1 METHODS 113 114At the end of the parse, call C<< $parser->root >> to get the 115tree's top node. 116 117=head1 Tree Contents 118 119Every element node in the parse tree is represented by an arrayref of 120the form: C<[ I<elementname>, \%attributes, I<...subnodes...> ]>. 121See the example tree dump in the Synopsis, above. 122 123Every text node in the tree is represented by a simple (non-ref) 124string scalar. So you can test C<ref($node)> to see whather you have 125an element node or just a text node. 126 127The top node in the tree is C<[ 'Document', \%attributes, 128I<...subnodes...> ]> 129 130 131=head1 SEE ALSO 132 133L<Pod::Simple> 134 135L<perllol> 136 137L<The "Tree" subsubsection in XML::Parser|XML::Parser/"Tree"> 138 139=head1 SUPPORT 140 141Questions or discussion about POD and Pod::Simple should be sent to the 142pod-people@perl.org mail list. Send an empty email to 143pod-people-subscribe@perl.org to subscribe. 144 145This module is managed in an open GitHub repository, 146L<http://github.com/theory/pod-simple/>. Feel free to fork and contribute, or 147to clone L<git://github.com/theory/pod-simple.git> and send patches! 148 149Patches against Pod::Simple are welcome. Please send bug reports to 150<bug-pod-simple@rt.cpan.org>. 151 152=head1 COPYRIGHT AND DISCLAIMERS 153 154Copyright (c) 2002 Sean M. Burke. 155 156This library is free software; you can redistribute it and/or modify it 157under the same terms as Perl itself. 158 159This program is distributed in the hope that it will be useful, but 160without any warranty; without even the implied warranty of 161merchantability or fitness for a particular purpose. 162 163=head1 AUTHOR 164 165Pod::Simple was created by Sean M. Burke <sburke@cpan.org>. 166But don't bother him, he's retired. 167 168Pod::Simple is maintained by: 169 170=over 171 172=item * Allison Randal C<allison@perl.org> 173 174=item * Hans Dieter Pearcey C<hdp@cpan.org> 175 176=item * David E. Wheeler C<dwheeler@cpan.org> 177 178=back 179 180=cut 181