2 # Licensed under GPL v2 or greater
3 # (c) 2007 Patrick Winnertz <patrick.winnertz@skolelinux.org>
5 package IkiWiki::Plugin::tex;
8 use Digest::MD5 qw(md5_hex);
9 use File::Temp qw(tempdir);
10 use URI::Escape qw(uri_escape);
14 hook(type => "preprocess", id => "tex", 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 "[[tex ".gettext("missing tex code"). "]]";
34 return create($code, check_height($height), \%params);
37 return "[[tex ".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 $teximgdir = "/teximages";
74 my $imglink = "$teximgdir/$digest.png";
75 my $imglog = "$teximgdir/$digest.log";
76 will_render($params->{destpage}, $imglink);
77 will_render($params->{destpage}, $imglog);
81 if (! $params->{preview}) {
82 $imgurl = urlto($imglink, $params->{destpage});
83 $logurl = urlto($imglog, $params->{destpage});
86 $imgurl="$config{url}/$teximgdir/$digest.png";
87 $logurl="$config{url}/$teximgdir/$digest.log";
90 if (-e "$config{destdir}/$imglink" ||
91 gen_image($code, $height, $digest, $teximgdir)) {
92 return qq{<img src="$imgurl" alt="}.uri_escape($code).qq{" class="teximage" />};
95 return qq{[[tex <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
99 sub gen_image ($$$$) { #{{{
100 # Actually creates the image.
104 my $imagedir = shift;
106 #TODO This should move into the setup file.
107 my $tex = '\documentclass['.$height.'pt]{scrartcl}';
108 $tex .= '\usepackage[version=3]{mhchem}';
109 $tex .= '\usepackage{amsmath}';
110 $tex .= '\usepackage{amsfonts}';
111 $tex .= '\usepackage{amssymb}';
112 $tex .= '\pagestyle{empty}';
113 $tex .= '\begin{document}';
114 $tex .= '$$'.$code.'$$';
115 $tex .= '\end{document}';
117 my $tmp = eval { create_tmp_dir($digest) };
119 writefile("$digest.tex", $tmp, $tex) &&
120 system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
121 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
122 # ensure destination directory exists
123 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
124 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
131 open(my $f, '<', "$tmp/$digest.log");
136 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
142 sub create_tmp_dir ($) { #{{{
143 # Create a temp directory, it will be removed when ikiwiki exits.
146 my $template = $base.".XXXXXXXXXX";
147 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
152 # Check if the code is ok
158 qr/\\includegraphic/,
173 foreach my $thing (@badthings) {
174 if ($code =~ m/$thing/ ) {