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);
38 description => "Should teximg use dvipng to render, or dvips and convert?",
44 example => $default_prefix,
45 description => "LaTeX prefix for teximg plugin",
46 safe => 0, # Not sure how secure LaTeX is...
51 example => $default_postfix,
52 description => "LaTeX postfix for teximg plugin",
53 safe => 0, # Not sure how secure LaTeX is...
61 my $height = $params{height};
62 if (! defined $height || ! length $height) {
66 $height =~ s#(\d+)#$1#;
69 my $code = $params{code};
70 if (! defined $code && ! length $code) {
71 error gettext("missing tex code");
73 return create($code, check_height($height), \%params);
76 sub check_height ($) {
77 # Since latex doesn't support unlimited scaling this function
78 # returns the closest supported size.
81 my @allowed=(8,9,10,11,12,14,17,20);
85 foreach my $val (@allowed) {
86 my $f = abs($val - $height);
87 if (! defined($fit) || $f < $fit ) {
96 # This function calls the image generating function and returns
97 # the <img .. /> for the generated image.
102 if (! defined($height) and not length($height) ) {
106 my $digest = md5_hex($code, $height);
108 my $imglink= $params->{page} . "/$digest.png";
109 my $imglog = $params->{page} . "/$digest.log";
110 will_render($params->{page}, $imglink);
111 will_render($params->{page}, $imglog);
113 my $imgurl=urlto($imglink, $params->{destpage});
114 my $logurl=urlto($imglog, $params->{destpage});
116 if (-e "$config{destdir}/$imglink" ||
117 gen_image($code, $height, $digest, $params->{page})) {
118 return qq{<img src="$imgurl" alt="}
119 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
120 .qq{" class="teximg" />};
123 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
127 sub gen_image ($$$$) {
128 # Actually creates the image.
132 my $imagedir = shift;
134 if (!defined $config{teximg_prefix}) {
135 $config{teximg_prefix} = $default_prefix;
137 if (!defined $config{teximg_postfix}) {
138 $config{teximg_postfix} = $default_postfix;
140 if (!defined $config{teximg_dvipng}) {
141 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
144 my $tex = $config{teximg_prefix};
145 $tex .= '$$'.$code.'$$';
146 $tex .= $config{teximg_postfix};
147 $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
148 $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
150 my $tmp = eval { create_tmp_dir($digest) };
152 writefile("$digest.tex", $tmp, $tex) &&
153 system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
154 # ensure destination directory exists
155 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
156 (($config{teximg_dvipng} &&
157 system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
158 ) || (!$config{teximg_dvipng} &&
159 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
160 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
168 if (open(my $f, '<', "$tmp/$digest.log")) {
174 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
180 sub create_tmp_dir ($) {
181 # Create a temp directory, it will be removed when ikiwiki exits.
184 my $template = $base.".XXXXXXXXXX";
185 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);