1 [[!template id=plugin name=asymptote author="PeterSimons"]]
3 This plugin provides the [[ikiwiki/directive/asymptote]]
4 [[ikiwiki/directive]] which allows embedding
5 [asymptote](http://asymptote.sourceforge.net/) diagrams in a page.
7 Security implications: asymptote has functions for reading files and
8 other dangerous stuff, so enabling this plugin means that everyone who
9 can edit your Wiki can also read any file from your hard drive thats
10 accessible to the user running Ikiwiki.
12 [[!if test="enabled(asymptote)" then="""
18 triangle t = triangle((0,0), (4,0), (0.5,2));
19 show(La="$D$", Lb="$E$", Lc="", t);
21 point pD = midpoint(t.BC); dot(pD);
22 point pE = midpoint(t.AC); dot(pE);
25 point A_ = (pD-t.A)*2+t.A; dot("$A'$", A_, NE);
26 draw(t.B--A_--t.C, dashed);
27 draw(t.A--A_, dashed);
29 point E_ = midpoint(line(t.B,A_)); dot(Label("$E'$", E_, E));
34 This plugin uses the [[!cpan Digest::SHA]] perl module.
36 The full source code is:
40 package IkiWiki::Plugin::asymptote;
43 use Digest::MD5 qw(md5_hex);
44 use File::Temp qw(tempdir);
50 hook(type => "getsetup", id => "asymptote", call => \&getsetup);
51 hook(type => "preprocess", id => "asymptote", call => \&preprocess);
66 my $code = $params{src};
67 if (! defined $code && ! length $code) {
68 error gettext("missing src attribute");
70 return create($code, \%params);
74 # This function calls the image generating function and returns
75 # the <img .. /> for the generated image.
79 my $digest = md5_hex(Encode::encode_utf8($code));
81 my $imglink= $params->{page} . "/$digest.png";
82 my $imglog = $params->{page} . "/$digest.log";
83 will_render($params->{page}, $imglink);
84 will_render($params->{page}, $imglog);
86 my $imgurl=urlto($imglink, $params->{destpage});
87 my $logurl=urlto($imglog, $params->{destpage});
89 if (-e "$config{destdir}/$imglink" ||
90 gen_image($code, $digest, $params->{page})) {
91 return qq{<img src="$imgurl}
92 .(exists $params->{alt} ? qq{" alt="} . $params->{alt} : qq{})
93 .qq{" class="asymptote" />};
96 error qq{<a href="$logurl">}.gettext("failed to generate image from code")."</a>";
100 sub gen_image ($$$$) {
101 # Actually creates the image.
104 my $imagedir = shift;
106 my $tmp = eval { create_tmp_dir($digest) };
108 writefile("$digest.asy", $tmp, $code) &&
109 writefile("$imagedir/$digest.png", $config{destdir}, "") &&
110 system("asy -render=2 -offscreen -f png -o $config{destdir}/$imagedir/$digest.png $tmp/$digest.asy &>$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);