]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/teximg.pm
avoid link fixup if page name stayed the same
[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 => "getsetup", id => "teximg", call => \&getsetup);
15         hook(type => "preprocess", id => "teximg", call => \&preprocess);
16 } #}}}
17
18 sub getsetup () { #{{{
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                 },
24 } #}}}
25
26 sub preprocess (@) { #{{{
27         my %params = @_;
28         
29         my $height = $params{height};
30         if (! defined $height || ! length $height) {
31                 $height = 12;
32         }
33         else {
34                 $height =~ s#(\d+)#$1#;
35         }
36         
37         my $code = $params{code};
38         if (! defined $code && ! length $code) {
39                 error gettext("missing tex code");
40         }
41
42         if (check($code)) {
43                 return create($code, check_height($height), \%params);
44         }
45         else {
46                 error gettext("code includes disallowed latex commands")
47         }
48 } #}}}
49
50 sub check_height ($) { #{{{
51         # Since latex doesn't support unlimited scaling this function
52         # returns the closest supported size.
53         my $height =shift;
54
55         my @allowed=(8,9,10,11,12,14,17,20);
56
57         my $ret;
58         my $fit;
59         foreach my $val (@allowed) {
60                 my $f = abs($val - $height);
61                 if (! defined($fit) || $f < $fit ) {
62                         $ret=$val;
63                         $fit=$f;
64                 }
65         }
66         return $ret;
67 } #}}}
68
69 sub create ($$$) { #{{{
70         # This function calls the image generating function and returns
71         # the <img .. /> for the generated image.
72         my $code = shift;
73         my $height = shift;
74         my $params = shift;
75
76         if (! defined($height) and not length($height) ) {
77                 $height = 12;
78         }
79
80         my $digest = md5_hex($code, $height);
81
82         my $imglink= $params->{page} . "/$digest.png";
83         my $imglog =  $params->{page} .  "/$digest.log";
84         will_render($params->{page}, $imglink);
85         will_render($params->{page}, $imglog);
86
87         my $imgurl=urlto($imglink, $params->{destpage});
88         my $logurl=urlto($imglog, $params->{destpage});
89         
90         if (-e "$config{destdir}/$imglink" ||
91             gen_image($code, $height, $digest, $params->{page})) {
92                 return qq{<img src="$imgurl" alt="}
93                         .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
94                         .qq{" class="teximg" />};
95         }
96         else {
97                 error qq{<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                         if (open(my $f, '<', "$tmp/$digest.log")) {
134                                 local $/=undef;
135                                 $log = <$f>;
136                                 close($f);
137                         }
138                 }
139                 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
140
141                 return 0;
142         }
143 } #}}}
144
145 sub create_tmp_dir ($) { #{{{
146         # Create a temp directory, it will be removed when ikiwiki exits.
147         my $base = shift;
148
149         my $template = $base.".XXXXXXXXXX";
150         my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
151         return $tmpdir;
152 } #}}}
153
154 sub check ($) { #{{{
155         # Check if the code is ok
156         my $code = shift;
157
158         my @badthings = (
159                 qr/\$\$/,
160                 qr/\\include/,
161                 qr/\\includegraphic/,
162                 qr/\\usepackage/,
163                 qr/\\newcommand/, 
164                 qr/\\renewcommand/,
165                 qr/\\def/,
166                 qr/\\input/,
167                 qr/\\open/,
168                 qr/\\loop/,
169                 qr/\\errorstopmode/,
170                 qr/\\scrollmode/,
171                 qr/\\batchmode/,
172                 qr/\\read/,
173                 qr/\\write/,
174         );
175         
176         foreach my $thing (@badthings) {
177                 if ($code =~ m/$thing/ ) {
178                         return 0;
179                 }
180         }
181         return 1;
182 } #}}}
183
184 1