blib/lib/Template/Provider/Pandoc.pm

filter: Statementstmt 47.5% 29 / 61 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 58.3% 7 / 12 Podpod n/a totaltotal 36.0% 36 / 100 CC 13 SCAR 37.0
CC
13
Cov
35%
__ANON__
90.0
__ANON__
30.0
_build_pandoc
2.0
CRAP 40.5
↑ Summary
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 three extra parameters, C<EXTENSIONS>, which defines the
33 file extensions that is used to identify files that require conversion,
34 C<OUTPUT_FORMAT> which defines the the format that template will be converted
35 into and C<STRIP_FRONT_MATTER> which will remove Jekyll-style front matter
36 from the file content before returning it.
37  
38 C<EXTENSIONS> is a hash reference. The default is to only handle Markdown
39 files (which are identified by the extension .md). You can get a full list
40 of the allowed input formats by running
41  
42 $ pandoc --list-input-formats
43  
44 at a command line.
45  
46 The C<EXTENSIONS> option supports one special option. If you use `*` as
47 an extenstion, then files with any extension will be converted using the
48 supplied format. So code like:
49  
50 my $provider = Template::Provider::Pandoc(
51 EXTENSIONS => { '*' => 'markdown' },
52 );
53  
54 will lead to all files being pre-processed as Markdown files before being
55 handed to the Template Toolkit.
56  
57 C<OUTPUT_FORMAT> is a single, scalar value containing the name of an output
58 format. The default value is C<html>. You can get a full list of the
59 allowed putput values by running
60  
61 $ pandoc --list-output-values
62  
63 at a command line.
64  
65 C<STRIP_FRONT_MATTER> is a flag that is either true or false. The default
66 value is false. If it is true then this module will remove any Jekyll-style
67 front matter from the file contents before returning them.
68  
69 Jekyll-style front matter is a format used by Jekyll and other, similar,
70 static site builders. It is a fragmant of YAML that is inserted at the top
71 of the file between two lines that consist only of three dashes, like this:
72  
73 ---
74 title: Title of the page
75 author: Joe Author
76 tags:
77 - list
78 - of
79 -tags
80 ---
81  
82 The current implementation doesn't check that the front matter is actually
83 YAML. It simply removes everything from the first line of dashes to the
84 second.
85  
86 =head1 Template::Provider::Markdown::Pandoc
87  
88 This module is a successor to Template::Provider::Markdown::Pandoc. This
89 replacement module has all the functionality of the older module, and a lot
90 more besides. And, as a bonus, it has a shorter name!
91  
92 =cut
93  
94 package Template::Provider::Pandoc;
95  
96 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 3 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 3 extends 'Template::Provider';
103  
104 3 ▶use Pandoc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 3 our $VERSION = '0.1.1';
107  
108 3 has pandoc => (
109 isa => 'Pandoc',
110 is => 'ro',
111 lazy_build => 1,
112 handles => [qw[convert]],
113 );
114  
115 0 ▶sub _build_pandoc {
Subroutine _build_pandoc: not called CC=1 SCAR=6.9
116 0 return Pandoc->new;
117 }
118  
119 3 has default_extensions => (
120 isa => 'HashRef',
121 is => 'ro',
122 lazy_build => 1,
123 );
124  
125 0 ▶sub _build_default_extensions {
Subroutine _build_default_extensions: not called CC=1 SCAR=6.9
126 return {
127 0 md => 'markdown',
128 };
129 }
130  
131 3 has default_output_format => (
132 isa => 'Str',
133 is => 'ro',
134 lazy_build => 1,
135 );
136  
137 0 ▶sub _build_default_output_format {
Subroutine _build_default_output_format: not called CC=1 SCAR=6.9
138 0 return 'html';
139 }
140  
141 0 ▶before _init => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
142 0 my $self = shift;
143 0 my ($opts) = @_;
144  
145 0 my $exts = $self->default_extensions;
146  
147 0 ▶ if (exists $opts->{EXTENSIONS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"EXTENSIONS"}) 0 0 0
148 0 $exts->{$_} = $opts->{EXTENSIONS}{$_} for keys %{$opts->{EXTENSIONS}};
149 0 delete $opts->{EXTENSIONS};
150 }
151  
152 0 $self->{EXTENSIONS} = $exts;
153  
154 $self->{OUTPUT_FORMAT} =
155 0 ▶ $opts->{OUTPUT_FORMAT} // $self->default_output_format;
Condition0%0/3
$opts->{"OUTPUT_FORMAT"} // $self->default_output_format
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"OUTPUT_FORMAT"} // $self->default_output_format
$opts->{"OUTPUT_FORMAT"} $self->default_output_format
Truth table0%0/3
A // B
A= $opts->{"OUTPUT_FORMAT"}B= $self->default_output_format
ABresult
1X1
011
000
156  
157 0 ▶ $self->{STRIP_FRONT_MATTER} = $opts->{STRIP_FRONT_MATTER} // 0;
Condition0%0/2
$opts->{"STRIP_FRONT_MATTER"} // 0
l!l
00
MC/DC0%0/1
$opts->{"STRIP_FRONT_MATTER"} // 0
$opts->{"STRIP_FRONT_MATTER"}
Truth table0%0/2
A
A= $opts->{"STRIP_FRONT_MATTER"}
Aresult
11
00
158 3 };
159  
160 0 ▶around _template_content => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
161 0 my $orig = shift;
162 0 my $self = shift;
163 0 my ($path) = @_;
164  
165 0 my ($data, $error, $mod_date) = $self->$orig(@_);
166  
167 0 ▶ if ($self->{STRIP_FRONT_MATTER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"STRIP_FRONT_MATTER"}) 0 0 0
168 0 $data =~ s/\A---\n.+?\n---\n//s;
169 }
170  
171 0 my $done = 0;
172  
173 0 for (keys %{$self->{EXTENSIONS}}) {
174 0 ▶ next if $_ eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "*" 0 0 0
175 0 ▶ if ($path =~ /\.\Q$_\E$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\.\Q$_\E$/) 0 0 0
176 0 ▶ if (defined $self->{EXTENSIONS}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"EXTENSIONS"}{$_}) 0 0 0
177 $data = $self->convert(
178 0 $self->{EXTENSIONS}{$_} => $self->{OUTPUT_FORMAT}, $data
179 );
180 }
181 0 $done = 1;
182 0 last;
183 }
184 }
185  
186 0 ▶ if (not $done and exists $self->{EXTENSIONS}{'*'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and exists $self->{"EXTENSIONS"}{"*"}) 0 0 0
Condition0%0/3
not $done and exists $self->{"EXTENSIONS"}{"*"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $done and exists $self->{"EXTENSIONS"}{"*"}
not $done exists $self->{"EXTENSIONS"}{"*"}
Truth table0%0/3
not(A) and B
A= not $doneB= exists $self->{"EXTENSIONS"}{"*"}
ABresult
0X0
100
111
187 $data = $self->convert(
188 0 $self->{EXTENSIONS}{'*'} => $self->{OUTPUT_FORMAT}, $data
189 );
190 }
191  
192 0 ▶ return ($data, $error, $mod_date) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
193 0 return $data;
194 3 };
195  
196 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 # no need to fiddle with inline_constructor here
198 3 __PACKAGE__->meta->make_immutable;
199  
200 3 1;
201  
202 =head1 AUTHOR
203  
204 Dave Cross E<lt>dave@perlhacks.comE<gt>
205  
206 =head1 COPYRIGHT
207  
208 Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.
209  
210 This module is free software; you can redistribute it and/or
211 modify it under the same terms as Perl itself.
212  
213 =head1 SEE ALSO
214  
215 L<Template>, L<Template::Provider>, L<Pandoc>.
216  
217 =cut
↑ Summary