]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Page/Base.pm
Convert div id="subtitle" to a h1 id="title"
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
... / ...
CommitLineData
1package Gruntmaster::Page::Base;
2
3use 5.014000;
4use strict;
5use warnings;
6
7use Encode qw/encode/;
8use File::Slurp qw/read_file/;
9use HTML::Template::Compiled;
10
11##################################################
12
13use POSIX ();
14use Gruntmaster::Data ();
15use List::Util ();
16use LWP::UserAgent;
17
18my $ua = LWP::UserAgent->new;
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 };
31 *{"${caller}::debug"} = sub {
32 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
33 $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
34 };
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 };
41}
42
43##################################################
44
45my %orig_header_templates = (
46 en => <<'HTML',
47<!DOCTYPE html>
48<title>TITLE_GOES_HERE</title>
49<meta charset="utf-8">
50<meta name="viewport" content="width=device-width, initial-scale=1.0">
51
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>
61
62<div class="collapse navbar-collapse">
63<ul class="nav navbar-nav">
64<li><a href="/pb/">Problems</a>
65<li><a href="/ct/">Contests</a>
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
87<h1 id="title">TITLE_GOES_HERE</h1>
88<div id="result"></div>
89HTML
90);
91
92my %orig_footer_templates = (
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
103sub patch_templates {
104 my $root = 'tmpl';
105 return %{$_[0]} unless -d $root;
106 my ($templates, $name) = @_;
107 my %out = %$templates;
108 for (<$root/$name.*>) {
109 m/\.(.+)$/;
110 $out{$1} = read_file $_
111 }
112
113 %out
114}
115
116my %header_templates = patch_templates \%orig_header_templates, 'header';
117my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
118
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
128sub cook_templates {
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
138##################################################
139
140my %templates;
141
142sub generate{
143 my ($self, $lang, @args) = @_;
144
145 $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self};
146
147 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
148 $self->_generate($htc, $lang, @args);
149 [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language', 'X-Forever' => 1], [ encode 'UTF-8' => $htc->output ] ]
150}
151
152sub _generate {}
153
154sub variants {
155 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
156}
157
1581
This page took 0.020075 seconds and 4 git commands to generate.