File Coverage

blib/lib/Template/Provider/Pandoc.pm
Criterion Covered Total %
statement 14 41 34.1
branch 0 12 0.0
condition 0 9 0.0
subroutine 5 7 71.4
pod n/a
total 19 69 27.5


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Template::Provider::Pandoc - expand Markdown templates to HTML
4              
5             =head1 SYNOPSIS
6              
7             use Template;
8             use Template::Provider::Pandoc;
9              
10             my $tt = Template->new(
11             LOAD_TEMPLATES = [ Template::Provider::Pandoc->new ],
12             );
13              
14             $tt->process('template.md', \%vars)
15              
16             =head1 DESCRIPTION
17              
18             Template::Provider::Pandoc is an extension to the Template Toolkit
19             which automatically converts Markdown files into HTML before they are
20             processed by TT.
21              
22             =head1 USAGE
23              
24             Like any Template provider module, you will usually use this module by
25             creating an instance of the object and passing that in the
26             C parameter to the Template module's C method.
27              
28             This module can accept all of the standard parameters that can be passed
29             to any Template provider module. See L for the full
30             list.
31              
32             This module accepts two extra parameters, C, which defines the
33             file extensions that is used to identify files that require conversion, and
34             C which defines the the format that template will be converted
35             into.
36              
37             C is a hash reference. The default is to only handle Markdown
38             files (which are identified by the extension .md). You can get a full list
39             of the allowed input formats by running
40              
41             $ pandoc --list-input-formats
42              
43             at a command line.
44              
45             The C option supports one special option. If you use `*` as
46             an extenstion, then files with any extension will be converted using the
47             supplied format. So code like:
48              
49             my $provider = Template::Provider::Pandoc(
50             EXTENSIONS => { '*' => 'markdown' },
51             );
52              
53             will lead to all files being pre-processed as Markdown files before being
54             handed to the Template Toolkit.
55              
56             C is a single, scalar value containing the name of an output
57             format. The default value is C. You can get a full list of the
58             allowed putput values by running
59              
60             $ pandoc --list-output-values
61              
62             at a command line.
63              
64             =head1 Template::Provider::Markdown::Pandoc
65              
66             This module is a successor to Template::Provider::Markdown::Pandoc. This
67             replacement module has all the functionality of the older module, and a lot
68             more besides. And, as a bonus, it has a shorter name!
69              
70             =cut
71              
72             package Template::Provider::Pandoc;
73              
74 2     2   34121 use strict;
  2         5  
  2         85  
75 2     2   14 use warnings;
  2         5  
  2         66  
76 2     2   58 use 5.010;
  2         21  
77              
78 2     2   775 use parent 'Template::Provider';
  2         492  
  2         11  
79 2     2   12763 use Pandoc;
  2         104236  
  2         15  
80              
81             our $VERSION = '0.0.2';
82              
83             my $pandoc;
84              
85             my $default_extensions = {
86             md => 'markdown',
87             };
88             my $default_output_format = 'html';
89              
90             sub _init {
91 0     0     my $self = shift;
92 0           my ($opts) = @_;
93              
94 0           my $exts = $default_extensions;
95 0 0         if (exists $opts->{EXTENSIONS}) {
96 0           $exts->{$_} = $opts->{EXTENSIONS}{$_} for keys %{$opts->{EXTENSIONS}};
  0            
97 0           delete $opts->{EXTENSIONS};
98             }
99              
100 0           $self->{EXTENSIONS} = $exts;
101              
102 0   0       $self->{OUTPUT_FORMAT} = $opts->{OUTPUT_FORMAT} // $default_output_format;
103              
104 0           return $self->SUPER::_init($opts);
105             }
106              
107             sub _template_content {
108 0     0     my $self = shift;
109 0           my ($path) = @_;
110              
111 0           my ($data, $error, $mod_date) = $self->SUPER::_template_content($path);
112              
113 0           my $done = 0;
114              
115 0           for (keys %{$self->{EXTENSIONS}}) {
  0            
116 0 0         next if $_ eq '*';
117 0 0         if ($path =~ /\.\Q$_\E$/) {
118 0 0         if (defined $self->{EXTENSIONS}{$_}) {
119 0   0       $pandoc //= pandoc;
120             $data = $pandoc->convert(
121 0           $self->{EXTENSIONS}{$_} => $self->{OUTPUT_FORMAT}, $data
122             );
123             }
124 0           $done = 1;
125 0           last;
126             }
127             }
128              
129 0 0 0       if (!$done and exists $self->{EXTENSIONS}{'*'}) {
130             $data = $pandoc->convert(
131 0           $self->{EXTENSIONS}{'*'} => $self->{OUTPUT_FORMAT}, $data
132             );
133             }
134              
135 0 0         return ($data, $error, $mod_date) if wantarray;
136 0           return $data;
137             }
138              
139             1;
140              
141             =head1 AUTHOR
142              
143             Dave Cross Edave@perlhacks.comE
144              
145             =head1 COPYRIGHT
146              
147             Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.
148              
149             This module is free software; you can redistribute it and/or
150             modify it under the same terms as Perl itself.
151              
152             =head1 SEE ALSO
153              
154             L