Skip to content

Commit

Permalink
Added the nocase option to the accessors in PullParser. Original pa…
Browse files Browse the repository at this point in the history
…tch from

Graham Barr, used for search.cpan.org. Documentation and tests from yours
truly.
  • Loading branch information
theory committed Dec 4, 2009
1 parent e6bf47e commit da00042
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Expand Up @@ -9,6 +9,12 @@

Add strip_verbatim_indent() from David E. Wheeler.

Added the "nocase" option to PullParser's get_title(),
get_version(), get_description(), and get_author() methods. This
allows one to fetch the contents of those sections regardless of
the case of the labels (e.g., "NAME" and "Name" and "name" are all
valid). Graham Barr.

2009-07-16 Allison Randal <allison@perl.org>
* Release 3.08

Expand Down
38 changes: 32 additions & 6 deletions lib/Pod/Simple/PullParser.pm
Expand Up @@ -319,6 +319,7 @@ sub _get_titled_section {
my $desperate_for_title = delete $options{'desperate'};
my $accept_verbatim = delete $options{'accept_verbatim'};
my $max_content_length = delete $options{'max_content_length'};
my $nocase = delete $options{'nocase'};
$max_content_length = 120 unless defined $max_content_length;

Carp::croak( "Unknown " . ((1 == keys %options) ? "option: " : "options: ")
Expand Down Expand Up @@ -366,6 +367,7 @@ sub _get_titled_section {
$head1_text_content .= $token->text;
} elsif( $token->is_end and $token->tagname eq 'head1' ) {
DEBUG and print " Found end of head1. Considering content...\n";
$head1_text_content = uc $head1_text_content if $nocase;
if($head1_text_content eq $titlename
or $head1_text_content =~ m/\($titlename_re\)/s
# We accept "=head1 Nomen Modularis (NAME)" for sake of i18n
Expand Down Expand Up @@ -626,7 +628,15 @@ For example, suppose you have a document that starts out:
Hoo::Boy::Wowza -- Stuff B<wow> yeah!
$parser->get_title on that document will return "Hoo::Boy::Wowza --
Stuff wow yeah!".
Stuff wow yeah!". If the document starts with:
=head1 Name
Hoo::Boy::W00t -- Stuff B<w00t> yeah!
Then you'll need to pass the C<nocase> option in order to recognize "Name":
$parser->get_title(nocase => 1);
In cases where get_title can't find the title, it will return empty-string
("").
Expand All @@ -652,7 +662,15 @@ But if the document starts out:
Hooboy, stuff B<wow> yeah!
then $parser->get_short_title on that document will return "Hooboy,
stuff wow yeah!".
stuff wow yeah!". If the document starts with:
=head1 Name
Hoo::Boy::W00t -- Stuff B<w00t> yeah!
Then you'll need to pass the C<nocase> option in order to recognize "Name":
$parser->get_short_title(nocase => 1);
If the title can't be found, then get_short_title returns empty-string
("").
Expand All @@ -661,22 +679,30 @@ If the title can't be found, then get_short_title returns empty-string
This works like get_title except that it returns the contents of the
"=head1 AUTHOR\n\nParagraph...\n" section, assuming that that section
isn't terribly long.
isn't terribly long. To recognize a "=head1 Author\n\nParagraph\n"
section, pass the C<nocase> otpion:
$parser->get_author(nocase => 1);
(This method tolerates "AUTHORS" instead of "AUTHOR" too.)
=item $description_name = $parser->get_description
This works like get_title except that it returns the contents of the
"=head1 PARAGRAPH\n\nParagraph...\n" section, assuming that that section
isn't terribly long.
"=head1 DESCRIPTION\n\nParagraph...\n" section, assuming that that section
isn't terribly long. To recognize a "=head1 Description\n\nParagraph\n"
section, pass the C<nocase> otpion:
$parser->get_description(nocase => 1);
=item $version_block = $parser->get_version
This works like get_title except that it returns the contents of
the "=head1 VERSION\n\n[BIG BLOCK]\n" block. Note that this does NOT
return the module's C<$VERSION>!!
return the module's C<$VERSION>!! To recognize a
"=head1 Version\n\n[BIG BLOCK]\n" section, pass the C<nocase> otpion:
$parser->get_version(nocase => 1);
=back
Expand Down
26 changes: 25 additions & 1 deletion t/pulltitl.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {

use strict;
use Test;
BEGIN { plan tests => 104 };
BEGIN { plan tests => 114 };

#use Pod::Simple::Debug (5);

Expand All @@ -29,6 +29,7 @@ my $p = Pod::Simple::PullParser->new;
$p->set_source( \qq{\n=head1 NAME\n\nBzorch\n\n=pod\n\nLala\n\n\=cut\n} );

ok $p->get_title(), 'Bzorch';

my $t;

ok( $t = $p->get_token);
Expand All @@ -47,6 +48,29 @@ ok( $t && $t->type eq 'text' && $t->text, 'NAME' );

###########################################################################

{
print "# Testing a set with nocase, at line ", __LINE__, "\n";
my $p = Pod::Simple::PullParser->new;
$p->set_source( \qq{\n=head1 Name\n\nShazbot\n\n=pod\n\nLala\n\n\=cut\n} );

ok $p->get_title(nocase => 1), 'Shazbot';

ok( my $t = $p->get_token);
ok( $t && $t->type, 'start');
ok( $t && $t->type eq 'start' && $t->tagname, 'Document' );

ok( $t = $p->get_token);
ok( $t && $t->type, 'start');
ok( $t && $t->type eq 'start' && $t->tagname, 'head1' );

ok( $t = $p->get_token);
ok( $t && $t->type, 'text');
ok( $t && $t->type eq 'text' && $t->text, 'Name' );

}

###########################################################################

{
print "# Testing another set, at line ", __LINE__, "\n";

Expand Down

0 comments on commit da00042

Please sign in to comment.