summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/progress.pm
blob: e459637b8664853171480989d32280c5bdb04659 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::progress;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. my $percentage_pattern = qr/[0-9]+\%/; # pattern to validate percentages
  7. sub import { #{{{
  8. hook(type => "getsetup", id => "progress", call => \&getsetup);
  9. hook(type => "preprocess", id => "progress", call => \&preprocess);
  10. hook(type => "format", id => "progress", call => \&format);
  11. } # }}}
  12. sub getsetup () { #{{{
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => undef,
  17. },
  18. } #}}}
  19. sub preprocess (@) { #{{{
  20. my %params=@_;
  21. my $fill;
  22. if (defined $params{percent}) {
  23. $fill = $params{percent};
  24. ($fill) = $fill =~ m/($percentage_pattern)/; # fill is untainted now
  25. }
  26. elsif (defined $params{totalpages} and defined $params{donepages}) {
  27. add_depends($params{page}, $params{totalpages});
  28. add_depends($params{page}, $params{donepages});
  29. my @pages=keys %pagesources;
  30. my $totalcount=0;
  31. my $donecount=0;
  32. foreach my $page (@pages) {
  33. $totalcount++ if pagespec_match($page, $params{totalpages}, location => $params{page});
  34. $donecount++ if pagespec_match($page, $params{donepages}, location => $params{page});
  35. }
  36. if ($totalcount == 0) {
  37. $fill = "100%";
  38. }
  39. else {
  40. my $number = $donecount/$totalcount*100;
  41. $fill = sprintf("%u%%", $number);
  42. }
  43. }
  44. else {
  45. error("need either `percent` or `totalpages` and `donepages` parameters");
  46. }
  47. return <<EODIV
  48. <div class="progress">
  49. <div class="progress-done" style="width: $fill">$fill</div>
  50. </div>
  51. EODIV
  52. } # }}}
  53. sub format(@) { #{{{
  54. my %params = @_;
  55. # If HTMLScrubber has removed the style attribute, then bring it back
  56. $params{content} =~ s!<div class="progress-done">($percentage_pattern)</div>!<div class="progress-done" style="width: $1">$1</div>!g;
  57. return $params{content};
  58. } #}}}
  59. 1