Optionally accept and emit comments' url/email/ip.
[ikiwiki] / 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                         section => "widget",
24                 },
25 }
26
27 sub preprocess (@) {
28         my %params=@_;
29         my $grammar = ($params{grammar} or 'polygen');
30         my $symbol = ($params{symbol} or undef);
31
32         # Sanitize parameters
33         $grammar =~ IkiWiki::basename($grammar);
34         $grammar =~ s/[^A-Za-z0-9]//g;
35         $grammar =~ s/\.grm$//;
36         $grammar .= '.grm';
37         $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
38         $symbol = IkiWiki::possibly_foolish_untaint($symbol) if defined $symbol;
39
40         my $grmfile = '/usr/share/polygen/ita/polygen.grm';
41         if (! -d '/usr/share/polygen') {
42                 error gettext("polygen not installed");
43         }
44         find({wanted => sub {
45                         if (substr($File::Find::name, -length($grammar)) eq $grammar) {
46                                 $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
47                         }
48                 },
49                 no_chdir => 1,
50         }, '/usr/share/polygen');
51         
52         my $res;
53         if (defined $symbol) {
54                 $res = `polygen -S $symbol $grmfile 2>/dev/null`;
55         }
56         else {
57                 $res = `polygen $grmfile 2>/dev/null`;
58         }
59
60         if ($?) {
61                 error gettext("command failed");
62         }
63
64         # Strip trailing spaces and newlines so that we flow well with the
65         # markdown text
66         $res =~ s/\s*$//;
67         return $res;
68 }
69
70 1