/usr/share/perl5/Pod/Abstract/Parser.pm is in libpod-abstract-perl 0.20-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | package Pod::Abstract::Parser;
use strict;
use Pod::Parser;
use Pod::Abstract::Node;
use Data::Dumper;
use base qw(Pod::Parser);
our $VERSION = '0.20';
=head1 NAME
Pod::Abstract::Parser - Internal Parser class of Pod::Abstract.
=head1 DESCRIPTION
This is a C<Pod::Parser> subclass, used by C<Pod::Abstract> to convert Pod
text into a Node tree. You do not need to use this class yourself, the
C<Pod::Abstract> class will do the work of creating the parser and running
it for you.
=head1 METHODS
=head2 new
Pod::Abstract::Parser->new( $pod_abstract );
Requires a Pod::Abstract object to load Pod data into. Should only be
called internally by Pod::Abstract.
=cut
sub new {
my $class = shift;
my $p_a = shift;
# Always accept non-POD paras, so that the input document can
# always be reproduced exactly as entered. These will be stored in
# the tree but will be available through distinct methods.
my $self = $class->SUPER::new();
$self->parseopts(
-want_nonPODs => 1,
-process_cut_cmd => 1,
);
$self->{pod_abstract} = $p_a;
my $root_node = Pod::Abstract::Node->new(
type => "[ROOT]",
);
$self->{cmd_stack} = [ $root_node ];
$self->{root} = $root_node;
return $self;
}
sub root {
my $self = shift;
return $self->{root};
}
# Automatically nest these items: A head1 section continues until the
# next head1, list items continue until the next item or end of list,
# etc. POD doesn't specify these relationships, but they are natural
# and make sense in the whole document context.
#
# SPECIAL: Start node with < to pull the end node out of the tree and
# into the opening node - e.g, pull a "back" into an "over", but not
# into an "item". Pulling a command stops it from closing any more
# elements, so begin/end style blocks need to use a pull, or one end
# will close all begins.
my %section_commands = (
'head1' => [ 'head1' ],
'head2' => [ 'head2', 'head1' ],
'head3' => [ 'head3', 'head2', 'head1' ],
'head4' => [ 'head4', 'head3', 'head2', 'head1' ],
'over' => [ '<back' ],
'item' => [ 'item', 'back' ],
'begin' => [ '<end' ],
);
# Don't parse anything inside these.
my %no_parse = (
'begin' => 1,
'for' => 1,
);
my %attr_names = (
head1 => 'heading',
head2 => 'heading',
head3 => 'heading',
head4 => 'heading',
item => 'label',
);
sub command {
my ($self, $command, $paragraph, $line_num) = @_;
my $cmd_stack = $self->{cmd_stack} || [ ];
my $p_break = "\n\n";
if($paragraph =~ s/([ \t]*\n[ \t]*\n)$//s) {
$p_break = $1;
}
if($self->cutting) {
# Treat as non-pod - i.e, verbatim program text block.
my $element_node = Pod::Abstract::Node->new(
type => "#cut",
body => "=$command $paragraph$p_break",
);
my $top = $cmd_stack->[$#$cmd_stack];
$top->push($element_node);
} else {
# Treat as command.
my $pull = undef;
while(@$cmd_stack > 0) {
my $last = scalar(@$cmd_stack) - 1;
my @should_end = ( );
@should_end =
grep { $command eq $_ }
@{$section_commands{$cmd_stack->[$last]->type}};
my @should_pull = ( );
@should_pull =
grep { "<$command" eq $_ }
@{$section_commands{$cmd_stack->[$last]->type}};
if(@should_end) {
my $end_cmd = pop @$cmd_stack;
} elsif(@should_pull) {
$pull = pop @$cmd_stack;
last;
} else {
last;
}
}
# Don't do anything special if we're on a no_parse node
my $top = $cmd_stack->[$#$cmd_stack];
if($no_parse{$top->type} && !$top->param('parse_me')) {
my $t_node = Pod::Abstract::Node->new(
type => ':text',
body => ($paragraph ne '' ?
"=$command $paragraph$p_break" :
"=$command$p_break"),
);
$top->push($t_node);
return;
}
# Some commands have to get expandable interior sequences
my $attr_node = undef;
my $attr_name = $attr_names{$command};
my %attr = ( parse_me => 0 );
if($attr_name) {
$attr_node = Pod::Abstract::Node->new(
type => '@attribute',
);
my $pt = $self->parse_text($paragraph);
$self->load_pt($attr_node, $pt);
$attr{$attr_name} = $attr_node;
$attr{body_attr} = $attr_name;
} elsif($paragraph =~ m/^\:/) {
$attr{parse_me} = 1;
}
my $element_node = Pod::Abstract::Node->new(
type => $command,
body => ($attr_name ? undef : $paragraph),
p_break => $p_break,
%attr,
);
if($pull) {
$pull->param('close_element', $element_node);
} else {
$top->push($element_node);
}
if($section_commands{$command}) {
push @$cmd_stack, $element_node;
} else {
# No push
}
}
$self->{cmd_stack} = $cmd_stack;
}
sub verbatim {
my ($self, $paragraph, $line_num) = @_;
my $cmd_stack = $self->{cmd_stack};
my $top = $cmd_stack->[$#$cmd_stack];
my $type = ':verbatim';
if($no_parse{$top->type} && !$top->param('parse_me')) {
$type = ':text';
}
my $element_node = Pod::Abstract::Node->new(
type => ':verbatim',
body => $paragraph,
);
$top->push($element_node);
}
sub preprocess_paragraph {
my ($self, $text, $line_num) = @_;
return $text unless $self->cutting;
# This is a non-pod text segment
my $element_node = Pod::Abstract::Node->new(
type => "#cut",
body => $text,
);
my $cmd_stack = $self->{cmd_stack};
my $top = $cmd_stack->[$#$cmd_stack];
$top->push($element_node);
}
sub textblock {
my ($self, $paragraph, $line_num) = @_;
my $p_break = "\n\n";
if($paragraph =~ s/([ \t]*\n[ \t]*\n)$//s) {
$p_break = $1;
}
my $cmd_stack = $self->{cmd_stack};
my $top = $cmd_stack->[$#$cmd_stack];
if($no_parse{$top->type} && !$top->param('parse_me')) {
my $element_node = Pod::Abstract::Node->new(
type => ':text',
body => "$paragraph$p_break",
);
$top->push($element_node);
return;
}
my $element_node = Pod::Abstract::Node->new(
type => ':paragraph',
p_break => $p_break,
);
my $pt = $self->parse_text($paragraph);
$self->load_pt($element_node, $pt);
$top->push($element_node);
}
# Recursive load
sub load_pt {
my $self = shift;
my $elt = shift;
my $pt = shift;
my @c = $pt->children;
foreach my $c(@c) {
if(ref $c) {
# Object;
if($c->isa('Pod::InteriorSequence')) {
my $cmd = $c->cmd_name;
my $i_node = Pod::Abstract::Node->new(
type => ":$cmd",
left_delimiter => $c->left_delimiter,
right_delimiter => $c->right_delimiter,
);
$self->load_pt($i_node, $c->parse_tree);
$elt->push($i_node);
} else {
die "$c not an interior sequence!";
}
} else {
# text
my $t_node = Pod::Abstract::Node->new(
type => ':text',
body => $c,
);
$elt->push($t_node);
}
}
return $elt;
}
sub end_pod {
my $self = shift;
my $cmd_stack = $self->{cmd_stack};
my $end_cmd;
while(defined $cmd_stack && @$cmd_stack) {
$end_cmd = pop @$cmd_stack;
}
die "Last node was not root node" unless $end_cmd->type eq '[ROOT]';
# Replace the root node.
push @$cmd_stack, $end_cmd;
}
=head1 AUTHOR
Ben Lilburne <bnej@mac.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2009 Ben Lilburne
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|