]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/progress.pm
typo
[git.ikiwiki.info.git] / IkiWiki / Plugin / progress.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::progress;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
8 my $percentage_pattern = qr/[0-9]+\%?/; # pattern to validate percentages
10 sub import {
11         hook(type => "getsetup", id => "progress", call => \&getsetup);
12         hook(type => "preprocess", id => "progress", call => \&preprocess);
13         hook(type => "format",     id => "progress", call => \&format);
14 }
16 sub getsetup () {
17         return 
18                 plugin => {
19                         safe => 1,
20                         rebuild => undef,
21                 },
22 }
24 sub preprocess (@) {
25         my %params=@_;
26         
27         my $fill;
28         
29         if (defined $params{percent}) {
30                 $fill = $params{percent};
31                 ($fill) = $fill =~ m/($percentage_pattern)/; # fill is untainted now
32                 $fill=~s/%$//;
33                 if (! defined $fill || ! length $fill || $fill > 100 || $fill < 0) {
34                         error(sprintf(gettext("illegal percent value %s"), $params{percent}));
35                 }
36                 $fill.="%";
37         }
38         elsif (defined $params{totalpages} and defined $params{donepages}) {
39                 my $totalcount=pagespec_match_list(
40                         $params{page}, $params{totalpages},
41                         deptype => deptype("presence"));
42                 my $donecount=pagespec_match_list(
43                         $params{page}, $params{donepages},
44                         deptype => deptype("presence"));
45                 
46                 if ($totalcount == 0) {
47                         $fill = "100%";
48                 }
49                 else {
50                         my $number = $donecount/$totalcount*100;
51                         $fill = sprintf("%u%%", $number);
52                 }
53         }
54         else {
55                 error(gettext("need either `percent` or `totalpages` and `donepages` parameters"));
56         }
58         return <<EODIV
59 <div class="progress">
60   <div class="progress-done" style="width: $fill">$fill</div>
61 </div>
62 EODIV
63 }
65 sub format(@) {
66         my %params = @_;
68         # If HTMLScrubber has removed the style attribute, then bring it back
70         $params{content} =~ s!<div class="progress-done">($percentage_pattern)</div>!<div class="progress-done" style="width: $1">$1</div>!g;
72         return $params{content};    
73 }
75 1