2008-09-09 20:44:58 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
package IkiWiki::Plugin::progress;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2008-09-09 20:44:58 +02:00
|
|
|
|
2008-09-19 19:09:50 +02:00
|
|
|
my $percentage_pattern = qr/[0-9]+\%?/; # pattern to validate percentages
|
2008-09-09 20:44:58 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-09-09 20:44:58 +02:00
|
|
|
hook(type => "getsetup", id => "progress", call => \&getsetup);
|
|
|
|
hook(type => "preprocess", id => "progress", call => \&preprocess);
|
|
|
|
hook(type => "format", id => "progress", call => \&format);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-09 20:44:58 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-09-09 20:44:58 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-09-09 20:44:58 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-09 20:44:58 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2008-09-09 20:44:58 +02:00
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
my $fill;
|
|
|
|
|
|
|
|
if (defined $params{percent}) {
|
|
|
|
$fill = $params{percent};
|
|
|
|
($fill) = $fill =~ m/($percentage_pattern)/; # fill is untainted now
|
2008-09-20 22:31:27 +02:00
|
|
|
$fill=~s/%$//;
|
2008-09-19 19:09:50 +02:00
|
|
|
if (! defined $fill || ! length $fill || $fill > 100 || $fill < 0) {
|
2008-09-19 19:48:36 +02:00
|
|
|
error(sprintf(gettext("illegal percent value %s"), $params{percent}));
|
2008-09-19 19:09:50 +02:00
|
|
|
}
|
2008-09-20 22:31:27 +02:00
|
|
|
$fill.="%";
|
2008-09-09 20:44:58 +02:00
|
|
|
}
|
|
|
|
elsif (defined $params{totalpages} and defined $params{donepages}) {
|
2009-10-09 18:48:16 +02:00
|
|
|
my $totalcount=pagespec_match_list(
|
|
|
|
$params{page}, $params{totalpages},
|
|
|
|
deptype => deptype("presence"));
|
|
|
|
my $donecount=pagespec_match_list(
|
|
|
|
$params{page}, $params{donepages},
|
|
|
|
deptype => deptype("presence"));
|
2008-09-09 20:44:58 +02:00
|
|
|
|
|
|
|
if ($totalcount == 0) {
|
|
|
|
$fill = "100%";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $number = $donecount/$totalcount*100;
|
|
|
|
$fill = sprintf("%u%%", $number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-09-19 19:48:36 +02:00
|
|
|
error(gettext("need either `percent` or `totalpages` and `donepages` parameters"));
|
2008-09-09 20:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return <<EODIV
|
|
|
|
<div class="progress">
|
|
|
|
<div class="progress-done" style="width: $fill">$fill</div>
|
|
|
|
</div>
|
|
|
|
EODIV
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-09 20:44:58 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub format(@) {
|
2008-09-09 20:44:58 +02:00
|
|
|
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};
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-09-09 20:44:58 +02:00
|
|
|
|
|
|
|
1
|