File Coverage

blib/lib/App/Embra/Plugin/TemplateToolkit.pm
Criterion Covered Total %
statement 46 47 97.8
branch 9 14 64.2
condition n/a
subroutine 14 15 93.3
pod n/a
total 69 76 90.7


line stmt bran cond sub pod time code
1 2     2   543 use strict;
  2         4  
  2         69  
2 2     2   9 use warnings;
  2         3  
  2         95  
3              
4             package App::Embra::Plugin::TemplateToolkit;
5             $App::Embra::Plugin::TemplateToolkit::VERSION = '0.001'; # TRIAL
6             # ABSTRACT: use Template::Toolkit to turn file content into HTML
7              
8 2     2   781 use Path::Class qw<>;
  2         69296  
  2         38  
9 2     2   18 use Method::Signatures;
  2         2  
  2         18  
10 2     2   1704 use Template;
  2         32174  
  2         61  
11 2     2   658 use Moo;
  2         7482  
  2         26  
12              
13              
14              
15             has 'include_path' => (
16             is => 'ro',
17             default => sub { 'templates' },
18             coerce => sub { Path::Class::dir( $_[0] ) },
19             );
20              
21              
22             has 'default_template' => (
23             is => 'lazy',
24             default => method { 'default'. $self->extension },
25             );
26              
27              
28             has 'extension' => (
29             is => 'ro',
30             default => sub { '.tt' },
31             );
32              
33              
34             has 'assembler' => (
35             is => 'lazy',
36 2 50   2   4016 );
  1     1   330  
  1         5  
37 1         7  
38             method _build_assembler {
39             Template->new({
40             INCLUDE_PATH => $self->include_path,
41             DEFAULT => $self->default_template,
42             TRIM => 1,
43             });
44 2 50   2   1071 }
  2     2   195  
  2         7  
45 2         3  
  2         29  
46 3 100       37 method assemble_files {
47             for my $file ( @{ $self->embra->files } ) {
48 2         15 next if $file->ext ne 'html';
49 2         3  
50 2         411 my $template = $file->with_ext( $self->extension );
51             my $assembled;
52             my $notes = {
53 2         22 content => $file->content,
54             name => $file->name,
55 2     2   222 %{ $file->notes },
  2         3  
  2         278  
56             };
57 2 50   2   96 use Try::Tiny;
58             try {
59 0     0   0 $self->assembler->process( $template, $notes, \$assembled ) or $self->debug( $self->assembler->error );
60 2         20 } catch {
61 2         66060 $self->debug( $_ );
62 2         20 };
63             $file->content( $assembled );
64             $file->notes->{assembled_by} = __PACKAGE__;
65             }
66 2 50   2   110342 }
  3 50   3   4  
  3         7  
  3         3  
  3         6  
67 3 100       15  
68 2         469 method exclude_file( $file ) {
69             return 1 if $self->include_path->subsumes( $file->name );
70             return;
71             }
72              
73             with 'App::Embra::Role::FileAssembler';
74             with 'App::Embra::Role::FilePruner';
75              
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             App::Embra::Plugin::TemplateToolkit - use Template::Toolkit to turn file content into HTML
87              
88             =head1 VERSION
89              
90             version 0.001
91              
92             =head1 DESCRIPTION
93              
94             This plugin will process site files through Template Toolkit. For each file with a C<.html> extension, it will look for a template in the C<include_path> with a matching name and use it to process the contents of the file into an assembled HTML document.
95              
96             Templates will be passed the file's content and body as variables, as well as each of the file's notes.
97              
98             =head1 ATTRIBUTES
99              
100             =head2 include_path
101              
102             Where to find templates. Defaults to F<templates> in the current directory. All files within the path will be pruned.
103              
104             =head2 default_template
105              
106             Template to use if a file doesn't have a matching template. Defaults to F<default.tt>.
107              
108             =head2 extension
109              
110             The extension of template files. Defaults to C<.tt>.
111              
112             =head2 assembler
113              
114             The object used to assemble files. Defaults to an instance of L<Template Toolkit|Template>.
115              
116             =head1 AUTHOR
117              
118             Daniel Holz <dgholz@gmail.com>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2015 by Daniel Holz.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut