summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/teximg.pm
blob: dba5372b5fee98c33cf044e3b28024c53033649a (plain)
  1. #!/usr/bin/perl
  2. # Licensed under GPL v2 or greater
  3. # (c) 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
  4. package IkiWiki::Plugin::teximg;
  5. use warnings;
  6. use strict;
  7. use Digest::MD5 qw(md5_hex);
  8. use File::Temp qw(tempdir);
  9. use HTML::Entities;
  10. use IkiWiki 3.00;
  11. my $default_prefix = <<EOPREFIX;
  12. \\documentclass{article}
  13. \\usepackage{amsmath}
  14. \\usepackage{amsfonts}
  15. \\usepackage{amssymb}
  16. \\pagestyle{empty}
  17. \\begin{document}
  18. EOPREFIX
  19. my $default_postfix = '\\end{document}';
  20. sub import {
  21. hook(type => "getsetup", id => "teximg", call => \&getsetup);
  22. hook(type => "preprocess", id => "teximg", call => \&preprocess);
  23. }
  24. sub getsetup () {
  25. return
  26. plugin => {
  27. safe => 1,
  28. rebuild => undef,
  29. },
  30. teximg_dvipng => {
  31. type => "boolean",
  32. description => "Should teximg use dvipng to render, or dvips and convert?",
  33. safe => 0,
  34. rebuild => undef,
  35. },
  36. teximg_prefix => {
  37. type => "string",
  38. example => $default_prefix,
  39. description => "LaTeX prefix for teximg plugin",
  40. safe => 0, # Not sure how secure LaTeX is...
  41. rebuild => 1,
  42. },
  43. teximg_postfix => {
  44. type => "string",
  45. example => $default_postfix,
  46. description => "LaTeX postfix for teximg plugin",
  47. safe => 0, # Not sure how secure LaTeX is...
  48. rebuild => 1,
  49. },
  50. }
  51. sub preprocess (@) {
  52. my %params = @_;
  53. my $height = $params{height};
  54. if (! defined $height || ! length $height) {
  55. $height = 12;
  56. }
  57. else {
  58. $height =~ s#(\d+)#$1#;
  59. }
  60. my $code = $params{code};
  61. if (! defined $code && ! length $code) {
  62. error gettext("missing tex code");
  63. }
  64. if (check($code)) {
  65. return create($code, check_height($height), \%params);
  66. }
  67. else {
  68. error gettext("code includes disallowed latex commands")
  69. }
  70. }
  71. sub check_height ($) {
  72. # Since latex doesn't support unlimited scaling this function
  73. # returns the closest supported size.
  74. my $height =shift;
  75. my @allowed=(8,9,10,11,12,14,17,20);
  76. my $ret;
  77. my $fit;
  78. foreach my $val (@allowed) {
  79. my $f = abs($val - $height);
  80. if (! defined($fit) || $f < $fit ) {
  81. $ret=$val;
  82. $fit=$f;
  83. }
  84. }
  85. return $ret;
  86. }
  87. sub create ($$$) {
  88. # This function calls the image generating function and returns
  89. # the <img .. /> for the generated image.
  90. my $code = shift;
  91. my $height = shift;
  92. my $params = shift;
  93. if (! defined($height) and not length($height) ) {
  94. $height = 12;
  95. }
  96. my $digest = md5_hex($code, $height);
  97. my $imglink= $params->{page} . "/$digest.png";
  98. my $imglog = $params->{page} . "/$digest.log";
  99. will_render($params->{page}, $imglink);
  100. will_render($params->{page}, $imglog);
  101. my $imgurl=urlto($imglink, $params->{destpage});
  102. my $logurl=urlto($imglog, $params->{destpage});
  103. if (-e "$config{destdir}/$imglink" ||
  104. gen_image($code, $height, $digest, $params->{page})) {
  105. return qq{<img src="$imgurl" alt="}
  106. .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
  107. .qq{" class="teximg" />};
  108. }
  109. else {
  110. error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
  111. }
  112. }
  113. sub gen_image ($$$$) {
  114. # Actually creates the image.
  115. my $code = shift;
  116. my $height = shift;
  117. my $digest = shift;
  118. my $imagedir = shift;
  119. if (!defined $config{teximg_prefix}) {
  120. $config{teximg_prefix} = $default_prefix;
  121. }
  122. if (!defined $config{teximg_postfix}) {
  123. $config{teximg_postfix} = $default_postfix;
  124. }
  125. if (!defined $config{teximg_dvipng}) {
  126. $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
  127. }
  128. my $tex = $config{teximg_prefix};
  129. $tex .= '$$'.$code.'$$';
  130. $tex .= $config{teximg_postfix};
  131. $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
  132. $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
  133. my $tmp = eval { create_tmp_dir($digest) };
  134. if (! $@ &&
  135. writefile("$digest.tex", $tmp, $tex) &&
  136. system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
  137. # ensure destination directory exists
  138. writefile("$imagedir/$digest.png", $config{destdir}, "") &&
  139. (($config{teximg_dvipng} &&
  140. system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
  141. ) || (!$config{teximg_dvipng} &&
  142. system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
  143. system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
  144. ))) {
  145. return 1;
  146. }
  147. else {
  148. # store failure log
  149. my $log="";
  150. {
  151. if (open(my $f, '<', "$tmp/$digest.log")) {
  152. local $/=undef;
  153. $log = <$f>;
  154. close($f);
  155. }
  156. }
  157. writefile("$digest.log", "$config{destdir}/$imagedir", $log);
  158. return 0;
  159. }
  160. }
  161. sub create_tmp_dir ($) {
  162. # Create a temp directory, it will be removed when ikiwiki exits.
  163. my $base = shift;
  164. my $template = $base.".XXXXXXXXXX";
  165. my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
  166. return $tmpdir;
  167. }
  168. sub check ($) {
  169. # Check if the code is ok
  170. my $code = shift;
  171. my @badthings = (
  172. qr/\$\$/,
  173. qr/\\include/,
  174. qr/\\includegraphic/,
  175. qr/\\usepackage/,
  176. qr/\\newcommand/,
  177. qr/\\renewcommand/,
  178. qr/\\def/,
  179. qr/\\input/,
  180. qr/\\open/,
  181. qr/\\loop/,
  182. qr/\\errorstopmode/,
  183. qr/\\scrollmode/,
  184. qr/\\batchmode/,
  185. qr/\\read/,
  186. qr/\\write/,
  187. );
  188. foreach my $thing (@badthings) {
  189. if ($code =~ m/$thing/ ) {
  190. return 0;
  191. }
  192. }
  193. return 1;
  194. }
  195. 1