]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Pb.pm
Centralize template cooking and introduce reloadable templates
[gruntmaster-page.git] / lib / Gruntmaster / Page / Pb.pm
1 package Gruntmaster::Page::Pb;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use parent qw/Exporter/;
7 our @EXPORT_OK = qw/generate/;
8 our $VERSION = '0.001';
9
10 use Fcntl qw/:flock/;
11 use HTML::Template::Compiled;
12 use IO::File;
13 use POSIX qw/strftime/;
14 use Gruntmaster::Page::Common qw/cook_templates reload_templates/;
15 use Gruntmaster::Data qw/problem_name problem_level problems/;
16
17 my %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>
45 HTML
46 );
47
48 my %templates = cook_templates %orig_templates, pb => 'Problems';
49
50 sub 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
68 1
This page took 0.043308 seconds and 5 git commands to generate.