667d73b6c5e90af016b7ed65cdc76a0de309a9c9
[ikiwiki.git] / IkiWiki / Plugin / htmlbalance.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::htmlbalance;
3
4 # htmlbalance: Parse and re-serialize HTML to ensure balanced tags
5 #
6 # Copyright 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
7 # Licensed under the GNU GPL, version 2, or any later version published by the
8 # Free Software Foundation
9
10 use warnings;
11 use strict;
12 use IkiWiki 2.00;
13
14 sub import { #{{{
15         hook(type => "getsetup", id => "htmlbalance", call => \&getsetup);
16         hook(type => "sanitize", id => "htmlbalance", call => \&sanitize);
17 } # }}}
18
19 sub getsetup () { #{{{
20         return
21                 plugin => {
22                         safe => 1,
23                         rebuild => undef,
24                 },
25 } #}}}
26
27 sub sanitize (@) { #{{{
28         my %params=@_;
29         my $ret = '';
30
31         eval {
32                 use HTML::TreeBuilder;
33                 use XML::Atom::Util qw(encode_xml);
34         };
35
36         if ($@) {
37                 error($@);
38                 return $params{content};
39         }
40
41         my $tree = HTML::TreeBuilder->new_from_content($params{content});
42         my @nodes = $tree->disembowel();
43         foreach my $node (@nodes) {
44                 if (ref $node) {
45                         $ret .= $node->as_XML();
46                         chomp $ret;
47                         $node->delete();
48                 }
49                 else {
50                         $ret .= encode_xml($node);
51                 }
52         }
53         $tree->delete();
54         return $ret;
55 } # }}}
56
57 1