summaryrefslogtreecommitdiff
path: root/plugins/rst
blob: 1d18dd7751be2ad8771a25d76ccaad1e6ce41fa7 (plain)
  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. __name__ = 'rstproc'
  14. __description__ = 'xml-rpc-based ikiwiki plugin to process RST files'
  15. __version__ = '0.2'
  16. __author__ = 'martin f. krafft <madduck@madduck.net>'
  17. __copyright__ = 'Copyright © ' + __author__
  18. __licence__ = 'GPLv2'
  19. from docutils.core import publish_string;
  20. import posix
  21. import select
  22. import sys
  23. import xmlrpclib
  24. import xml.parsers.expat
  25. from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
  26. def write(s):
  27. # no comment
  28. sys.stdout.write(s)
  29. sys.stdout.flush()
  30. def debug(s):
  31. print >>sys.stderr, __name__ + ':DEBUG:' + s
  32. sys.stderr.flush()
  33. def rpc_read(processor):
  34. acc = ''
  35. ret = None
  36. while True:
  37. line = sys.stdin.readline()
  38. if line is None: continue
  39. if len(line) == 0: sys.exit(posix.EX_OK)
  40. # debug('read line: ' + line)
  41. acc += line
  42. try:
  43. ret = processor(acc)
  44. # debug('processed: ' + acc)
  45. # debug('got back: ' + ret.__class__.__name__)
  46. return ret
  47. except xml.parsers.expat.ExpatError:
  48. # debug('request invalid or incomplete: ' + acc)
  49. pass
  50. return None
  51. def rpc_call(cmd, **kwargs):
  52. call = xmlrpclib.dumps(sum(kwargs.items(), ()), cmd)
  53. write(call + '\n')
  54. resp = rpc_read(lambda resp: resp)
  55. class SimpleStdinOutXMLRPCHandler(SimpleXMLRPCDispatcher):
  56. def __init__(self, allow_none=False, encoding=None):
  57. if SimpleXMLRPCDispatcher.__init__.func_code.co_argcount == 1:
  58. # python2.4 and before only took one argument
  59. SimpleXMLRPCDispatcher.__init__(self)
  60. else:
  61. SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
  62. def process_request(self, req):
  63. write(self._marshaled_dispatch(req))
  64. def handle_request(self):
  65. def processor(req):
  66. self.process_request(req)
  67. while True:
  68. ret = rpc_read(processor)
  69. if ret is not None: return ret
  70. def rst2html(*kwargs):
  71. # FIXME arguments should be treated as a hash, the order could change
  72. # at any time and break this.
  73. html = publish_string(kwargs[3], writer_name='html',
  74. settings_overrides = { 'halt_level': 6
  75. , 'file_insertion_enabled': 0
  76. , 'raw_enabled': 1
  77. })
  78. content = html.split('<div class="document">', 1)[1]
  79. content = content.split('</div>\n</body>')[:-1][0].strip()
  80. # debug('content = ' + content)
  81. return content
  82. def importme():
  83. rpc_call('hook', type='htmlize', id='rst', call='rst2html')
  84. handler = SimpleStdinOutXMLRPCHandler()
  85. handler.register_function(importme, name='import')
  86. handler.register_function(rst2html)
  87. while True:
  88. handler.handle_request()