summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/polygen.pm
blob: 2ed81c8e46d13ec0d94f1d6d38b73dfdd8cb0573 (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. if (! -d '/usr/share/polygen') {
  26. return "[[polygen not installed]]";
  27. }
  28. find({wanted => sub {
  29. if (substr($File::Find::name, -length($grammar)) eq $grammar) {
  30. $grmfile = IkiWiki::possibly_foolish_untaint($File::Find::name);
  31. }
  32. },
  33. no_chdir => 1,
  34. }, '/usr/share/polygen');
  35. my $res;
  36. if (defined $symbol) {
  37. $res = `polygen -S $symbol $grmfile 2>/dev/null`;
  38. }
  39. else {
  40. $res = `polygen $grmfile 2>/dev/null`;
  41. }
  42. if ($?) {
  43. $res="[[polygen failed]]";
  44. }
  45. # Strip trainling spaces and newlines so that we flow well with the
  46. # markdown text
  47. $res =~ s/\s*$//;
  48. return $res;
  49. } # }}}
  50. 1