summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/teximg.pm
blob: 8c3e88c6974a8f2d5aa00fc119ba1f8941f4c943 (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->{page}, $imglink);
  63. will_render($params->{page}, $imglog);
  64. my $imgurl=urlto($imglink, $params->{destpage});
  65. my $logurl=urlto($imglog, $params->{destpage});
  66. if (-e "$config{destdir}/$imglink" ||
  67. gen_image($code, $height, $digest, $params->{page})) {
  68. return qq{<img src="$imgurl" alt="}
  69. .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
  70. .qq{" class="teximg" />};
  71. }
  72. else {
  73. return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
  74. }
  75. } #}}}
  76. sub gen_image ($$$$) { #{{{
  77. # Actually creates the image.
  78. my $code = shift;
  79. my $height = shift;
  80. my $digest = shift;
  81. my $imagedir = shift;
  82. #TODO This should move into the setup file.
  83. my $tex = '\documentclass['.$height.'pt]{scrartcl}';
  84. $tex .= '\usepackage[version=3]{mhchem}';
  85. $tex .= '\usepackage{amsmath}';
  86. $tex .= '\usepackage{amsfonts}';
  87. $tex .= '\usepackage{amssymb}';
  88. $tex .= '\pagestyle{empty}';
  89. $tex .= '\begin{document}';
  90. $tex .= '$$'.$code.'$$';
  91. $tex .= '\end{document}';
  92. my $tmp = eval { create_tmp_dir($digest) };
  93. if (! $@ &&
  94. writefile("$digest.tex", $tmp, $tex) &&
  95. system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
  96. system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
  97. # ensure destination directory exists
  98. writefile("$imagedir/$digest.png", $config{destdir}, "") &&
  99. system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
  100. return 1;
  101. }
  102. else {
  103. # store failure log
  104. my $log="";
  105. {
  106. if (open(my $f, '<', "$tmp/$digest.log")) {
  107. local $/=undef;
  108. $log = <$f>;
  109. close($f);
  110. }
  111. }
  112. writefile("$digest.log", "$config{destdir}/$imagedir", $log);
  113. return 0;
  114. }
  115. } #}}}
  116. sub create_tmp_dir ($) { #{{{
  117. # Create a temp directory, it will be removed when ikiwiki exits.
  118. my $base = shift;
  119. my $template = $base.".XXXXXXXXXX";
  120. my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
  121. return $tmpdir;
  122. } #}}}
  123. sub check ($) { #{{{
  124. # Check if the code is ok
  125. my $code = shift;
  126. my @badthings = (
  127. qr/\$\$/,
  128. qr/\\include/,
  129. qr/\\includegraphic/,
  130. qr/\\usepackage/,
  131. qr/\\newcommand/,
  132. qr/\\renewcommand/,
  133. qr/\\def/,
  134. qr/\\input/,
  135. qr/\\open/,
  136. qr/\\loop/,
  137. qr/\\errorstopmode/,
  138. qr/\\scrollmode/,
  139. qr/\\batchmode/,
  140. qr/\\read/,
  141. qr/\\write/,
  142. );
  143. foreach my $thing (@badthings) {
  144. if ($code =~ m/$thing/ ) {
  145. return 0;
  146. }
  147. }
  148. return 1;
  149. } #}}}
  150. 1