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 => "preprocess", id => "teximg", call => \&preprocess);
17 sub preprocess (@) { #{{{
20 my $height = $params{height};
21 if (! defined $height || ! length $height) {
25 $height =~ s#(\d+)#$1#;
28 my $code = $params{code};
29 if (! defined $code && ! length $code) {
30 return "[[teximg ".gettext("missing tex code"). "]]";
32 return create($code, check_height($height), \%params);
35 sub check_height ($) { #{{{
36 # Since latex doesn't support unlimited scaling this function
37 # returns the closest supported size.
40 my @allowed=(8,9,10,11,12,14,17,20);
44 foreach my $val (@allowed) {
45 my $f = abs($val - $height);
46 if (! defined($fit) || $f < $fit ) {
54 sub create ($$$) { #{{{
55 # This function calls the image generating function and returns
56 # the <img .. /> for the generated image.
61 if (! defined($height) and not length($height) ) {
65 my $digest = md5_hex($code, $height);
67 my $imglink= $params->{page} . "/$digest.png";
68 my $imglog = $params->{page} . "/$digest.log";
69 will_render($params->{page}, $imglink);
70 will_render($params->{page}, $imglog);
72 my $imgurl=urlto($imglink, $params->{destpage});
73 my $logurl=urlto($imglog, $params->{destpage});
75 if (-e "$config{destdir}/$imglink" ||
76 gen_image($code, $height, $digest, $params->{page})) {
77 return qq{<img src="$imgurl" alt="}
78 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
79 .qq{" class="teximg" />};
82 return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
86 sub gen_image ($$$$) { #{{{
87 # Actually creates the image.
93 #TODO This should move into the setup file.
94 my $tex = '\documentclass['.$height.'pt]{scrartcl}';
95 $tex .= '\usepackage[version=3]{mhchem}';
96 $tex .= '\usepackage{amsmath}';
97 $tex .= '\usepackage{amsfonts}';
98 $tex .= '\usepackage{amssymb}';
99 $tex .= '\pagestyle{empty}';
100 $tex .= '\begin{document}';
101 $tex .= '$$'.$code.'$$';
102 $tex .= '\end{document}';
104 my $tmp = eval { create_tmp_dir($digest) };
106 writefile("$digest.tex", $tmp, $tex) &&
107 system("cd $tmp; shell_escape=f openout_any=p openin_any=p latex --interaction=nonstopmode $digest.tex < /dev/null > /dev/null") == 0 &&
108 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
109 # ensure destination directory exists
110 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
111 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
118 if (open(my $f, '<', "$tmp/$digest.log")) {
124 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
130 sub create_tmp_dir ($) { #{{{
131 # Create a temp directory, it will be removed when ikiwiki exits.
134 my $template = $base.".XXXXXXXXXX";
135 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);