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"). "]]";
34 return create($code, check_height($height), \%params);
37 return "[[teximg ".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 = "/teximag";
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="}
93 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
94 .qq{" class="teximg" />};
97 return qq{[[teximg <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 open(my $f, '<', "$tmp/$digest.log");
138 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
144 sub create_tmp_dir ($) { #{{{
145 # Create a temp directory, it will be removed when ikiwiki exits.
148 my $template = $base.".XXXXXXXXXX";
149 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
154 # Check if the code is ok
160 qr/\\includegraphic/,
175 foreach my $thing (@badthings) {
176 if ($code =~ m/$thing/ ) {