]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page/Base.pm
Heeere's Plack/PSGI!
[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
MG
6
7use File::Slurp qw/read_file/;
bb95f538
MG
8use HTML::Template::Compiled;
9
10##################################################
11
12use POSIX ();
13use Gruntmaster::Data ();
14use List::Util ();
15
16sub import {
17 my $caller = caller;
18 my ($self, $name, $title) = @_;
19
20 Gruntmaster::Data->export_to_level(1, $caller);
21 List::Util->export_to_level(1, $caller, qw/sum/);
22
23 no strict 'refs';
24 *{"${caller}::strftime"} = \&POSIX::strftime;
25 *{"${caller}::NAME"} = sub () { $name };
26 *{"${caller}::TITLE"} = sub () { $title };
7dc32473
MG
27 *{"${caller}::debug"} = sub {
28 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
29 $_[0]->({qw/level debug message/ => $_[1]})
30 };
bb95f538
MG
31}
32
33##################################################
832cb45e 34
2b0036ac 35my %orig_header_templates = (
832cb45e
MG
36 en => <<'HTML',
37<!DOCTYPE html>
38<title>TITLE_GOES_HERE</title>
7dc32473
MG
39<link rel="stylesheet" href="/static/gm.css">
40<script src="/static/zepto.var" defer></script>
41<script src="/static/view.js" defer></script>
42<script src="/static/form.js" defer></script>
832cb45e
MG
43<meta charset="utf-8">
44
45<span id="admin"></span>
46<div id="title"><span class="i">i</span><span class="Eval">Eval</span></div>
47<div id="subtitle">TITLE_GOES_HERE</div>
48
7dc32473
MG
49<nav><ul>
50<li><a href="/pb/">Problems</a>
51<li><a href="/ct/">Contests</a>
52<li><a href="/log/">Job log</a></ul></nav>
832cb45e
MG
53
54HTML
55);
56
2b0036ac 57my %orig_footer_templates = (
832cb45e
MG
58 en => <<'HTML',
59
60<footer>
61Dilmom: Why don't you call your product the Gruntmaster 6000?
62Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
63Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
64</footer>
65HTML
66);
67
2b0036ac 68sub patch_templates {
eafc7f54 69 my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]};
6d78fc24 70 my ($templates, $name) = @_;
2b0036ac 71 my %out = %$templates;
d1a026f7 72 for (<$root/$name.*>) {
6d78fc24 73 m/\.(.+)$/;
2b0036ac 74 $out{$1} = read_file $_
6d78fc24 75 }
2b0036ac
MG
76
77 %out
6d78fc24
MG
78}
79
bb95f538
MG
80sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} }
81
2b0036ac
MG
82my %header_templates = patch_templates \%orig_header_templates, 'header';
83my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
84
832cb45e
MG
85sub header{
86 my ($language, $title) = @_;
2b0036ac 87 %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates;
832cb45e
MG
88 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
89}
90
91sub footer{
2b0036ac 92 %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates;
832cb45e
MG
93 $footer_templates{$_[0]};
94}
95
bb95f538 96sub cook_templates {
2b0036ac
MG
97 my ($templates, $name, $title) = @_;
98
99 my %out = patch_templates $templates, $name;
100 $out{$_} = header ($_, $title) . $out{$_} for keys %out;
101 $out{$_} .= footer $_ for keys %out;
102
103 %out
104}
105
bb95f538
MG
106##################################################
107
108my %templates;
109
110sub generate{
7dc32473 111 my ($self, $lang, @args) = @_;
bb95f538
MG
112
113 $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates;
114
115 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
7dc32473
MG
116 $self->_generate($htc, $lang, @args);
117 [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1]], [ $htc->output ] ]
bb95f538
MG
118}
119
120sub _generate {}
121
7dc32473
MG
122sub variants {
123 [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
124}
125
bb95f538 1261
This page took 0.043612 seconds and 4 git commands to generate.