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 if SimpleXMLRPCDispatcher.__init__.func_code.co_argcount == 1:
66 # python2.4 and before only took one argument
67 SimpleXMLRPCDispatcher.__init__(self)
69 SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
71 def process_request(self, req):
72 write(self._marshaled_dispatch(req))
74 def handle_request(self):
76 self.process_request(req)
78 ret = rpc_read(processor)
79 if ret is not None: return ret
81 def rst2html(*kwargs):
82 # FIXME arguments should be treated as a hash, the order could change
83 # at any time and break this.
84 html = publish_string(kwargs[3], writer_name='html',
85 settings_overrides = { 'halt_level': 6
86 , 'file_insertion_enabled': 0
89 content = html.split('<div class="document">', 1)[1]
90 content = content.split('</div>\n</body>')[:-1][0].strip()
91 # debug('content = ' + content)
95 rpc_call('hook', type='htmlize', id='rst', call='rst2html')
97 handler = SimpleStdinOutXMLRPCHandler()
98 handler.register_function(importme, name='import')
99 handler.register_function(rst2html)
101 handler.handle_request()