]>
Commit | Line | Data |
---|---|---|
bb95f538 | 1 | package Gruntmaster::Page::Base; |
832cb45e MG |
2 | |
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
e046c73a | 6 | our $VERSION = '5999.000_001'; |
6d78fc24 MG |
7 | |
8 | use File::Slurp qw/read_file/; | |
bb95f538 MG |
9 | use HTML::Template::Compiled; |
10 | ||
11 | ################################################## | |
12 | ||
9da2f495 MG |
13 | sub read_templates { |
14 | my $root = 'tmpl'; | |
15 | my $name = shift; | |
16 | ||
17 | map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>; | |
18 | } | |
19 | ||
20 | my %header_templates = read_templates 'header'; | |
21 | my %footer_templates = read_templates 'footer'; | |
22 | ||
23 | sub header{ | |
24 | my ($language, $title) = @_; | |
25 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; | |
26 | } | |
27 | ||
28 | sub footer{ | |
29 | $footer_templates{$_[0]}; | |
30 | } | |
31 | ||
32 | ################################################## | |
33 | ||
bb95f538 | 34 | use POSIX (); |
bb95f538 | 35 | use List::Util (); |
49c1467a | 36 | use LWP::UserAgent; |
5c6aea93 MG |
37 | use Plack::Request (); |
38 | use feature (); | |
49c1467a MG |
39 | |
40 | my $ua = LWP::UserAgent->new; | |
9da2f495 | 41 | my %templates; |
bb95f538 | 42 | |
fdbf59e5 MG |
43 | use Carp qw/cluck/; |
44 | ||
45 | sub import_to { | |
46 | my ($self, $caller, $name, $title) = @_; | |
bb95f538 | 47 | |
5c6aea93 MG |
48 | strict->import; |
49 | feature->import(':5.14'); | |
50 | warnings->import; | |
51 | File::Slurp->export_to_level(1, $caller, qw/read_file/); | |
bb95f538 MG |
52 | List::Util->export_to_level(1, $caller, qw/sum/); |
53 | ||
54 | no strict 'refs'; | |
5c6aea93 MG |
55 | *{"${caller}::ISA"} = [__PACKAGE__]; |
56 | *{"${caller}::VERSION"} = $VERSION; | |
bb95f538 | 57 | *{"${caller}::strftime"} = \&POSIX::strftime; |
7dc32473 MG |
58 | *{"${caller}::debug"} = sub { |
59 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
191f4979 | 60 | $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) |
7dc32473 | 61 | }; |
d3200993 | 62 | *{"${caller}::db"} = sub { $_[0]->{'gruntmaster.dbic'} }; |
ff57a758 | 63 | *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] }; |
49c1467a MG |
64 | *{"${caller}::purge"} = sub { |
65 | return unless $ENV{PURGE_HOST}; | |
66 | my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]"); | |
67 | $ua->request($req) | |
68 | }; | |
832cb45e | 69 | |
9da2f495 MG |
70 | if ($name) { |
71 | $templates{$caller} = { read_templates $name }; | |
72 | $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller}; | |
73 | $templates{$caller}{$_} .= footer $_ for keys $templates{$caller}; | |
6d78fc24 | 74 | } |
2b0036ac MG |
75 | } |
76 | ||
fdbf59e5 MG |
77 | sub import { |
78 | return unless $_[0] eq __PACKAGE__; | |
79 | splice @_, 1, 0, scalar caller; | |
80 | goto &import_to | |
81 | } | |
82 | ||
bb95f538 MG |
83 | ################################################## |
84 | ||
bb95f538 | 85 | sub generate{ |
7dc32473 | 86 | my ($self, $lang, @args) = @_; |
bb95f538 | 87 | |
81cce380 | 88 | my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML', use_perl => 1); |
7dc32473 | 89 | $self->_generate($htc, $lang, @args); |
1053baee | 90 | my $out = $htc->output; |
b8d6a6f1 | 91 | utf8::decode($out); |
821c3e69 MG |
92 | utf8::decode($out); |
93 | utf8::encode($out); | |
c86e504e MG |
94 | my $vary = 'Accept-Language, ' . $self->vary; |
95 | [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ] | |
bb95f538 MG |
96 | } |
97 | ||
98 | sub _generate {} | |
99 | ||
fdbf59e5 | 100 | sub vary { '' } |
c86e504e | 101 | |
ff57a758 MG |
102 | sub max_age { 60 } |
103 | ||
7dc32473 | 104 | sub variants { |
16aa291d | 105 | return [] unless exists $templates{$_[0]}; |
9da2f495 | 106 | [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ] |
7dc32473 MG |
107 | } |
108 | ||
bb95f538 | 109 | 1 |