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