summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/polygen.pm
blob: c148891672ebc54cfc6196b1143d26725c95974e (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Include polygen output in a page
  4. #
  5. # by Enrico Zini
  6. package IkiWiki::Plugin::polygen;
  7. use warnings;
  8. use strict;
  9. use IkiWiki;
  10. use File::Find;
  11. sub import { #{{{
  12. IkiWiki::hook(type => "preprocess", id => "polygen",
  13. call => \&preprocess);
  14. } # }}}
  15. sub preprocess (@) { #{{{
  16. my %params=@_;
  17. my $grammar = ($params{grammar} or 'polygen');
  18. my $symbol = ($params{symbol} or undef);
  19. # Sanitize parameters
  20. $grammar =~ IkiWiki::basename($grammar);
  21. $grammar =~ s/\.grm$//;
  22. $grammar .= '.grm';
  23. $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
  24. my $grmfile = '/usr/share/polygen/ita/polygen.grm';
  25. find({wanted => sub {
  26. if (substr($File::Find::name, -length($grammar)) eq $grammar) {
  27. $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
  28. }
  29. },
  30. no_chdir => 1,
  31. }, '/usr/share/polygen');
  32. my $res;
  33. if (defined $symbol) {
  34. $res = `polygen -S $symbol $grmfile 2>/dev/null`;
  35. }
  36. else {
  37. $res = `polygen $grmfile 2>/dev/null`;
  38. }
  39. if ($?) {
  40. $res="[[polygen failed]]";
  41. }
  42. # Strip trainling spaces and newlines so that we flow well with the
  43. # markdown text
  44. $res =~ s/\s*$//;
  45. return $res;
  46. } # }}}
  47. 1