File Coverage

blib/lib/App/DuckPAN/Template.pm
Criterion Covered Total %
statement 29 30 96.6
branch 4 4 100.0
condition n/a
subroutine 8 9 88.8
pod 0 1 0.0
total 41 44 93.1


line stmt bran cond sub pod time code
1             package App::DuckPAN::Template;
2             our $AUTHORITY = 'cpan:DDG';
3             # ABSTRACT: Template to generate one file of an Instant Answer
4             $App::DuckPAN::Template::VERSION = '1018';
5             # An Instant Answer has multiple templates, each of which can be used
6             # to generate one output file.
7              
8 2     2   11 use Moo;
  2         4  
  2         10  
9              
10 2     2   557 use Try::Tiny;
  2         3  
  2         83  
11 2     2   846 use Text::Xslate;
  2         14215  
  2         86  
12 2     2   14 use Path::Tiny qw(path);
  2         4  
  2         93  
13              
14 2     2   784 use namespace::clean;
  2         11757  
  2         14  
15              
16             has name => (
17             is => 'ro',
18             required => 1,
19             doc => 'Name of the template',
20             );
21              
22             has label => (
23             is => 'ro',
24             required => 1,
25             doc => 'Label of the template',
26             );
27              
28             has input_file => (
29             is => 'ro',
30             required => 1,
31             doc => 'Path of the input file for the template',
32             );
33              
34             has output_file => (
35             is => 'ro',
36             required => 1,
37             doc => 'Path of the output file for the template. ' .
38             'This string is rendered through Text::Xslate to get the final path.',
39             );
40              
41             has output_directory => (
42             is => 'ro',
43             init_arg => undef,
44             lazy => 1,
45             builder => 1,
46             doc => 'Directory known to contain all of the generated template output files and subdirectories',
47             );
48              
49             sub _build_output_directory {
50 2     2   1793 my ($self) = @_;
51 2         9 my $out_dir = path($self->output_file);
52              
53             # Get the directory that is certain to be the closest ancestor of the
54             # output file i.e., keep removing directory parts from the right till the
55             # path does not contain any Text::Xslate syntax.
56 2         31 $out_dir = $out_dir->parent while $out_dir =~ /<:/;
57              
58 2         229 return $out_dir;
59             }
60              
61             # Create the output file from the input file
62             sub generate {
63 11     11 0 8275 my ($self, $vars) = @_;
64              
65             # Increased verbosity to help while writing templates
66 11         57 my $tx = Text::Xslate->new(type => 'text', verbose => 2);
67 11         2781 my $input_file = path($self->input_file);
68              
69             # (should not occur for users)
70 11 100       175 die "Template input file '$input_file' not found" unless $input_file->exists;
71              
72             # The output file path is a Text::Xslate template, so we generate the
73             # actual path here
74 9         199 my $output_file = path($tx->render_string($self->output_file, $vars));
75              
76 9 100       93313 die "Template output file '" . $output_file . "' already exists" if $output_file->exists;
77              
78 8         178 my $content = $tx->render($input_file, $vars);
79              
80             try {
81 8     8   605 path($output_file)->touchpath->spew_utf8($content);
82             } catch {
83 0     0   0 die "Error creating output file '$output_file' from template: $_";
84 8         10111 };
85              
86 8         7717 return $output_file;
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =head1 NAME
96              
97             App::DuckPAN::Template - Template to generate one file of an Instant Answer
98              
99             =head1 VERSION
100              
101             version 1018
102              
103             =head1 AUTHOR
104              
105             DuckDuckGo <open@duckduckgo.com>, Zach Thompson <zach@duckduckgo.com>, Zaahir Moolla <moollaza@duckduckgo.com>, Torsten Raudssus <torsten@raudss.us> L<https://raudss.us/>
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             This software is Copyright (c) 2013 by DuckDuckGo, Inc. L<https://duckduckgo.com/>.
110              
111             This is free software, licensed under:
112              
113             The Apache License, Version 2.0, January 2004
114              
115             =cut