2 # Licensed under GPL v2 or greater
3 # (c) 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
5 package IkiWiki::Plugin::teximg;
8 use Digest::MD5 qw(md5_hex);
9 use File::Temp qw(tempdir);
13 my $default_prefix = <<EOPREFIX;
14 \\documentclass{article}
16 \\usepackage{amsfonts}
22 my $default_postfix = '\\end{document}';
25 hook(type => "getsetup", id => "teximg", call => \&getsetup);
26 hook(type => "preprocess", id => "teximg", call => \&preprocess);
37 description => "Should teximg use dvipng to render, or dvips and convert?",
43 example => $default_prefix,
44 description => "LaTeX prefix for teximg plugin",
45 safe => 0, # Not sure how secure LaTeX is...
50 example => $default_postfix,
51 description => "LaTeX postfix for teximg plugin",
52 safe => 0, # Not sure how secure LaTeX is...
60 my $height = $params{height};
61 if (! defined $height || ! length $height) {
65 $height =~ s#(\d+)#$1#;
68 my $code = $params{code};
69 if (! defined $code && ! length $code) {
70 error gettext("missing tex code");
72 return create($code, check_height($height), \%params);
75 sub check_height ($) {
76 # Since latex doesn't support unlimited scaling this function
77 # returns the closest supported size.
80 my @allowed=(8,9,10,11,12,14,17,20);
84 foreach my $val (@allowed) {
85 my $f = abs($val - $height);
86 if (! defined($fit) || $f < $fit ) {
95 # This function calls the image generating function and returns
96 # the <img .. /> for the generated image.
101 if (! defined($height) and not length($height) ) {
105 my $digest = md5_hex($code, $height);
107 my $imglink= $params->{page} . "/$digest.png";
108 my $imglog = $params->{page} . "/$digest.log";
109 will_render($params->{page}, $imglink);
110 will_render($params->{page}, $imglog);
112 my $imgurl=urlto($imglink, $params->{destpage});
113 my $logurl=urlto($imglog, $params->{destpage});
115 if (-e "$config{destdir}/$imglink" ||
116 gen_image($code, $height, $digest, $params->{page})) {
117 return qq{<img src="$imgurl" alt="}
118 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
119 .qq{" class="teximg" />};
122 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
126 sub gen_image ($$$$) {
127 # Actually creates the image.
131 my $imagedir = shift;
133 if (!defined $config{teximg_prefix}) {
134 $config{teximg_prefix} = $default_prefix;
136 if (!defined $config{teximg_postfix}) {
137 $config{teximg_postfix} = $default_postfix;
139 if (!defined $config{teximg_dvipng}) {
140 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
143 my $tex = $config{teximg_prefix};
144 $tex .= '$$'.$code.'$$';
145 $tex .= $config{teximg_postfix};
146 $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
147 $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
149 my $tmp = eval { create_tmp_dir($digest) };
151 writefile("$digest.tex", $tmp, $tex) &&
152 system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
153 # ensure destination directory exists
154 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
155 (($config{teximg_dvipng} &&
156 system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
157 ) || (!$config{teximg_dvipng} &&
158 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
159 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
167 if (open(my $f, '<', "$tmp/$digest.log")) {
173 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
179 sub create_tmp_dir ($) {
180 # Create a temp directory, it will be removed when ikiwiki exits.
183 my $template = $base.".XXXXXXXXXX";
184 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);