Commit | Line | Data |
351625bd |
1 | |
2 | |
3 | require 5; |
4 | package Pod::Simple::SimpleTree; |
5 | use strict; |
6 | use Carp (); |
7 | use Pod::Simple (); |
8 | use vars qw( $ATTR_PAD @ISA $VERSION $SORT_ATTRS); |
a242eeb4 |
9 | $VERSION = '3.11'; |
351625bd |
10 | BEGIN { |
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 | |
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 |
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 | |
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"; |
42 | return; |
43 | } |
44 | |
45 | sub _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. |
53 | sub _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 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
60 | 1; |
61 | __END__ |
62 | |
63 | =head1 NAME |
64 | |
65 | Pod::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 | |
102 | This class is of interest to people writing a Pod processor/formatter. |
103 | |
104 | This class takes Pod and parses it, returning a parse tree made just |
105 | of arrayrefs, and hashrefs, and strings. |
106 | |
107 | This is a subclass of L<Pod::Simple> and inherits all its methods. |
108 | |
109 | This class is inspired by XML::Parser's "Tree" parsing-style, although |
110 | it doesn't use exactly the same LoL format. |
111 | |
112 | =head1 METHODS |
113 | |
114 | At the end of the parse, call C<< $parser->root >> to get the |
115 | tree's top node. |
116 | |
117 | =head1 Tree Contents |
118 | |
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. |
122 | |
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. |
126 | |
127 | The top node in the tree is C<[ 'Document', \%attributes, |
128 | I<...subnodes...> ]> |
129 | |
130 | |
131 | =head1 SEE ALSO |
132 | |
133 | L<Pod::Simple> |
134 | |
135 | L<perllol> |
136 | |
137 | L<The "Tree" subsubsection in XML::Parser|XML::Parser/"Tree"> |
138 | |
a242eeb4 |
139 | =head1 SUPPORT |
140 | |
141 | Questions or discussion about POD and Pod::Simple should be sent to the |
142 | pod-people@perl.org mail list. Send an empty email to |
143 | pod-people-subscribe@perl.org to subscribe. |
144 | |
145 | This module is managed in an open GitHub repository, |
146 | L<http://github.com/theory/pod-simple/>. Feel free to fork and contribute, or |
147 | to clone L<git://github.com/theory/pod-simple.git> and send patches! |
148 | |
149 | Patches against Pod::Simple are welcome. Please send bug reports to |
150 | <bug-pod-simple@rt.cpan.org>. |
151 | |
351625bd |
152 | =head1 COPYRIGHT AND DISCLAIMERS |
153 | |
154 | Copyright (c) 2002 Sean M. Burke. All rights reserved. |
155 | |
156 | This library is free software; you can redistribute it and/or modify it |
157 | under the same terms as Perl itself. |
158 | |
159 | This program is distributed in the hope that it will be useful, but |
160 | without any warranty; without even the implied warranty of |
161 | merchantability or fitness for a particular purpose. |
162 | |
163 | =head1 AUTHOR |
164 | |
a242eeb4 |
165 | Pod::Simple was created by Sean M. Burke <sburke@cpan.org>. |
166 | But don't bother him, he's retired. |
351625bd |
167 | |
a242eeb4 |
168 | Pod::Simple is maintained by: |
169 | |
170 | =over |
351625bd |
171 | |
a242eeb4 |
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 |