blib/lib/App/Microsite/Assemble.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 171 CC 10 SCAR 25.5
CC
10
Cov
0%
assemble
30.0
_handlebars
20.0
_generate_page
6.0
CRAP 12.9
1 package App::Microsite::Assemble;
2 {
3 $App::Microsite::Assemble::VERSION = '0.03';
4 }
5 # ABSTRACT: Assemble a microsite with Handlebars
6 use strict;
7 use warnings;
8 use File::Next;
9 use Path::Class;
10 use JSON;
11 use Try::Tiny;
12 use Text::Handlebars;
13 use Text::Xslate 'mark_raw';
14  
15 sub assemble {
16 my $class = shift;
17 my $args = {
18 wrapper_name => 'wrapper.handlebars',
19 content_name => 'content.handlebars',
20 config_name => 'config.json',
21 templates_dir => 'templates/',
22 fragments_dir => 'fragments/',
23 build_dir => 'build/',
24 missing_fragment => sub {
25 my ($name, $paths) = @_;
26 die "Fragment named '$name' does not exist: (paths: @$paths)\n";
27 },
28 @_,
29 };
30  
31 for my $arg (qw/templates_dir fragments_dir build_dir/) {
32 $args->{$arg} = dir($args->{$arg});
33 }
34  
35 $args->{_seen_fragments} = {};
36  
37 $args->{build_dir}->mkpath;
38  
39 my %seen;
40  
41 my $dir_iter = File::Next::dirs($args->{templates_dir});
42 while (defined(my $dir_name = $dir_iter->())) {
43 my $dir = dir($dir_name);
44  
45 for my $child ($dir->children) {
46 # File::Next recurses into subdirectories, so
47 # we can skip them
48 next if $child->is_dir;
49  
50 # any level in the hierarchy can have a wrapper.handlebars or config.json
51 next if $child->basename eq $args->{wrapper_name}
52 || $child->basename eq $args->{config_name};
53  
54 # look, it's a page to generate!
55 if ($child->basename eq $args->{content_name}) {
56 my ($output, $config) = $class->_generate_page($dir, $args);
57 my $target = $config->{target}
58 or die "No configured 'target' file for '$dir'. Add: \"target\":\"path.html\" to $child\n";
59  
60 my $outfile = $args->{build_dir}->file($target);
61  
62 if ($seen{$outfile}) {
63 die "Outfile '$outfile' for $dir was already built by $seen{$outfile}\n";
64 }
65 $seen{$outfile} = $dir;
66  
67 $outfile->spew(iomode => '>:encoding(UTF-8)', $output);
68  
69 next;
70 }
71 }
72 }
73  
74 return {
75 seen_fragments => $args->{_seen_fragments},
76 built_files => \%seen,
77 };
78 }
79  
80 sub _generate_page {
81 my $class = shift;
82 my $dir = shift;
83 my $args = shift;
84  
85 my $input_file = $dir->file($args->{content_name});
86 my $input = $input_file->slurp
87 or die "Expected $args->{content_name} in $dir\n";
88  
89 my @paths;
90 my $path_dir = $dir;
91 while ($args->{templates_dir}->subsumes($path_dir)) {
92 push @paths, "$path_dir";
93 $path_dir = $path_dir->parent;
94 }
95  
96 my $handlebars = $class->_handlebars(\@paths, $args);
97  
98 my %config = %{ $class->_merge_config($dir, $args) };
99  
100 $args->{_current_template} = [$input_file];
101  
102 # render the innermost content
103 my $output = $handlebars->render_string($input, \%config);
104  
105 # now wrap it with all its parent dirs' wrapper.handlebars templates
106 my $wrapper_dir = $dir;
107 while ($args->{templates_dir}->subsumes($wrapper_dir)) {
108 my $wrapper_file = $wrapper_dir->file($args->{wrapper_name});
109 if (-e $wrapper_file) {
110 $config{content} = mark_raw($output);
111 my $wrapper = $wrapper_file->slurp;
112  
113 push @{ $args->{_current_template} }, $wrapper_file;
114 $output = $handlebars->render_string($wrapper, \%config);
115 }
116  
117 $wrapper_dir = $wrapper_dir->parent;
118 }
119  
120 return ($output, \%config);
121 }
122  
123 sub _handlebars {
124 my $class = shift;
125 my $path = shift;
126 my $args = shift;
127  
128 my $hb;
129 $hb = Text::Handlebars->new(
130 # errors are caught and kind of suppressed inside xslate,
131 # so make their presence more violently known
132 # also, current_template provides more context for where the error
133 # occurred
134 die_handler => sub {
135 my $error = shift;
136 my $template = shift @{ $args->{_current_template} };
137 $template = "$_ (wrapping $template)"
138 for @{ $args->{_current_template} };
139 warn "Unable to process $template\n $error";
140 exit 1;
141 },
142 helpers => {
143 %{ $args->{helpers} || {} },
144 fragment => sub {
145 my ($context, $name) = @_;
146  
147 # {{fragment foo}} doesn't work, need {{fragment "foo"}}
148 die "{{fragment NAME}} helper needs a quoted name\n"
149 if !$name;
150  
151 my @paths = @$path;
152 s{^templates\b}{fragments} for @paths;
153  
154 for my $path (@paths) {
155 my $fragment = dir($path)->file($name);
156 if (-e $fragment) {
157 $args->{_seen_fragments}{$fragment}++;
158  
159 my $content = $fragment->slurp(iomode => '<:encoding(UTF-8)');
160 if ($args->{fragment_filter}) {
161 $content = $args->{fragment_filter}->($content, $fragment, $name);
162 }
163  
164 return mark_raw($hb->render_string($content));
165 }
166 }
167  
168 my $content = $args->{missing_fragment}->($name, \@paths);
169 return mark_raw($hb->render_string($content));
170 },
171 },
172 suffix => '.partial',
173 path => $path,
174 );
175 }
176  
177 # merge all the config.json's in this hierarchy
178 sub _merge_config {
179 my $class = shift;
180 my $dir = shift;
181 my $args = shift;
182  
183 my %config;
184  
185 my $config_dir = $dir;
186 while ($args->{templates_dir}->subsumes($config_dir)) {
187 my $config_file = $config_dir->file($args->{config_name});
188 if (-e $config_file) {
189 my $config_contents = $config_file->slurp;
190  
191 # provide more context for JSON syntax errors
192 try {
193 # more specific dirs override config from wrapper dirs
194 %config = (
195 %{ from_json($config_contents) },
196 %config,
197 );
198 } catch {
199 die "Parse error in $config_file:\n $_\n";
200 };
201 }
202  
203 $config_dir = $config_dir->parent;
204 }
205  
206 return \%config;
207 }
208  
209 1;
210  
211 __END__