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