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}
17 \\usepackage{amsfonts}
23 my $default_postfix = '\\end{document}';
26 hook(type => "getsetup", id => "teximg", call => \&getsetup);
27 hook(type => "preprocess", id => "teximg", call => \&preprocess);
39 description => "Should teximg use dvipng to render, or dvips and convert?",
45 example => $default_prefix,
46 description => "LaTeX prefix for teximg plugin",
47 safe => 0, # Not sure how secure LaTeX is...
52 example => $default_postfix,
53 description => "LaTeX postfix for teximg plugin",
54 safe => 0, # Not sure how secure LaTeX is...
62 my $height = $params{height};
63 if (! defined $height || ! length $height) {
67 $height =~ s#(\d+)#$1#;
70 my $code = $params{code};
71 if (! defined $code && ! length $code) {
72 error gettext("missing tex code");
74 return create($code, check_height($height), \%params);
77 sub check_height ($) {
78 # Since latex doesn't support unlimited scaling this function
79 # returns the closest supported size.
82 my @allowed=(8,9,10,11,12,14,17,20);
86 foreach my $val (@allowed) {
87 my $f = abs($val - $height);
88 if (! defined($fit) || $f < $fit ) {
97 # This function calls the image generating function and returns
98 # the <img .. /> for the generated image.
103 if (! defined($height) and not length($height) ) {
107 my $digest = md5_hex(Encode::encode_utf8($code), $height);
109 my $imglink= $params->{page} . "/$digest.png";
110 my $imglog = $params->{page} . "/$digest.log";
111 will_render($params->{page}, $imglink);
112 will_render($params->{page}, $imglog);
114 my $imgurl=urlto($imglink, $params->{destpage});
115 my $logurl=urlto($imglog, $params->{destpage});
117 if (-e "$config{destdir}/$imglink" ||
118 gen_image($code, $height, $digest, $params->{page})) {
119 return qq{<img src="$imgurl" alt="}
120 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
121 .qq{" class="teximg" />};
124 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
128 sub gen_image ($$$$) {
129 # Actually creates the image.
133 my $imagedir = shift;
135 if (!defined $config{teximg_prefix}) {
136 $config{teximg_prefix} = $default_prefix;
138 if (!defined $config{teximg_postfix}) {
139 $config{teximg_postfix} = $default_postfix;
141 if (!defined $config{teximg_dvipng}) {
142 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
145 my $tex = $config{teximg_prefix};
146 $tex .= '$$'.$code.'$$';
147 $tex .= $config{teximg_postfix};
148 $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
149 $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
151 my $tmp = eval { create_tmp_dir($digest) };
153 writefile("$digest.tex", $tmp, $tex) &&
154 system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
155 # ensure destination directory exists
156 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
157 (($config{teximg_dvipng} &&
158 system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
159 ) || (!$config{teximg_dvipng} &&
160 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
161 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
169 if (open(my $f, '<', "$tmp/$digest.log")) {
175 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
181 sub create_tmp_dir ($) {
182 # Create a temp directory, it will be removed when ikiwiki exits.
185 my $template = $base.".XXXXXXXXXX";
186 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);