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