]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Base.pm
Update version
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
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 Gruntmaster::Data ();
36 use List::Util ();
37 use LWP::UserAgent;
38 use Plack::Request ();
39 use feature ();
40
41 my $ua = LWP::UserAgent->new;
42 my %templates;
43
44 use Carp qw/cluck/;
45
46 sub import_to {
47 my ($self, $caller, $name, $title) = @_;
48
49 strict->import;
50 feature->import(':5.14');
51 warnings->import;
52 File::Slurp->export_to_level(1, $caller, qw/read_file/);
53 Gruntmaster::Data->export_to_level(1, $caller);
54 List::Util->export_to_level(1, $caller, qw/sum/);
55
56 no strict 'refs';
57 *{"${caller}::ISA"} = [__PACKAGE__];
58 *{"${caller}::VERSION"} = $VERSION;
59 *{"${caller}::strftime"} = \&POSIX::strftime;
60 *{"${caller}::debug"} = sub {
61 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
62 $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
63 };
64 *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] };
65 *{"${caller}::purge"} = sub {
66 return unless $ENV{PURGE_HOST};
67 my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
68 $ua->request($req)
69 };
70
71 if ($name) {
72 $templates{$caller} = { read_templates $name };
73 $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller};
74 $templates{$caller}{$_} .= footer $_ for keys $templates{$caller};
75 }
76 }
77
78 sub import {
79 return unless $_[0] eq __PACKAGE__;
80 splice @_, 1, 0, scalar caller;
81 goto &import_to
82 }
83
84 ##################################################
85
86 sub generate{
87 my ($self, $lang, @args) = @_;
88
89 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML', use_perl => 1);
90 $self->_generate($htc, $lang, @args);
91 my $out = $htc->output;
92 utf8::downgrade($out);
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 ] ]
95 }
96
97 sub _generate {}
98
99 sub vary { '' }
100
101 sub max_age { 60 }
102
103 sub variants {
104 return [] unless exists $templates{$_[0]};
105 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
106 }
107
108 1
This page took 0.071667 seconds and 5 git commands to generate.