summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/polygen.pm
blob: 82fd575a78e5b921e1c73977626ff11d44825b4f (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. $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 "[[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="[[polygen failed]]";
  45. }
  46. # Strip trainling spaces and newlines so that we flow well with the
  47. # markdown text
  48. $res =~ s/\s*$//;
  49. return $res;
  50. } # }}}
  51. 1