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