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