summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/rst.pm
blob: 1f4c8bf9e9c0adf6081d9a67fd04c23a32b3975d (plain)
  1. #!/usr/bin/perl
  2. # Very simple reStructuredText processor.
  3. #
  4. # This plugin calls python and requires python-docutils to transform the text
  5. # into html.
  6. #
  7. # Its main problem is that it does not support ikiwiki's WikiLinks nor
  8. # Preprocessor Directives.
  9. #
  10. # Probably Wikilinks and Preprocessor Directives should support a list of
  11. # extensions to process (i.e. the linkify function could be transformed into
  12. # reStructuredText instead of HTML using a hook on rst.py instead of the
  13. # current linkify function)
  14. #
  15. # by Sergio Talens-Oliag <sto@debian.org>
  16. package IkiWiki::Plugin::rst;
  17. use warnings;
  18. use strict;
  19. use IkiWiki 2.00;
  20. use IPC::Open2;
  21. # Simple python script, maybe it should be implemented using an external script.
  22. # The settings_overrides are given to avoid potential security risks when
  23. # reading external files or if raw html is included on rst pages.
  24. my $pyCmnd = "
  25. from docutils.core import publish_string;
  26. from sys import stdin;
  27. html = publish_string(stdin.read(), writer_name='html',
  28. settings_overrides = { 'halt_level': 6,
  29. 'file_insertion_enabled': 0,
  30. 'raw_enabled': 0 }
  31. );
  32. print html[html.find('<body>')+6:html.find('</body>')].strip();
  33. ";
  34. sub import { #{{{
  35. hook(type => "htmlize", id => "rst", call => \&htmlize);
  36. } # }}}
  37. sub htmlize (@) { #{{{
  38. my %params=@_;
  39. my $content=$params{content};
  40. my $pid;
  41. my $sigpipe=0;
  42. $SIG{PIPE}=sub { $sigpipe=1 };
  43. $pid=open2(*IN, *OUT, "python", "-c", $pyCmnd);
  44. # open2 doesn't respect "use open ':utf8'"
  45. binmode (IN, ':utf8');
  46. binmode (OUT, ':utf8');
  47. print OUT $content;
  48. close OUT;
  49. local $/ = undef;
  50. my $ret=<IN>;
  51. close IN;
  52. waitpid $pid, 0;
  53. return $content if $sigpipe;
  54. $SIG{PIPE}="DEFAULT";
  55. return $ret;
  56. } # }}}
  57. 1