]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page/Pb.pm
Split problem list into levels
[gruntmaster-page.git] / lib / Gruntmaster / Page / Pb.pm
CommitLineData
42546e6c
MG
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 constant TITLE => 'Problems';
11
12use Fcntl qw/:flock/;
13use HTML::Template::Compiled;
14use IO::File;
15use POSIX qw/strftime/;
16use YAML::Any qw/LoadFile/;
832cb45e 17use Gruntmaster::Page::Common qw/header footer/;
42546e6c
MG
18
19my %templates = (
20 en => <<'HTML',
9be91908 21<h2>Beginner</h2>
42546e6c 22<ul>
9be91908
MG
23<tmpl_loop beginner><li><a href="<tmpl_var id>"><tmpl_var name></a>
24</tmpl_loop></ul>
25
26<h2>Easy</h2>
27<ul>
28<tmpl_loop easy><li><a href="<tmpl_var id>"><tmpl_var name></a>
29</tmpl_loop></ul>
30
31<h2>Medium</h2>
32<ul>
33<tmpl_loop medium><li><a href="<tmpl_var id>"><tmpl_var name></a>
34</tmpl_loop></ul>
35
36<h2>Advanced</h2>
37<ul>
38<tmpl_loop advanced><li><a href="<tmpl_var id>"><tmpl_var name></a>
39</tmpl_loop></ul>
40
41<h2>Hard</h2>
42<ul>
43<tmpl_loop hard><li><a href="<tmpl_var id>"><tmpl_var name></a>
42546e6c
MG
44</tmpl_loop></ul>
45HTML
46);
47
48$templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates;
49$templates{$_} .= footer $_ for keys %templates;
50
51sub generate{
fe78f0c1
MG
52 my ($path, $lang) = @_;
53 $path =~ s,/index\.html$,,;
54 my $template = $templates{$lang};
42546e6c 55 my $htc = HTML::Template::Compiled->new(scalarref => \$template);
fe78f0c1
MG
56 IO::File->new(">$path/meta.yml")->close unless -f "$path/meta.yml";
57 flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH;
42546e6c
MG
58 my @problems = sort { $b->{name} cmp $a->{name} } map {
59 my $meta = LoadFile $_;
fe78f0c1 60 my $id = (m,^$path/(.*)/meta.yml$,)[0];
9be91908
MG
61 +{ id => $id, name => $meta->{name}, level => $meta->{level} } } <$path/*/meta.yml>;
62 for my $d(qw/beginner easy medium advanced hard/) {
63 $htc->param($d => [grep {$_->{level} eq $d} @problems]);
64 }
42546e6c
MG
65 $htc->output
66}
67
681
This page took 0.029362 seconds and 4 git commands to generate.