]> sipb.mit.edu Git - ikiwiki.git/blob - plugins/rst
236ff1071de119395ba9eebccd02d13c462ac2ac
[ikiwiki.git] / plugins / rst
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # rst — xml-rpc-based ikiwiki plugin to process RST files
5 #
6 # based a little bit on rst.pm by Sergio Talens-Oliag, but only a little bit. :)
7 #
8 # Copyright © 2007-2008 martin f. krafft <madduck@madduck.net>
9 #             2007-2011 Joey Hess <joey@kitenet.net>
10 #             2009      Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
11 #             2011      Simon McVittie <smcv@debian.org>
12 #             2012      W. Trevor King <wking@tremily.us>
13
14 #  Redistribution and use in source and binary forms, with or without
15 # modification, are permitted provided that the following conditions
16 # are met:
17 # 1. Redistributions of source code must retain the above copyright
18 #    notice, this list of conditions and the following disclaimer.
19 # 2. Redistributions in binary form must reproduce the above copyright
20 #    notice, this list of conditions and the following disclaimer in the
21 #    documentation and/or other materials provided with the distribution.
22 # .
23 # THIS SOFTWARE IS PROVIDED BY IKIWIKI AND CONTRIBUTORS ``AS IS''
24 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
27 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 # SUCH DAMAGE.
35
36 __name__ = 'rst'
37 __description__ = 'xml-rpc-based ikiwiki plugin to process RST files'
38 __version__ = '0.3'
39 __author__ = 'martin f. krafft <madduck@madduck.net>'
40 __copyright__ = 'Copyright © ' + __author__
41 __licence__ = 'BSD-2-clause'
42
43 from proxy import IkiWikiProcedureProxy
44
45 publish_parts = None
46
47 def rst2html(proxy, *args):
48     # delayed import so docutils is only needed if you *use* rst -
49     # http://bugs.debian.org/637604
50     global publish_parts
51     if publish_parts is None:
52         try:
53             from docutils.core import publish_parts
54         except ImportError, e:
55             proxy.error('cannot import docutils.core: %s: %s' %
56                         (e.__class__.__name__, e))
57             raise
58
59     kwargs = _to_dict(args)
60     parts = publish_parts(kwargs["content"],
61                           writer_name="html",
62                           settings_overrides = { 'halt_level': 6
63                                                , 'file_insertion_enabled': 0
64                                                , 'raw_enabled': 1
65                                                })
66     return '\n'.join(parts['html_body'].splitlines()[1:-1])
67
68 def _to_dict(args):
69     # args is a list paired by key, value, so we turn it into a dict
70     return dict((k, v) for k, v in zip(*[iter(args)]*2))
71
72 def getsetup(proxy, *kwargs):
73     return 'plugin', { 'safe' : 1, 'rebuild' : 1, 'section' : 'format' }
74
75 import sys
76 def debug(s):
77     sys.stderr.write(__name__ + ':DEBUG:%s\n' % s)
78     sys.stderr.flush()
79
80 proxy = IkiWikiProcedureProxy(__name__, debug_fn=None)
81 proxy.hook('getsetup', getsetup)
82 proxy.hook('htmlize', rst2html)
83 proxy.run()