summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/polygen.pm
blob: 63f6bf5db59b7ad94987ca620fcda7ae837e973d (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. hook(type => "preprocess", id => "polygen", call => \&preprocess);
  13. } # }}}
  14. sub preprocess (@) { #{{{
  15. my %params=@_;
  16. my $grammar = ($params{grammar} or 'polygen');
  17. my $symbol = ($params{symbol} or undef);
  18. # Sanitize parameters
  19. $grammar =~ IkiWiki::basename($grammar);
  20. $grammar =~ s/[^A-Za-z0-9]//g;
  21. $grammar =~ s/\.grm$//;
  22. $grammar .= '.grm';
  23. $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
  24. $symbol = IkiWiki::possibly_foolish_untaint($symbol) if defined $symbol;
  25. my $grmfile = '/usr/share/polygen/ita/polygen.grm';
  26. if (! -d '/usr/share/polygen') {
  27. return "[[".gettext("polygen not installed")."]]";
  28. }
  29. find({wanted => sub {
  30. if (substr($File::Find::name, -length($grammar)) eq $grammar) {
  31. $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
  32. }
  33. },
  34. no_chdir => 1,
  35. }, '/usr/share/polygen');
  36. my $res;
  37. if (defined $symbol) {
  38. $res = `polygen -S $symbol $grmfile 2>/dev/null`;
  39. }
  40. else {
  41. $res = `polygen $grmfile 2>/dev/null`;
  42. }
  43. if ($?) {
  44. $res="[[".gettext("polygen failed")."]]";
  45. }
  46. # Strip trailing spaces and newlines so that we flow well with the
  47. # markdown text
  48. $res =~ s/\s*$//;
  49. return $res;
  50. } # }}}
  51. 1