2 # -*- coding: utf-8 -*-
4 # rstproc — xml-rpc-based ikiwiki plugin to process RST files
6 # TODO: the top of this file should be converted to a python library for
9 # based a little bit on rst.pm by Sergio Talens-Oliag, but only a little bit. :)
11 # Copyright © martin f. krafft <madduck@madduck.net>
12 # Released under the terms of the GNU GPL version 2
15 __description__ = 'xml-rpc-based ikiwiki plugin to process RST files'
17 __author__ = 'martin f. krafft <madduck@madduck.net>'
18 __copyright__ = 'Copyright © ' + __author__
21 from docutils.core import publish_string;
26 import xml.parsers.expat
27 from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
35 print >>sys.stderr, __name__ + ':DEBUG:' + s
38 def rpc_read(processor):
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)
49 # debug('processed: ' + acc)
50 # debug('got back: ' + ret.__class__.__name__)
52 except xml.parsers.expat.ExpatError:
53 # debug('request invalid or incomplete: ' + acc)
57 def rpc_call(cmd, **kwargs):
58 call = xmlrpclib.dumps(sum(kwargs.items(), ()), cmd)
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)
70 SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
72 def process_request(self, req):
73 write(self._marshaled_dispatch(req))
75 def handle_request(self):
77 self.process_request(req)
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
90 content = html.split('<div class="document">', 1)[1]
91 content = content.split('</div>\n</body>')[:-1][0].strip()
92 # debug('content = ' + content)
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)
102 handler.handle_request()