AnnoCPAN::PodParser - Parse a pod and load the paragraphs into the database


AnnoCPAN documentation Contained in the AnnoCPAN distribution.

Index


Code Index:

NAME

Top

AnnoCPAN::PodParser - Parse a pod and load the paragraphs into the database

SYNOPSIS

Top

    sub filter_pod {
        my ($self, $code, $podver) = @_;
        my $fh_in = IO::String->new($code);
        my $parser =  AnnoCPAN::PodParser->new(
            ac_podver  => $podver,
            ac_pos     => 0,
            ac_verbose => $self->verbose,
        );
        $parser->parse_from_filehandle($fh_in);
    }

DESCRIPTION

Top

This module is used by AnnoCPAN::Dist when loading a new distribution into the database. It is a subclass of Pod::Parser that overrides the verbatim, command, and textblock methods and uses them to insert the almost unparsed POD into the database tables.

SEE ALSO

Top

AnnoCPAN::DBI, AnnoCPAN::Update, AnnoCPAN::Dist, Pod::Parser

AUTHOR

Top

Ivan Tubert-Brohman <itub@cpan.org>

COPYRIGHT

Top


AnnoCPAN documentation Contained in the AnnoCPAN distribution.
package AnnoCPAN::PodParser;

$VERSION = '0.22';

use strict;
use warnings;

use base qw(Pod::Parser);

our @EXPORT_OK   = qw(VERBATIM TEXTBLOCK COMMAND);
our %EXPORT_TAGS = (all => \@EXPORT_OK);

use constant {
    VERBATIM  => 1,
    TEXTBLOCK => 2,
    COMMAND   => 4,
};

sub verbatim {
    my ($self, $text, $line_num, $pod_para) = @_;
    #print "VERBATIM:    $text\n";
    $self->store_section(VERBATIM, $text);
}

sub textblock {
    my ($self, $text, $line_num, $pod_para) = @_;
    #print "TEXTBLOCK:   $text\n";
    $self->store_section(TEXTBLOCK, $text);
}

sub command {
    my ($self, $cmd, $text, $line_num, $pod_para)  = @_;
    #print "COMMAND:     " . $pod_para->raw_text() . "\n";
    $self->store_section(COMMAND, $pod_para->raw_text);
}

sub store_section {
    my ($self, $type, $content) = @_;

    # get rid of nuls so that we can use them safely in PodToHtml
    $content =~ s/\0//g; 

    ++$self->{ac_pos};
    return if ($content =~ /^\s*$/); # skip blank paragraphs

    my $podver = $self->{ac_podver};
    if ($self->{ac_pos} == 1 and $content =~ /^=head1\s+NAME/) {
        $self->{ac_has_title} = 1;
    } elsif ($self->{ac_pos} == 2 and $self->{ac_has_title}) {
        if ($content =~ /^\s*(\S+)[\s-]+(.*)/) {
            ($self->{ac_name}, $self->{ac_desc}) = ($1, $2);
        }
    }

    my $section;
    if ($type == VERBATIM and $self->{ac_last_verbatim}) {
        # append to previous verbatim section
        my $prev = $self->{ac_last_verbatim};
        $prev->content($prev->content . $content);
        $prev->update;
        $section = $prev;
    } else {
        # create new section
        $section = AnnoCPAN::DBI::Section->create({
            podver  => $podver,
            type    => $type,
            content => $content,
            pos     => $self->{ac_pos},
        });
        $self->{ac_last_verbatim} = $type == VERBATIM ? $section : undef;
    }

    $section;
}

sub ac_metadata {
    my ($self) = @_;
    ($self->{ac_name}, $self->{ac_desc});
}

1;