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):
65 SimpleXMLRPCDispatcher.__init__(self)
67 def process_request(self, req):
68 write(self._marshaled_dispatch(req))
70 def handle_request(self):
72 self.process_request(req)
74 ret = rpc_read(processor)
75 if ret is not None: return ret
77 def rst2html(*kwargs):
78 # FIXME arguments should be treated as a hash, the order could change
79 # at any time and break this.
80 html = publish_string(kwargs[3], writer_name='html',
81 settings_overrides = { 'halt_level': 6
82 , 'file_insertion_enabled': 0
85 content = html.split('<div class="document">', 1)[1]
86 content = content.split('</div>\n</body>')[:-1][0].strip()
87 # debug('content = ' + content)
91 rpc_call('hook', type='htmlize', id='rst', call='rst2html')
93 handler = SimpleStdinOutXMLRPCHandler()
94 handler.register_function(importme, name='import')
95 handler.register_function(rst2html)
97 handler.handle_request()