]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - plugins/rst
Provide XML-RPC proxy abstraction for Python plugins
[git.ikiwiki.info.git] / plugins / rst
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # rstproc — xml-rpc-based ikiwiki plugin to process RST files
5 #
6 # TODO: the top of this file should be converted to a python library for
7 # ikiwiki plugins
8
9 # based a little bit on rst.pm by Sergio Talens-Oliag, but only a little bit. :)
10 #
11 # Copyright © martin f. krafft <madduck@madduck.net>
12 # Released under the terms of the GNU GPL version 2
14 __name__ = 'rstproc'
15 __description__ = 'xml-rpc-based ikiwiki plugin to process RST files'
16 __version__ = '0.2'
17 __author__ = 'martin f. krafft <madduck@madduck.net>'
18 __copyright__ = 'Copyright © ' + __author__
19 __licence__ = 'GPLv2'
21 from docutils.core import publish_string;
22 import posix
23 import select
24 import sys
25 import xmlrpclib
26 import xml.parsers.expat
27 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
29 def write(s):
30     # no comment
31     sys.stdout.write(s)
32     sys.stdout.flush()
34 def debug(s):
35     print >>sys.stderr, __name__ + ':DEBUG:' + s
36     sys.stderr.flush()
38 def rpc_read(processor):
39     acc = ''
40     ret = None
41     while True:
42         line = sys.stdin.readline()
43         if line is None: continue
44         if len(line) == 0: sys.exit(posix.EX_OK)
45 #        debug('read line: ' + line)
46         acc += line
47         try:
48             ret = processor(acc)
49 #            debug('processed: ' + acc)
50 #            debug('got back: ' + ret.__class__.__name__)
51             return ret
52         except xml.parsers.expat.ExpatError:
53 #            debug('request invalid or incomplete: ' + acc)
54             pass
55     return None
57 def rpc_call(cmd, **kwargs):
58     call = xmlrpclib.dumps(sum(kwargs.items(), ()), cmd)
59     write(call + '\n')
60     resp = rpc_read(lambda resp: resp)
62 class SimpleStdinOutXMLRPCHandler(SimpleXMLRPCDispatcher):
64     def __init__(self, allow_none=False, encoding=None):
65         # see http://bugs.debian.org/470645
66         if SimpleXMLRPCDispatcher.__init__.func_code.co_argcount == 1:
67             # python2.4 and before only took one argument
68             SimpleXMLRPCDispatcher.__init__(self)
69         else:
70             SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
72     def process_request(self, req):
73         write(self._marshaled_dispatch(req))
75     def handle_request(self):
76         def processor(req):
77             self.process_request(req)
78         while True:
79             ret = rpc_read(processor)
80             if ret is not None: return ret
82 def rst2html(*kwargs):
83     # FIXME arguments should be treated as a hash, the order could change
84     # at any time and break this.
85     html = publish_string(kwargs[3], writer_name='html',
86             settings_overrides = { 'halt_level': 6
87                                  , 'file_insertion_enabled': 0
88                                  , 'raw_enabled': 1
89                                  })
90     content = html.split('<div class="document">', 1)[1]
91     content = content.split('</div>\n</body>')[:-1][0].strip()
92 #    debug('content = ' + content)
93     return content
95 def importme():
96     rpc_call('hook', type='htmlize', id='rst', call='rst2html')
98 handler = SimpleStdinOutXMLRPCHandler()
99 handler.register_function(importme, name='import')
100 handler.register_function(rst2html)
101 while True:
102     handler.handle_request()