]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Base.pm
Merge branch 'master' into mindcoding
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
1 package Gruntmaster::Page::Base;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6
7 use File::Slurp qw/read_file/;
8 use HTML::Template::Compiled;
9
10 ##################################################
11
12 sub read_templates {
13 my $root = 'tmpl';
14 my $name = shift;
15
16 map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>;
17 }
18
19 my %header_templates = read_templates 'header';
20 my %footer_templates = read_templates 'footer';
21
22 sub header{
23 my ($language, $title) = @_;
24 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
25 }
26
27 sub footer{
28 $footer_templates{$_[0]};
29 }
30
31 ##################################################
32
33 use POSIX ();
34 use Gruntmaster::Data ();
35 use List::Util ();
36 use LWP::UserAgent;
37
38 my $ua = LWP::UserAgent->new;
39 my %templates;
40
41 sub import {
42 my $caller = caller;
43 my ($self, $name, $title) = @_;
44
45 Gruntmaster::Data->export_to_level(1, $caller);
46 List::Util->export_to_level(1, $caller, qw/sum/);
47
48 no strict 'refs';
49 *{"${caller}::strftime"} = \&POSIX::strftime;
50 *{"${caller}::debug"} = sub {
51 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
52 $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
53 };
54 *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] };
55 *{"${caller}::purge"} = sub {
56 return unless $ENV{PURGE_HOST};
57 my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
58 $ua->request($req)
59 };
60
61 if ($name) {
62 $templates{$caller} = { read_templates $name };
63 $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller};
64 $templates{$caller}{$_} .= footer $_ for keys $templates{$caller};
65 }
66 }
67
68 ##################################################
69
70 sub generate{
71 my ($self, $lang, @args) = @_;
72
73 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
74 $self->_generate($htc, $lang, @args);
75 my $out = $htc->output;
76 utf8::downgrade($out);
77 [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language', 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ]
78 }
79
80 sub _generate {}
81
82 sub max_age { 60 }
83
84 sub variants {
85 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
86 }
87
88 1
This page took 0.054944 seconds and 5 git commands to generate.