[Zope-Checkins] SVN: Zope/trunk/lib/python/ Added a render() method to reStructuredText which returns the rendered parts and the warning object

Christian 'Tiran' Heimes heimes at faho.rwth-aachen.de
Fri May 14 10:59:56 EDT 2004


Log message for revision 24653:
Added a render() method to reStructuredText which returns the rendered parts and the warning object
Added default configuration values to App.config


-=-
Modified: Zope/trunk/lib/python/App/config.py
===================================================================
--- Zope/trunk/lib/python/App/config.py	2004-05-14 14:49:08 UTC (rev 24652)
+++ Zope/trunk/lib/python/App/config.py	2004-05-14 14:59:55 UTC (rev 24653)
@@ -18,6 +18,8 @@
 attributes for each bit of information.
 """
 
+import sys
+
 _config = None
 
 def getConfiguration():
@@ -60,7 +62,6 @@
     os.environ["SOFTWARE_HOME"] = cfg.softwarehome
     os.environ["ZOPE_HOME"] = cfg.zopehome
 
-    import sys
     if "Globals" in sys.modules:
         # XXX We *really* want to avoid this if Globals hasn't already
         # been imported, due to circular imports.  ;-(
@@ -85,3 +86,9 @@
         self.dbtab = None
         self.debug_mode = True
         self.enable_product_installation = True
+
+        # restructured text
+        default_enc = sys.getdefaultencoding()
+        self.rest_input_encoding = default_enc
+        self.rest_output_encoding = default_enc
+        self.rest_header_level = 3

Modified: Zope/trunk/lib/python/reStructuredText/__init__.py
===================================================================
--- Zope/trunk/lib/python/reStructuredText/__init__.py	2004-05-14 14:49:08 UTC (rev 24652)
+++ Zope/trunk/lib/python/reStructuredText/__init__.py	2004-05-14 14:59:55 UTC (rev 24653)
@@ -25,7 +25,7 @@
 default_input_encoding = getConfiguration().rest_input_encoding or default_enc
 
 # starting level for <H> elements (default behaviour inside Zope is <H3>)
-default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3))
+default_level = 3
 initial_header_level = getConfiguration().rest_header_level or default_level
 
 # default language
@@ -42,6 +42,38 @@
     def write(self, message):
         self.messages.append(message)
 
+def render(src,
+           writer='html4css1',
+           report_level=1,
+           stylesheet='default.css',
+           input_encoding=default_input_encoding,
+           output_encoding=default_output_encoding,
+           language_code=default_lang,
+           initial_header_level = initial_header_level,
+           settings = {}):
+    """get the rendered parts of the document the and warning object
+    """
+    # Docutils settings:
+    settings = settings.copy()
+    settings['input_encoding'] = input_encoding
+    settings['output_encoding'] = output_encoding
+    settings['stylesheet'] = stylesheet
+    settings['language_code'] = language_code
+    # starting level for <H> elements:
+    settings['initial_header_level'] = initial_header_level
+    # set the reporting level to something sane:
+    settings['report_level'] = report_level
+    # don't break if we get errors:
+    settings['halt_level'] = 6
+    # remember warnings:
+    settings['warning_stream'] = warning_stream = Warnings()
+
+    parts = publish_parts(source=src, writer_name=writer,
+                          settings_overrides=settings,
+                          config_section='zope application')
+
+    return parts, warning_stream
+
 def HTML(src,
          writer='html4css1',
          report_level=1,
@@ -49,9 +81,9 @@
          input_encoding=default_input_encoding,
          output_encoding=default_output_encoding,
          language_code=default_lang,
+         initial_header_level = initial_header_level,
          warnings = None,
          settings = {}):
-
     """ render HTML from a reStructuredText string 
 
         - 'src'  -- string containing a valid reST document
@@ -70,39 +102,34 @@
 
         - 'language_code' - docutils language
         
+        - 'initial_header_level' - level of the first header tag
+        
         - 'warnings' - will be overwritten with a string containing the warnings
         
         - 'settings' - dict of settings to pass in to Docutils, with priority
 
     """
-    # Docutils settings:
-    settings = settings.copy()
-    settings['input_encoding'] = input_encoding
-    settings['output_encoding'] = output_encoding
-    settings['stylesheet'] = stylesheet
-    settings['language_code'] = language_code
-    # starting level for <H> elements:
-    settings['initial_header_level'] = initial_header_level
-    # set the reporting level to something sane:
-    settings['report_level'] = report_level
-    # don't break if we get errors:
-    settings['halt_level'] = 6
-    # remember warnings:
-    settings['warning_stream'] = warning_stream = Warnings()
+    parts, warning_stream = render(src,
+                                   writer = writer,
+                                   report_level = report_level,
+                                   stylesheet = stylesheet,
+                                   input_encoding = input_encoding,
+                                   output_encoding = output_encoding,
+                                   language_code=language_code,
+                                   initial_header_level = initial_header_level,
+                                   settings = settings)
 
-    parts = publish_parts(source=src, writer_name=writer,
-                          settings_overrides=settings,
-                          config_section='zope application')
+    output = ('<h%(level)s class="title">%(title)s</h%(level)s>\n'
+              '%(docinfo)s%(body)s' % {
+                  'level': initial_header_level,
+                  'title': parts['title'],
+                  'docinfo': parts['docinfo'],
+                  'body': parts['body']
+              }).encode(output_encoding)
 
-    output = ('<h%(level)s class="title">%(title)s</h%(level)s>\n%(body)s'
-              % {'level': initial_header_level,
-                 'title': parts['title'],
-                 'body': parts['body']}).encode(output_encoding)
-
-    # what to do with this? (not used in the original code)
     warnings = ''.join(warning_stream.messages)
 
     return output
 
 
-__all__ = ("HTML", )
+__all__ = ("HTML", 'render')




More information about the Zope-Checkins mailing list