summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/polygen.pm
blob: 05ad4416ec58c7ad4962305a20dfef6374a47dfa (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/\.grm$//;
  21. $grammar .= '.grm';
  22. $symbol =~ s/[^A-Za-z0-9]//g if defined $symbol;
  23. $symbol = IkiWiki::possibly_foolish_untaint($symbol) 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