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