]>
Commit | Line | Data |
---|---|---|
bb95f538 | 1 | package Gruntmaster::Page::Base; |
832cb45e MG |
2 | |
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6d78fc24 MG |
6 | |
7 | use File::Slurp qw/read_file/; | |
bb95f538 MG |
8 | use HTML::Template::Compiled; |
9 | ||
10 | ################################################## | |
11 | ||
12 | use POSIX (); | |
13 | use Gruntmaster::Data (); | |
14 | use List::Util (); | |
49c1467a MG |
15 | use LWP::UserAgent; |
16 | ||
17 | my $ua = LWP::UserAgent->new; | |
bb95f538 MG |
18 | |
19 | sub import { | |
20 | my $caller = caller; | |
21 | my ($self, $name, $title) = @_; | |
22 | ||
23 | Gruntmaster::Data->export_to_level(1, $caller); | |
24 | List::Util->export_to_level(1, $caller, qw/sum/); | |
25 | ||
26 | no strict 'refs'; | |
27 | *{"${caller}::strftime"} = \&POSIX::strftime; | |
28 | *{"${caller}::NAME"} = sub () { $name }; | |
29 | *{"${caller}::TITLE"} = sub () { $title }; | |
7dc32473 MG |
30 | *{"${caller}::debug"} = sub { |
31 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
191f4979 | 32 | $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) |
7dc32473 | 33 | }; |
49c1467a MG |
34 | *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain'], [ @_ ] ] }; |
35 | *{"${caller}::purge"} = sub { | |
36 | return unless $ENV{PURGE_HOST}; | |
37 | my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]"); | |
38 | $ua->request($req) | |
39 | }; | |
bb95f538 MG |
40 | } |
41 | ||
42 | ################################################## | |
832cb45e | 43 | |
2b0036ac | 44 | my %orig_header_templates = ( |
832cb45e MG |
45 | en => <<'HTML', |
46 | <!DOCTYPE html> | |
47 | <title>TITLE_GOES_HERE</title> | |
832cb45e | 48 | <meta charset="utf-8"> |
a94f8453 | 49 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
832cb45e | 50 | |
fc8783c6 | 51 | <link rel="shortcut icon" href="/static/favicon.png"> |
1fb046b0 | 52 | <link rel="stylesheet" href="/css/cosmo" id="stylesheet"> |
a94f8453 MG |
53 | <script src="/js" type="text/javascript"></script> |
54 | ||
55 | <nav class="navbar navbar-default navbar-static-top" role="navigation"> | |
56 | <div class="container-fluid"> | |
57 | <div class="navbar-header"> | |
58 | <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> | |
fc8783c6 | 59 | <a class="navbar-brand" href="/"><div class="logo logo-sigla"></div></a> |
a94f8453 | 60 | </div> |
832cb45e | 61 | |
a94f8453 MG |
62 | <div class="collapse navbar-collapse"> |
63 | <ul class="nav navbar-nav"> | |
1c13bea0 | 64 | <li><a href="/pb/">Problems</a> |
7dc32473 | 65 | <li><a href="/ct/">Contests</a> |
a94f8453 MG |
66 | <li><a href="/account">Account</a> |
67 | </ul> | |
68 | ||
69 | <ul class="nav navbar-nav navbar-right"> | |
70 | <li><a class="dropdown-toggle" data-toggle="dropdown"> Theme <span class="caret"></span></a> | |
71 | ||
72 | <ul class="dropdown-menu" role="menu"> | |
73 | <li><a href="#" id="theme_slate">Gunmetal gray</a> | |
74 | <li><a href="#" id="theme_cyborg">Black</a> | |
75 | <li><a href="#" id="theme_cerulean">White</a> | |
76 | <li><a href="#" id="theme_cosmo">Metro</a> | |
77 | </ul> | |
78 | ||
79 | <li><a href="/log/">Job log</a> | |
80 | </ul> | |
81 | </div> | |
82 | </div> | |
83 | </nav> | |
84 | ||
85 | <div class="container-fluid"> | |
86 | ||
1fb046b0 MG |
87 | <div class="logo logo-mindcoding center-block"></div> |
88 | ||
fc8783c6 | 89 | <h1 id="title">TITLE_GOES_HERE | MindCoding</h1> |
52f5c679 | 90 | <div id="result"></div> |
514a5a0e MG |
91 | |
92 | <div id="content"> | |
832cb45e MG |
93 | HTML |
94 | ); | |
95 | ||
2b0036ac | 96 | my %orig_footer_templates = ( |
832cb45e MG |
97 | en => <<'HTML', |
98 | ||
514a5a0e | 99 | </div> |
514a5a0e | 100 | |
1fb046b0 MG |
101 | <div id="sponsors"> |
102 | <h3>Official sponsors</h3> | |
103 | <div class="logo logo-yardi"></div> | |
104 | </div> | |
514a5a0e | 105 | |
832cb45e | 106 | <footer> |
1fb046b0 MG |
107 | <div class="row"> |
108 | <div class="col-md-6 column text-center"> | |
109 | <a class="logo logo-hermes" href="http://www.societatea-hermes.ro"></a> | |
110 | </div> | |
111 | ||
112 | <div class="col-md-6 column text-center"> | |
113 | <a class="logo logo-facebook" href="http://www.facebook.com/mindcodingcluj"></a> | |
114 | </div> | |
115 | </div> | |
116 | ||
117 | <h3> Partners </h3> | |
118 | <a href="http://cs.ubbcluj.ro" class="logo logo-csf"></a> | |
119 | <a href="http://www.uniunea.ro" class="logo logo-usr"></a> | |
120 | <a href="http://www.coscj.ro" class="logo logo-cos"></a> | |
121 | <a href="http://www.cluj2015.ro/romana" class="logo logo-eyc"></a> | |
122 | <h3> Media Partners </h3> | |
123 | <a href="http://www.clujlife.com" class="logo logo-clujlife"></a> | |
124 | <a href="http://www.vinsieu.ro/eveniment/cluj/cluj-napoca/alte-evenimente/mindcoding/88970/e.html" class="logo logo-vinsieu"></a> | |
125 | <a href="http://www.welcome2cluj.com" class="logo logo-welcome2cluj"></a> | |
832cb45e | 126 | </footer> |
1fb046b0 | 127 | </div> |
832cb45e MG |
128 | HTML |
129 | ); | |
130 | ||
2b0036ac | 131 | sub patch_templates { |
db3d5b78 MG |
132 | my $root = 'tmpl'; |
133 | return %{$_[0]} unless -d $root; | |
6d78fc24 | 134 | my ($templates, $name) = @_; |
2b0036ac | 135 | my %out = %$templates; |
d1a026f7 | 136 | for (<$root/$name.*>) { |
6d78fc24 | 137 | m/\.(.+)$/; |
2b0036ac | 138 | $out{$1} = read_file $_ |
6d78fc24 | 139 | } |
2b0036ac MG |
140 | |
141 | %out | |
6d78fc24 MG |
142 | } |
143 | ||
2b0036ac MG |
144 | my %header_templates = patch_templates \%orig_header_templates, 'header'; |
145 | my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; | |
146 | ||
832cb45e MG |
147 | sub header{ |
148 | my ($language, $title) = @_; | |
149 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; | |
150 | } | |
151 | ||
152 | sub footer{ | |
153 | $footer_templates{$_[0]}; | |
154 | } | |
155 | ||
bb95f538 | 156 | sub cook_templates { |
2b0036ac MG |
157 | my ($templates, $name, $title) = @_; |
158 | ||
159 | my %out = patch_templates $templates, $name; | |
160 | $out{$_} = header ($_, $title) . $out{$_} for keys %out; | |
161 | $out{$_} .= footer $_ for keys %out; | |
162 | ||
163 | %out | |
164 | } | |
165 | ||
bb95f538 MG |
166 | ################################################## |
167 | ||
168 | my %templates; | |
169 | ||
170 | sub generate{ | |
7dc32473 | 171 | my ($self, $lang, @args) = @_; |
bb95f538 | 172 | |
db3d5b78 | 173 | $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self}; |
bb95f538 MG |
174 | |
175 | my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',); | |
7dc32473 | 176 | $self->_generate($htc, $lang, @args); |
1053baee MG |
177 | my $out = $htc->output; |
178 | utf8::downgrade($out); | |
179 | [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language', 'X-Forever' => 1], [ $out ] ] | |
bb95f538 MG |
180 | } |
181 | ||
182 | sub _generate {} | |
183 | ||
7dc32473 MG |
184 | sub variants { |
185 | [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ] | |
186 | } | |
187 | ||
bb95f538 | 188 | 1 |