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