]> sipb.mit.edu Git - ikiwiki.git/blob - plugins/rst
Added a comment
[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.4'
39 __author__ = 'martin f. krafft <madduck@madduck.net>'
40 __copyright__ = 'Copyright © ' + __author__
41 __licence__ = 'BSD-2-clause'
42
43 import sys as _sys
44
45 from proxy import IkiWikiProcedureProxy
46
47
48 publish_parts = None
49
50
51 def rst2html(proxy, *args):
52     # delayed import so docutils is only needed if you *use* rst -
53     # http://bugs.debian.org/637604
54     global publish_parts
55     if publish_parts is None:
56         try:
57             from docutils.core import publish_parts
58         except ImportError as e:
59             proxy.error('cannot import docutils.core: {0}: {1}'.format(
60                     e.__class__.__name__, e))
61             raise
62
63     kwargs = _to_dict(args)
64     parts = publish_parts(kwargs["content"],
65                           writer_name="html",
66                           settings_overrides = { 'halt_level': 6
67                                                , 'file_insertion_enabled': 0
68                                                , 'raw_enabled': 1
69                                                })
70     return '\n'.join(parts['html_body'].splitlines()[1:-1])
71
72 def _to_dict(args):
73     # args is a list paired by key, value, so we turn it into a dict
74     return dict((k, v) for k, v in zip(*[iter(args)]*2))
75
76 def getsetup(proxy, *kwargs):
77     return 'plugin', { 'safe' : 1, 'rebuild' : 1, 'section' : 'format' }
78
79 def debug(s):
80     _sys.stderr.write(__name__ + ':DEBUG:{0}\n'.format(s))
81     _sys.stderr.flush()
82
83 proxy = IkiWikiProcedureProxy(__name__, debug_fn=None)
84 proxy.hook('getsetup', getsetup)
85 proxy.hook('htmlize', rst2html)
86 proxy.run()