<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# HTML Tidy plugin
# requires 'tidy' binary, found in Debian or http://tidy.sf.net/
# mostly a proof-of-concept on how to use external filters.
# It is particularly useful when the html plugin is used.
#
# by Faidon Liambotis
package IkiWiki::Plugin::htmltidy;

use warnings;
use strict;
use IkiWiki 3.00;
use IPC::Open2;

sub import {
	hook(type =&gt; "getsetup", id =&gt; "tidy", call =&gt; \&amp;getsetup);
	hook(type =&gt; "sanitize", id =&gt; "tidy", call =&gt; \&amp;sanitize);
	hook(type =&gt; "checkconfig", id =&gt; "tidy", call =&gt; \&amp;checkconfig);
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
		},
		htmltidy =&gt; {
			type =&gt; "string",
			description =&gt; "tidy command line",
			safe =&gt; 0, # path
			rebuild =&gt; undef,
		},
}

sub checkconfig () {
	if (! defined $config{htmltidy}) {
		$config{htmltidy}="tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no --markup yes";
	}
}

sub sanitize (@) {
	my %params=@_;

	return $params{content} unless defined $config{htmltidy};

	my $pid;
	my $sigpipe=0;
	$SIG{PIPE}=sub { $sigpipe=1 };
	$pid=open2(*IN, *OUT, "$config{htmltidy} 2&gt;/dev/null");

	# open2 doesn't respect "use open ':utf8'"
	binmode (IN, ':utf8');
	binmode (OUT, ':utf8');
	
	print OUT $params{content};
	close OUT;

	local $/ = undef;
	my $ret=&lt;IN&gt;;
	close IN;
	waitpid $pid, 0;

	$SIG{PIPE}="DEFAULT";
	if ($sigpipe || ! defined $ret) {
		return gettext("htmltidy failed to parse this html");
	}

	return $ret;
}

1
</pre></body></html>