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);
14 my $default_prefix = <<EOPREFIX;
15 \\documentclass{article}
16 \\usepackage[utf8]{inputenc}
18 \\usepackage{amsfonts}
24 my $default_postfix = '\\end{document}';
27 hook(type => "getsetup", id => "teximg", call => \&getsetup);
28 hook(type => "preprocess", id => "teximg", call => \&preprocess);
40 description => "Should teximg use dvipng to render, or dvips and convert?",
46 example => $default_prefix,
47 description => "LaTeX prefix for teximg plugin",
48 safe => 0, # Not sure how secure LaTeX is...
53 example => $default_postfix,
54 description => "LaTeX postfix for teximg plugin",
55 safe => 0, # Not sure how secure LaTeX is...
63 my $height = $params{height};
64 if (! defined $height || ! length $height) {
68 $height =~ s#(\d+)#$1#;
71 my $code = $params{code};
72 if (! defined $code && ! length $code) {
73 error gettext("missing tex code");
75 return create($code, check_height($height), \%params);
78 sub check_height ($) {
79 # Since latex doesn't support unlimited scaling this function
80 # returns the closest supported size.
83 my @allowed=(8,9,10,11,12,14,17,20);
87 foreach my $val (@allowed) {
88 my $f = abs($val - $height);
89 if (! defined($fit) || $f < $fit ) {
98 # This function calls the image generating function and returns
99 # the <img .. /> for the generated image.
104 if (! defined($height) and not length($height) ) {
108 my $digest = md5_hex(Encode::encode_utf8($code), $height);
110 my $imglink= $params->{page} . "/$digest.png";
111 my $imglog = $params->{page} . "/$digest.log";
112 will_render($params->{page}, $imglink);
113 will_render($params->{page}, $imglog);
115 my $imgurl=urlto($imglink, $params->{destpage});
116 my $logurl=urlto($imglog, $params->{destpage});
118 if (-e "$config{destdir}/$imglink" ||
119 gen_image($code, $height, $digest, $params->{page})) {
120 return qq{<img src="$imgurl" alt="}
121 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
122 .qq{" class="teximg" />};
125 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
129 sub gen_image ($$$$) {
130 # Actually creates the image.
134 my $imagedir = shift;
136 if (!defined $config{teximg_prefix}) {
137 $config{teximg_prefix} = $default_prefix;
139 if (!defined $config{teximg_postfix}) {
140 $config{teximg_postfix} = $default_postfix;
142 if (!defined $config{teximg_dvipng}) {
143 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
146 my $tex = $config{teximg_prefix};
147 $tex .= '\['.$code.'\]';
148 $tex .= $config{teximg_postfix};
149 $tex =~ s!\\documentclass\{article}!\\documentclass[${height}pt]{article}!g;
150 $tex =~ s!\\documentclass\{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
152 my $tmp = eval { create_tmp_dir($digest) };
154 writefile("$digest.tex", $tmp, $tex) &&
155 system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
156 # ensure destination directory exists
157 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
158 (($config{teximg_dvipng} &&
159 system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
160 ) || (!$config{teximg_dvipng} &&
161 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
162 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
170 if (open(my $f, '<', "$tmp/$digest.log")) {
176 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
182 sub create_tmp_dir ($) {
183 # Create a temp directory, it will be removed when ikiwiki exits.
186 my $template = $base.".XXXXXXXXXX";
187 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);