2008-11-16 19:11:39 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::htmlbalance;
|
|
|
|
|
|
|
|
# htmlbalance: Parse and re-serialize HTML to ensure balanced tags
|
|
|
|
#
|
|
|
|
# Copyright 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
|
|
|
|
# Licensed under the GNU GPL, version 2, or any later version published by the
|
|
|
|
# Free Software Foundation
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2008-11-17 20:27:11 +01:00
|
|
|
use HTML::Entities;
|
2008-11-16 19:11:39 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-11-16 19:11:39 +01:00
|
|
|
hook(type => "getsetup", id => "htmlbalance", call => \&getsetup);
|
|
|
|
hook(type => "sanitize", id => "htmlbalance", call => \&sanitize);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-11-16 19:11:39 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-11-16 19:11:39 +01:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-11-16 19:11:39 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub sanitize (@) {
|
2008-11-16 19:11:39 +01:00
|
|
|
my %params=@_;
|
|
|
|
my $ret = '';
|
|
|
|
|
2008-12-29 21:08:18 +01:00
|
|
|
eval q{use HTML::TreeBuilder};
|
|
|
|
error $@ if $@;
|
2008-11-18 12:25:13 +01:00
|
|
|
my $tree = HTML::TreeBuilder->new();
|
|
|
|
$tree->ignore_unknown(0);
|
|
|
|
$tree->ignore_ignorable_whitespace(0);
|
|
|
|
$tree->no_space_compacting(1);
|
|
|
|
$tree->p_strict(1);
|
|
|
|
$tree->store_comments(0);
|
|
|
|
$tree->store_declarations(0);
|
|
|
|
$tree->store_pis(0);
|
|
|
|
$tree->parse_content($params{content});
|
2008-11-16 19:11:39 +01:00
|
|
|
my @nodes = $tree->disembowel();
|
|
|
|
foreach my $node (@nodes) {
|
|
|
|
if (ref $node) {
|
2010-09-26 23:12:10 +02:00
|
|
|
$ret .= $node->as_HTML(undef, '', {});
|
2008-11-16 19:11:39 +01:00
|
|
|
chomp $ret;
|
|
|
|
$node->delete();
|
|
|
|
}
|
|
|
|
else {
|
2008-11-17 20:27:11 +01:00
|
|
|
$ret .= encode_entities($node);
|
2008-11-16 19:11:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$tree->delete();
|
|
|
|
return $ret;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-11-16 19:11:39 +01:00
|
|
|
|
|
|
|
1
|