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 error gettext("missing tex code");
34 return create($code, check_height($height), \%params);
37 error gettext("code includes disallowed latex commands")
41 sub check_height ($) { #{{{
42 # Since latex doesn't support unlimited scaling this function
43 # returns the closest supported size.
46 my @allowed=(8,9,10,11,12,14,17,20);
50 foreach my $val (@allowed) {
51 my $f = abs($val - $height);
52 if (! defined($fit) || $f < $fit ) {
60 sub create ($$$) { #{{{
61 # This function calls the image generating function and returns
62 # the <img .. /> for the generated image.
67 if (! defined($height) and not length($height) ) {
71 my $digest = md5_hex($code, $height);
73 my $imglink= $params->{page} . "/$digest.png";
74 my $imglog = $params->{page} . "/$digest.log";
75 will_render($params->{page}, $imglink);
76 will_render($params->{page}, $imglog);
78 my $imgurl=urlto($imglink, $params->{destpage});
79 my $logurl=urlto($imglog, $params->{destpage});
81 if (-e "$config{destdir}/$imglink" ||
82 gen_image($code, $height, $digest, $params->{page})) {
83 return qq{<img src="$imgurl" alt="}
84 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
85 .qq{" class="teximg" />};
88 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
92 sub gen_image ($$$$) { #{{{
93 # Actually creates the image.
99 #TODO This should move into the setup file.
100 my $tex = '\documentclass['.$height.'pt]{scrartcl}';
101 $tex .= '\usepackage[version=3]{mhchem}';
102 $tex .= '\usepackage{amsmath}';
103 $tex .= '\usepackage{amsfonts}';
104 $tex .= '\usepackage{amssymb}';
105 $tex .= '\pagestyle{empty}';
106 $tex .= '\begin{document}';
107 $tex .= '$$'.$code.'$$';
108 $tex .= '\end{document}';
110 my $tmp = eval { create_tmp_dir($digest) };
112 writefile("$digest.tex", $tmp, $tex) &&
113 system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
114 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
115 # ensure destination directory exists
116 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
117 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
124 if (open(my $f, '<', "$tmp/$digest.log")) {
130 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
136 sub create_tmp_dir ($) { #{{{
137 # Create a temp directory, it will be removed when ikiwiki exits.
140 my $template = $base.".XXXXXXXXXX";
141 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
146 # Check if the code is ok
152 qr/\\includegraphic/,
167 foreach my $thing (@badthings) {
168 if ($code =~ m/$thing/ ) {