summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/teximg.pm
blob: 521af499fc77323870951883b49736509081cd8f (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 Encode;
  11. use IkiWiki 3.00;
  12. my $default_prefix = <<EOPREFIX;
  13. \\documentclass{article}
  14. \\usepackage{amsmath}
  15. \\usepackage{amsfonts}
  16. \\usepackage{amssymb}
  17. \\pagestyle{empty}
  18. \\begin{document}
  19. EOPREFIX
  20. my $default_postfix = '\\end{document}';
  21. sub import {
  22. hook(type => "getsetup", id => "teximg", call => \&getsetup);
  23. hook(type => "preprocess", id => "teximg", call => \&preprocess);
  24. }
  25. sub getsetup () {
  26. return
  27. plugin => {
  28. safe => 1,
  29. rebuild => undef,
  30. section => "widget",
  31. },
  32. teximg_dvipng => {
  33. type => "boolean",
  34. description => "Should teximg use dvipng to render, or dvips and convert?",
  35. safe => 0,
  36. rebuild => undef,
  37. },
  38. teximg_prefix => {
  39. type => "string",
  40. example => $default_prefix,
  41. description => "LaTeX prefix for teximg plugin",
  42. safe => 0, # Not sure how secure LaTeX is...
  43. rebuild => 1,
  44. },
  45. teximg_postfix => {
  46. type => "string",
  47. example => $default_postfix,
  48. description => "LaTeX postfix for teximg plugin",
  49. safe => 0, # Not sure how secure LaTeX is...
  50. rebuild => 1,
  51. },
  52. }
  53. sub preprocess (@) {
  54. my %params = @_;
  55. my $height = $params{height};
  56. if (! defined $height || ! length $height) {
  57. $height = 12;
  58. }
  59. else {
  60. $height =~ s#(\d+)#$1#;
  61. }
  62. my $code = $params{code};
  63. if (! defined $code && ! length $code) {
  64. error gettext("missing tex code");
  65. }
  66. return create($code, check_height($height), \%params);
  67. }
  68. sub check_height ($) {
  69. # Since latex doesn't support unlimited scaling this function
  70. # returns the closest supported size.
  71. my $height =shift;
  72. my @allowed=(8,9,10,11,12,14,17,20);
  73. my $ret;
  74. my $fit;
  75. foreach my $val (@allowed) {
  76. my $f = abs($val - $height);
  77. if (! defined($fit) || $f < $fit ) {
  78. $ret=$val;
  79. $fit=$f;
  80. }
  81. }
  82. return $ret;
  83. }
  84. sub create ($$$) {
  85. # This function calls the image generating function and returns
  86. # the <img .. /> for the generated image.
  87. my $code = shift;
  88. my $height = shift;
  89. my $params = shift;
  90. if (! defined($height) and not length($height) ) {
  91. $height = 12;
  92. }
  93. my $digest = md5_hex(Encode::encode_utf8($code), $height);
  94. my $imglink= $params->{page} . "/$digest.png";
  95. my $imglog = $params->{page} . "/$digest.log";
  96. will_render($params->{page}, $imglink);
  97. will_render($params->{page}, $imglog);
  98. my $imgurl=urlto($imglink, $params->{destpage});
  99. my $logurl=urlto($imglog, $params->{destpage});
  100. if (-e "$config{destdir}/$imglink" ||
  101. gen_image($code, $height, $digest, $params->{page})) {
  102. return qq{<img src="$imgurl" alt="}
  103. .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
  104. .qq{" class="teximg" />};
  105. }
  106. else {
  107. error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
  108. }
  109. }
  110. sub gen_image ($$$$) {
  111. # Actually creates the image.
  112. my $code = shift;
  113. my $height = shift;
  114. my $digest = shift;
  115. my $imagedir = shift;
  116. if (!defined $config{teximg_prefix}) {
  117. $config{teximg_prefix} = $default_prefix;
  118. }
  119. if (!defined $config{teximg_postfix}) {
  120. $config{teximg_postfix} = $default_postfix;
  121. }
  122. if (!defined $config{teximg_dvipng}) {
  123. $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
  124. }
  125. my $tex = $config{teximg_prefix};
  126. $tex .= '$$'.$code.'$$';
  127. $tex .= $config{teximg_postfix};
  128. $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
  129. $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
  130. my $tmp = eval { create_tmp_dir($digest) };
  131. if (! $@ &&
  132. writefile("$digest.tex", $tmp, $tex) &&
  133. system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
  134. # ensure destination directory exists
  135. writefile("$imagedir/$digest.png", $config{destdir}, "") &&
  136. (($config{teximg_dvipng} &&
  137. system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
  138. ) || (!$config{teximg_dvipng} &&
  139. system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
  140. system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
  141. ))) {
  142. return 1;
  143. }
  144. else {
  145. # store failure log
  146. my $log="";
  147. {
  148. if (open(my $f, '<', "$tmp/$digest.log")) {
  149. local $/=undef;
  150. $log = <$f>;
  151. close($f);
  152. }
  153. }
  154. writefile("$digest.log", "$config{destdir}/$imagedir", $log);
  155. return 0;
  156. }
  157. }
  158. sub create_tmp_dir ($) {
  159. # Create a temp directory, it will be removed when ikiwiki exits.
  160. my $base = shift;
  161. my $template = $base.".XXXXXXXXXX";
  162. my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
  163. return $tmpdir;
  164. }
  165. 1