]>
Commit | Line | Data |
---|---|---|
3b69df7a MG |
1 | package Plack::App::Gruntmaster::HTML; |
2 | use v5.14; | |
3 | use parent qw/Exporter/; | |
4 | our @EXPORT = qw/render render_article/; | |
5 | ||
6 | use File::Slurp qw/read_file/; | |
7 | use HTML::Seamstress; | |
8 | use POSIX qw//; | |
9 | use Data::Dumper qw/Dumper/; | |
10 | ||
11 | sub ftime ($) { POSIX::strftime '%c', localtime shift } | |
12 | sub literal ($) { HTML::Element::Library::super_literal shift // '' } | |
13 | ||
14 | sub HTML::Element::edit_href { | |
15 | my ($self, $sub) = @_; | |
16 | local $_ = $self->attr('href'); | |
17 | $sub->(); | |
18 | $self->attr(href => $_); | |
19 | } | |
20 | ||
21 | sub HTML::Element::iter3 { | |
22 | my ($self, $data, $code) = @_; | |
23 | my $orig = $self; | |
24 | my $prev = $orig; | |
25 | for my $el (@$data) { | |
26 | my $current = $orig->clone; | |
27 | $code->($el, $current); | |
28 | $prev->postinsert($current); | |
29 | $prev = $current; | |
30 | } | |
31 | $orig->detach; | |
32 | } | |
33 | ||
34 | sub HTML::Element::fid { shift->look_down(id => shift) } | |
28ec8060 | 35 | sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) } |
3b69df7a MG |
36 | |
37 | sub HTML::Element::namedlink { | |
38 | my ($self, $id, $name) = @_; | |
39 | $name = $id unless $name =~ /[[:graph:]]/; | |
40 | $self = $self->find('a'); | |
41 | $self->edit_href(sub {s/id/$id/}); | |
42 | $self->replace_content($name); | |
43 | } | |
44 | ||
4b3f09fe MG |
45 | my %page_cache; |
46 | for (<tmpl/*>) { | |
47 | my ($tmpl, $lang) = m,tmpl/(\w+)\.(\w+),; | |
48 | my $builder = HTML::Seamstress->new; | |
49 | $builder->ignore_unknown(0); | |
50 | $page_cache{$tmpl, $lang} = $builder->parse_file($_); | |
51 | } | |
52 | ||
3b69df7a MG |
53 | sub render { |
54 | my ($tmpl, $lang, %args) = @_; | |
55 | $lang //= 'en'; | |
56 | my $meat = _render($tmpl, $lang, %args); | |
57 | _render('skel', $lang, %args, meat => $meat) | |
58 | } | |
59 | ||
60 | sub render_article { | |
6533844f | 61 | my ($art, $lang, %args) = @_; |
3b69df7a MG |
62 | $lang //= 'en'; |
63 | my $title = read_file "a/$art.$lang.title"; | |
64 | my $meat = read_file "a/$art.$lang"; | |
6533844f | 65 | _render('skel', $lang, title => $title , meat => $meat, %args) |
3b69df7a MG |
66 | } |
67 | ||
68 | sub _render { | |
69 | my ($tmpl, $lang, %args) = @_; | |
4b3f09fe | 70 | my $tree = $page_cache{$tmpl, $lang}->clone or die "No such template/language combination: $tmpl/$lang\n"; |
3b69df7a MG |
71 | $tree = $tree->guts unless $tmpl eq 'skel'; |
72 | $tree->defmap(smap => \%args); | |
73 | my $process = __PACKAGE__->can("process_$tmpl"); | |
74 | $process->($tree, %args) if $process; | |
6533844f MG |
75 | $_->detach for $tree->look_down(static => $args{static} ? 'no' : 'yes'); |
76 | $_->attr('static', undef) for $tree->look_down(sub {$_[0]->attr('static')}); | |
f4154608 | 77 | $_->attr('smap', undef) for $tree->look_down(sub {$_[0]->attr('defmap')}); |
3b69df7a MG |
78 | $tree->as_HTML; |
79 | } | |
80 | ||
444212be MG |
81 | my $logo = read_file 'mclogo.svg'; |
82 | $logo =~ y/\n//d; | |
83 | $logo =~ s/(\s+)/ /g; | |
84 | ||
3b69df7a MG |
85 | sub process_skel { |
86 | my ($tree, %args) = @_; | |
444212be | 87 | $tree->fid('logo')->replace_with(literal $logo); |
3b69df7a MG |
88 | $tree->content_handler( |
89 | title => $args{title}, | |
90 | content => literal $args{meat}); | |
91 | } | |
92 | ||
93 | sub process_us_entry { | |
94 | my ($tree, %args) = @_; | |
95 | $tree->fid($_)->attr('href', "/$_/?owner=$args{id}") for qw/log pb/; | |
f1c090e7 | 96 | $tree->fid('track_user')->attr('data-user', $args{id}); |
9a8a3012 MG |
97 | my @solved = map { $_->{solved} ? ($_->{problem}) : () } @{$args{problems}}; |
98 | my @attempted = map { !$_->{solved} ? ($_->{problem}) : () } @{$args{problems}}; | |
99 | ||
100 | my $pbiter = sub { | |
101 | my ($data, $li) = @_; | |
102 | $li->find('a')->namedlink($data); | |
103 | }; | |
104 | $tree->fid('solved')->find('li')->iter3(\@solved, $pbiter); | |
105 | $tree->fid('attempted')->find('li')->iter3(\@attempted, $pbiter); | |
106 | $tree->fid('solved_count')->replace_content(scalar @solved); | |
107 | $tree->fid('attempted_count')->replace_content(scalar @attempted); | |
108 | ||
109 | my $ctiter = sub { | |
110 | my ($data, $td) = @_; | |
dc6ca3bc | 111 | $td->fclass('contest')->namedlink($data->{contest}, $data->{contest_name}); |
9a8a3012 MG |
112 | $td->fclass('score')->replace_content($data->{score}); |
113 | $td->fclass('rank')->replace_content($data->{rank}); | |
114 | }; | |
115 | $tree->find('table')->find('tbody')->find('tr')->iter3($args{contests}, $ctiter); | |
3b69df7a MG |
116 | } |
117 | ||
118 | sub process_us { | |
119 | my ($tree, %args) = @_; | |
c5ff0b09 MG |
120 | my $iter = sub { |
121 | my ($data, $tr) = @_; | |
122 | $tr->fclass('user')->namedlink($data->{id}, $data->{name}); | |
123 | $tr->fclass($_)->replace_content($data->{$_}) for qw/solved attempted contests/; | |
124 | }; | |
125 | $tree->find('tbody')->find('tr')->iter3($args{us}, $iter); | |
3b69df7a MG |
126 | } |
127 | ||
128 | sub process_ct_entry { | |
129 | my ($tree, %args) = @_; | |
130 | $_->edit_href (sub {s/contest_id/$args{id}/}) for $tree->find('a'); | |
f34b3eac | 131 | $tree->fid('editorial')->detach unless $args{finished}; |
3b69df7a | 132 | $tree->fid('links')->detach unless $args{started}; |
20777d84 MG |
133 | my $status = ($args{time} < $args{start} ? 'starts' : 'ends'); |
134 | $tree->fclass('timer')->attr('data-stop', $status eq 'ends' ? $args{stop} : $args{start}); | |
3b69df7a MG |
135 | $tree->content_handler( |
136 | start => ftime $args{start}, | |
137 | stop => ftime $args{stop}, | |
20777d84 | 138 | status => $status, |
3b69df7a | 139 | description => literal $args{description}); |
00398f5c | 140 | $tree->fid('ctcountdown')->detach if $args{time} >= $args{stop}; |
3b69df7a MG |
141 | } |
142 | ||
143 | sub process_ct { | |
144 | my ($tree, %args) = @_; | |
145 | my $iter = sub { | |
146 | my ($data, $tr) = @_; | |
147 | $data->{$_} = ftime $data->{$_} for qw/start stop/; | |
148 | $tr->hashmap(class => $data, [qw/name owner/]); | |
149 | $tr->fclass('name')->namedlink($data->{id}, $data->{name}); | |
150 | $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name}); | |
151 | }; | |
152 | $args{$_} ? $tree->fid($_)->find('tbody')->find('tr')->iter3($args{$_}, $iter) : $tree->fid($_)->detach for qw/running pending finished/; | |
153 | } | |
154 | ||
155 | sub process_pb_entry { | |
156 | my ($tree, %args) = @_; | |
157 | $tree->fid('owner')->edit_href(sub{s/owner_id/$args{owner}/}); | |
158 | $tree->fid('job_log')->edit_href(sub{s/problem_id/$args{id}/}); | |
e4d5bdf5 | 159 | $tree->fid('solution')->edit_href(sub{s/problem_id/$args{id}/}); |
3b69df7a MG |
160 | $tree->content_handler( |
161 | statement => literal $args{statement}, | |
6eb88ef9 | 162 | level => ucfirst $args{level}, |
3b69df7a MG |
163 | author => $args{author}, |
164 | owner => $args{owner_name} || $args{owner}); | |
14582b6f MG |
165 | if ($args{limits}) { |
166 | my @limits = (@{$args{limits}}, {format => 'Other', timeout => $args{timeout} }); | |
167 | @limits = map { sprintf '%s (%s)', @{$_}{qw/timeout format/} } @limits; | |
168 | $tree->look_down(smap => 'timeout')->replace_content(join ', ', @limits); | |
169 | } | |
9a4806b3 | 170 | if ($args{contest_stop}) { |
e4d5bdf5 MG |
171 | $tree->fid('solution')->detach; |
172 | $tree->fid('solution_modal')->detach; | |
b3da3730 | 173 | $tree->fid('score')->replace_content($args{value}); |
20777d84 | 174 | $tree->fid('countdown')->attr('data-stop' => $args{contest_stop}); |
9a4806b3 | 175 | } else { |
bca1707c | 176 | $tree->fid('job_log')->edit_href(sub{$_ .= "&private=$args{private}"}) if $args{private}; |
c016b644 | 177 | $tree->fid('solution')->detach unless $args{solution}; |
9a4806b3 | 178 | $_->detach for $tree->fclass('rc'); # requires contest |
e4d5bdf5 | 179 | $tree->fid('solution_modal')->fclass('modal-body')->replace_content(literal $args{solution}); |
9a4806b3 | 180 | } |
3b69df7a | 181 | if ($args{cansubmit}) { |
a221db10 | 182 | $tree->fid('nosubmit')->detach; |
3b69df7a MG |
183 | $tree->look_down(name => 'problem')->attr(value => $args{id}); |
184 | my $contest = $tree->look_down(name => 'contest'); | |
7e158d3c MG |
185 | $contest->attr(value => $args{args}{contest}) if $args{args}{contest}; |
186 | $contest->detach unless $args{args}{contest} | |
3b69df7a | 187 | } else { |
a221db10 | 188 | $tree->fid('nosubmit')->find('a')->edit_href(sub{s/id/$args{id}/}); |
3b69df7a MG |
189 | $tree->fid('submit')->detach |
190 | } | |
191 | } | |
192 | ||
e4d5bdf5 MG |
193 | sub process_sol { |
194 | my ($tree, %args) = @_; | |
195 | $tree->content_handler(solution => literal $args{solution}); | |
196 | } | |
197 | ||
3b69df7a MG |
198 | sub process_pb { |
199 | my ($tree, %args) = @_; | |
200 | my $titer = sub { | |
201 | my ($data, $tr) = @_; | |
202 | $tr->set_child_content(class => 'author', $data->{author}); | |
203 | $tr->fclass('name')->namedlink($data->{id}, $data->{name}); | |
204 | $tr->fclass('name')->find('a')->edit_href(sub {$_ .= "?contest=$args{contest}"}) if $args{contest}; | |
205 | $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name}); | |
3994b2a7 | 206 | $tr->find('td')->attr(class => $tr->find('td')->attr('class').' warning') if $data->{private} && !$args{contest}; |
3b69df7a MG |
207 | }; |
208 | my $iter = sub { | |
209 | my ($data, $div) = @_; | |
210 | $div->attr(id => $data); | |
211 | $div->find('h2')->replace_content(ucfirst $data); | |
caafa703 | 212 | $div->find('tbody')->find('tr')->iter3($args{$data}, $titer); |
3b69df7a MG |
213 | }; |
214 | $tree->fid('beginner')->iter3([grep {$args{$_}} qw/beginner easy medium hard/], $iter); | |
89d8eeb7 | 215 | $tree->fid('open-alert')->detach unless $args{contest}; |
3b69df7a MG |
216 | } |
217 | ||
218 | sub process_log_entry { | |
219 | my ($tree, %args) = @_; | |
435a869c MG |
220 | $tree->fid('problem')->namedlink(@args{qw/problem problem_name/}); |
221 | $tree->fid('owner')->namedlink(@args{qw/owner owner_name/}); | |
222 | $tree->fid('source')->namedlink("$args{id}.$args{extension}", sprintf '%.2fKB', $args{size}/1024); | |
223 | if ($args{contest}) { | |
224 | $tree->fid('contest')->namedlink(@args{qw/contest contest_name/}); | |
225 | $tree->fid('problem')->find('a')->edit_href(sub {$_.="?contest=$args{contest}"}); | |
226 | } else { | |
227 | $tree->fid('contest')->left->detach; | |
228 | $tree->fid('contest')->detach; | |
229 | } | |
230 | ||
3b69df7a MG |
231 | $args{errors} ? $tree->fid('errors')->find('pre')->replace_content($args{errors}) : $tree->fid('errors')->detach; |
232 | my $iter = sub { | |
233 | my ($data, $tr) = @_; | |
234 | $data->{time} = sprintf '%.4fs', $data->{time}; | |
235 | $tr->defmap(class => $data); | |
236 | $tr->fclass('result_text')->attr(class => "r$data->{result}") | |
237 | }; | |
e1fab7d7 MG |
238 | $tree->fclass('result_text')->replace_content($args{result_text}); |
239 | $tree->fclass('result_text')->attr(class => "r$args{result}"); | |
435a869c | 240 | $args{results} ? $tree->fid('results')->find('tbody')->find('tr')->iter3($args{results}, $iter) : $tree->fid('results')->detach; |
0f67fb90 | 241 | $tree->fid('no_results')->detach if $tree->fid('results') || $tree->fid('errors'); |
3b69df7a MG |
242 | } |
243 | ||
244 | sub process_log { | |
245 | my ($tree, %args) = @_; | |
246 | my $iter = sub { | |
247 | my ($data, $tr) = @_; | |
248 | $tr->fclass('id')->namedlink($data->{id}); | |
249 | $tr->fclass('problem')->namedlink($data->{problem}, $data->{problem_name}); | |
7e158d3c | 250 | $tr->fclass('problem')->find('a')->edit_href(sub{$_ .= "?contest=$args{args}{contest}"}) if $args{args}{contest}; |
8d690af5 MG |
251 | $tr->fclass('contest')->namedlink($data->{contest}, $data->{contest_name}) if $data->{contest}; |
252 | $tr->fclass('contest')->replace_content('None') unless $data->{contest}; | |
3b69df7a | 253 | $tr->fclass('date')->replace_content(ftime $data->{date}); |
93ed6f84 | 254 | $tr->fclass('source')->namedlink("$data->{id}.$data->{extension}", sprintf "%.2fKB %s", $data->{size}/1024, Plack::App::Gruntmaster::FORMAT_EXTENSION()->{$data->{format}}); |
3b69df7a MG |
255 | $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name}); |
256 | $tr->fclass('result_text')->replace_content($data->{result_text}); | |
257 | $tr->fclass('result_text')->attr(class => "r$data->{result}"); | |
a33e01dd | 258 | $tr->find('td')->attr(class => $tr->find('td')->attr('class').' warning') if $data->{private}; |
3b69df7a MG |
259 | }; |
260 | $tree->find('table')->find('tbody')->find('tr')->iter3($args{log}, $iter); | |
fddf958b MG |
261 | $args{next_page} ? $tree->fclass('next')->namedlink($args{next_page}, 'Next') : $tree->fclass('next')->detach; |
262 | $args{previous_page} ? $tree->fclass('previous')->namedlink($args{previous_page}, 'Previous') : $tree->fclass('previous')->detach; | |
4a3952b2 MG |
263 | for my $cls (qw/next previous/) { |
264 | my $elem = $tree->fclass($cls); | |
265 | next unless $elem; | |
266 | delete $args{args}{page}; | |
418101af | 267 | my $str = join '&', map { $_ . '=' . $args{args}{$_} } keys %{$args{args}}; |
8678e0dd | 268 | $elem->find('a')->edit_href(sub{s/$/&$str/}) if $str; |
4a3952b2 | 269 | } |
3b69df7a MG |
270 | $tree->fclass('current')->replace_content("Page $args{current_page} of $args{last_page}"); |
271 | } | |
272 | ||
273 | sub process_st { | |
274 | my ($tree, %args) = @_; | |
275 | $args{problems} //= []; | |
276 | my $pbiter = sub { | |
277 | my ($data, $th) = @_; | |
278 | $th->attr(class => undef); | |
ebca729d | 279 | $th->namedlink(@$data); |
b7aa9f38 | 280 | $th->find('a')->edit_href(sub{s/$/?contest=$args{args}{contest}/}); |
3b69df7a MG |
281 | }; |
282 | $tree->fclass('problem')->iter3($args{problems}, $pbiter); | |
283 | my $iter = sub { | |
284 | my ($st, $tr) = @_; | |
285 | $tr->set_child_content(class => 'rank', $st->{rank}); | |
286 | $tr->set_child_content(class => 'score', $st->{score}); | |
dc7e3b7c | 287 | $tr->fclass('user')->namedlink($st->{user}, $st->{user_name}); |
3b69df7a | 288 | my $pbscore = $tr->fclass('pbscore'); |
3b69df7a MG |
289 | $pbscore->iter($pbscore => @{$st->{scores}}); |
290 | }; | |
291 | $tree->find('tbody')->find('tr')->iter3($args{st}, $iter); | |
292 | } | |
645cfb7d MG |
293 | |
294 | sub process_ed { | |
295 | my ($tree, %args) = @_; | |
00c887ce | 296 | $tree->content_handler(editorial => literal $args{editorial}); |
645cfb7d MG |
297 | my $iter = sub { |
298 | my ($data, $div) = @_; | |
299 | $div->set_child_content(class => 'value', $data->{value}); | |
300 | $div->set_child_content(class => 'solution', literal $data->{solution}); | |
301 | $div->fclass('problem')->namedlink($data->{id}, $data->{name}); | |
302 | }; | |
c7f959c0 | 303 | my @pb = map { @{$args{$_} // []} } qw/beginner easy medium hard/; |
645cfb7d MG |
304 | $tree->fclass('well')->iter3(\@pb, $iter); |
305 | } | |
4b3f09fe MG |
306 | |
307 | 1; | |
308 | __END__ |