package Gruntmaster::Page::Pb;

use 5.014000;
use strict;
use warnings;
use parent qw/Exporter/;
our @EXPORT_OK = qw/generate/;
our $VERSION = '0.001';

use constant TITLE => 'Problems';

use Fcntl qw/:flock/;
use HTML::Template::Compiled;
use IO::File;
use POSIX qw/strftime/;
use YAML::Any qw/LoadFile/;
use Gruntmaster::Page qw/header footer/;

my %templates = (
  en => <<'HTML',
<ul>
<tmpl_loop problems><li><a href="<tmpl_var id>"><tmpl_var name></a>
</tmpl_loop></ul>
HTML
);

$templates{$_}  = header($_, TITLE) . $templates{$_} for keys %templates;
$templates{$_} .= footer $_ for keys %templates;

sub generate{
  my ($path, $lang) = @_;
  $path =~ s,/index\.html$,,;
  my $template = $templates{$lang};
  my $htc = HTML::Template::Compiled->new(scalarref => \$template);
  IO::File->new(">$path/meta.yml")->close unless -f "$path/meta.yml";
  flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH;
  my @problems = sort { $b->{name} cmp $a->{name} } map {
	my $meta = LoadFile $_;
	my $id = (m,^$path/(.*)/meta.yml$,)[0];
	+{ id => $id, name => $meta->{name} } } <$path/*/meta.yml>;
  $htc->param(problems => \@problems);
  $htc->output
}

1
