Import some changes from the mindcoding branch
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
CommitLineData
bb95f538 1package Gruntmaster::Page::Base;
832cb45e
MG
2
3use 5.014000;
4use strict;
5use warnings;
e046c73a 6our $VERSION = '5999.000_001';
6d78fc24
MG
7
8use File::Slurp qw/read_file/;
bb95f538
MG
9use HTML::Template::Compiled;
10
11##################################################
12
9da2f495
MG
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
bb95f538 34use POSIX ();
bb95f538 35use List::Util ();
49c1467a 36use LWP::UserAgent;
5c6aea93
MG
37use Plack::Request ();
38use feature ();
49c1467a
MG
39
40my $ua = LWP::UserAgent->new;
9da2f495 41my %templates;
bb95f538 42
fdbf59e5
MG
43use Carp qw/cluck/;
44
45sub 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
77sub import {
78 return unless $_[0] eq __PACKAGE__;
79 splice @_, 1, 0, scalar caller;
80 goto &import_to
81}
82
bb95f538
MG
83##################################################
84
bb95f538 85sub 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;
a122bb9b 91 utf8::decode($out) for 1 .. 3;
a46fb222 92 utf8::encode($out);
c86e504e
MG
93 my $vary = 'Accept-Language, ' . $self->vary;
94 [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ]
bb95f538
MG
95}
96
97sub _generate {}
98
fdbf59e5 99sub vary { '' }
c86e504e 100
ff57a758
MG
101sub max_age { 60 }
102
7dc32473 103sub variants {
16aa291d 104 return [] unless exists $templates{$_[0]};
9da2f495 105 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
7dc32473
MG
106}
107
bb95f538 1081
This page took 0.026737 seconds and 4 git commands to generate.