add paths to plugin files in copyright
[ikiwiki.git] / IkiWiki / Plugin / teximg.pm
blob3d6fa9942c11f3dd304026458655b22ab30e5e50
1 #!/usr/bin/perl
2 # Licensed under GPL v2 or greater
3 # (c) 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
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;
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
24 my $default_postfix = '\\end{document}';
26 sub import {
27 hook(type => "getsetup", id => "teximg", call => \&getsetup);
28 hook(type => "preprocess", id => "teximg", call => \&preprocess);
31 sub getsetup () {
32 return
33 plugin => {
34 safe => 1,
35 rebuild => undef,
36 section => "widget",
38 teximg_dvipng => {
39 type => "boolean",
40 description => "Should teximg use dvipng to render, or dvips and convert?",
41 safe => 0,
42 rebuild => undef,
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,
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,
60 sub preprocess (@) {
61 my %params = @_;
63 my $height = $params{height};
64 if (! defined $height || ! length $height) {
65 $height = 12;
67 else {
68 $height =~ s#(\d+)#$1#;
71 my $code = $params{code};
72 if (! defined $code && ! length $code) {
73 error gettext("missing tex code");
75 return create($code, check_height($height), \%params);
78 sub check_height ($) {
79 # Since latex doesn't support unlimited scaling this function
80 # returns the closest supported size.
81 my $height =shift;
83 my @allowed=(8,9,10,11,12,14,17,20);
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;
94 return $ret;
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;
104 if (! defined($height) and not length($height) ) {
105 $height = 12;
108 my $digest = md5_hex(Encode::encode_utf8($code), $height);
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);
115 my $imgurl=urlto($imglink, $params->{destpage});
116 my $logurl=urlto($imglog, $params->{destpage});
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" />};
124 else {
125 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
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;
136 if (!defined $config{teximg_prefix}) {
137 $config{teximg_prefix} = $default_prefix;
139 if (!defined $config{teximg_postfix}) {
140 $config{teximg_postfix} = $default_postfix;
142 if (!defined $config{teximg_dvipng}) {
143 $config{teximg_dvipng} = length `which dvipng 2>/dev/null`;
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;
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;
166 else {
167 # store failure log
168 my $log="";
170 if (open(my $f, '<', "$tmp/$digest.log")) {
171 local $/=undef;
172 $log = <$f>;
173 close($f);
176 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
178 return 0;
182 sub create_tmp_dir ($) {
183 # Create a temp directory, it will be removed when ikiwiki exits.
184 my $base = shift;
186 my $template = $base.".XXXXXXXXXX";
187 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
188 return $tmpdir;