]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/polygen.pm
when autogenerating a dir naem, include "feed/" in it
[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         IkiWiki::hook(type => "preprocess", id => "polygen",
15                 call => \&preprocess);
16 } # }}}
17
18 sub preprocess (@) { #{{{
19         my %params=@_;
20         my $grammar = ($params{grammar} or 'polygen');
21         my $symbol = ($params{symbol} or undef);
22
23         # Sanitize parameters
24         $grammar =~ IkiWiki::basename($grammar);
25         $grammar =~ s/\.grm$//;
26         $grammar .= '.grm';
27         $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
28
29         my $grmfile = '/usr/share/polygen/ita/polygen.grm';
30         find({wanted => sub {
31                         if (substr($File::Find::name, -length($grammar)) eq $grammar) {
32                                 $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
33                         }
34                 },
35                 no_chdir => 1,
36         }, '/usr/share/polygen');
37         
38         my $res;
39         if (defined $symbol) {
40                 $res = `polygen -S $symbol $grmfile 2>/dev/null`;
41         }
42         else {
43                 $res = `polygen $grmfile 2>/dev/null`;
44         }
45
46         if ($?) {
47                 $res="[[polygen failed]]";
48         }
49
50         # Strip trainling spaces and newlines so that we flow well with the
51         # markdown text
52         $res =~ s/\s*$//;
53         return $res;
54 } # }}}
55
56 1