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