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