summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/teximg.pm
blob: 5dff5feef30913bd598da7e02ab7b2f42b5cfa2b (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 $imglink= $params->{page} . "/$digest.png";
  61. my $imglog = $params->{page} . "/$digest.log";
  62. will_render($params->{destpage}, $imglink);
  63. will_render($params->{destpage}, $imglog);
  64. my $imgurl;
  65. my $logurl;
  66. if (! $params->{preview}) {
  67. $imgurl = urlto($imglink, $params->{destpage});
  68. $logurl = urlto($imglog, $params->{destpage});
  69. }
  70. else {
  71. $imgurl=$params->{page}."/$digest.png";
  72. $logurl=$params->{page}."/$digest.log";
  73. }
  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. return qq{[[teximg <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. open(my $f, '<', "$tmp/$digest.log");
  115. local $/=undef;
  116. $log = <$f>;
  117. close($f);
  118. }
  119. writefile("$digest.log", "$config{destdir}/$imagedir", $log);
  120. return 0;
  121. }
  122. } #}}}
  123. sub create_tmp_dir ($) { #{{{
  124. # Create a temp directory, it will be removed when ikiwiki exits.
  125. my $base = shift;
  126. my $template = $base.".XXXXXXXXXX";
  127. my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
  128. return $tmpdir;
  129. } #}}}
  130. sub check ($) { #{{{
  131. # Check if the code is ok
  132. my $code = shift;
  133. my @badthings = (
  134. qr/\$\$/,
  135. qr/\\include/,
  136. qr/\\includegraphic/,
  137. qr/\\usepackage/,
  138. qr/\\newcommand/,
  139. qr/\\renewcommand/,
  140. qr/\\def/,
  141. qr/\\input/,
  142. qr/\\open/,
  143. qr/\\loop/,
  144. qr/\\errorstopmode/,
  145. qr/\\scrollmode/,
  146. qr/\\batchmode/,
  147. qr/\\read/,
  148. qr/\\write/,
  149. );
  150. foreach my $thing (@badthings) {
  151. if ($code =~ m/$thing/ ) {
  152. return 0;
  153. }
  154. }
  155. return 1;
  156. } #}}}
  157. 1