summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/progress.pm
blob: 26c537a84022ae51001beb9be86db90b1da25e0a (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::progress;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.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. $fill=~s/%$//;
  26. if (! defined $fill || ! length $fill || $fill > 100 || $fill < 0) {
  27. error(sprintf(gettext("illegal percent value %s"), $params{percent}));
  28. }
  29. $fill.="%";
  30. }
  31. elsif (defined $params{totalpages} and defined $params{donepages}) {
  32. add_depends($params{page}, $params{totalpages}, presence => 1);
  33. add_depends($params{page}, $params{donepages}, presence => 1);
  34. my @pages=keys %pagesources;
  35. my $totalcount=0;
  36. my $donecount=0;
  37. foreach my $page (@pages) {
  38. $totalcount++ if pagespec_match($page, $params{totalpages}, location => $params{page});
  39. $donecount++ if pagespec_match($page, $params{donepages}, location => $params{page});
  40. }
  41. if ($totalcount == 0) {
  42. $fill = "100%";
  43. }
  44. else {
  45. my $number = $donecount/$totalcount*100;
  46. $fill = sprintf("%u%%", $number);
  47. }
  48. }
  49. else {
  50. error(gettext("need either `percent` or `totalpages` and `donepages` parameters"));
  51. }
  52. return <<EODIV
  53. <div class="progress">
  54. <div class="progress-done" style="width: $fill">$fill</div>
  55. </div>
  56. EODIV
  57. }
  58. sub format(@) {
  59. my %params = @_;
  60. # If HTMLScrubber has removed the style attribute, then bring it back
  61. $params{content} =~ s!<div class="progress-done">($percentage_pattern)</div>!<div class="progress-done" style="width: $1">$1</div>!g;
  62. return $params{content};
  63. }
  64. 1