File Coverage

blib/lib/App/pod2pandoc.pm
Criterion Covered Total %
statement 48 86 55.8
branch 7 32 21.8
condition 3 20 15.0
subroutine 13 16 81.2
pod 2 2 100.0
total 73 156 46.7


line stmt bran cond sub pod time code
1             package App::pod2pandoc;
2 4     4   283524 use strict;
  4         32  
  4         138  
3 4     4   28 use warnings;
  4         12  
  4         131  
4 4     4   97 use 5.010;
  4         17  
5              
6             our $VERSION = '0.3.2';
7              
8 4     4   3168 use Getopt::Long qw(:config pass_through);
  4         57855  
  4         29  
9 4     4   3727 use Pod::Usage;
  4         200167  
  4         569  
10 4     4   1803 use Pod::Simple::Pandoc;
  4         16  
  4         175  
11 4     4   36 use Pandoc;
  4         8  
  4         33  
12 4     4   453 use Pandoc::Elements;
  4         11  
  4         1113  
13 4     4   34 use Scalar::Util qw(reftype blessed);
  4         9  
  4         233  
14 4     4   30 use JSON;
  4         8  
  4         34  
15 4     4   493 use Carp;
  4         11  
  4         241  
16              
17 4     4   29 use parent 'Exporter';
  4         9  
  4         46  
18             our @EXPORT = qw(pod2pandoc);
19             our @EXPORT_OK = qw(pod2pandoc parse_arguments);
20              
21             sub parse_arguments {
22 2     2 1 106 my %opt;
23 2 50       15 Getopt::Long::GetOptionsFromArray(
24             \@_, \%opt, 'help|h|?', 'version',
25             'parse=s', 'podurl=s', 'ext=s', 'index=s',
26             'wiki', 'default-meta=s', 'update', 'quiet',
27             'name',
28             ) or exit 1;
29 2 50       2476 pod2usage(1) if $opt{help};
30 2 50       9 if ( $opt{version} ) {
31 0         0 say "$0 $VERSION";
32 0         0 exit 0;
33             }
34              
35 2 50       10 my @input = @_ ? () : '-';
36              
37 2         8 my ($index) = grep { $_[$_] eq '--' } ( 0 .. @_ - 1 );
  9         30  
38              
39 2 100       9 if ( defined $index ) {
40 1         7 push @input, shift @_ for 0 .. $index - 1;
41 1         4 shift @_; # --
42             }
43             else {
44 1   66     19 push( @input, shift @_ ) while @_ and $_[0] !~ /^-./;
45             }
46              
47 2 50 33     10 if ( $opt{parse} and $opt{parse} ne '*' ) {
48 0         0 $opt{parse} = [ split /[, ]+/, $opt{parse} ];
49             }
50              
51 2         28 return ( \@input, \%opt, @_ );
52             }
53              
54             # TODO: move to Pandoc::Elements
55             sub _add_default_meta {
56 0     0     my ( $doc, $meta ) = @_;
57 0 0         return unless $meta;
58 0   0       $doc->meta->{$_} //= $meta->{$_} for keys %$meta;
59             }
60              
61             sub _default_meta {
62 0   0 0     my $meta = shift || {};
63 0 0         return $meta if ref $meta;
64              
65             # read default metadata from file
66 0 0         if ( $meta =~ /\.json$/ ) {
67 0 0         open( my $fh, "<:encoding(UTF-8)", $meta )
68             or croak "failed to open $meta";
69 0           local $/;
70 0           $meta = decode_json(<$fh>);
71 0           for ( keys %$meta ) {
72 0           $meta->{$_} = metadata( $meta->{$_} );
73             }
74 0           return $meta;
75             }
76             else {
77 0           pandoc->require('1.12.1');
78 0           return pandoc->file($meta)->meta;
79             }
80             }
81              
82             sub pod2pandoc {
83 0     0 1   my $input = shift;
84 0 0         my $opt = ref $_[0] ? shift : {};
85 0           my @args = @_;
86              
87             $opt->{meta} =
88 0   0       _default_meta( $opt->{meta} // delete $opt->{'default-meta'} );
89              
90             # directories
91 0 0 0       if ( @$input > 0 and -d $input->[0] ) {
92 0 0         my $target = @$input > 1 ? pop @$input : $input->[0];
93              
94 0           my $modules = Pod::Pandoc::Modules->new;
95 0           foreach my $dir (@$input) {
96 0           my $found = Pod::Simple::Pandoc->new->parse_modules($dir);
97             warn "no .pm, .pod or Perl script found in $dir\n"
98 0 0 0       unless %$found or $opt->{quiet};
99 0           $modules->add( $_ => $found->{$_} ) for keys %$found;
100             }
101              
102 0           _add_default_meta( $modules->{$_}, $opt->{meta} ) for %$modules;
103              
104 0           $modules->serialize( $target, $opt, @args );
105             }
106              
107             # files and/or module names
108             else {
109 0           my $parser = Pod::Simple::Pandoc->new(%$opt);
110 0 0         my $doc = $parser->parse_and_merge( @$input ? @$input : '-' );
111              
112 0           _add_default_meta( $doc, $opt->{meta} );
113              
114 0 0         if (@args) {
115 0           pandoc->require('1.12.1');
116 0           $doc->pandoc_version( pandoc->version );
117 0           print $doc->to_pandoc(@args);
118             }
119             else {
120 0           print $doc->to_json, "\n";
121             }
122             }
123             }
124              
125             1;
126             __END__