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