summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/teximg.pm
blob: 369c1088203cb2ec86e397bd46e9a98e1a4e544d (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 => "getsetup", id => "teximg", call => \&getsetup);
  13. hook(type => "preprocess", id => "teximg", call => \&preprocess);
  14. } #}}}
  15. sub getsetup () { #{{{
  16. return
  17. plugin => {
  18. safe => 1,
  19. rebuild => undef,
  20. },
  21. } #}}}
  22. sub preprocess (@) { #{{{
  23. my %params = @_;
  24. my $height = $params{height};
  25. if (! defined $height || ! length $height) {
  26. $height = 12;
  27. }
  28. else {
  29. $height =~ s#(\d+)#$1#;
  30. }
  31. my $code = $params{code};
  32. if (! defined $code && ! length $code) {
  33. error gettext("missing tex code");
  34. }
  35. if (check($code)) {
  36. return create($code, check_height($height), \%params);
  37. }
  38. else {
  39. error gettext("code includes disallowed latex commands")
  40. }
  41. } #}}}
  42. sub check_height ($) { #{{{
  43. # Since latex doesn't support unlimited scaling this function
  44. # returns the closest supported size.
  45. my $height =shift;
  46. my @allowed=(8,9,10,11,12,14,17,20);
  47. my $ret;
  48. my $fit;
  49. foreach my $val (@allowed) {
  50. my $f = abs($val - $height);
  51. if (! defined($fit) || $f < $fit ) {
  52. $ret=$val;
  53. $fit=$f;
  54. }
  55. }
  56. return $ret;
  57. } #}}}
  58. sub create ($$$) { #{{{
  59. # This function calls the image generating function and returns
  60. # the <img .. /> for the generated image.
  61. my $code = shift;
  62. my $height = shift;
  63. my $params = shift;
  64. if (! defined($height) and not length($height) ) {
  65. $height = 12;
  66. }
  67. my $digest = md5_hex($code, $height);
  68. my $imglink= $params->{page} . "/$digest.png";
  69. my $imglog = $params->{page} . "/$digest.log";
  70. will_render($params->{page}, $imglink);
  71. will_render($params->{page}, $imglog);
  72. my $imgurl=urlto($imglink, $params->{destpage});
  73. my $logurl=urlto($imglog, $params->{destpage});
  74. if (-e "$config{destdir}/$imglink" ||
  75. gen_image($code, $height, $digest, $params->{page})) {
  76. return qq{<img src="$imgurl" alt="}
  77. .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
  78. .qq{" class="teximg" />};
  79. }
  80. else {
  81. error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
  82. }
  83. } #}}}
  84. sub gen_image ($$$$) { #{{{
  85. # Actually creates the image.
  86. my $code = shift;
  87. my $height = shift;
  88. my $digest = shift;
  89. my $imagedir = shift;
  90. #TODO This should move into the setup file.
  91. my $tex = '\documentclass['.$height.'pt]{scrartcl}';
  92. $tex .= '\usepackage[version=3]{mhchem}';
  93. $tex .= '\usepackage{amsmath}';
  94. $tex .= '\usepackage{amsfonts}';
  95. $tex .= '\usepackage{amssymb}';
  96. $tex .= '\pagestyle{empty}';
  97. $tex .= '\begin{document}';
  98. $tex .= '$$'.$code.'$$';
  99. $tex .= '\end{document}';
  100. my $tmp = eval { create_tmp_dir($digest) };
  101. if (! $@ &&
  102. writefile("$digest.tex", $tmp, $tex) &&
  103. system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
  104. system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
  105. # ensure destination directory exists
  106. writefile("$imagedir/$digest.png", $config{destdir}, "") &&
  107. system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
  108. return 1;
  109. }
  110. else {
  111. # store failure log
  112. my $log="";
  113. {
  114. if (open(my $f, '<', "$tmp/$digest.log")) {
  115. local $/=undef;
  116. $log = <$f>;
  117. close($f);
  118. }
  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