]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/teximg.pm
inline: Allow the "feedshow" parameter to take values greater than the value for "show".
[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 $imglink= $params->{destpage} . "/$digest.png";
74         my $imglog =  $params->{destpage} .  "/$digest.log";
75         will_render($params->{destpage}, $imglink);
76         will_render($params->{destpage}, $imglog);
77
78         my $imgurl=urlto($imglink, $params->{destpage});
79         my $logurl=urlto($imglink, $params->{destpage});
80         
81         if (-e "$config{destdir}/$imglink" ||
82             gen_image($code, $height, $digest, $params->{page})) {
83                 return qq{<img src="$imgurl" alt="}
84                         .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
85                         .qq{" class="teximg" />};
86         }
87         else {
88                 return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
89         }
90 } #}}}
91
92 sub gen_image ($$$$) { #{{{
93         # Actually creates the image.
94         my $code = shift;
95         my $height = shift;
96         my $digest = shift;
97         my $imagedir = shift;
98
99         #TODO This should move into the setup file.
100         my $tex = '\documentclass['.$height.'pt]{scrartcl}';
101         $tex .= '\usepackage[version=3]{mhchem}';
102         $tex .= '\usepackage{amsmath}';
103         $tex .= '\usepackage{amsfonts}';
104         $tex .= '\usepackage{amssymb}';
105         $tex .= '\pagestyle{empty}';
106         $tex .= '\begin{document}';
107         $tex .= '$$'.$code.'$$';
108         $tex .= '\end{document}';
109
110         my $tmp = eval { create_tmp_dir($digest) };
111         if (! $@ &&
112             writefile("$digest.tex", $tmp, $tex) &&
113             system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
114             system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
115             # ensure destination directory exists
116             writefile("$imagedir/$digest.png", $config{destdir}, "") &&
117             system("convert -density 120  -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
118                 return 1;
119         }
120         else {
121                 # store failure log
122                 my $log;
123                 {
124                         open(my $f, '<', "$tmp/$digest.log");
125                         local $/=undef;
126                         $log = <$f>;
127                         close($f);
128                 }
129                 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
130
131                 return 0;
132         }
133 } #}}}
134
135 sub create_tmp_dir ($) { #{{{
136         # Create a temp directory, it will be removed when ikiwiki exits.
137         my $base = shift;
138
139         my $template = $base.".XXXXXXXXXX";
140         my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
141         return $tmpdir;
142 } #}}}
143
144 sub check ($) { #{{{
145         # Check if the code is ok
146         my $code = shift;
147
148         my @badthings = (
149                 qr/\$\$/,
150                 qr/\\include/,
151                 qr/\\includegraphic/,
152                 qr/\\usepackage/,
153                 qr/\\newcommand/, 
154                 qr/\\renewcommand/,
155                 qr/\\def/,
156                 qr/\\input/,
157                 qr/\\open/,
158                 qr/\\loop/,
159                 qr/\\errorstopmode/,
160                 qr/\\scrollmode/,
161                 qr/\\batchmode/,
162                 qr/\\read/,
163                 qr/\\write/,
164         );
165         
166         foreach my $thing (@badthings) {
167                 if ($code =~ m/$thing/ ) {
168                         return 0;
169                 }
170         }
171         return 1;
172 } #}}}
173
174 1