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