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