2006-07-29 22:01:29 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Include polygen output in a page
|
|
|
|
#
|
|
|
|
# by Enrico Zini
|
|
|
|
package IkiWiki::Plugin::polygen;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-07-29 22:01:29 +02:00
|
|
|
use File::Find;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 23:20:21 +02:00
|
|
|
hook(type => "getsetup", id => "polygen", call => \&getsetup);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "preprocess", id => "polygen", call => \&preprocess);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 22:01:29 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 23:20:21 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 23:20:21 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 23:20:21 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2006-07-29 22:01:29 +02:00
|
|
|
my %params=@_;
|
|
|
|
my $grammar = ($params{grammar} or 'polygen');
|
|
|
|
my $symbol = ($params{symbol} or undef);
|
2015-06-09 12:40:14 +02:00
|
|
|
my $options = ($config{deterministic} ? '-seed 42' : '');
|
2006-07-29 22:01:29 +02:00
|
|
|
|
|
|
|
# Sanitize parameters
|
|
|
|
$grammar =~ IkiWiki::basename($grammar);
|
2006-10-22 23:12:50 +02:00
|
|
|
$grammar =~ s/[^A-Za-z0-9]//g;
|
2006-07-29 22:01:29 +02:00
|
|
|
$grammar =~ s/\.grm$//;
|
|
|
|
$grammar .= '.grm';
|
|
|
|
$symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
|
2006-08-30 00:09:16 +02:00
|
|
|
$symbol = IkiWiki::possibly_foolish_untaint($symbol) if defined $symbol;
|
2006-07-29 22:01:29 +02:00
|
|
|
|
|
|
|
my $grmfile = '/usr/share/polygen/ita/polygen.grm';
|
2006-07-30 23:54:03 +02:00
|
|
|
if (! -d '/usr/share/polygen') {
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("polygen not installed");
|
2006-07-30 23:54:03 +02:00
|
|
|
}
|
2006-07-29 22:01:29 +02:00
|
|
|
find({wanted => sub {
|
|
|
|
if (substr($File::Find::name, -length($grammar)) eq $grammar) {
|
|
|
|
$grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
no_chdir => 1,
|
|
|
|
}, '/usr/share/polygen');
|
|
|
|
|
|
|
|
my $res;
|
|
|
|
if (defined $symbol) {
|
2015-06-09 12:40:14 +02:00
|
|
|
$res = `polygen -S $symbol $options $grmfile 2>/dev/null`;
|
2006-07-29 22:01:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-06-09 12:40:14 +02:00
|
|
|
$res = `polygen $options $grmfile 2>/dev/null`;
|
2006-07-29 22:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($?) {
|
2008-07-13 21:05:34 +02:00
|
|
|
error gettext("command failed");
|
2006-07-29 22:01:29 +02:00
|
|
|
}
|
|
|
|
|
2006-12-29 05:38:40 +01:00
|
|
|
# Strip trailing spaces and newlines so that we flow well with the
|
2006-07-29 22:01:29 +02:00
|
|
|
# markdown text
|
|
|
|
$res =~ s/\s*$//;
|
|
|
|
return $res;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 22:01:29 +02:00
|
|
|
|
|
|
|
1
|