Parse and reserialize literal HTML
[plack-app-gruntmaster.git] / lib / Plack / App / Gruntmaster / HTML.pm
index b4f863ad2187550ea35dd9ee242dc71111d4339a..3cfbc54be43acb876d8bce726fc61b442ece0d1b 100644 (file)
@@ -4,12 +4,18 @@ use parent qw/Exporter/;
 our @EXPORT = qw/render render_article/;
 
 use File::Slurp qw/read_file/;
-use HTML::Seamstress;
+use HTML::Element::Library;
+use HTML::TreeBuilder;
 use POSIX qw//;
 use Data::Dumper qw/Dumper/;
 
 sub ftime ($)   { POSIX::strftime '%c', localtime shift }
-sub literal ($) { HTML::Element::Library::super_literal shift // '' }
+sub literal ($) {
+       my $b = HTML::TreeBuilder->new;
+       $b->ignore_unknown(0);
+       $b->parse(shift // '');
+       HTML::Element::Library::super_literal $b->guts->as_HTML;
+}
 
 sub HTML::Element::edit_href {
        my ($self, $sub) = @_;
@@ -42,6 +48,14 @@ sub HTML::Element::namedlink {
        $self->replace_content($name);
 }
 
+my %page_cache;
+for (<tmpl/*>) {
+       my ($tmpl, $lang) = m,tmpl/(\w+)\.(\w+),;
+       my $builder = HTML::TreeBuilder->new;
+       $builder->ignore_unknown(0);
+       $page_cache{$tmpl, $lang} = $builder->parse_file($_);
+}
+
 sub render {
        my ($tmpl, $lang, %args) = @_;
        $lang //= 'en';
@@ -50,22 +64,23 @@ sub render {
 }
 
 sub render_article {
-       my ($art, $lang) = @_;
+       my ($art, $lang, %args) = @_;
        $lang //= 'en';
        my $title = read_file "a/$art.$lang.title";
        my $meat  = read_file "a/$art.$lang";
-       _render('skel', $lang, title => $title , meat => $meat)
+       _render('skel', $lang, title => $title , meat => $meat, %args)
 }
 
 sub _render {
        my ($tmpl, $lang, %args) = @_;
-       my $builder = HTML::Seamstress->new;
-       $builder->ignore_unknown(0);
-       my $tree = $builder->parse_file("tmpl/$tmpl.$lang");
+       my $tree = $page_cache{$tmpl, $lang}->clone or die "No such template/language combination: $tmpl/$lang\n";
        $tree = $tree->guts unless $tmpl eq 'skel';
        $tree->defmap(smap => \%args);
        my $process = __PACKAGE__->can("process_$tmpl");
        $process->($tree, %args) if $process;
+       $_->detach for $tree->look_down(static => $args{static} ? 'no' : 'yes');
+       $_->attr('static', undef) for $tree->look_down(sub {$_[0]->attr('static')});
+       $_->attr('smap', undef) for $tree->look_down(sub {$_[0]->attr('smap')});
        $tree->as_HTML;
 }
 
@@ -143,12 +158,16 @@ sub process_pb_entry {
        $tree->fid('owner')->edit_href(sub{s/owner_id/$args{owner}/});
        $tree->fid('job_log')->edit_href(sub{s/problem_id/$args{id}/});
        $tree->fid('solution')->edit_href(sub{s/problem_id/$args{id}/});
-       $tree->fid('job_log')->edit_href(sub{$_ .= "&private=$args{private}"}) if $args{private};
        $tree->content_handler(
                statement => literal $args{statement},
                level     => ucfirst $args{level},
                author    =>         $args{author},
                owner     =>         $args{owner_name} || $args{owner});
+       if ($args{limits}) {
+               my @limits = (@{$args{limits}}, {format => 'Other', timeout => $args{timeout} });
+               @limits = map { sprintf '%s (%s)', @{$_}{qw/timeout format/} } @limits;
+               $tree->look_down(smap => 'timeout')->replace_content(join ', ', @limits);
+       }
        if ($args{contest_stop}) {
                $tree->fid('solution')->detach;
                $tree->fid('solution_modal')->detach;
@@ -158,6 +177,7 @@ sub process_pb_entry {
                $score->attr('data-value'  => $args{value});
                $tree->fid('countdown')->attr('data-stop' => $args{contest_stop});
        } else {
+               $tree->fid('job_log')->edit_href(sub{$_ .= "&private=$args{private}"}) if $args{private};
                $tree->fid('solution')->detach unless $args{solution};
                $_->detach for $tree->fclass('rc'); # requires contest
                $tree->fid('solution_modal')->fclass('modal-body')->replace_content(literal $args{solution});
@@ -166,8 +186,8 @@ sub process_pb_entry {
                $tree->fid('nosubmit')->detach;
                $tree->look_down(name => 'problem')->attr(value => $args{id});
                my $contest = $tree->look_down(name => 'contest');
-               $contest->attr(value => $args{contest}) if $args{contest};
-               $contest->detach unless $args{contest}
+               $contest->attr(value => $args{args}{contest}) if $args{args}{contest};
+               $contest->detach unless $args{args}{contest}
        } else {
                $tree->fid('nosubmit')->find('a')->edit_href(sub{s/id/$args{id}/});
                $tree->fid('submit')->detach
@@ -231,11 +251,11 @@ sub process_log {
                my ($data, $tr) = @_;
                $tr->fclass('id')->namedlink($data->{id});
                $tr->fclass('problem')->namedlink($data->{problem}, $data->{problem_name});
-               $tr->fclass('problem')->find('a')->edit_href(sub{$_ .= "?contest=$args{contest}"}) if $args{contest};
+               $tr->fclass('problem')->find('a')->edit_href(sub{$_ .= "?contest=$args{args}{contest}"}) if $args{args}{contest};
                $tr->fclass('contest')->namedlink($data->{contest}, $data->{contest_name}) if $data->{contest};
                $tr->fclass('contest')->replace_content('None') unless $data->{contest};
                $tr->fclass('date')->replace_content(ftime $data->{date});
-               $tr->fclass('source')->namedlink("$data->{id}.$data->{extension}", sprintf "%.2fKB %s", $data->{size}/1024, $data->{format});
+               $tr->fclass('source')->namedlink("$data->{id}.$data->{extension}", sprintf "%.2fKB %s", $data->{size}/1024, Plack::App::Gruntmaster::FORMAT_EXTENSION()->{$data->{format}});
                $tr->fclass('owner')->namedlink($data->{owner}, $data->{owner_name});
                $tr->fclass('result_text')->replace_content($data->{result_text});
                $tr->fclass('result_text')->attr(class => "r$data->{result}");
@@ -252,6 +272,10 @@ sub process_log {
                $elem->find('a')->edit_href(sub{s/$/&$str/}) if $str;
        }
        $tree->fclass('current')->replace_content("Page $args{current_page} of $args{last_page}");
+
+       my @detach;
+       push @detach, $args{args}{$_} ? $tree->fclass($_) : () for qw/problem contest owner/;
+       $_->detach for @detach;
 }
 
 sub process_st {
@@ -287,3 +311,6 @@ sub process_ed {
        my @pb = map { @{$args{$_} // []} } qw/beginner easy medium hard/;
        $tree->fclass('well')->iter3(\@pb, $iter);
 }
+
+1;
+__END__
This page took 0.012302 seconds and 4 git commands to generate.