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