Package eggbasket :: Module rest
[hide private]

Source Code for Module eggbasket.rest

  1  ############################################################################## 
  2  # 
  3  # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved. 
  4  # 
  5  # This software is subject to the provisions of the Zope Public License, 
  6  # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution. 
  7  # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED 
  8  # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  9  # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
 10  # FOR A PARTICULAR PURPOSE 
 11  # 
 12  ############################################################################## 
 13   
 14  """ rest-to-html conversion taken from the Zope core  
 15  This implementation requires docutils 0.4.0+ from http://docutils.sf.net/ 
 16  """ 
 17   
 18  try: 
 19      import docutils 
 20  except ImportError: 
 21      raise ImportError, 'Please install docutils 0.4.0+ from http://docutils.sourceforge.net/#download.' 
 22   
 23  version = docutils.__version__.split('.') 
 24  if not (version >= ['0', '4', '0'] or  version >= ['0', '4']): 
 25      raise ImportError, """Old version of docutils found: 
 26  Got: %(version)s, required: 0.4.0+ 
 27  Please remove docutils from %(path)s and replace it with a new version. You 
 28  can download docutils at http://docutils.sourceforge.net/#download. 
 29  """ % {'version' : docutils.__version__, 'path' : docutils.__path__[0] } 
 30   
 31  # Disable inclusion of files for security reasons.  We do this by 
 32  # changing the default value of the ``file_insertion_enabled`` 
 33  # parameter to False. 
 34  import docutils.parsers.rst 
 35  for title, options, conf in docutils.parsers.rst.Parser.settings_spec[2]: 
 36      if options == ['--file-insertion-enabled']: 
 37          conf['default'] = 0 
 38          break 
 39   
 40  import sys, os, locale 
 41  from docutils.core import publish_parts 
 42   
 43  # get encoding 
 44  default_enc = sys.getdefaultencoding() 
 45  default_output_encoding = 'unicode' 
 46  default_input_encoding = 'unicode' 
 47   
 48  # starting level for <H> elements (default behaviour inside Zope is <H3>) 
 49  default_level = 3 
 50  initial_header_level = default_level 
 51   
 52  # default language used for internal translations and language mappings for DTD 
 53  # elements 
 54  default_language_code = 'en' 
 55   
 56   
57 -class Warnings:
58
59 - def __init__(self):
60 self.messages = []
61
62 - def write(self, message):
63 self.messages.append(message)
64
65 -def render(src, 66 writer='html4css1', 67 report_level=1, 68 stylesheet=None, 69 input_encoding=default_input_encoding, 70 output_encoding=default_output_encoding, 71 language_code=default_language_code, 72 initial_header_level = initial_header_level, 73 settings = {}):
74 """get the rendered parts of the document the and warning object 75 """ 76 # Docutils settings: 77 settings = settings.copy() 78 settings['input_encoding'] = input_encoding 79 settings['output_encoding'] = output_encoding 80 settings['stylesheet'] = stylesheet 81 settings['stylesheet_path'] = None 82 settings['file_insertion_enabled'] = 0 83 settings['raw_enabled'] = 0 84 if language_code: 85 settings['language_code'] = language_code 86 settings['language_code'] = language_code 87 # starting level for <H> elements: 88 settings['initial_header_level'] = initial_header_level + 1 89 # set the reporting level to something sane: 90 settings['report_level'] = report_level 91 # don't break if we get errors: 92 settings['halt_level'] = 6 93 # remember warnings: 94 settings['warning_stream'] = warning_stream = Warnings() 95 96 parts = publish_parts(source=src, writer_name=writer, 97 settings_overrides=settings, 98 config_section='zope application') 99 100 return parts, warning_stream
101
102 -def HTML(src, 103 writer='html4css1', 104 report_level=1, 105 stylesheet=None, 106 input_encoding=default_input_encoding, 107 output_encoding=default_output_encoding, 108 language_code=default_language_code, 109 initial_header_level = initial_header_level, 110 warnings = None, 111 settings = {}):
112 """ render HTML from a reStructuredText string 113 114 - 'src' -- string containing a valid reST document 115 116 - 'writer' -- docutils writer 117 118 - 'report_level' - verbosity of reST parser 119 120 - 'stylesheet' - Stylesheet to be used 121 122 - 'input_encoding' - encoding of the reST input string 123 124 - 'output_encoding' - encoding of the rendered HTML output 125 126 - 'report_level' - verbosity of reST parser 127 128 - 'language_code' - docutils language 129 130 - 'initial_header_level' - level of the first header tag 131 132 - 'warnings' - will be overwritten with a string containing the warnings 133 134 - 'settings' - dict of settings to pass in to Docutils, with priority 135 136 """ 137 parts, warning_stream = render(src, 138 writer = writer, 139 report_level = report_level, 140 stylesheet = stylesheet, 141 input_encoding = input_encoding, 142 output_encoding = output_encoding, 143 language_code=language_code, 144 initial_header_level = initial_header_level, 145 settings = settings) 146 147 header = '<h%(level)s class="title">%(title)s</h%(level)s>\n' % { 148 'level': initial_header_level, 149 'title': parts['title'], 150 } 151 152 subheader = '<h%(level)s class="subtitle">%(subtitle)s</h%(level)s>\n' % { 153 'level': initial_header_level+1, 154 'subtitle': parts['subtitle'], 155 } 156 157 body = '%(docinfo)s%(body)s' % { 158 'docinfo': parts['docinfo'], 159 'body': parts['body'], 160 } 161 162 163 output = '' 164 if parts['title']: 165 output = output + header 166 if parts['subtitle']: 167 output = output + subheader 168 output = output + body 169 170 171 warnings = ''.join(warning_stream.messages) 172 173 if output_encoding != 'unicode': 174 return output.encode(output_encoding) 175 else: 176 return output
177 178 __all__ = ("HTML", 'render') 179 180 if __name__ == '__main__': 181 import sys 182 print HTML(open(sys.argv[1]).read()) 183