Parse and reserialize literal HTML
[plack-app-gruntmaster.git] / lib / Plack / App / Gruntmaster / HTML.pm
index f2b4ec9cead19e8e98af996ff35a3e0f4f8bc83f..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) = @_;
@@ -45,7 +51,7 @@ sub HTML::Element::namedlink {
 my %page_cache;
 for (<tmpl/*>) {
        my ($tmpl, $lang) = m,tmpl/(\w+)\.(\w+),;
-       my $builder = HTML::Seamstress->new;
+       my $builder = HTML::TreeBuilder->new;
        $builder->ignore_unknown(0);
        $page_cache{$tmpl, $lang} = $builder->parse_file($_);
 }
@@ -74,7 +80,7 @@ sub _render {
        $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('defmap')});
+       $_->attr('smap', undef) for $tree->look_down(sub {$_[0]->attr('smap')});
        $tree->as_HTML;
 }
 
@@ -266,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 {
This page took 0.01048 seconds and 4 git commands to generate.