summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/progress.pm
blob: 75da16d16de80e72b14b76dc2a11d63fe84c88f3 (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. $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});
  33. add_depends($params{page}, $params{donepages});
  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