]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page/Base.pm
Do not escape problem statement
[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 };
27}
28
29##################################################
832cb45e 30
2b0036ac 31my %orig_header_templates = (
832cb45e
MG
32 en => <<'HTML',
33<!DOCTYPE html>
34<title>TITLE_GOES_HERE</title>
35<link rel="stylesheet" href="/gm.css">
879063d7
MG
36<script src="/zepto.var" defer></script>
37<script src="/view.js" defer></script>
38<script src="/form.js" defer></script>
832cb45e
MG
39<meta charset="utf-8">
40
41<span id="admin"></span>
42<div id="title"><span class="i">i</span><span class="Eval">Eval</span></div>
43<div id="subtitle">TITLE_GOES_HERE</div>
44
e5a94ee5 45<nav><ul><li><a href="/learn.var">Learn</a><li><a href="/pb/">Practice</a><li><a href="/ct/">Compete</a><li><a href="/log/">Job log</a></ul></nav>
832cb45e
MG
46
47HTML
48);
49
2b0036ac 50my %orig_footer_templates = (
832cb45e
MG
51 en => <<'HTML',
52
53<footer>
54Dilmom: Why don't you call your product the Gruntmaster 6000?
55Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
56Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
57</footer>
58HTML
59);
60
2b0036ac 61sub patch_templates {
6d78fc24
MG
62 my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return;
63 my ($templates, $name) = @_;
2b0036ac 64 my %out = %$templates;
6d78fc24
MG
65 for (<$root/$name*>) {
66 m/\.(.+)$/;
2b0036ac 67 $out{$1} = read_file $_
6d78fc24 68 }
2b0036ac
MG
69
70 %out
6d78fc24
MG
71}
72
bb95f538
MG
73sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} }
74
2b0036ac
MG
75my %header_templates = patch_templates \%orig_header_templates, 'header';
76my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
77
832cb45e
MG
78sub header{
79 my ($language, $title) = @_;
2b0036ac 80 %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates;
832cb45e
MG
81 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
82}
83
84sub footer{
2b0036ac 85 %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates;
832cb45e
MG
86 $footer_templates{$_[0]};
87}
88
bb95f538 89sub cook_templates {
2b0036ac
MG
90 my ($templates, $name, $title) = @_;
91
92 my %out = patch_templates $templates, $name;
93 $out{$_} = header ($_, $title) . $out{$_} for keys %out;
94 $out{$_} .= footer $_ for keys %out;
95
96 %out
97}
98
bb95f538
MG
99##################################################
100
101my %templates;
102
103sub generate{
104 my ($self, $path, $lang) = @_;
105
106 $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates;
107
108 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
109 $self->_generate($htc, $path, $lang);
110 $htc->output
111}
112
113sub _generate {}
114
1151
This page took 0.052904 seconds and 4 git commands to generate.