]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/teximg.pm
* More compact output for the brokenlinks plugin.
[ikiwiki.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>
4
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;
12
13 sub import { #{{{
14         hook(type => "preprocess", id => "teximg", call => \&preprocess);
15 } #}}}
16
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
33         if (check($code)) {
34                 return create($code, check_height($height), \%params);
35         }
36         else {
37                 return "[[teximg ".gettext("code includes disallowed latex commands"). "]]";
38         }
39 } #}}}
40
41 sub check_height ($) { #{{{
42         # Since latex doesn't support unlimited scaling this function
43         # returns the closest supported size.
44         my $height =shift;
45
46         my @allowed=(8,9,10,11,12,14,17,20);
47
48         my $ret;
49         my $fit;
50         foreach my $val (@allowed) {
51                 my $f = abs($val - $height);
52                 if (! defined($fit) || $f < $fit ) {
53                         $ret=$val;
54                         $fit=$f;
55                 }
56         }
57         return $ret;
58 } #}}}
59
60 sub create ($$$) { #{{{
61         # This function calls the image generating function and returns
62         # the <img .. /> for the generated image.
63         my $code = shift;
64         my $height = shift;
65         my $params = shift;
66
67         if (! defined($height) and not length($height) ) {
68                 $height = 12;
69         }
70
71         my $digest = md5_hex($code, $height);
72
73         my $teximgdir = "/teximg";
74         my $imglink = "$teximgdir/$digest.png";
75         my $imglog = "$teximgdir/$digest.log";
76         will_render($params->{destpage}, $imglink);
77         will_render($params->{destpage}, $imglog);
78
79         my $imgurl;
80         my $logurl;
81         if (! $params->{preview}) {
82                 $imgurl = urlto($imglink, $params->{destpage});
83                 $logurl = urlto($imglog, $params->{destpage});
84         }
85         else {
86                 $imgurl="$config{url}/$teximgdir/$digest.png";
87                 $logurl="$config{url}/$teximgdir/$digest.log";
88         }
89         
90         if (-e "$config{destdir}/$imglink" ||
91             gen_image($code, $height, $digest, $teximgdir)) {
92                 return qq{<img src="$imgurl" alt="}
93                         .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
94                         .qq{" class="teximg" />};
95         }
96         else {
97                 return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
98         }
99 } #}}}
100
101 sub gen_image ($$$$) { #{{{
102         # Actually creates the image.
103         my $code = shift;
104         my $height = shift;
105         my $digest = shift;
106         my $imagedir = shift;
107
108         #TODO This should move into the setup file.
109         my $tex = '\documentclass['.$height.'pt]{scrartcl}';
110         $tex .= '\usepackage[version=3]{mhchem}';
111         $tex .= '\usepackage{amsmath}';
112         $tex .= '\usepackage{amsfonts}';
113         $tex .= '\usepackage{amssymb}';
114         $tex .= '\pagestyle{empty}';
115         $tex .= '\begin{document}';
116         $tex .= '$$'.$code.'$$';
117         $tex .= '\end{document}';
118
119         my $tmp = eval { create_tmp_dir($digest) };
120         if (! $@ &&
121             writefile("$digest.tex", $tmp, $tex) &&
122             system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
123             system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
124             # ensure destination directory exists
125             writefile("$imagedir/$digest.png", $config{destdir}, "") &&
126             system("convert -density 120  -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
127                 return 1;
128         }
129         else {
130                 # store failure log
131                 my $log;
132                 {
133                         open(my $f, '<', "$tmp/$digest.log");
134                         local $/=undef;
135                         $log = <$f>;
136                         close($f);
137                 }
138                 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
139
140                 return 0;
141         }
142 } #}}}
143
144 sub create_tmp_dir ($) { #{{{
145         # Create a temp directory, it will be removed when ikiwiki exits.
146         my $base = shift;
147
148         my $template = $base.".XXXXXXXXXX";
149         my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
150         return $tmpdir;
151 } #}}}
152
153 sub check ($) { #{{{
154         # Check if the code is ok
155         my $code = shift;
156
157         my @badthings = (
158                 qr/\$\$/,
159                 qr/\\include/,
160                 qr/\\includegraphic/,
161                 qr/\\usepackage/,
162                 qr/\\newcommand/, 
163                 qr/\\renewcommand/,
164                 qr/\\def/,
165                 qr/\\input/,
166                 qr/\\open/,
167                 qr/\\loop/,
168                 qr/\\errorstopmode/,
169                 qr/\\scrollmode/,
170                 qr/\\batchmode/,
171                 qr/\\read/,
172                 qr/\\write/,
173         );
174         
175         foreach my $thing (@badthings) {
176                 if ($code =~ m/$thing/ ) {
177                         return 0;
178                 }
179         }
180         return 1;
181 } #}}}
182
183 1