]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Page/Base.pm
Add title to job entries
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
... / ...
CommitLineData
1package Gruntmaster::Page::Base;
2
3use 5.014000;
4use strict;
5use warnings;
6our $VERSION = '5999.000_001';
7
8use File::Slurp qw/read_file/;
9use HTML::Template::Compiled;
10
11##################################################
12
13sub read_templates {
14 my $root = 'tmpl';
15 my $name = shift;
16
17 map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>;
18}
19
20my %header_templates = read_templates 'header';
21my %footer_templates = read_templates 'footer';
22
23sub header{
24 my ($language, $title) = @_;
25 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
26}
27
28sub footer{
29 $footer_templates{$_[0]};
30}
31
32##################################################
33
34use POSIX ();
35use List::Util ();
36use LWP::UserAgent;
37use Plack::Request ();
38use feature ();
39
40my $ua = LWP::UserAgent->new;
41my %templates;
42
43use Carp qw/cluck/;
44
45sub 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
77sub import {
78 return unless $_[0] eq __PACKAGE__;
79 splice @_, 1, 0, scalar caller;
80 goto &import_to
81}
82
83##################################################
84
85sub 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
96sub _generate {}
97
98sub vary { '' }
99
100sub max_age { 60 }
101
102sub variants {
103 return [] unless exists $templates{$_[0]};
104 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
105}
106
1071
This page took 0.020288 seconds and 4 git commands to generate.