]>
Commit | Line | Data |
---|---|---|
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) } | |
35 | sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) } | |
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 | ||
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 | ||
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 { | |
61 | my ($art, $lang, %args) = @_; | |
62 | $lang //= 'en'; | |
63 | my $title = read_file "a/$art.$lang.title"; | |
64 | my $meat = read_file "a/$art.$lang"; | |
65 | _render('skel', $lang, title => $title , meat => $meat, %args) | |
66 | } | |
67 | ||
68 | sub _render { | |
69 | my ($tmpl, $lang, %args) = @_; | |
70 | my $tree = $page_cache{$tmpl, $lang}->clone or die "No such template/language combination: $tmpl/$lang\n"; | |
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; | |
75 | $_->detach for $tree->look_down(static => $args{static} ? 'no' : 'yes'); | |
76 | $_->attr('static', undef) for $tree->look_down(sub {$_[0]->attr('static')}); | |
77 | $_->attr('smap', undef) for $tree->look_down(sub {$_[0]->attr('defmap')}); | |
78 | $tree->as_HTML; | |
79 | } | |
80 | ||
81 | my $logo = read_file 'mclogo.svg'; | |
82 | $logo =~ y/\n//d; | |
83 | $logo =~ s/(\s+)/ /g; | |
84 | ||
85 | sub process_skel { | |
86 | my ($tree, %args) = @_; | |
87 | $tree->fid('logo')->replace_with(literal $logo); | |
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/; | |
96 | $tree->fid('track_user')->attr('data-user', $args{id}); | |
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) = @_; | |
111 | $td->fclass('contest')->namedlink($data->{contest}, $data->{contest_name}); | |
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); | |
116 | } | |
117 | ||
118 | sub process_us { | |
119 | my ($tree, %args) = @_; | |
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); | |
126 | } | |
127 | ||
128 | sub process_ct_entry { | |
129 | my ($tree, %args) = @_; | |
130 | $_->edit_href (sub {s/contest_id/$args{id}/}) for $tree->find('a'); | |
131 | $tree->fid('editorial')->detach unless $args{finished}; | |
132 | $tree->fid('links')->detach unless $args{started}; | |
133 | my $status = ($args{time} < $args{start} ? 'starts' : 'ends'); | |
134 | $tree->fclass('timer')->attr('data-stop', $status eq 'ends' ? $args{stop} : $args{start}); | |
135 | $tree->content_handler( | |
136 | start => ftime $args{start}, | |
137 | stop => ftime $args{stop}, | |
138 | status => $status, | |
139 | description => literal $args{description}); | |
140 | $tree->fid('ctcountdown')->detach if $args{time} >= $args{stop}; | |
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}/}); | |
159 | $tree->fid('solution')->edit_href(sub{s/problem_id/$args{id}/}); | |
160 | $tree->content_handler( | |
161 | statement => literal $args{statement}, | |
162 | level => ucfirst $args{level}, | |
163 | author => $args{author}, | |
164 | owner => $args{owner_name} || $args{owner}); | |
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 | } | |
170 | if ($args{contest_stop}) { | |
171 | $tree->fid('solution')->detach; | |
172 | $tree->fid('solution_modal')->detach; | |
173 | $tree->fid('score')->replace_content($args{value}); | |
174 | $tree->fid('countdown')->attr('data-stop' => $args{contest_stop}); | |
175 | } else { | |
176 | $tree->fid('job_log')->edit_href(sub{$_ .= "&private=$args{private}"}) if $args{private}; | |
177 | $tree->fid('solution')->detach unless $args{solution}; | |
178 | $_->detach for $tree->fclass('rc'); # requires contest | |
179 | $tree->fid('solution_modal')->fclass('modal-body')->replace_content(literal $args{solution}); | |
180 | } | |
181 | if ($args{cansubmit}) { | |
182 | $tree->fid('nosubmit')->detach; | |
183 | $tree->look_down(name => 'problem')->attr(value => $args{id}); | |
184 | my $contest = $tree->look_down(name => 'contest'); | |
185 | $contest->attr(value => $args{args}{contest}) if $args{args}{contest}; | |
186 | $contest->detach unless $args{args}{contest} | |
187 | } else { | |
188 | $tree->fid('nosubmit')->find('a')->edit_href(sub{s/id/$args{id}/}); | |
189 | $tree->fid('submit')->detach | |
190 | } | |
191 | } | |
192 | ||
193 | sub process_sol { | |
194 | my ($tree, %args) = @_; | |
195 | $tree->content_handler(solution => literal $args{solution}); | |
196 | } | |
197 | ||
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}); | |
206 | $tr->find('td')->attr(class => $tr->find('td')->attr('class').' warning') if $data->{private} && !$args{contest}; | |
207 | }; | |
208 | my $iter = sub { | |
209 | my ($data, $div) = @_; | |
210 | $div->attr(id => $data); | |
211 | $div->find('h2')->replace_content(ucfirst $data); | |
212 | $div->find('tbody')->find('tr')->iter3($args{$data}, $titer); | |
213 | }; | |
214 | $tree->fid('beginner')->iter3([grep {$args{$_}} qw/beginner easy medium hard/], $iter); | |
215 | $tree->fid('open-alert')->detach unless $args{contest}; | |
216 | } | |
217 | ||
218 | sub process_log_entry { | |
219 | my ($tree, %args) = @_; | |
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 | ||
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 | }; | |
238 | $tree->fclass('result_text')->replace_content($args{result_text}); | |
239 | $tree->fclass('result_text')->attr(class => "r$args{result}"); | |
240 | $args{results} ? $tree->fid('results')->find('tbody')->find('tr')->iter3($args{results}, $iter) : $tree->fid('results')->detach; | |
241 | $tree->fid('no_results')->detach if $tree->fid('results') || $tree->fid('errors'); | |
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}); | |
250 | $tr->fclass('problem')->find('a')->edit_href(sub{$_ .= "?contest=$args{args}{contest}"}) if $args{args}{contest}; | |
251 | $tr->fclass('contest')->namedlink($data->{contest}, $data->{contest_name}) if $data->{contest}; | |
252 | $tr->fclass('contest')->replace_content('None') unless $data->{contest}; | |
253 | $tr->fclass('date')->replace_content(ftime $data->{date}); | |
254 | $tr->fclass('source')->namedlink("$data->{id}.$data->{extension}", sprintf "%.2fKB %s", $data->{size}/1024, Plack::App::Gruntmaster::FORMAT_EXTENSION()->{$data->{format}}); | |
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}"); | |
258 | $tr->find('td')->attr(class => $tr->find('td')->attr('class').' warning') if $data->{private}; | |
259 | }; | |
260 | $tree->find('table')->find('tbody')->find('tr')->iter3($args{log}, $iter); | |
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; | |
263 | for my $cls (qw/next previous/) { | |
264 | my $elem = $tree->fclass($cls); | |
265 | next unless $elem; | |
266 | delete $args{args}{page}; | |
267 | my $str = join '&', map { $_ . '=' . $args{args}{$_} } keys %{$args{args}}; | |
268 | $elem->find('a')->edit_href(sub{s/$/&$str/}) if $str; | |
269 | } | |
270 | $tree->fclass('current')->replace_content("Page $args{current_page} of $args{last_page}"); | |
271 | ||
272 | my @detach; | |
273 | push @detach, $args{args}{$_} ? $tree->fclass($_) : () for qw/problem contest owner/; | |
274 | $_->detach for @detach; | |
275 | } | |
276 | ||
277 | sub process_st { | |
278 | my ($tree, %args) = @_; | |
279 | $args{problems} //= []; | |
280 | my $pbiter = sub { | |
281 | my ($data, $th) = @_; | |
282 | $th->attr(class => undef); | |
283 | $th->namedlink(@$data); | |
284 | $th->find('a')->edit_href(sub{s/$/?contest=$args{args}{contest}/}); | |
285 | }; | |
286 | $tree->fclass('problem')->iter3($args{problems}, $pbiter); | |
287 | my $iter = sub { | |
288 | my ($st, $tr) = @_; | |
289 | $tr->set_child_content(class => 'rank', $st->{rank}); | |
290 | $tr->set_child_content(class => 'score', $st->{score}); | |
291 | $tr->fclass('user')->namedlink($st->{user}, $st->{user_name}); | |
292 | my $pbscore = $tr->fclass('pbscore'); | |
293 | $pbscore->iter($pbscore => @{$st->{scores}}); | |
294 | }; | |
295 | $tree->find('tbody')->find('tr')->iter3($args{st}, $iter); | |
296 | } | |
297 | ||
298 | sub process_ed { | |
299 | my ($tree, %args) = @_; | |
300 | $tree->content_handler(editorial => literal $args{editorial}); | |
301 | my $iter = sub { | |
302 | my ($data, $div) = @_; | |
303 | $div->set_child_content(class => 'value', $data->{value}); | |
304 | $div->set_child_content(class => 'solution', literal $data->{solution}); | |
305 | $div->fclass('problem')->namedlink($data->{id}, $data->{name}); | |
306 | }; | |
307 | my @pb = map { @{$args{$_} // []} } qw/beginner easy medium hard/; | |
308 | $tree->fclass('well')->iter3(\@pb, $iter); | |
309 | } | |
310 | ||
311 | 1; | |
312 | __END__ |