From 7f0ab9818534c6720aff98d10eb6e55aa63dbeee Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 17 Sep 2010 23:50:22 -0500 Subject: basic sketch of the site rendering code --- lib/Tozt.pm | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'lib') 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 = <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; -- cgit v1.2.3-54-g00ecf