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