summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/progress.pm
blob: fe64b40b1da13fe9b21936e2f5098c7bcc225c79 (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. my $totalcount=pagespec_match_list(
  33. $params{page}, $params{totalpages},
  34. deptype => deptype("presence"));
  35. my $donecount=pagespec_match_list(
  36. $params{page}, $params{donepages},
  37. deptype => deptype("presence"));
  38. if ($totalcount == 0) {
  39. $fill = "100%";
  40. }
  41. else {
  42. my $number = $donecount/$totalcount*100;
  43. $fill = sprintf("%u%%", $number);
  44. }
  45. }
  46. else {
  47. error(gettext("need either `percent` or `totalpages` and `donepages` parameters"));
  48. }
  49. return <<EODIV
  50. <div class="progress">
  51. <div class="progress-done" style="width: $fill">$fill</div>
  52. </div>
  53. EODIV
  54. }
  55. sub format(@) {
  56. my %params = @_;
  57. # If HTMLScrubber has removed the style attribute, then bring it back
  58. $params{content} =~ s!<div class="progress-done">($percentage_pattern)</div>!<div class="progress-done" style="width: $1">$1</div>!g;
  59. return $params{content};
  60. }
  61. 1