]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/polygen.pm
update
[ikiwiki.git] / IkiWiki / Plugin / polygen.pm
1 #!/usr/bin/perl
2 #
3 # Include polygen output in a page
4
5 # by Enrico Zini
6 package IkiWiki::Plugin::polygen;
7
8 use warnings;
9 use strict;
10 use IkiWiki;
11 use File::Find;
12
13 sub import { #{{{
14         hook(type => "preprocess", id => "polygen", call => \&preprocess);
15 } # }}}
16
17 sub preprocess (@) { #{{{
18         my %params=@_;
19         my $grammar = ($params{grammar} or 'polygen');
20         my $symbol = ($params{symbol} or undef);
21
22         # Sanitize parameters
23         $grammar =~ IkiWiki::basename($grammar);
24         $grammar =~ s/\.grm$//;
25         $grammar .= '.grm';
26         $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
27         $symbol = IkiWiki::possibly_foolish_untaint($symbol) if defined $symbol;
28
29         my $grmfile = '/usr/share/polygen/ita/polygen.grm';
30         if (! -d '/usr/share/polygen') {
31                 return "[[polygen not installed]]";
32         }
33         find({wanted => sub {
34                         if (substr($File::Find::name, -length($grammar)) eq $grammar) {
35                                 $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
36                         }
37                 },
38                 no_chdir => 1,
39         }, '/usr/share/polygen');
40         
41         my $res;
42         if (defined $symbol) {
43                 $res = `polygen -S $symbol $grmfile 2>/dev/null`;
44         }
45         else {
46                 $res = `polygen $grmfile 2>/dev/null`;
47         }
48
49         if ($?) {
50                 $res="[[polygen failed]]";
51         }
52
53         # Strip trainling spaces and newlines so that we flow well with the
54         # markdown text
55         $res =~ s/\s*$//;
56         return $res;
57 } # }}}
58
59 1