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