]>
Commit | Line | Data |
---|---|---|
1 | package Gruntmaster::Page::Base; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | use File::Slurp qw/read_file/; | |
8 | use HTML::Template::Compiled; | |
9 | ||
10 | ################################################## | |
11 | ||
12 | use POSIX (); | |
13 | use Gruntmaster::Data (); | |
14 | use List::Util (); | |
15 | ||
16 | sub 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 | *{"${caller}::debug"} = sub { | |
28 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
29 | $_[0]->({qw/level debug message/ => $_[1]}) | |
30 | }; | |
31 | } | |
32 | ||
33 | ################################################## | |
34 | ||
35 | my %orig_header_templates = ( | |
36 | en => <<'HTML', | |
37 | <!DOCTYPE html> | |
38 | <title>TITLE_GOES_HERE</title> | |
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> | |
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 | ||
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> | |
53 | ||
54 | HTML | |
55 | ); | |
56 | ||
57 | my %orig_footer_templates = ( | |
58 | en => <<'HTML', | |
59 | ||
60 | <footer> | |
61 | Dilmom: Why don't you call your product the Gruntmaster 6000? | |
62 | Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? | |
63 | Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. | |
64 | </footer> | |
65 | HTML | |
66 | ); | |
67 | ||
68 | sub patch_templates { | |
69 | my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]}; | |
70 | my ($templates, $name) = @_; | |
71 | my %out = %$templates; | |
72 | for (<$root/$name.*>) { | |
73 | m/\.(.+)$/; | |
74 | $out{$1} = read_file $_ | |
75 | } | |
76 | ||
77 | %out | |
78 | } | |
79 | ||
80 | sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} } | |
81 | ||
82 | my %header_templates = patch_templates \%orig_header_templates, 'header'; | |
83 | my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; | |
84 | ||
85 | sub header{ | |
86 | my ($language, $title) = @_; | |
87 | %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates; | |
88 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; | |
89 | } | |
90 | ||
91 | sub footer{ | |
92 | %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates; | |
93 | $footer_templates{$_[0]}; | |
94 | } | |
95 | ||
96 | sub cook_templates { | |
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 | ||
106 | ################################################## | |
107 | ||
108 | my %templates; | |
109 | ||
110 | sub generate{ | |
111 | my ($self, $lang, @args) = @_; | |
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',); | |
116 | $self->_generate($htc, $lang, @args); | |
117 | [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1]], [ $htc->output ] ] | |
118 | } | |
119 | ||
120 | sub _generate {} | |
121 | ||
122 | sub variants { | |
123 | [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ] | |
124 | } | |
125 | ||
126 | 1 |