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
|
package PPI::Token::Pod;
# Represents a section of POD
use strict;
use UNIVERSAL 'isa';
use base 'PPI::Token';
use vars qw{$VERSION};
BEGIN {
$VERSION = '0.903';
}
### XS -> PPI/XS.xs:_PPI_Token_Pod__significant 0.900+
sub significant { '' }
sub _on_line_start {
my $t = $_[1];
# Add the line to the token first
$t->{token}->{content} .= $t->{line};
# Check the line to see if it is a =cut line
if ( $t->{line} =~ /^=(\w+)/ ) {
# End of the token
$t->_finalize_token if lc $1 eq 'cut';
}
0;
}
# Breaks the pod into lines, returned as a reference to an array
sub lines { [ split /(?:\015{1,2}\012|\015|\012)/, $_[0]->{content} ] }
# Merges one or more Pod tokens.
# Can be called as either a class or object method.
# If called as a class method, returns a new Pod token object.
# If called as an object method, modifies the object, and also returns
# it as a convenience.
sub merge {
my $either = $_[0];
# Check there are no bad arguments
if ( grep { ! isa( $_, 'PPI::Token::Pod' ) } @_ ) {
return undef;
}
# Get the tokens, and extract the lines
my @content = map { $_->lines } grep { ref $_ } @_;
return undef unless @content; # No pod tokens...
# Remove the leading =pod tags, trailing =cut tags, and any empty lines
# between them and the pod contents.
foreach my $pod ( @content ) {
# Leading =pod tag
if ( @$pod and $pod->[0] =~ /^=pod\b/o ) {
shift @$pod;
}
# Trailing =cut tag
if ( @$pod and $pod->[-1] =~ /^=cut\b/o ) {
pop @$pod;
}
# Leading and trailing empty lines
while ( @$pod and $pod->[0] eq '' ) {
shift @$pod;
}
while ( @$pod and $pod->[-1] eq '' ) {
pop @$pod;
}
}
# Remove any empty pod sections, and add the =pod and =cut tags
# for the merged pod back to it.
@content = ( [ '=pod' ], grep { @$_ } @content, [ '=cut' ] );
# Convert back into a single string
my $merged = join "\n", map { join( "\n", @$_ ) . "\n" } @content;
# Was this an object method
if ( ref $either ) {
$either->{content} = $merged;
return $either;
}
# Return the static method response
$either->new( $merged );
}
1;
|