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);
29 sub getsetup () { #{{{
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...
57 sub preprocess (@) { #{{{
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");
74 return create($code, check_height($height), \%params);
77 error gettext("code includes disallowed latex commands")
81 sub check_height ($) { #{{{
82 # Since latex doesn't support unlimited scaling this function
83 # returns the closest supported size.
86 my @allowed=(8,9,10,11,12,14,17,20);
90 foreach my $val (@allowed) {
91 my $f = abs($val - $height);
92 if (! defined($fit) || $f < $fit ) {
100 sub create ($$$) { #{{{
101 # This function calls the image generating function and returns
102 # the <img .. /> for the generated image.
107 if (! defined($height) and not length($height) ) {
111 my $digest = md5_hex($code, $height);
113 my $imglink= $params->{page} . "/$digest.png";
114 my $imglog = $params->{page} . "/$digest.log";
115 will_render($params->{page}, $imglink);
116 will_render($params->{page}, $imglog);
118 my $imgurl=urlto($imglink, $params->{destpage});
119 my $logurl=urlto($imglog, $params->{destpage});
121 if (-e "$config{destdir}/$imglink" ||
122 gen_image($code, $height, $digest, $params->{page})) {
123 return qq{<img src="$imgurl" alt="}
124 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
125 .qq{" class="teximg" />};
128 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
132 sub gen_image ($$$$) { #{{{
133 # Actually creates the image.
137 my $imagedir = shift;
139 if (!defined $config{teximg_prefix}) {
140 $config{teximg_prefix} = $default_prefix;
142 if (!defined $config{teximg_postfix}) {
143 $config{teximg_postfix} = $default_postfix;
145 if (!defined $config{teximg_dvipng}) {
146 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
149 my $tex = $config{teximg_prefix};
150 $tex .= '$$'.$code.'$$';
151 $tex .= $config{teximg_postfix};
152 $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
153 $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
155 my $tmp = eval { create_tmp_dir($digest) };
157 writefile("$digest.tex", $tmp, $tex) &&
158 system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
159 # ensure destination directory exists
160 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
161 (($config{teximg_dvipng} &&
162 system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
163 ) || (!$config{teximg_dvipng} &&
164 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
165 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
173 if (open(my $f, '<', "$tmp/$digest.log")) {
179 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
185 sub create_tmp_dir ($) { #{{{
186 # Create a temp directory, it will be removed when ikiwiki exits.
189 my $template = $base.".XXXXXXXXXX";
190 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
195 # Check if the code is ok
201 qr/\\includegraphic/,
216 foreach my $thing (@badthings) {
217 if ($code =~ m/$thing/ ) {