summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/teximg.pm
blob: fc890666ae32e8c548bdff2fe40270852b06563c (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 2.00;
  11. sub import { #{{{
  12. hook(type => "preprocess", id => "teximg", call => \&preprocess);
  13. } #}}}
  14. sub preprocess (@) { #{{{
  15. my %params = @_;
  16. my $height = $params{height};
  17. if (! defined $height || ! length $height) {
  18. $height = 12;
  19. }
  20. else {
  21. $height =~ s#(\d+)#$1#;
  22. }
  23. my $code = $params{code};
  24. if (! defined $code && ! length $code) {
  25. return "[[teximg ".gettext("missing tex code"). "]]";
  26. }
  27. if (check($code)) {
  28. return create($code, check_height($height), \%params);
  29. }
  30. else {
  31. return "[[teximg ".gettext("code includes disallowed latex commands"). "]]";
  32. }
  33. } #}}}
  34. sub check_height ($) { #{{{
  35. # Since latex doesn't support unlimited scaling this function
  36. # returns the closest supported size.
  37. my $height =shift;
  38. my @allowed=(8,9,10,11,12,14,17,20);
  39. my $ret;
  40. my $fit;
  41. foreach my $val (@allowed) {
  42. my $f = abs($val - $height);
  43. if (! defined($fit) || $f < $fit ) {
  44. $ret=$val;
  45. $fit=$f;
  46. }
  47. }
  48. return $ret;
  49. } #}}}
  50. sub create ($$$) { #{{{
  51. # This function calls the image generating function and returns
  52. # the <img .. /> for the generated image.
  53. my $code = shift;
  54. my $height = shift;
  55. my $params = shift;
  56. if (! defined($height) and not length($height) ) {
  57. $height = 12;
  58. }
  59. my $digest = md5_hex($code, $height);
  60. my $teximgdir = "/teximg";
  61. my $imglink = "$teximgdir/$digest.png";
  62. my $imglog = "$teximgdir/$digest.log";
  63. will_render($params->{destpage}, $imglink);
  64. will_render($params->{destpage}, $imglog);
  65. my $imgurl;
  66. my $logurl;
  67. if (! $params->{preview}) {
  68. $imgurl = urlto($imglink, $params->{destpage});
  69. $logurl = urlto($imglog, $params->{destpage});
  70. }
  71. else {
  72. $imgurl="$config{url}/$teximgdir/$digest.png";
  73. $logurl="$config{url}/$teximgdir/$digest.log";
  74. }
  75. if (-e "$config{destdir}/$imglink" ||
  76. gen_image($code, $height, $digest, $teximgdir)) {
  77. return qq{<img src="$imgurl" alt="}
  78. .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
  79. .qq{" class="teximg" />};
  80. }
  81. else {
  82. return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
  83. }
  84. } #}}}
  85. sub gen_image ($$$$) { #{{{
  86. # Actually creates the image.
  87. my $code = shift;
  88. my $height = shift;
  89. my $digest = shift;
  90. my $imagedir = shift;
  91. #TODO This should move into the setup file.
  92. my $tex = '\documentclass['.$height.'pt]{scrartcl}';
  93. $tex .= '\usepackage[version=3]{mhchem}';
  94. $tex .= '\usepackage{amsmath}';
  95. $tex .= '\usepackage{amsfonts}';
  96. $tex .= '\usepackage{amssymb}';
  97. $tex .= '\pagestyle{empty}';
  98. $tex .= '\begin{document}';
  99. $tex .= '$$'.$code.'$$';
  100. $tex .= '\end{document}';
  101. my $tmp = eval { create_tmp_dir($digest) };
  102. if (! $@ &&
  103. writefile("$digest.tex", $tmp, $tex) &&
  104. system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
  105. system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
  106. # ensure destination directory exists
  107. writefile("$imagedir/$digest.png", $config{destdir}, "") &&
  108. system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
  109. return 1;
  110. }
  111. else {
  112. # store failure log
  113. my $log;
  114. {
  115. open(my $f, '<', "$tmp/$digest.log");
  116. local $/=undef;
  117. $log = <$f>;
  118. close($f);
  119. }
  120. writefile("$digest.log", "$config{destdir}/$imagedir", $log);
  121. return 0;
  122. }
  123. } #}}}
  124. sub create_tmp_dir ($) { #{{{
  125. # Create a temp directory, it will be removed when ikiwiki exits.
  126. my $base = shift;
  127. my $template = $base.".XXXXXXXXXX";
  128. my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
  129. return $tmpdir;
  130. } #}}}
  131. sub check ($) { #{{{
  132. # Check if the code is ok
  133. my $code = shift;
  134. my @badthings = (
  135. qr/\$\$/,
  136. qr/\\include/,
  137. qr/\\includegraphic/,
  138. qr/\\usepackage/,
  139. qr/\\newcommand/,
  140. qr/\\renewcommand/,
  141. qr/\\def/,
  142. qr/\\input/,
  143. qr/\\open/,
  144. qr/\\loop/,
  145. qr/\\errorstopmode/,
  146. qr/\\scrollmode/,
  147. qr/\\batchmode/,
  148. qr/\\read/,
  149. qr/\\write/,
  150. );
  151. foreach my $thing (@badthings) {
  152. if ($code =~ m/$thing/ ) {
  153. return 0;
  154. }
  155. }
  156. return 1;
  157. } #}}}
  158. 1