« Plagger/Plugin/Filter/AtomLinkRelated.pm
↑ Summary
Plagger/Plugin/Filter/HatenaBookmarkUsersCount.pm »
| 1 | package Plagger::Plugin::Filter::HTMLTidy; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use HTML::Tidy; | ||
| 6 | |||
| 7 | sub register { | ||
| 8 | my($self, $context) = @_; | ||
| 9 | $context->register_hook( | ||
| 10 | $self, | ||
| 11 | 'update.entry.fixup' => \&filter, | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | |||
| 15 | our %defaults = ( | ||
| 16 | doctype => 'omit', | ||
| 17 | output_xhtml => 1, | ||
| 18 | wrap => 0, | ||
| 19 | break_before_br => 0, | ||
| 20 | input_encoding => 'utf8', | ||
| 21 | output_encoding => 'utf8', | ||
| 22 | tidy_mark => 0, | ||
| 23 | ); | ||
| 24 | |||
| 25 | sub filter { | ||
| 26 | my($self, $context, $args) = @_; | ||
| 27 | |||
| 28 | my $body = $args->{entry}->body; | ||
| 29 | return unless $body && $body->is_html; | ||
| 30 | |||
| 31 | my $conf = $self->conf || {}; | ||
| 32 | while (my($key, $value) = each %defaults) { | ||
| 33 | $conf->{$key} = $value unless exists $conf->{$key}; | ||
| 34 | } | ||
| 35 | |||
| 36 | my $tidy = HTML::Tidy->new( $self->conf || {} ); | ||
| 37 | $tidy->ignore( type => TIDY_WARNING ); | ||
| 38 | my $new_body = $tidy->clean($body->data); # pass in Unicode string, not UTF-8 | ||
| 39 | |||
| 40 | # HACK to extract <body /> only | ||
| 41 | $new_body =~ s!^.*<body>\s*(.*?)\s*</body>\s*</html>\s*$!$1!s; | ||
| 42 | |||
| 43 | $args->{entry}->body($new_body); | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; | ||
| 47 | __END__ |