File Coverage

blib/lib/Template/Provider/Markdown/Pandoc.pm
Criterion Covered Total %
statement 14 29 48.2
branch 0 6 0.0
condition 0 6 0.0
subroutine 5 7 71.4
pod 1 1 100.0
total 20 49 40.8


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 L 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   31123 use strict;
  2         2  
  2         49  
81 2     2   6 use warnings;
  2         2  
  2         37  
82 2     2   34 use 5.010;
  2         6  
83              
84 2     2   744 use parent 'Template::Provider';
  2         410  
  2         9  
85 2     2   11749 use Pandoc;
  2         87627  
  2         11  
86              
87             our $VERSION = '0.0.2';
88              
89             my $pandoc;
90              
91             sub new {
92 0     0 1   my $class = shift;
93 0           my %opts = @_;
94              
95 0           my $ext = 'md';
96 0 0         $ext = delete $opts{EXTENSION} if exists $opts{EXTENSION};
97              
98 0           my $self = $class->SUPER::new(%opts);
99              
100 0           $self->{EXTENSION} = $ext;
101              
102 0           return bless $self, $class;
103             }
104              
105             sub _template_content {
106 0     0     my $self = shift;
107 0           my ($path) = @_;
108              
109 0           my ($data, $error, $mod_date) = $self->SUPER::_template_content($path);
110              
111 0 0 0       if (! defined $self->{EXTENSION} or $path =~ /\.\Q$self->{EXTENSION}\E$/) {
112 0   0       $pandoc //= pandoc;
113 0           $data = $pandoc->convert(markdown => 'html', $data);
114             }
115              
116 0 0         return ($data, $error, $mod_date) if wantarray;
117 0           return $data;
118             }
119              
120             1;
121              
122             =head1 AUTHOR
123              
124             Dave Cross Edave@perlhacks.comE
125              
126             =head1 COPYRIGHT
127              
128             Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.
129              
130             This module is free software; you can redistribute it and/or
131             modify it under the same terms as Perl itself.
132              
133             =head1 SEE ALSO
134              
135             L