]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page/Base.pm
Convert div id="subtitle" to a h1 id="title"
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
CommitLineData
bb95f538 1package Gruntmaster::Page::Base;
832cb45e
MG
2
3use 5.014000;
4use strict;
5use warnings;
6d78fc24 6
a94f8453 7use Encode qw/encode/;
6d78fc24 8use File::Slurp qw/read_file/;
bb95f538
MG
9use HTML::Template::Compiled;
10
11##################################################
12
13use POSIX ();
14use Gruntmaster::Data ();
15use List::Util ();
49c1467a
MG
16use LWP::UserAgent;
17
18my $ua = LWP::UserAgent->new;
bb95f538
MG
19
20sub import {
21 my $caller = caller;
22 my ($self, $name, $title) = @_;
23
24 Gruntmaster::Data->export_to_level(1, $caller);
25 List::Util->export_to_level(1, $caller, qw/sum/);
26
27 no strict 'refs';
28 *{"${caller}::strftime"} = \&POSIX::strftime;
29 *{"${caller}::NAME"} = sub () { $name };
30 *{"${caller}::TITLE"} = sub () { $title };
7dc32473
MG
31 *{"${caller}::debug"} = sub {
32 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
191f4979 33 $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
7dc32473 34 };
49c1467a
MG
35 *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain'], [ @_ ] ] };
36 *{"${caller}::purge"} = sub {
37 return unless $ENV{PURGE_HOST};
38 my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
39 $ua->request($req)
40 };
bb95f538
MG
41}
42
43##################################################
832cb45e 44
2b0036ac 45my %orig_header_templates = (
832cb45e
MG
46 en => <<'HTML',
47<!DOCTYPE html>
48<title>TITLE_GOES_HERE</title>
832cb45e 49<meta charset="utf-8">
a94f8453 50<meta name="viewport" content="width=device-width, initial-scale=1.0">
832cb45e 51
a94f8453
MG
52<link rel="stylesheet" href="/css/cyborg" id="stylesheet">
53<script src="/js" type="text/javascript"></script>
54
55<nav class="navbar navbar-default navbar-static-top" role="navigation">
56<div class="container-fluid">
57<div class="navbar-header">
58<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
59<a class="navbar-brand" href="/">Gruntmaster 6000</a>
60</div>
832cb45e 61
a94f8453
MG
62<div class="collapse navbar-collapse">
63<ul class="nav navbar-nav">
1c13bea0 64<li><a href="/pb/">Problems</a>
7dc32473 65<li><a href="/ct/">Contests</a>
a94f8453
MG
66<li><a href="/account">Account</a>
67</ul>
68
69<ul class="nav navbar-nav navbar-right">
70<li><a class="dropdown-toggle" data-toggle="dropdown"> Theme <span class="caret"></span></a>
71
72<ul class="dropdown-menu" role="menu">
73<li><a href="#" id="theme_slate">Gunmetal gray</a>
74<li><a href="#" id="theme_cyborg">Black</a>
75<li><a href="#" id="theme_cerulean">White</a>
76<li><a href="#" id="theme_cosmo">Metro</a>
77</ul>
78
79<li><a href="/log/">Job log</a>
80</ul>
81</div>
82</div>
83</nav>
84
85<div class="container-fluid">
86
05ebc75e 87<h1 id="title">TITLE_GOES_HERE</h1>
52f5c679 88<div id="result"></div>
832cb45e
MG
89HTML
90);
91
2b0036ac 92my %orig_footer_templates = (
832cb45e
MG
93 en => <<'HTML',
94
95<footer>
96Dilmom: Why don't you call your product the Gruntmaster 6000?
97Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
98Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
99</footer>
100HTML
101);
102
2b0036ac 103sub patch_templates {
db3d5b78
MG
104 my $root = 'tmpl';
105 return %{$_[0]} unless -d $root;
6d78fc24 106 my ($templates, $name) = @_;
2b0036ac 107 my %out = %$templates;
d1a026f7 108 for (<$root/$name.*>) {
6d78fc24 109 m/\.(.+)$/;
2b0036ac 110 $out{$1} = read_file $_
6d78fc24 111 }
2b0036ac
MG
112
113 %out
6d78fc24
MG
114}
115
2b0036ac
MG
116my %header_templates = patch_templates \%orig_header_templates, 'header';
117my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
118
832cb45e
MG
119sub header{
120 my ($language, $title) = @_;
121 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
122}
123
124sub footer{
125 $footer_templates{$_[0]};
126}
127
bb95f538 128sub cook_templates {
2b0036ac
MG
129 my ($templates, $name, $title) = @_;
130
131 my %out = patch_templates $templates, $name;
132 $out{$_} = header ($_, $title) . $out{$_} for keys %out;
133 $out{$_} .= footer $_ for keys %out;
134
135 %out
136}
137
bb95f538
MG
138##################################################
139
140my %templates;
141
142sub generate{
7dc32473 143 my ($self, $lang, @args) = @_;
bb95f538 144
db3d5b78 145 $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self};
bb95f538
MG
146
147 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
7dc32473 148 $self->_generate($htc, $lang, @args);
49c1467a 149 [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language', 'X-Forever' => 1], [ encode 'UTF-8' => $htc->output ] ]
bb95f538
MG
150}
151
152sub _generate {}
153
7dc32473
MG
154sub variants {
155 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
156}
157
bb95f538 1581
This page took 0.040357 seconds and 4 git commands to generate.