diff options
author | Jesse Luehrs <doy@tozt.net> | 2010-09-17 23:50:22 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2010-09-18 11:34:55 -0500 |
commit | 7f0ab9818534c6720aff98d10eb6e55aa63dbeee (patch) | |
tree | ca483c234279eb3570b7f0172c248a5238d09808 /lib | |
parent | a3ac0614071f5935f3c6754fbc2e498bddc9d692 (diff) | |
download | tozt.net-7f0ab9818534c6720aff98d10eb6e55aa63dbeee.tar.gz tozt.net-7f0ab9818534c6720aff98d10eb6e55aa63dbeee.zip |
basic sketch of the site rendering code
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Tozt.pm | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/Tozt.pm b/lib/Tozt.pm index e69de29..e8034b8 100644 --- a/lib/Tozt.pm +++ b/lib/Tozt.pm @@ -0,0 +1,86 @@ +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; + +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 => ( + is => 'ro', + isa => 'Template', + lazy => 1, + 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); + } +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; |