]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/teximg.pm
teximg: Make TeX handle preventing unsafe things; remove insufficient blacklist
[git.ikiwiki.info.git] / IkiWiki / Plugin / teximg.pm
1 #!/usr/bin/perl
2 # Licensed under GPL v2 or greater
3 # (c) 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
5 package IkiWiki::Plugin::teximg;
6 use warnings;
7 use strict;
8 use Digest::MD5 qw(md5_hex);
9 use File::Temp qw(tempdir);
10 use HTML::Entities;
11 use IkiWiki 2.00;
13 sub import { #{{{
14         hook(type => "preprocess", id => "teximg", call => \&preprocess);
15 } #}}}
17 sub preprocess (@) { #{{{
18         my %params = @_;
19         
20         my $height = $params{height};
21         if (! defined $height || ! length $height) {
22                 $height = 12;
23         }
24         else {
25                 $height =~ s#(\d+)#$1#;
26         }
27         
28         my $code = $params{code};
29         if (! defined $code && ! length $code) {
30                 return "[[teximg ".gettext("missing tex code"). "]]";
31         }
32         return create($code, check_height($height), \%params);
33 } #}}}
35 sub check_height ($) { #{{{
36         # Since latex doesn't support unlimited scaling this function
37         # returns the closest supported size.
38         my $height =shift;
40         my @allowed=(8,9,10,11,12,14,17,20);
42         my $ret;
43         my $fit;
44         foreach my $val (@allowed) {
45                 my $f = abs($val - $height);
46                 if (! defined($fit) || $f < $fit ) {
47                         $ret=$val;
48                         $fit=$f;
49                 }
50         }
51         return $ret;
52 } #}}}
54 sub create ($$$) { #{{{
55         # This function calls the image generating function and returns
56         # the <img .. /> for the generated image.
57         my $code = shift;
58         my $height = shift;
59         my $params = shift;
61         if (! defined($height) and not length($height) ) {
62                 $height = 12;
63         }
65         my $digest = md5_hex($code, $height);
67         my $imglink= $params->{page} . "/$digest.png";
68         my $imglog =  $params->{page} .  "/$digest.log";
69         will_render($params->{page}, $imglink);
70         will_render($params->{page}, $imglog);
72         my $imgurl=urlto($imglink, $params->{destpage});
73         my $logurl=urlto($imglog, $params->{destpage});
74         
75         if (-e "$config{destdir}/$imglink" ||
76             gen_image($code, $height, $digest, $params->{page})) {
77                 return qq{<img src="$imgurl" alt="}
78                         .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
79                         .qq{" class="teximg" />};
80         }
81         else {
82                 return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
83         }
84 } #}}}
86 sub gen_image ($$$$) { #{{{
87         # Actually creates the image.
88         my $code = shift;
89         my $height = shift;
90         my $digest = shift;
91         my $imagedir = shift;
93         #TODO This should move into the setup file.
94         my $tex = '\documentclass['.$height.'pt]{scrartcl}';
95         $tex .= '\usepackage[version=3]{mhchem}';
96         $tex .= '\usepackage{amsmath}';
97         $tex .= '\usepackage{amsfonts}';
98         $tex .= '\usepackage{amssymb}';
99         $tex .= '\pagestyle{empty}';
100         $tex .= '\begin{document}';
101         $tex .= '$$'.$code.'$$';
102         $tex .= '\end{document}';
104         my $tmp = eval { create_tmp_dir($digest) };
105         if (! $@ &&
106             writefile("$digest.tex", $tmp, $tex) &&
107             system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
108             system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
109             # ensure destination directory exists
110             writefile("$imagedir/$digest.png", $config{destdir}, "") &&
111             system("convert -density 120  -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
112                 return 1;
113         }
114         else {
115                 # store failure log
116                 my $log="";
117                 {
118                         if (open(my $f, '<', "$tmp/$digest.log")) {
119                                 local $/=undef;
120                                 $log = <$f>;
121                                 close($f);
122                         }
123                 }
124                 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
126                 return 0;
127         }
128 } #}}}
130 sub create_tmp_dir ($) { #{{{
131         # Create a temp directory, it will be removed when ikiwiki exits.
132         my $base = shift;
134         my $template = $base.".XXXXXXXXXX";
135         my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
136         return $tmpdir;
137 } #}}}