]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Page/Base.pm
Add ranks to standings and reduce warnings
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
... / ...
CommitLineData
1package Gruntmaster::Page::Base;
2
3use 5.014000;
4use strict;
5use warnings;
6
7use File::Slurp qw/read_file/;
8use HTML::Template::Compiled;
9
10##################################################
11
12sub read_templates {
13 my $root = 'tmpl';
14 my $name = shift;
15
16 map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>;
17}
18
19my %header_templates = read_templates 'header';
20my %footer_templates = read_templates 'footer';
21
22sub header{
23 my ($language, $title) = @_;
24 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
25}
26
27sub footer{
28 $footer_templates{$_[0]};
29}
30
31##################################################
32
33use POSIX ();
34use Gruntmaster::Data ();
35use List::Util ();
36use LWP::UserAgent;
37
38my $ua = LWP::UserAgent->new;
39my %templates;
40
41sub 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
70sub 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
80sub _generate {}
81
82sub max_age { 60 }
83
84sub variants {
85 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
86}
87
881
This page took 0.017614 seconds and 4 git commands to generate.