]>
Commit | Line | Data |
---|---|---|
bb95f538 | 1 | package Gruntmaster::Page::Base; |
832cb45e MG |
2 | |
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6d78fc24 | 6 | |
a94f8453 | 7 | use Encode qw/encode/; |
6d78fc24 | 8 | use File::Slurp qw/read_file/; |
bb95f538 MG |
9 | use HTML::Template::Compiled; |
10 | ||
11 | ################################################## | |
12 | ||
13 | use POSIX (); | |
14 | use Gruntmaster::Data (); | |
15 | use List::Util (); | |
16 | ||
17 | sub 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 }; | |
7dc32473 MG |
28 | *{"${caller}::debug"} = sub { |
29 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
30 | $_[0]->({qw/level debug message/ => $_[1]}) | |
31 | }; | |
bb95f538 MG |
32 | } |
33 | ||
34 | ################################################## | |
832cb45e | 35 | |
2b0036ac | 36 | my %orig_header_templates = ( |
832cb45e MG |
37 | en => <<'HTML', |
38 | <!DOCTYPE html> | |
39 | <title>TITLE_GOES_HERE</title> | |
832cb45e | 40 | <meta charset="utf-8"> |
a94f8453 | 41 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
832cb45e | 42 | |
a94f8453 MG |
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> | |
832cb45e | 52 | |
a94f8453 MG |
53 | <div class="collapse navbar-collapse"> |
54 | <ul class="nav navbar-nav"> | |
55 | <li><a href="/pb/">Problem list</a> | |
7dc32473 | 56 | <li><a href="/ct/">Contests</a> |
a94f8453 MG |
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="title"><span class="i">i</span><span class="Eval">Eval</span></div> | |
79 | <div id="subtitle">TITLE_GOES_HERE</div> | |
832cb45e MG |
80 | |
81 | HTML | |
82 | ); | |
83 | ||
2b0036ac | 84 | my %orig_footer_templates = ( |
832cb45e MG |
85 | en => <<'HTML', |
86 | ||
87 | <footer> | |
88 | Dilmom: Why don't you call your product the Gruntmaster 6000? | |
89 | Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? | |
90 | Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. | |
91 | </footer> | |
92 | HTML | |
93 | ); | |
94 | ||
2b0036ac | 95 | sub patch_templates { |
eafc7f54 | 96 | my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]}; |
6d78fc24 | 97 | my ($templates, $name) = @_; |
2b0036ac | 98 | my %out = %$templates; |
d1a026f7 | 99 | for (<$root/$name.*>) { |
6d78fc24 | 100 | m/\.(.+)$/; |
2b0036ac | 101 | $out{$1} = read_file $_ |
6d78fc24 | 102 | } |
2b0036ac MG |
103 | |
104 | %out | |
6d78fc24 MG |
105 | } |
106 | ||
bb95f538 MG |
107 | sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} } |
108 | ||
2b0036ac MG |
109 | my %header_templates = patch_templates \%orig_header_templates, 'header'; |
110 | my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; | |
111 | ||
832cb45e MG |
112 | sub header{ |
113 | my ($language, $title) = @_; | |
2b0036ac | 114 | %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates; |
832cb45e MG |
115 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; |
116 | } | |
117 | ||
118 | sub footer{ | |
2b0036ac | 119 | %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates; |
832cb45e MG |
120 | $footer_templates{$_[0]}; |
121 | } | |
122 | ||
bb95f538 | 123 | sub cook_templates { |
2b0036ac MG |
124 | my ($templates, $name, $title) = @_; |
125 | ||
126 | my %out = patch_templates $templates, $name; | |
127 | $out{$_} = header ($_, $title) . $out{$_} for keys %out; | |
128 | $out{$_} .= footer $_ for keys %out; | |
129 | ||
130 | %out | |
131 | } | |
132 | ||
bb95f538 MG |
133 | ################################################## |
134 | ||
135 | my %templates; | |
136 | ||
137 | sub generate{ | |
7dc32473 | 138 | my ($self, $lang, @args) = @_; |
bb95f538 MG |
139 | |
140 | $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates; | |
141 | ||
142 | my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',); | |
7dc32473 | 143 | $self->_generate($htc, $lang, @args); |
a94f8453 | 144 | [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1]], [ encode 'UTF-8' => $htc->output ] ] |
bb95f538 MG |
145 | } |
146 | ||
147 | sub _generate {} | |
148 | ||
7dc32473 MG |
149 | sub variants { |
150 | [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ] | |
151 | } | |
152 | ||
bb95f538 | 153 | 1 |