<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::progress;

use warnings;
use strict;
use IkiWiki 3.00;

my $percentage_pattern = qr/[0-9]+\%?/; # pattern to validate percentages

sub import {
	hook(type =&gt; "getsetup", id =&gt; "progress", call =&gt; \&amp;getsetup);
	hook(type =&gt; "preprocess", id =&gt; "progress", call =&gt; \&amp;preprocess);
	hook(type =&gt; "format",     id =&gt; "progress", call =&gt; \&amp;format);
}

sub getsetup () {
	return 
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; undef,
		},
}

sub preprocess (@) {
	my %params=@_;
	
	my $fill;
	
	if (defined $params{percent}) {
		$fill = $params{percent};
		($fill) = $fill =~ m/($percentage_pattern)/; # fill is untainted now
		$fill=~s/%$//;
		if (! defined $fill || ! length $fill || $fill &gt; 100 || $fill &lt; 0) {
			error(sprintf(gettext("illegal percent value %s"), $params{percent}));
		}
		$fill.="%";
	}
	elsif (defined $params{totalpages} and defined $params{donepages}) {
		add_depends($params{page}, $params{totalpages}, deptype("presence"));
		add_depends($params{page}, $params{donepages}, deptype("presence"));

		my @pages=keys %pagesources;
		my $totalcount=0;
		my $donecount=0;
		foreach my $page (@pages) {
			$totalcount++ if pagespec_match($page, $params{totalpages}, location =&gt; $params{page});
			$donecount++ if pagespec_match($page, $params{donepages}, location =&gt; $params{page});
		}
		
		if ($totalcount == 0) {
			$fill = "100%";
		}
		else {
			my $number = $donecount/$totalcount*100;
			$fill = sprintf("%u%%", $number);
		}
	}
	else {
		error(gettext("need either `percent` or `totalpages` and `donepages` parameters"));
	}

	return &lt;&lt;EODIV
&lt;div class="progress"&gt;
  &lt;div class="progress-done" style="width: $fill"&gt;$fill&lt;/div&gt;
&lt;/div&gt;
EODIV
}

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

	# If HTMLScrubber has removed the style attribute, then bring it back

	$params{content} =~ s!&lt;div class="progress-done"&gt;($percentage_pattern)&lt;/div&gt;!&lt;div class="progress-done" style="width: $1"&gt;$1&lt;/div&gt;!g;

	return $params{content};    
}

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