App-Microsite-Assemble
view release on metacpan or search on metacpan
lib/App/Microsite/Assemble.pm view on Meta::CPAN
package App::Microsite::Assemble;
{
$App::Microsite::Assemble::VERSION = '0.03';
}
# ABSTRACT: Assemble a microsite with Handlebars
use strict;
use warnings;
use File::Next;
use Path::Class;
use JSON;
use Try::Tiny;
use Text::Handlebars;
use Text::Xslate 'mark_raw';
sub assemble {
my $class = shift;
my $args = {
wrapper_name => 'wrapper.handlebars',
content_name => 'content.handlebars',
config_name => 'config.json',
templates_dir => 'templates/',
fragments_dir => 'fragments/',
build_dir => 'build/',
missing_fragment => sub {
my ($name, $paths) = @_;
die "Fragment named '$name' does not exist: (paths: @$paths)\n";
},
@_,
};
for my $arg (qw/templates_dir fragments_dir build_dir/) {
$args->{$arg} = dir($args->{$arg});
}
$args->{_seen_fragments} = {};
$args->{build_dir}->mkpath;
my %seen;
my $dir_iter = File::Next::dirs($args->{templates_dir});
while (defined(my $dir_name = $dir_iter->())) {
my $dir = dir($dir_name);
for my $child ($dir->children) {
# File::Next recurses into subdirectories, so
# we can skip them
next if $child->is_dir;
# any level in the hierarchy can have a wrapper.handlebars or config.json
next if $child->basename eq $args->{wrapper_name}
|| $child->basename eq $args->{config_name};
# look, it's a page to generate!
if ($child->basename eq $args->{content_name}) {
my ($output, $config) = $class->_generate_page($dir, $args);
my $target = $config->{target}
or die "No configured 'target' file for '$dir'. Add: \"target\":\"path.html\" to $child\n";
my $outfile = $args->{build_dir}->file($target);
if ($seen{$outfile}) {
die "Outfile '$outfile' for $dir was already built by $seen{$outfile}\n";
}
$seen{$outfile} = $dir;
$outfile->spew(iomode => '>:encoding(UTF-8)', $output);
next;
}
}
( run in 1.099 second using v1.01-cache-2.11-cpan-39bf76dae61 )