aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/progress.pm
blob: d27df5ca894b3aacf77fb441901fb5e47cf359b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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 => "getsetup", id => "progress", call => \&getsetup);
	hook(type => "preprocess", id => "progress", call => \&preprocess);
	hook(type => "format",     id => "progress", call => \&format);
}

sub getsetup () {
	return 
		plugin => {
			safe => 1,
			rebuild => undef,
			section => "widget",
		},
}

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 > 100 || $fill < 0) {
			error(sprintf(gettext("illegal percent value %s"), $params{percent}));
		}
		$fill.="%";
	}
	elsif (defined $params{totalpages} and defined $params{donepages}) {
		my $totalcount=pagespec_match_list(
			$params{page}, $params{totalpages},
			deptype => deptype("presence"));
		my $donecount=pagespec_match_list(
			$params{page}, $params{donepages},
			deptype => deptype("presence"));
		
		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 <<EODIV
<div class="progress">
  <div class="progress-done" style="width: $fill">$fill</div>
</div>
EODIV
}

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

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

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

	return $params{content};    
}

1