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 $imglink= $params->{page} . "/$digest.png";
74 my $imglog = $params->{page} . "/$digest.log";
75 will_render($params->{destpage}, $imglink);
76 will_render($params->{destpage}, $imglog);
80 if (! $params->{preview}) {
81 $imgurl = urlto($imglink, $params->{destpage});
82 $logurl = urlto($imglog, $params->{destpage});
85 $imgurl=$params->{page}."/$digest.png";
86 $logurl=$params->{page}."/$digest.log";
89 if (-e "$config{destdir}/$imglink" ||
90 gen_image($code, $height, $digest, $params->{page})) {
91 return qq{<img src="$imgurl" alt="}
92 .(exists $params->{alt} ? $params->{alt} : encode_entities($code))
93 .qq{" class="teximg" />};
96 return qq{[[teximg <a href="$logurl">}.gettext("failed to generate image from code")."</a>]]";
100 sub gen_image ($$$$) { #{{{
101 # Actually creates the image.
105 my $imagedir = shift;
107 #TODO This should move into the setup file.
108 my $tex = '\documentclass['.$height.'pt]{scrartcl}';
109 $tex .= '\usepackage[version=3]{mhchem}';
110 $tex .= '\usepackage{amsmath}';
111 $tex .= '\usepackage{amsfonts}';
112 $tex .= '\usepackage{amssymb}';
113 $tex .= '\pagestyle{empty}';
114 $tex .= '\begin{document}';
115 $tex .= '$$'.$code.'$$';
116 $tex .= '\end{document}';
118 my $tmp = eval { create_tmp_dir($digest) };
120 writefile("$digest.tex", $tmp, $tex) &&
121 system("cd $tmp; latex --interaction=nonstopmode $tmp/$digest.tex > /dev/null") == 0 &&
122 system("dvips -E $tmp/$digest.dvi -o $tmp/$digest.ps 2> $tmp/$digest.log") == 0 &&
123 # ensure destination directory exists
124 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
125 system("convert -density 120 -trim -transparent \"#FFFFFF\" $tmp/$digest.ps $config{destdir}/$imagedir/$digest.png > $tmp/$digest.log") == 0) {
132 open(my $f, '<', "$tmp/$digest.log");
137 writefile("$digest.log", "$config{destdir}/$imagedir", $log);
143 sub create_tmp_dir ($) { #{{{
144 # Create a temp directory, it will be removed when ikiwiki exits.
147 my $template = $base.".XXXXXXXXXX";
148 my $tmpdir = tempdir($template, TMPDIR => 1, CLEANUP => 1);
153 # Check if the code is ok
159 qr/\\includegraphic/,
174 foreach my $thing (@badthings) {
175 if ($code =~ m/$thing/ ) {