web commit by http://harningt.eharning.us/: Added TrustBearer OpenID link, great...
[ikiwiki] / 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
13
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'
20
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
28
29 def write(s):
30     # no comment
31     sys.stdout.write(s)
32     sys.stdout.flush()
33
34 def debug(s):
35     print >>sys.stderr, __name__ + ':DEBUG:' + s
36     sys.stderr.flush()
37
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
56
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)
61
62 class SimpleStdinOutXMLRPCHandler(SimpleXMLRPCDispatcher):
63
64     def __init__(self):
65         SimpleXMLRPCDispatcher.__init__(self)
66
67     def process_request(self, req):
68         write(self._marshaled_dispatch(req))
69
70     def handle_request(self):
71         def processor(req):
72             self.process_request(req)
73         while True:
74             ret = rpc_read(processor)
75             if ret is not None: return ret
76
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
83                                  , 'raw_enabled': 1
84                                  })
85     content = html.split('<div class="document">', 1)[1]
86     content = content.split('</div>\n</body>')[:-1][0].strip()
87 #    debug('content = ' + content)
88     return content
89
90 def importme():
91     rpc_call('hook', type='htmlize', id='rst', call='rst2html')
92
93 handler = SimpleStdinOutXMLRPCHandler()
94 handler.register_function(importme, name='import')
95 handler.register_function(rst2html)
96 while True:
97     handler.handle_request()