summaryrefslogtreecommitdiffstats
path: root/lib/Tozt.pm
blob: 3c139a587381f6b4e01776f99d432a790c668080 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package Tozt;
use Moose;
use MooseX::Types::Path::Class qw(Dir);
use namespace::autoclean;

use File::Copy::Recursive qw(rcopy);
use Path::Class ();
use Template;

with 'MooseX::Getopt';

has output_dir => (
    is      => 'ro',
    isa     => Dir,
    coerce  => 1,
    default => 'site',
);

has template_dir => (
    is      => 'ro',
    isa     => Dir,
    coerce  => 1,
    default => 'root/template',
);

has static_dir => (
    is      => 'ro',
    isa     => Dir,
    coerce  => 1,
    default => 'root/static',
);

has templater => (
    traits   => ['NoGetopt'],
    is       => 'ro',
    isa      => 'Template',
    lazy     => 1,
    init_arg => undef,
    default  => sub {
        Template->new(
            INCLUDE_PATH => shift->template_dir,
        );
    },
);

sub pages {
    my $self = shift;
    map { substr($_->basename, 0, -3) }
        grep { $_->isa('Path::Class::File')
            && $_->basename =~ /\.tt$/
            && $_->basename ne 'wrapper.tt' } $self->template_dir->children;
}

sub render_page {
    my $self = shift;
    my ($page) = @_;
    my $contents = $self->template_dir->file("$page.tt")->slurp;
    my $wrapper = <<WRAPPER;
[% WRAPPER wrapper.tt %]
$contents
[% END %]
WRAPPER

    mkdir $self->output_dir unless -d $self->output_dir;
    $self->templater->process(
        \$wrapper,
        { page => $page },
        $self->output_dir->file("$page.html")->stringify,
    );
}

sub render_all {
    my $self = shift;
    for my $page ($self->pages) {
        print "Rendering $page...\n";
        $self->render_page($page);
    }
}

sub update_static {
    my $self = shift;
    for my $file ($self->static_dir->children) {
        rcopy($file, $self->output_dir->stringify);
    }
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;