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