]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Page/Pb.pm
Centralize template cooking and introduce reloadable templates
[gruntmaster-page.git] / lib / Gruntmaster / Page / Pb.pm
... / ...
CommitLineData
1package Gruntmaster::Page::Pb;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7our @EXPORT_OK = qw/generate/;
8our $VERSION = '0.001';
9
10use Fcntl qw/:flock/;
11use HTML::Template::Compiled;
12use IO::File;
13use POSIX qw/strftime/;
14use Gruntmaster::Page::Common qw/cook_templates reload_templates/;
15use Gruntmaster::Data qw/problem_name problem_level problems/;
16
17my %orig_templates = (
18 en => <<'HTML',
19<tmpl_if levels>
20<h2>Beginner</h2>
21<ul>
22<tmpl_loop beginner><li><a href="<tmpl_var id>.var"><tmpl_var name></a>
23</tmpl_loop></ul>
24
25<h2>Easy</h2>
26<ul>
27<tmpl_loop easy><li><a href="<tmpl_var id>.var"><tmpl_var name></a>
28</tmpl_loop></ul>
29
30<h2>Medium</h2>
31<ul>
32<tmpl_loop medium><li><a href="<tmpl_var id>.var"><tmpl_var name></a>
33</tmpl_loop></ul>
34
35<h2>Hard</h2>
36<ul>
37<tmpl_loop hard><li><a href="<tmpl_var id>.var"><tmpl_var name></a>
38</tmpl_loop></ul>
39
40<tmpl_else>
41<ul>
42<tmpl_loop problems><li><a href="<tmpl_var id>.var"><tmpl_var name></a>
43</tmpl_loop></ul>
44</tmpl_if>
45HTML
46);
47
48my %templates = cook_templates %orig_templates, pb => 'Problems';
49
50sub generate{
51 %templates = cook_templates %orig_templates, pb => 'Problems' if reload_templates;
52 $_[0] =~ m,^(?:ct/([^/]+)/)?pb/index.html$,;
53 local $Gruntmaster::Data::contest = $1;
54
55 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$_[1]});
56 my @problems = sort { $b->{name} cmp $a->{name} } map +{
57 id => $_,
58 name => problem_name,
59 level => problem_level}, problems;
60 for my $d (qw/beginner easy medium advanced hard/) {
61 $htc->param($d => [grep {$_->{level} and $_->{level} eq $d} @problems]);
62 }
63 $htc->param(levels => grep { $_->{level} } @problems);
64 $htc->param(problems => \@problems);
65 $htc->output
66}
67
681
This page took 0.018332 seconds and 4 git commands to generate.