summaryrefslogtreecommitdiff
path: root/plugins/rst
blob: abf835e2ed01e297679bda0475a83cd5d1bf4756 (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):
  57. SimpleXMLRPCDispatcher.__init__(self)
  58. def process_request(self, req):
  59. write(self._marshaled_dispatch(req))
  60. def handle_request(self):
  61. def processor(req):
  62. self.process_request(req)
  63. while True:
  64. ret = rpc_read(processor)
  65. if ret is not None: return ret
  66. def rst2html(*kwargs):
  67. # FIXME arguments should be treated as a hash, the order could change
  68. # at any time and break this.
  69. html = publish_string(kwargs[3], writer_name='html',
  70. settings_overrides = { 'halt_level': 6
  71. , 'file_insertion_enabled': 0
  72. , 'raw_enabled': 1
  73. })
  74. content = html.split('<div class="document">', 1)[1]
  75. content = content.split('</div>\n</body>')[:-1][0].strip()
  76. # debug('content = ' + content)
  77. return content
  78. def importme():
  79. rpc_call('hook', type='htmlize', id='rst', call='rst2html')
  80. handler = SimpleStdinOutXMLRPCHandler()
  81. handler.register_function(importme, name='import')
  82. handler.register_function(rst2html)
  83. while True:
  84. handler.handle_request()