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