PageRenderTime 82ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/tool/rest/rest.py

https://bitbucket.org/evelyn559/pypy
Python | 79 lines | 57 code | 7 blank | 15 comment | 11 complexity | 8182dce07f3be1584308619ab9480cd8 MD5 | raw file
  1. import py
  2. import sys, os, traceback
  3. import re
  4. if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()):
  5. def log(msg):
  6. print msg
  7. else:
  8. def log(msg):
  9. pass
  10. def convert_rest_html(source, source_path, stylesheet=None, encoding='latin1'):
  11. """ return html latin1-encoded document for the given input.
  12. source a ReST-string
  13. sourcepath where to look for includes (basically)
  14. stylesheet path (to be used if any)
  15. """
  16. from docutils.core import publish_string
  17. kwargs = {
  18. 'stylesheet' : stylesheet,
  19. 'stylesheet_path': None,
  20. 'traceback' : 1,
  21. 'embed_stylesheet': 0,
  22. 'output_encoding' : encoding,
  23. #'halt' : 0, # 'info',
  24. 'halt_level' : 2,
  25. }
  26. # docutils uses os.getcwd() :-(
  27. source_path = os.path.abspath(str(source_path))
  28. prevdir = os.getcwd()
  29. try:
  30. #os.chdir(os.path.dirname(source_path))
  31. return publish_string(source, source_path, writer_name='html',
  32. settings_overrides=kwargs)
  33. finally:
  34. os.chdir(prevdir)
  35. def process(txtpath, encoding='latin1'):
  36. """ process a textfile """
  37. log("processing %s" % txtpath)
  38. assert txtpath.check(ext='.txt')
  39. if isinstance(txtpath, py.path.svnwc):
  40. txtpath = txtpath.localpath
  41. htmlpath = txtpath.new(ext='.html')
  42. #svninfopath = txtpath.localpath.new(ext='.svninfo')
  43. style = txtpath.dirpath('style.css')
  44. if style.check():
  45. stylesheet = style.basename
  46. else:
  47. stylesheet = None
  48. content = unicode(txtpath.read(), encoding)
  49. doc = convert_rest_html(content, txtpath, stylesheet=stylesheet, encoding=encoding)
  50. htmlpath.write(doc)
  51. #log("wrote %r" % htmlpath)
  52. #if txtpath.check(svnwc=1, versioned=1):
  53. # info = txtpath.info()
  54. # svninfopath.dump(info)
  55. rex1 = re.compile(ur'.*<body>(.*)</body>.*', re.MULTILINE | re.DOTALL)
  56. rex2 = re.compile(ur'.*<div class="document">(.*)</div>.*', re.MULTILINE | re.DOTALL)
  57. def strip_html_header(string, encoding='utf8'):
  58. """ return the content of the body-tag """
  59. uni = unicode(string, encoding)
  60. for rex in rex1,rex2:
  61. match = rex.search(uni)
  62. if not match:
  63. break
  64. uni = match.group(1)
  65. return uni
  66. class Project: # used for confrest.py files
  67. def __init__(self, sourcepath):
  68. self.sourcepath = sourcepath
  69. def process(self, path):
  70. return process(path)
  71. def get_htmloutputpath(self, path):
  72. return path.new(ext='html')