File Coverage

blib/lib/Template/Provider/Markdown/Pandoc.pm
Criterion Covered Total %
statement 26 27 96.3
branch 5 6 83.3
condition 5 6 83.3
subroutine 6 6 100.0
pod 1 1 100.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Template::Provider::Markdown::Pandoc - expand Markdown templates to HTML
4              
5             =head1 SYNOPSIS
6              
7             use Template;
8             use Template::Provider::Markdown::Pandoc;
9              
10             my $tt = Template->new(
11             LOAD_TEMPLATES = [ Template::Provider::Markdown::Pandoc->new ],
12             );
13              
14             $tt->process('template.md', \%vars)
15              
16             =head1 DESCRIPTION
17              
18             Template::Provider::Markdown::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 one extra parameter, C, which defines the
33             file extension that is used to identify Markdown files. Only template
34             files with this extension will be pre-processed by this module. The
35             default extension is 'md', so you don't need to pass an C
36             parameter if you're happy to use that extension.
37              
38             If you want to pre-process all template files, then you need to explicitly
39             set the C parameter to C.
40              
41             y $tt = Template->new(
42             LOAD_TEMPLATES = [
43             Template::Provider::Markdown::Pandoc->new(
44             EXTENSION => undef,
45             },
46             ],
47             );
48              
49             =head1 Template::Provider::Markdown
50              
51             There is already a module called L available
52             on CPAN, so why did I write another, very similar-sounding, module? There
53             are two reasons.
54              
55             =over 4
56              
57             =item 1
58              
59             Template::Provider::Markdown uses Text::Markdown to do the conversion and
60             I've found a few problems with the Markdown conversion in that module. This
61             module uses C (see L) a very powerful and
62             flexible tool for converting between document formats.
63              
64             =item 2
65              
66             Template::Provider::Markdown assumes that all of your templates are in
67             Markdown and converts them all. That didn't fit with what I wanted to. I
68             only wanted to convert specific templates.
69              
70             However, because I'm using file extensions to recognise the templates
71             that need conversion, this module can only be used to pre-process templates
72             that are stored in files. This isn't a restriction in my use cases.
73              
74             =back
75              
76             =cut
77              
78             package Template::Provider::Markdown::Pandoc;
79              
80 2     2   31385 use strict;
  2         4  
  2         48  
81 2     2   6 use warnings;
  2         4  
  2         48  
82 2     2   760 use parent 'Template::Provider';
  2         421  
  2         8  
83 2     2   11494 use Pandoc;
  2         95593  
  2         13  
84              
85             our $VERSION = '0.0.1';
86              
87             my $pandoc;
88              
89             sub new {
90 3     3 1 29250 my $class = shift;
91 3         9 my %opts = @_;
92              
93 3         4 my $ext = 'md';
94 3 100       11 $ext = delete $opts{EXTENSION} if exists $opts{EXTENSION};
95              
96 3         22 my $self = $class->SUPER::new(%opts);
97              
98 3         193 $self->{EXTENSION} = $ext;
99              
100 3         9 return bless $self, $class;
101             }
102              
103             sub _template_content {
104 6     6   20697 my $self = shift;
105 6         7 my ($path) = @_;
106              
107 6         19 my ($data, $error, $mod_date) = $self->SUPER::_template_content($path);
108              
109 6 100 100     810 if (! defined $self->{EXTENSION} or $path =~ /\.\Q$self->{EXTENSION}\E$/) {
110 4   66     17 $pandoc //= pandoc;
111 4         531 $data = $pandoc->convert(markdown => 'html', $data);
112             }
113              
114 2 50       10 return ($data, $error, $mod_date) if wantarray;
115 0           return $data;
116             }
117              
118             1;
119              
120             =head1 AUTHOR
121              
122             Dave Cross Edave@perlhacks.comE
123              
124             =head1 COPYRIGHT
125              
126             Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.
127              
128             This module is free software; you can redistribute it and/or
129             modify it under the same terms as Perl itself.
130              
131             =head1 SEE ALSO
132              
133             L