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