X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FPage%2FBase.pm;h=475be80d6952521e4d0fc96c6c82881913b64764;hb=16aa291dc49dceda7e612ed7eb2745036790d81c;hp=5b93f20992e00411b7d981f0af15edfc32a1e7c6;hpb=eafc7f54b9d964aac41b0715e18caf5ed58ef89a;p=gruntmaster-page.git diff --git a/lib/Gruntmaster/Page/Base.pm b/lib/Gruntmaster/Page/Base.pm index 5b93f20..475be80 100644 --- a/lib/Gruntmaster/Page/Base.pm +++ b/lib/Gruntmaster/Page/Base.pm @@ -9,107 +9,91 @@ use HTML::Template::Compiled; ################################################## -use POSIX (); -use Gruntmaster::Data (); -use List::Util (); +sub read_templates { + my $root = 'tmpl'; + my $name = shift; -sub import { - my $caller = caller; - my ($self, $name, $title) = @_; - - Gruntmaster::Data->export_to_level(1, $caller); - List::Util->export_to_level(1, $caller, qw/sum/); - - no strict 'refs'; - *{"${caller}::strftime"} = \&POSIX::strftime; - *{"${caller}::NAME"} = sub () { $name }; - *{"${caller}::TITLE"} = sub () { $title }; + map { m/\.(.+)$/; $1 => scalar read_file $_ } ; } -################################################## - -my %orig_header_templates = ( - en => <<'HTML', - -TITLE_GOES_HERE - - - - - - - -
iEval
-
TITLE_GOES_HERE
- - - -HTML -); - -my %orig_footer_templates = ( - en => <<'HTML', - - -HTML -); - -sub patch_templates { - my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]}; - my ($templates, $name) = @_; - my %out = %$templates; - for (<$root/$name.*>) { - m/\.(.+)$/; - $out{$1} = read_file $_ - } - - %out -} - -sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} } - -my %header_templates = patch_templates \%orig_header_templates, 'header'; -my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; +my %header_templates = read_templates 'header'; +my %footer_templates = read_templates 'footer'; sub header{ my ($language, $title) = @_; - %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates; $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; } sub footer{ - %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates; $footer_templates{$_[0]}; } -sub cook_templates { - my ($templates, $name, $title) = @_; +################################################## - my %out = patch_templates $templates, $name; - $out{$_} = header ($_, $title) . $out{$_} for keys %out; - $out{$_} .= footer $_ for keys %out; +use POSIX (); +use Gruntmaster::Data (); +use List::Util (); +use LWP::UserAgent; - %out +my $ua = LWP::UserAgent->new; +my %templates; + +use Carp qw/cluck/; + +sub import_to { + my ($self, $caller, $name, $title) = @_; + + Gruntmaster::Data->export_to_level(1, $caller); + List::Util->export_to_level(1, $caller, qw/sum/); + + no strict 'refs'; + *{"${caller}::strftime"} = \&POSIX::strftime; + *{"${caller}::debug"} = sub { + local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; + $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) + }; + *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] }; + *{"${caller}::purge"} = sub { + return unless $ENV{PURGE_HOST}; + my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]"); + $ua->request($req) + }; + + if ($name) { + $templates{$caller} = { read_templates $name }; + $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller}; + $templates{$caller}{$_} .= footer $_ for keys $templates{$caller}; + } } -################################################## +sub import { + return unless $_[0] eq __PACKAGE__; + splice @_, 1, 0, scalar caller; + goto &import_to +} -my %templates; +################################################## sub generate{ - my ($self, $path, $lang) = @_; - - $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates; + my ($self, $lang, @args) = @_; my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',); - $self->_generate($htc, $path, $lang); - $htc->output + $self->_generate($htc, $lang, @args); + my $out = $htc->output; + utf8::downgrade($out); + my $vary = 'Accept-Language, ' . $self->vary; + [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ] } sub _generate {} +sub vary { '' } + +sub max_age { 60 } + +sub variants { + return [] unless exists $templates{$_[0]}; + [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ] +} + 1