]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/teximg.pm
Merge branch 'master' of git://github.com/joeyh/ikiwiki
[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 3.00;
12
13 my $default_prefix = <<EOPREFIX;
14 \\documentclass{article}
15 \\usepackage{amsmath}
16 \\usepackage{amsfonts}
17 \\usepackage{amssymb}
18 \\pagestyle{empty}
19 \\begin{document}
20 EOPREFIX
21
22 my $default_postfix = '\\end{document}';
23
24 sub import {
25         hook(type => "getsetup", id => "teximg", call => \&getsetup);
26         hook(type => "preprocess", id => "teximg", call => \&preprocess);
27 }
28
29 sub getsetup () {
30         return
31                 plugin => {
32                         safe => 1,
33                         rebuild => undef,
34                 },
35                 teximg_dvipng => {
36                         type => "boolean",
37                         description => "Should teximg use dvipng to render, or dvips and convert?",
38                         safe => 0,
39                         rebuild => undef,
40                 },
41                 teximg_prefix => {
42                         type => "string",
43                         example => $default_prefix,
44                         description => "LaTeX prefix for teximg plugin",
45                         safe => 0, # Not sure how secure LaTeX is...
46                         rebuild => 1,
47                 },
48                 teximg_postfix => {
49                         type => "string",
50                         example => $default_postfix,
51                         description => "LaTeX postfix for teximg plugin",
52                         safe => 0, # Not sure how secure LaTeX is...
53                         rebuild => 1,
54                 },
55 }
56
57 sub preprocess (@) {
58         my %params = @_;
59         
60         my $height = $params{height};
61         if (! defined $height || ! length $height) {
62                 $height = 12;
63         }
64         else {
65                 $height =~ s#(\d+)#$1#;
66         }
67         
68         my $code = $params{code};
69         if (! defined $code && ! length $code) {
70                 error gettext("missing tex code");
71         }
72         return create($code, check_height($height), \%params);
73 }
74
75 sub check_height ($) {
76         # Since latex doesn't support unlimited scaling this function
77         # returns the closest supported size.
78         my $height =shift;
79
80         my @allowed=(8,9,10,11,12,14,17,20);
81
82         my $ret;
83         my $fit;
84         foreach my $val (@allowed) {
85                 my $f = abs($val - $height);
86                 if (! defined($fit) || $f < $fit ) {
87                         $ret=$val;
88                         $fit=$f;
89                 }
90         }
91         return $ret;
92 }
93
94 sub create ($$$) {
95         # This function calls the image generating function and returns
96         # the <img .. /> for the generated image.
97         my $code = shift;
98         my $height = shift;
99         my $params = shift;
100
101         if (! defined($height) and not length($height) ) {
102                 $height = 12;
103         }
104
105         my $digest = md5_hex($code, $height);
106
107         my $imglink= $params->{page} . "/$digest.png";
108         my $imglog =  $params->{page} .  "/$digest.log";
109         will_render($params->{page}, $imglink);
110         will_render($params->{page}, $imglog);
111
112         my $imgurl=urlto($imglink, $params->{destpage});
113         my $logurl=urlto($imglog, $params->{destpage});
114         
115         if (-e "$config{destdir}/$imglink" ||
116             gen_image($code, $height, $digest, $params->{page})) {
117                 return qq{<img src="$imgurl" alt="}
118                         .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
119                         .qq{" class="teximg" />};
120         }
121         else {
122                 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
123         }
124 }
125
126 sub gen_image ($$$$) {
127         # Actually creates the image.
128         my $code = shift;
129         my $height = shift;
130         my $digest = shift;
131         my $imagedir = shift;
132
133         if (!defined $config{teximg_prefix}) {
134                 $config{teximg_prefix} = $default_prefix;
135         }
136         if (!defined $config{teximg_postfix}) {
137                 $config{teximg_postfix} = $default_postfix;
138         }
139         if (!defined $config{teximg_dvipng}) {
140                 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
141         }
142         
143         my $tex = $config{teximg_prefix};
144         $tex .= '$$'.$code.'$$';
145         $tex .= $config{teximg_postfix};
146         $tex =~ s!\\documentclass{article}!\\documentclass[${height}pt]{article}!g;
147         $tex =~ s!\\documentclass{scrartcl}!\\documentclass[${height}pt]{scrartcl}!g;
148
149         my $tmp = eval { create_tmp_dir($digest) };
150         if (! $@ &&
151             writefile("$digest.tex", $tmp, $tex) &&
152             system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
153             # ensure destination directory exists
154             writefile("$imagedir/$digest.png", $config{destdir}, "") &&
155             (($config{teximg_dvipng} &&
156                 system("dvipng -D 120 -bg Transparent -T tight -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.dvi > $tmp/$digest.log") == 0
157             ) || (!$config{teximg_dvipng} &&
158                 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
159                 system("convert -density 120  -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0
160             ))) {
161                 return 1;
162         }
163         else {
164                 # store failure log
165                 my $log="";
166                 {
167                         if (open(my $f, '<', "$tmp/$digest.log")) {
168                                 local $/=undef;
169                                 $log = <$f>;
170                                 close($f);
171                         }
172                 }
173                 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
174
175                 return 0;
176         }
177 }
178
179 sub create_tmp_dir ($) {
180         # Create a temp directory, it will be removed when ikiwiki exits.
181         my $base = shift;
182
183         my $template = $base.".XXXXXXXXXX";
184         my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
185         return $tmpdir;
186 }
187
188 1