]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/teximg.pm
rename to txt
[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->{page} . "/$digest.png";
74         my $imglog =  $params->{page} .  "/$digest.log";
75         will_render($params->{page}, $imglink);
76         will_render($params->{page}, $imglog);
77
78         my $imgurl=urlto($imglink, $params->{destpage});
79         my $logurl=urlto($imglog, $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                         if (open(my $f, '<', "$tmp/$digest.log")) {
125                                 local $/=undef;
126                                 $log = <$f>;
127                                 close($f);
128                         }
129                 }
130                 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
131
132                 return 0;
133         }
134 } #}}}
135
136 sub create_tmp_dir ($) { #{{{
137         # Create a temp directory, it will be removed when ikiwiki exits.
138         my $base = shift;
139
140         my $template = $base.".XXXXXXXXXX";
141         my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
142         return $tmpdir;
143 } #}}}
144
145 sub check ($) { #{{{
146         # Check if the code is ok
147         my $code = shift;
148
149         my @badthings = (
150                 qr/\$\$/,
151                 qr/\\include/,
152                 qr/\\includegraphic/,
153                 qr/\\usepackage/,
154                 qr/\\newcommand/, 
155                 qr/\\renewcommand/,
156                 qr/\\def/,
157                 qr/\\input/,
158                 qr/\\open/,
159                 qr/\\loop/,
160                 qr/\\errorstopmode/,
161                 qr/\\scrollmode/,
162                 qr/\\batchmode/,
163                 qr/\\read/,
164                 qr/\\write/,
165         );
166         
167         foreach my $thing (@badthings) {
168                 if ($code =~ m/$thing/ ) {
169                         return 0;
170                 }
171         }
172         return 1;
173 } #}}}
174
175 1