]>
Commit | Line | Data |
---|---|---|
1 | package Gruntmaster::Page::Base; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | our $VERSION = '5999.000_001'; | |
7 | ||
8 | use File::Slurp qw/read_file/; | |
9 | use HTML::Template::Compiled; | |
10 | ||
11 | ################################################## | |
12 | ||
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 | ||
34 | use POSIX (); | |
35 | use List::Util (); | |
36 | use LWP::UserAgent; | |
37 | use Plack::Request (); | |
38 | use feature (); | |
39 | ||
40 | my $ua = LWP::UserAgent->new; | |
41 | my %templates; | |
42 | ||
43 | use Carp qw/cluck/; | |
44 | ||
45 | sub import_to { | |
46 | my ($self, $caller, $name, $title) = @_; | |
47 | ||
48 | strict->import; | |
49 | feature->import(':5.14'); | |
50 | warnings->import; | |
51 | File::Slurp->export_to_level(1, $caller, qw/read_file/); | |
52 | List::Util->export_to_level(1, $caller, qw/sum/); | |
53 | ||
54 | no strict 'refs'; | |
55 | *{"${caller}::ISA"} = [__PACKAGE__]; | |
56 | *{"${caller}::VERSION"} = $VERSION; | |
57 | *{"${caller}::strftime"} = \&POSIX::strftime; | |
58 | *{"${caller}::debug"} = sub { | |
59 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
60 | $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) | |
61 | }; | |
62 | *{"${caller}::db"} = sub { $_[0]->{'gruntmaster.dbic'} }; | |
63 | *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] }; | |
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 | }; | |
69 | ||
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}; | |
74 | } | |
75 | } | |
76 | ||
77 | sub import { | |
78 | return unless $_[0] eq __PACKAGE__; | |
79 | splice @_, 1, 0, scalar caller; | |
80 | goto &import_to | |
81 | } | |
82 | ||
83 | ################################################## | |
84 | ||
85 | sub generate{ | |
86 | my ($self, $lang, @args) = @_; | |
87 | ||
88 | my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML', use_perl => 1); | |
89 | $self->_generate($htc, $lang, @args); | |
90 | my $out = $htc->output; | |
91 | utf8::downgrade($out); | |
92 | my $vary = 'Accept-Language, ' . $self->vary; | |
93 | [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ] | |
94 | } | |
95 | ||
96 | sub _generate {} | |
97 | ||
98 | sub vary { '' } | |
99 | ||
100 | sub max_age { 60 } | |
101 | ||
102 | sub variants { | |
103 | return [] unless exists $templates{$_[0]}; | |
104 | [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ] | |
105 | } | |
106 | ||
107 | 1 |