2 # Licensed under GPL v2 or greater
3 # (c) 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
5 package IkiWiki::Plugin::teximg;
8 use Digest::MD5 qw(md5_hex);
9 use File::Temp qw(tempdir);
14 hook(type => "getsetup", id => "teximg", call => \&getsetup);
15 hook(type => "preprocess", id => "teximg", call => \&preprocess);
18 sub getsetup () { #{{{
26 sub preprocess (@) { #{{{
29 my $height = $params{height};
30 if (! defined $height || ! length $height) {
34 $height =~ s#(\d+)#$1#;
37 my $code = $params{code};
38 if (! defined $code && ! length $code) {
39 error gettext("missing tex code");
43 return create($code, check_height($height), \%params);
46 error gettext("code includes disallowed latex commands")
50 sub check_height ($) { #{{{
51 # Since latex doesn't support unlimited scaling this function
52 # returns the closest supported size.
55 my @allowed=(8,9,10,11,12,14,17,20);
59 foreach my $val (@allowed) {
60 my $f = abs($val - $height);
61 if (! defined($fit) || $f < $fit ) {
69 sub create ($$$) { #{{{
70 # This function calls the image generating function and returns
71 # the <img .. /> for the generated image.
76 if (! defined($height) and not length($height) ) {
80 my $digest = md5_hex($code, $height);
82 my $imglink= $params->{page} . "/$digest.png";
83 my $imglog = $params->{page} . "/$digest.log";
84 will_render($params->{page}, $imglink);
85 will_render($params->{page}, $imglog);
87 my $imgurl=urlto($imglink, $params->{destpage});
88 my $logurl=urlto($imglog, $params->{destpage});
90 if (-e "$config{destdir}/$imglink" ||
91 gen_image($code, $height, $digest, $params->{page})) {
92 return qq{<img src="$imgurl" alt="}
93 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
94 .qq{" class="teximg" />};
97 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
101 sub gen_image ($$$$) { #{{{
102 # Actually creates the image.
106 my $imagedir = shift;
108 #TODO This should move into the setup file.
109 my $tex = '\documentclass['.$height.'pt]{scrartcl}';
110 $tex .= '\usepackage[version=3]{mhchem}';
111 $tex .= '\usepackage{amsmath}';
112 $tex .= '\usepackage{amsfonts}';
113 $tex .= '\usepackage{amssymb}';
114 $tex .= '\pagestyle{empty}';
115 $tex .= '\begin{document}';
116 $tex .= '$$'.$code.'$$';
117 $tex .= '\end{document}';
119 my $tmp = eval { create_tmp_dir($digest) };
121 writefile("$digest.tex", $tmp, $tex) &&
122 system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
123 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
124 # ensure destination directory exists
125 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
126 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
133 if (open(my $f, '<', "$tmp/$digest.log")) {
139 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
145 sub create_tmp_dir ($) { #{{{
146 # Create a temp directory, it will be removed when ikiwiki exits.
149 my $template = $base.".XXXXXXXXXX";
150 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
155 # Check if the code is ok
161 qr/\\includegraphic/,
176 foreach my $thing (@badthings) {
177 if ($code =~ m/$thing/ ) {