]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/polygen.pm
Merge branch 'master' of git://git.ikiwiki.info
[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 3.00;
11 use File::Find;
12
13 sub import {
14         hook(type => "getsetup", id => "polygen", call => \&getsetup);
15         hook(type => "preprocess", id => "polygen", call => \&preprocess);
16 }
17
18 sub getsetup () {
19         return 
20                 plugin => {
21                         safe => 1,
22                         rebuild => undef,
23                 },
24 }
25
26 sub preprocess (@) {
27         my %params=@_;
28         my $grammar = ($params{grammar} or 'polygen');
29         my $symbol = ($params{symbol} or undef);
30
31         # Sanitize parameters
32         $grammar =~ IkiWiki::basename($grammar);
33         $grammar =~ s/[^A-Za-z0-9]//g;
34         $grammar =~ s/\.grm$//;
35         $grammar .= '.grm';
36         $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
37         $symbol = IkiWiki::possibly_foolish_untaint($symbol) if defined $symbol;
38
39         my $grmfile = '/usr/share/polygen/ita/polygen.grm';
40         if (! -d '/usr/share/polygen') {
41                 error gettext("polygen not installed");
42         }
43         find({wanted => sub {
44                         if (substr($File::Find::name, -length($grammar)) eq $grammar) {
45                                 $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
46                         }
47                 },
48                 no_chdir => 1,
49         }, '/usr/share/polygen');
50         
51         my $res;
52         if (defined $symbol) {
53                 $res = `polygen -S $symbol $grmfile 2>/dev/null`;
54         }
55         else {
56                 $res = `polygen $grmfile 2>/dev/null`;
57         }
58
59         if ($?) {
60                 error gettext("command failed");
61         }
62
63         # Strip trailing spaces and newlines so that we flow well with the
64         # markdown text
65         $res =~ s/\s*$//;
66         return $res;
67 }
68
69 1