Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 28, 2012
@barefootcoder barefootcoder test for modules using MooseX::Declare
a failing test which demonstrates the problem
f068f9b
@barefootcoder barefootcoder fix for modules using MooseX::Declare
simple patch which fixes the problem
e6057eb
@barefootcoder barefootcoder refactoring common code into Role::Section
this is a bit cleaner: it extracts common functionality from Section::Name and Section::Version
note that this technically changes the previous behavior:
	formerly, "%m" in a version format string wouldn't pick up a module name from a PODNAME comment
	however, I think the new behavior is more correct
I wasn't going to pull in get_abstract, but it also relies on _extract_comment_content, so it came along for the ride
ALTERNATIVE: this code could be put into a new role instead of using Role::Section for this purpose
043e89b
View
79 lib/Pod/Weaver/Role/Section.pm
@@ -17,5 +17,84 @@ free to behave differently if it's needed to do something really cool.
requires 'weave_section';
+sub _extract_comment_content {
+ my ($self, $ppi_document, $regex) = @_;
+
+ my $content;
+ my $finder = sub {
+ my $node = $_[1];
+ return 0 unless $node->isa('PPI::Token::Comment');
+ if ( $node->content =~ $regex ) {
+ $content = $1;
+ return 1;
+ }
+ return 0;
+ };
+
+ $ppi_document->find_first($finder);
+
+ return $content;
+}
+
+sub _get_docname_via_statement {
+ my ($self, $ppi_document) = @_;
+
+ my $pkg_node = $ppi_document->find_first('PPI::Statement::Package');
+ return unless $pkg_node;
+ return $pkg_node->namespace;
+}
+
+sub _get_docname_via_class_decl {
+ my ($self, $ppi_document) = @_;
+
+ my $word_node = $ppi_document->find_first(sub {
+ $_[1]->isa('PPI::Token::Word')
+ and $_[1]->content eq 'class'
+ and $_[1]->snext_sibling
+ and $_[1]->snext_sibling->isa('PPI::Token::Word')
+ });
+ return unless $word_node;
+ return $word_node->snext_sibling->content;
+}
+
+sub _get_docname_via_comment {
+ my ($self, $ppi_document) = @_;
+
+ return $self->_extract_comment_content(
+ $ppi_document,
+ qr/^\s*#+\s*PODNAME:\s*(.+)$/m,
+ );
+}
+
+sub get_docname {
+ my ($self, $input) = @_;
+
+ return unless exists $input->{ppi_document};
+ my $ppi_document = $input->{ppi_document};
+
+ my $docname = $self->_get_docname_via_comment($ppi_document)
+ || $self->_get_docname_via_statement($ppi_document)
+ || $self->_get_docname_via_class_decl($ppi_document);
+
+ return $docname;
+}
+
+sub get_abstract {
+ my ($self, $input) = @_;
+
+ my $comment = $self->_extract_comment_content(
+ $input->{ppi_document},
+ qr/^\s*#+\s*ABSTRACT:\s*(.+)$/m,
+ );
+
+ return $comment if $comment;
+
+ # If that failed, fall back to searching the whole document
+ my ($abstract)
+ = $input->{ppi_document}->serialize =~ /^\s*#+\s*ABSTRACT:\s*(.+)$/m;
+
+ return $abstract;
+}
+
no Moose::Role;
1;
View
68 lib/Pod/Weaver/Section/Name.pm
@@ -30,77 +30,13 @@ use Pod::Elemental::Element::Pod5::Command;
use Pod::Elemental::Element::Pod5::Ordinary;
use Pod::Elemental::Element::Nested;
-sub _get_docname_via_statement {
- my ($self, $ppi_document) = @_;
-
- my $pkg_node = $ppi_document->find_first('PPI::Statement::Package');
- return unless $pkg_node;
- return $pkg_node->namespace;
-}
-
-sub _get_docname_via_comment {
- my ($self, $ppi_document) = @_;
-
- return $self->_extract_comment_content(
- $ppi_document,
- qr/^\s*#+\s*PODNAME:\s*(.+)$/m,
- );
-}
-
-sub _get_docname {
- my ($self, $input) = @_;
-
- my $ppi_document = $input->{ppi_document};
-
- my $docname = $self->_get_docname_via_comment($ppi_document)
- || $self->_get_docname_via_statement($ppi_document);
-
- return $docname;
-}
-
-sub _get_abstract {
- my ($self, $input) = @_;
-
- my $comment = $self->_extract_comment_content(
- $input->{ppi_document},
- qr/^\s*#+\s*ABSTRACT:\s*(.+)$/m,
- );
-
- return $comment if $comment;
-
- # If that failed, fall back to searching the whole document
- my ($abstract)
- = $input->{ppi_document}->serialize =~ /^\s*#+\s*ABSTRACT:\s*(.+)$/m;
-
- return $abstract;
-}
-
-sub _extract_comment_content {
- my ($self, $ppi_document, $regex) = @_;
-
- my $content;
- my $finder = sub {
- my $node = $_[1];
- return 0 unless $node->isa('PPI::Token::Comment');
- if ( $node->content =~ $regex ) {
- $content = $1;
- return 1;
- }
- return 0;
- };
-
- $ppi_document->find_first($finder);
-
- return $content;
-}
-
sub weave_section {
my ($self, $document, $input) = @_;
my $filename = $input->{filename} || 'file';
- my $docname = $self->_get_docname($input);
- my $abstract = $self->_get_abstract($input);
+ my $docname = $self->get_docname($input);
+ my $abstract = $self->get_abstract($input);
Carp::croak sprintf "couldn't determine document name for %s", $filename
unless $docname;
View
5 lib/Pod/Weaver/Section/Version.pm
@@ -117,10 +117,7 @@ sub weave_section {
);
$args{zilla} = $input->{zilla} if exists $input->{zilla};
- if ( exists $input->{ppi_document} ) {
- my $pkg_node = $input->{ppi_document}->find_first('PPI::Statement::Package');
- $args{module} = $pkg_node->namespace if $pkg_node;
- }
+ $args{module} = $self->get_docname($input);
my $content = _format_version($self->format, \%args);
if ( $self->is_verbatim ) {
View
74 t/class.t
@@ -0,0 +1,74 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Differences;
+use Moose::Autobox 0.10;
+
+use PPI;
+
+use Pod::Elemental;
+use Pod::Weaver;
+
+do_weave( configer( format => "This is version %v of %m." ), 'class' );
+
+sub configer {
+ my %opts = @_;
+
+ # TODO Hmpf, is there an easier way for this? --APOCAL
+ my $assembler = Pod::Weaver::Config::Assembler->new;
+ $assembler->sequence->add_section( $assembler->section_class->new({ name => '_' }) );
+ $assembler->change_section('@CorePrep');
+ $assembler->change_section('Name');
+ $assembler->change_section('Version');
+ foreach my $k ( keys %opts ) {
+ $assembler->add_value( $k => $opts{ $k } );
+ }
+ $assembler->change_section('Leftovers');
+
+ return Pod::Weaver->new_from_config_sequence( $assembler->sequence );
+}
+
+my $perl_document;
+sub do_weave {
+ my( $weaver, $filename ) = @_;
+
+ my $in_pod = do { local $/; open my $fh, '<', "t/eg/$filename.in.pod"; <$fh> };
+ my $expected = do { local $/; open my $fh, '<', "t/eg/$filename.out.pod"; <$fh> };
+ my $document = Pod::Elemental->read_string($in_pod);
+
+ $perl_document = do { local $/; <DATA> } if ! defined $perl_document;
+ my $ppi_document = PPI::Document->new(\$perl_document);
+
+ my $woven = $weaver->weave_document({
+ pod_document => $document,
+ ppi_document => $ppi_document,
+ version => '1.012078',
+ });
+
+ # XXX: This test is extremely risky as things change upstream.
+ # -- rjbs, 2009-10-23
+ eq_or_diff(
+ $woven->as_pod_string,
+ $expected,
+ "exactly the pod string we wanted after weaving for $filename!",
+ );
+}
+
+done_testing;
+
+__DATA__
+
+use MooseX::Declare;
+
+class Module::Name
+{
+ # ABSTRACT: abstract text
+
+ has something => (is => 'ro', isa => 'Str');
+
+ method foo (Str $foo)
+ {
+ return $self->something . $foo;
+ }
+}
View
21 t/eg/class.in.pod
@@ -0,0 +1,21 @@
+=pod
+
+=head1 DESCRIPTION
+
+This is a simple document meant to be used in testing Pod::Weaver.
+
+It does not do very much.
+
+=head1 BE FOREWARNED
+
+This is not supported:
+
+ much at all
+
+Happy hacking!
+
+=head1 SYNOPSIS
+
+This should probably get moved up front.
+
+=cut
View
29 t/eg/class.out.pod
@@ -0,0 +1,29 @@
+=pod
+
+=head1 NAME
+
+Module::Name - abstract text
+
+=head1 VERSION
+
+This is version 1.012078 of Module::Name.
+
+=head1 DESCRIPTION
+
+This is a simple document meant to be used in testing Pod::Weaver.
+
+It does not do very much.
+
+=head1 BE FOREWARNED
+
+This is not supported:
+
+ much at all
+
+Happy hacking!
+
+=head1 SYNOPSIS
+
+This should probably get moved up front.
+
+=cut

No commit comments for this range

Something went wrong with that request. Please try again.