]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Page/Base.pm
Fix template patch support
[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 ();
16
17sub import {
18 my $caller = caller;
19 my ($self, $name, $title) = @_;
20
21 Gruntmaster::Data->export_to_level(1, $caller);
22 List::Util->export_to_level(1, $caller, qw/sum/);
23
24 no strict 'refs';
25 *{"${caller}::strftime"} = \&POSIX::strftime;
26 *{"${caller}::NAME"} = sub () { $name };
27 *{"${caller}::TITLE"} = sub () { $title };
28 *{"${caller}::debug"} = sub {
29 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
30 $_[0]->({qw/level debug message/ => $_[1]})
31 };
32}
33
34##################################################
35
36my %orig_header_templates = (
37 en => <<'HTML',
38<!DOCTYPE html>
39<title>TITLE_GOES_HERE</title>
40<meta charset="utf-8">
41<meta name="viewport" content="width=device-width, initial-scale=1.0">
42
43<link rel="stylesheet" href="/css/cyborg" id="stylesheet">
44<script src="/js" type="text/javascript"></script>
45
46<nav class="navbar navbar-default navbar-static-top" role="navigation">
47<div class="container-fluid">
48<div class="navbar-header">
49<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>
50<a class="navbar-brand" href="/">Gruntmaster 6000</a>
51</div>
52
53<div class="collapse navbar-collapse">
54<ul class="nav navbar-nav">
55<li><a href="/pb/">Problems</a>
56<li><a href="/ct/">Contests</a>
57<li><a href="/account">Account</a>
58</ul>
59
60<ul class="nav navbar-nav navbar-right">
61<li><a class="dropdown-toggle" data-toggle="dropdown"> Theme <span class="caret"></span></a>
62
63<ul class="dropdown-menu" role="menu">
64<li><a href="#" id="theme_slate">Gunmetal gray</a>
65<li><a href="#" id="theme_cyborg">Black</a>
66<li><a href="#" id="theme_cerulean">White</a>
67<li><a href="#" id="theme_cosmo">Metro</a>
68</ul>
69
70<li><a href="/log/">Job log</a>
71</ul>
72</div>
73</div>
74</nav>
75
76<div class="container-fluid">
77
78<div id="subtitle">TITLE_GOES_HERE</div>
79
80HTML
81);
82
83my %orig_footer_templates = (
84 en => <<'HTML',
85
86<footer>
87Dilmom: Why don't you call your product the Gruntmaster 6000?
88Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
89Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
90</footer>
91HTML
92);
93
94sub patch_templates {
95 my $root = 'tmpl';
96 return %{$_[0]} unless -d $root;
97 my ($templates, $name) = @_;
98 my %out = %$templates;
99 for (<$root/$name.*>) {
100 m/\.(.+)$/;
101 $out{$1} = read_file $_
102 }
103
104 %out
105}
106
107my %header_templates = patch_templates \%orig_header_templates, 'header';
108my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
109
110sub header{
111 my ($language, $title) = @_;
112 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
113}
114
115sub footer{
116 $footer_templates{$_[0]};
117}
118
119sub cook_templates {
120 my ($templates, $name, $title) = @_;
121
122 my %out = patch_templates $templates, $name;
123 $out{$_} = header ($_, $title) . $out{$_} for keys %out;
124 $out{$_} .= footer $_ for keys %out;
125
126 %out
127}
128
129##################################################
130
131my %templates;
132
133sub generate{
134 my ($self, $lang, @args) = @_;
135
136 $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self};
137
138 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
139 $self->_generate($htc, $lang, @args);
140 [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language'], [ encode 'UTF-8' => $htc->output ] ]
141}
142
143sub _generate {}
144
145sub variants {
146 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
147}
148
1491
This page took 0.016289 seconds and 4 git commands to generate.