summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/sparkline.pm
blob: fb4849492eb0e20aad9045bae2d51902885abfb5 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::sparkline;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. use IPC::Open2;
  7. my $match_num=qr/[-+]?[0-9]+(?:\.[0-9]+)?/;
  8. my %locmap=(
  9. top => 'TEXT_TOP',
  10. right => 'TEXT_RIGHT',
  11. bottom => 'TEXT_BOTTOM',
  12. left => 'TEXT_LEFT',
  13. );
  14. sub import {
  15. hook(type => "getsetup", id => "sparkline", call => \&getsetup);
  16. hook(type => "preprocess", id => "sparkline", call => \&preprocess);
  17. }
  18. sub getsetup () {
  19. return
  20. plugin => {
  21. safe => 1,
  22. rebuild => undef,
  23. },
  24. }
  25. sub preprocess (@) {
  26. my %params=@_;
  27. my $php;
  28. my $style=(exists $params{style} && $params{style} eq "bar") ? "Bar" : "Line";
  29. $php=qq{<?php
  30. require_once('sparkline/Sparkline_$style.php');
  31. \$sparkline = new Sparkline_$style();
  32. \$sparkline->SetDebugLevel(DEBUG_NONE);
  33. };
  34. foreach my $param (qw{BarWidth BarSpacing YMin YMaz}) {
  35. if (exists $params{lc($param)}) {
  36. $php.=qq{\$sparkline->Set$param(}.int($params{lc($param)}).qq{);\n};
  37. }
  38. }
  39. my $c=0;
  40. while (@_) {
  41. my $key=shift;
  42. my $value=shift;
  43. if ($key=~/^($match_num)(?:,($match_num))?(?:\(([a-z]+)\))?$/) {
  44. $c++;
  45. my ($x, $y);
  46. if (defined $2) {
  47. $x=$1;
  48. $y=$2;
  49. }
  50. else {
  51. $x=$c;
  52. $y=$1;
  53. }
  54. if ($style eq "Bar" && defined $3) {
  55. $php.=qq{\$sparkline->SetData($x, $y, '$3');\n};
  56. }
  57. else {
  58. $php.=qq{\$sparkline->SetData($x, $y);\n};
  59. }
  60. }
  61. elsif (! length $value) {
  62. error gettext("parse error")." \"$key\"";
  63. }
  64. elsif ($key eq 'featurepoint') {
  65. my ($x, $y, $color, $diameter, $text, $location)=
  66. split(/\s*,\s*/, $value);
  67. if (! defined $diameter || $diameter < 0) {
  68. error gettext("invalid featurepoint diameter");
  69. }
  70. $x=int($x);
  71. $y=int($y);
  72. $color=~s/[^a-z]+//g;
  73. $diameter=int($diameter);
  74. $text=~s/[^-a-zA-Z0-9]+//g if defined $text;
  75. if (defined $location) {
  76. $location=$locmap{$location};
  77. if (! defined $location) {
  78. error gettext("invalid featurepoint location");
  79. }
  80. }
  81. $php.=qq{\$sparkline->SetFeaturePoint($x, $y, '$color', $diameter};
  82. $php.=qq{, '$text'} if defined $text;
  83. $php.=qq{, $location} if defined $location;
  84. $php.=qq{);\n};
  85. }
  86. }
  87. if ($c eq 0) {
  88. error gettext("missing values");
  89. }
  90. my $height=int($params{height} || 20);
  91. if ($height < 2 || $height > 100) {
  92. error gettext("invalid height value");
  93. }
  94. if ($style eq "Bar") {
  95. $php.=qq{\$sparkline->Render($height);\n};
  96. }
  97. else {
  98. if (! exists $params{width}) {
  99. error gettext("missing width parameter");
  100. }
  101. my $width=int($params{width});
  102. if ($width < 2 || $width > 1024) {
  103. error gettext("invalid width value");
  104. }
  105. $php.=qq{\$sparkline->RenderResampled($width, $height);\n};
  106. }
  107. $php.=qq{\$sparkline->Output();\n?>\n};
  108. # Use the sha1 of the php code that generates the sparkline as
  109. # the base for its filename.
  110. eval q{use Digest::SHA1};
  111. error($@) if $@;
  112. my $fn=$params{page}."/sparkline-".
  113. IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($php)).
  114. ".png";
  115. will_render($params{page}, $fn);
  116. if (! -e "$config{destdir}/$fn") {
  117. my $pid;
  118. my $sigpipe=0;
  119. $SIG{PIPE}=sub { $sigpipe=1 };
  120. $pid=open2(*IN, *OUT, "php");
  121. # open2 doesn't respect "use open ':utf8'"
  122. binmode (OUT, ':utf8');
  123. print OUT $php;
  124. close OUT;
  125. my $png;
  126. {
  127. local $/=undef;
  128. $png=<IN>;
  129. }
  130. close IN;
  131. waitpid $pid, 0;
  132. $SIG{PIPE}="DEFAULT";
  133. if ($sigpipe || ! defined $png) {
  134. error gettext("failed to run php");
  135. }
  136. if (! $params{preview}) {
  137. writefile($fn, $config{destdir}, $png, 1);
  138. }
  139. else {
  140. # can't write the file, so embed it in a data uri
  141. eval q{use MIME::Base64};
  142. error($@) if $@;
  143. return "<img src=\"data:image/png;base64,".
  144. encode_base64($png)."\" />";
  145. }
  146. }
  147. return '<img src="'.urlto($fn, $params{destpage}).'" alt="graph" />';
  148. }
  149. 1