preliminary rst plugin by Sergio Talens-Oliag
[ikiwiki] / doc / patchqueue / rst.mdwn
1 This is a whole lot better than nothing, but it's a shame it forks python
2 every page. Anyone want to get [this](http://search.cpan.org/~nodine/Text-Restructured-0.003016/) into Debian and use it instead?
3 -- [[Joey]]
4         
5         #!/usr/bin/perl
6         # Very simple reStructuredText processor.
7         #
8         # This plugin calls python and requires python-docutils to transform the text
9         # into html.
10         #
11         # It's main problem is that it does not support ikiwiki's WikiLinks nor
12         # Preprocessor Directives (in fact the same problem applies to the current
13         # Wikitext processor, although in that case the output looks less worse ;)
14         #
15         # Probably Wikilinks and Preprocessor Directives should support a list of
16         # extensions to process (i.e. the linkify function could be transformed into
17         # reStructuredText instead of HTML using a hook on rst.py instead of the
18         # current linkify function)
19         #
20         # by Sergio Talens-Oliag <sto@debian.org>
21         
22         package IkiWiki::Plugin::rst;
23         
24         use warnings;
25         use strict;
26         use IkiWiki;
27         use IPC::Open2;
28         
29         # Simple python script, maybe it should be implemented using an external script.
30         # The settings_overrides are given to avoid potential security risks when
31         # reading external files or if raw html is included on rst pages.
32         my $pyCmnd = "
33         from docutils.core import publish_string;
34         from sys import stdin;
35         html = publish_string(stdin.read(), writer_name='html', 
36                settings_overrides = { 'halt_level': 6, 
37                                       'file_insertion_enabled': 0,
38                                       'raw_enabled': 0 }
39         );
40         print html[html.find('<body>')+6:html.find('</body>')].strip();
41         ";
42         
43         sub import { #{{{
44                 IkiWiki::hook(type => "htmlize", id => "rst", call => \&htmlize);
45         } # }}}
46         
47         sub htmlize ($) { #{{{
48                 my $content = shift;
49         
50                 # Try to call python and run our command
51                 open2(*IN, *OUT, "python", "-c",  "$pyCmnd") or return $content;
52         
53                 # open2 doesn't respect "use open ':utf8'"
54                 binmode (IN, ':utf8');
55                 binmode (OUT, ':utf8');
56                 
57                 print OUT $content;
58                 close OUT;
59                 local $/ = undef;
60                 return <IN>;
61         } # }}}
62         
63         1