Skip to content

Commit

Permalink
Let .pod override .pm in module sets (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Feb 4, 2017
1 parent d284f8a commit 09f9ee4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for Pod-Pandoc

{{$NEXT}}
- Guess NAME in parse_modules if missing
- Let .pod override .pm in module sets (#4)

0.3.2 2017-02-03 13:44:42 CET
- Fix converting with pod2pandoc to STDOUT
Expand Down
26 changes: 12 additions & 14 deletions lib/Pod/Pandoc/Modules.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ sub new {
sub add {
my ( $self, $name, $doc ) = @_;

# TODO: don't override .pod with .pm
if ( $self->{$name} ) {
return;
}
else {
$self->{$name} = $doc;
if ( my $given = $self->{$name} ) {
return # override .pm with .pod, otherwise skip
if $given->metavalue('file') !~ /\.pm/
or $doc->metavalue('file') !~ /\.pod/;
}
$doc->meta->{title} //= MetaString $name;

$self->{$name} = $doc;
}

sub module_link {
Expand Down Expand Up @@ -160,16 +161,13 @@ as Jekyll).
See L<Pod::Simple::Pandoc> for how to create instances of this module.
=head1 EXAMPLES
Create GitHub Wiki pages:
=head1 METHODS
$modules->serialize(
{ dir => 'wiki', ext => 'md', index => 'Home', wiki => 1 },
...
);
=head2 add( $name => $doc )
=head1 METHODS
Add a module given as L<Pandoc::Document> unless a module of same C<$name>
already exists. As an exception a parsed L<.pod> file will override a parsed
L<.pm> file. The document title is set to the module name if missing.
=head2 serialize ( [ $dir ] [, \%options ] [, @args ] )
Expand Down
7 changes: 3 additions & 4 deletions lib/Pod/Simple/Pandoc.pm
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ sub parse_dir {
$base =~ s/\.\.$//;
$doc->meta->{base} = MetaString $base;
$files->{$file} = $doc;
}
}
},
$directory
);
Expand All @@ -165,17 +165,16 @@ sub parse_dir {

sub parse_modules {
my ( $parser, $dir, %opt ) = @_;
return unless -d $dir;

my $modules = Pod::Pandoc::Modules->new;
return $modules unless -d $dir;

foreach my $doc ( values %{ $parser->parse_dir($dir) } ) {
my $file = $doc->metavalue('file');
my $module = File::Spec->abs2rel( $file, $dir );
$module =~ s{\.(pm|pod)$}{}g;
$module =~ s{/}{::}g;
$doc->meta->{title} //= MetaString $module;
if ( $doc->metavalue('title') eq $module ) {
if ( ( $doc->metavalue('title') // $module ) eq $module ) {
if ( not $modules->add( $module => $doc ) and $opt{quiet} ) {
warn "$file skipped for "
. $modules->{$module}->metavalue('file');
Expand Down
1 change: 0 additions & 1 deletion t/examples/Empty.pod

This file was deleted.

Empty file added t/examples/Pandoc.pod
Empty file.
26 changes: 23 additions & 3 deletions t/pod-pandoc-modules.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@ use Pod::Simple::Pandoc;
use Pod::Pandoc::Modules;
use Test::Exception;

my $parser = Pod::Simple::Pandoc->new;

# add
{
my $modules = Pod::Pandoc::Modules->new;
my $name = 'Pod::Simple::Pandoc';
my $file = 'lib/Pod/Simple/Pandoc.pm';
my $doc = $parser->parse_file($file);
ok $modules->add( $name => $doc ), 'add';
is $modules->{$name}, $doc, 'added';

ok !$modules->add( $name => $parser->parse_file('script/pod2pandoc') );
is $modules->{$name}, $doc, 'add doesn\'t override';

$file = 't/examples/Pandoc.pod';
ok $modules->add( $name => $parser->parse_file($file) ), 'add';
is $modules->{$name}->metavalue('file'), $file, '.pod overrides .pm';
is $modules->{$name}->metavalue('title'), $name, 'title without NAME';
}

# constructor
my $modules = Pod::Pandoc::Modules->new({
'Pod::Simple::Pandoc' => Pod::Simple::Pandoc->new->parse_file('lib/Pod/Simple/Pandoc.pm')
'Pod::Simple::Pandoc' => $parser->parse_file('lib/Pod/Simple/Pandoc.pm')
});

# index
sub is_index {
my ( $name, $opt, $meta, $url, $title ) = @_;

Expand Down Expand Up @@ -38,7 +60,5 @@ is_index(
'Pod-Simple-Pandoc', 'wikilink'
);

$modules = Pod::Simple::Pandoc->parse_modules('t/examples');
is $modules->{Empty}->metavalue('title'), 'Empty', 'title without NAME';

done_testing;

0 comments on commit 09f9ee4

Please sign in to comment.