Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / polygen.pm
blob78e3611e1c1aa9a592ff6f156d3b796a01b9a5bd
1 #!/usr/bin/perl
3 # Include polygen output in a page
4 #
5 # by Enrico Zini
6 package IkiWiki::Plugin::polygen;
8 use warnings;
9 use strict;
10 use IkiWiki 3.00;
11 use File::Find;
13 sub import {
14 hook(type => "getsetup", id => "polygen", call => \&getsetup);
15 hook(type => "preprocess", id => "polygen", call => \&preprocess);
18 sub getsetup () {
19 return
20 plugin => {
21 safe => 1,
22 rebuild => undef,
23 section => "widget",
27 sub preprocess (@) {
28 my %params=@_;
29 my $grammar = ($params{grammar} or 'polygen');
30 my $symbol = ($params{symbol} or undef);
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;
40 my $grmfile = '/usr/share/polygen/ita/polygen.grm';
41 if (! -d '/usr/share/polygen') {
42 error gettext("polygen not installed");
44 find({wanted => sub {
45 if (substr($File::Find::name, -length($grammar)) eq $grammar) {
46 $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
49 no_chdir => 1,
50 }, '/usr/share/polygen');
52 my $res;
53 if (defined $symbol) {
54 $res = `polygen -S $symbol $grmfile 2>/dev/null`;
56 else {
57 $res = `polygen $grmfile 2>/dev/null`;
60 if ($?) {
61 error gettext("command failed");
64 # Strip trailing spaces and newlines so that we flow well with the
65 # markdown text
66 $res =~ s/\s*$//;
67 return $res;