File Coverage

blib/lib/Template/Provider/Pandoc.pm
Criterion Covered Total %
statement 20 23 86.9
branch n/a
condition n/a
subroutine 7 10 70.0
pod n/a
total 27 33 81.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Template::Provider::Pandoc - pre-process templates with Pandoc
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 processes templates using Pandoc 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<LOAD_TEMPLATES> parameter to the Template module's C<new> method.
27              
28             This module accepts all of the standard parameters that can be passed
29             to any Template provider module. See L<Template::Provider> for the full
30             list.
31              
32             This module accepts two extra parameters, C<EXTENSIONS>, which defines the
33             file extensions that is used to identify files that require conversion, and
34             C<OUTPUT_FORMAT> which defines the the format that template will be converted
35             into.
36              
37             C<EXTENSIONS> 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<EXTENSIONS> 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<OUTPUT_FORMAT> is a single, scalar value containing the name of an output
57             format. The default value is C<html>. 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   98738 use strict;
  2         13  
  2         59  
75 2     2   11 use warnings;
  2         4  
  2         64  
76 2     2   58 use 5.010;
  2         6  
77              
78 2     2   1375 use Moose;
  2         970820  
  2         13  
79 2     2   17512 use MooseX::NonMoose;
  2         2263  
  2         9  
80             extends 'Template::Provider';
81              
82 2     2   146427 use Pandoc ();
  2         133380  
  2         778  
83              
84             our $VERSION = '0.0.4';
85              
86             has pandoc => (
87             isa => 'Pandoc',
88             is => 'ro',
89             lazy_build => 1,
90             handles => [qw[convert]],
91             );
92              
93             sub _build_pandoc {
94 0     0     return Pandoc->new;
95             }
96              
97             has default_extensions => (
98             isa => 'HashRef',
99             is => 'ro',
100             lazy_build => 1,
101             );
102              
103             sub _build_default_extensions {
104             return {
105 0     0     md => 'markdown',
106             };
107             }
108              
109             has default_output_format => (
110             isa => 'Str',
111             is => 'ro',
112             lazy_build => 1,
113             );
114              
115             sub _build_default_output_format {
116 0     0     return 'html';
117             }
118              
119             before _init => sub {
120             my $self = shift;
121             my ($opts) = @_;
122              
123             my $exts = $self->default_extensions;
124              
125             if (exists $opts->{EXTENSIONS}) {
126             $exts->{$_} = $opts->{EXTENSIONS}{$_} for keys %{$opts->{EXTENSIONS}};
127             delete $opts->{EXTENSIONS};
128             }
129              
130             $self->{EXTENSIONS} = $exts;
131              
132             $self->{OUTPUT_FORMAT} =
133             $opts->{OUTPUT_FORMAT} // $self->default_output_format;
134             };
135              
136             around _template_content => sub {
137             my $orig = shift;
138             my $self = shift;
139             my ($path) = @_;
140              
141             my ($data, $error, $mod_date) = $self->$orig(@_);
142              
143             my $done = 0;
144              
145             for (keys %{$self->{EXTENSIONS}}) {
146             next if $_ eq '*';
147             if ($path =~ /\.\Q$_\E$/) {
148             if (defined $self->{EXTENSIONS}{$_}) {
149             $data = $self->convert(
150             $self->{EXTENSIONS}{$_} => $self->{OUTPUT_FORMAT}, $data
151             );
152             }
153             $done = 1;
154             last;
155             }
156             }
157              
158             if (not $done and exists $self->{EXTENSIONS}{'*'}) {
159             $data = $self->convert(
160             $self->{EXTENSIONS}{'*'} => $self->{OUTPUT_FORMAT}, $data
161             );
162             }
163              
164             return ($data, $error, $mod_date) if wantarray;
165             return $data;
166             };
167              
168 2     2   19 no Moose;
  2         5  
  2         22  
169             # no need to fiddle with inline_constructor here
170             __PACKAGE__->meta->make_immutable;
171              
172             1;
173              
174             =head1 AUTHOR
175              
176             Dave Cross E<lt>dave@perlhacks.comE<gt>
177              
178             =head1 COPYRIGHT
179              
180             Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.
181              
182             This module is free software; you can redistribute it and/or
183             modify it under the same terms as Perl itself.
184              
185             =head1 SEE ALSO
186              
187             L<Template>, L<Template::Provider>, L<Pandoc>.
188              
189             =cut