summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/progress.pm
blob: d27df5ca894b3aacf77fb441901fb5e47cf359b9 (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. section => "widget",
  18. },
  19. }
  20. sub preprocess (@) {
  21. my %params=@_;
  22. my $fill;
  23. if (defined $params{percent}) {
  24. $fill = $params{percent};
  25. ($fill) = $fill =~ m/($percentage_pattern)/; # fill is untainted now
  26. $fill=~s/%$//;
  27. if (! defined $fill || ! length $fill || $fill > 100 || $fill < 0) {
  28. error(sprintf(gettext("illegal percent value %s"), $params{percent}));
  29. }
  30. $fill.="%";
  31. }
  32. elsif (defined $params{totalpages} and defined $params{donepages}) {
  33. my $totalcount=pagespec_match_list(
  34. $params{page}, $params{totalpages},
  35. deptype => deptype("presence"));
  36. my $donecount=pagespec_match_list(
  37. $params{page}, $params{donepages},
  38. deptype => deptype("presence"));
  39. if ($totalcount == 0) {
  40. $fill = "100%";
  41. }
  42. else {
  43. my $number = $donecount/$totalcount*100;
  44. $fill = sprintf("%u%%", $number);
  45. }
  46. }
  47. else {
  48. error(gettext("need either `percent` or `totalpages` and `donepages` parameters"));
  49. }
  50. return <<EODIV
  51. <div class="progress">
  52. <div class="progress-done" style="width: $fill">$fill</div>
  53. </div>
  54. EODIV
  55. }
  56. sub format(@) {
  57. my %params = @_;
  58. # If HTMLScrubber has removed the style attribute, then bring it back
  59. $params{content} =~ s!<div class="progress-done">($percentage_pattern)</div>!<div class="progress-done" style="width: $1">$1</div>!g;
  60. return $params{content};
  61. }
  62. 1