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