[Zope-Checkins] CVS: Zope/lib/python/reStructuredText - html4zope.py:1.1.2.1.8.2 __init__.py:1.7.2.2.4.2

Christian 'Tiran' Heimes heimes at faho.rwth-aachen.de
Wed May 12 14:28:20 EDT 2004


Update of /cvs-repository/Zope/lib/python/reStructuredText
In directory cvs.zope.org:/tmp/cvs-serv22851/lib/python/reStructuredText

Modified Files:
      Tag: tiran-restfixing-branch
	html4zope.py __init__.py 
Log Message:
Merged with changes from David Goodger and Lele Gaifax


=== Zope/lib/python/reStructuredText/html4zope.py 1.1.2.1.8.1 => 1.1.2.1.8.2 ===
--- Zope/lib/python/reStructuredText/html4zope.py:1.1.2.1.8.1	Sat Apr 24 18:59:48 2004
+++ Zope/lib/python/reStructuredText/html4zope.py	Wed May 12 14:28:19 2004
@@ -16,9 +16,8 @@
 from docutils.writers.html4css1 import Writer  as CSS1Writer, HTMLTranslator as CSS1HTMLTranslator
 import os
 
-from App.config import getConfiguration
-default_level = getConfiguration().rest_header_level
-print "REST: %s " % default_level
+from reStructuredText import initial_header_level
+
 
 class Writer(CSS1Writer):
     def __init__(self):
@@ -46,11 +45,11 @@
             # document title
             self.head.append('<title>%s</title>\n'
                              % self.encode(node.astext()))
-            self.body.append(self.starttag(node, 'h%d' % default_level, '', CLASS='title'))
-            self.context.append('</h%d>\n' % default_level)
+            self.body.append(self.starttag(node, 'h%d' % initial_header_level, '', CLASS='title'))
+            self.context.append('</h%d>\n' % initial_header_level)
         else:
             self.body.append(
-                  self.starttag(node, 'h%s' % (default_level+self.section_level-1), ''))
+                  self.starttag(node, 'h%s' % (initial_header_level+self.section_level-1), ''))
             atts = {}
             if node.parent.hasattr('id'):
                 atts['name'] = node.parent['id']
@@ -58,4 +57,4 @@
                 atts['class'] = 'toc-backref'
                 atts['href'] = '#' + node['refid']
             self.body.append(self.starttag({}, 'a', '', **atts))
-            self.context.append('</a></h%s>\n' % ((default_level+self.section_level-1)))
+            self.context.append('</a></h%s>\n' % ((initial_header_level+self.section_level-1)))


=== Zope/lib/python/reStructuredText/__init__.py 1.7.2.2.4.1 => 1.7.2.2.4.2 ===
--- Zope/lib/python/reStructuredText/__init__.py:1.7.2.2.4.1	Sat Apr 24 18:59:48 2004
+++ Zope/lib/python/reStructuredText/__init__.py	Wed May 12 14:28:19 2004
@@ -13,22 +13,24 @@
 
 """ Wrapper to integrate reStructuredText into Zope """
 
-__all__ = ("HTML", ) 
 
 import sys, os
-from docutils.core import Publisher
-from docutils import io, writers
 from App.config import getConfiguration 
+from docutils import __version__ as docutils_version
 
 # get encoding
 default_enc = sys.getdefaultencoding()
 default_output_encoding = getConfiguration().rest_output_encoding or default_enc
 default_input_encoding = getConfiguration().rest_input_encoding or default_enc
 
-# add html4zope to the docutils.writers
-import html4zope
-writers.html4zope = html4zope
-sys.modules['docutils.writers.html4zope'] = html4zope
+# starting level for <H> elements (default behaviour inside Zope is <H3>)
+default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3))
+initial_header_level = getConfiguration().rest_header_level or default_level
+
+# default language
+default_lang = getConfiguration().locale or locale.getdefaultlocale()[0]
+if default_lang and '_' in default_lang:
+    default_lang = default_lang[:default_lang.index('_')]
 
 
 class Warnings:
@@ -39,12 +41,26 @@
     def write(self, message):
         self.messages.append(message)
 
-def HTML(src, 
+######################################################################
+# old style implementation
+# works with older versions of docutils
+
+from docutils.core import Publisher
+from docutils import io, writers
+
+# add html4zope to the docutils.writers
+import html4zope
+writers.html4zope = html4zope
+sys.modules['docutils.writers.html4zope'] = html4zope
+
+def HTML_old(src, 
          writer='html4zope', 
          report_level=1, 
          stylesheet='default.css',
          input_encoding=default_input_encoding, 
          output_encoding=default_output_encoding,
+         language_code=default_lang,
+         warnings = None,
          **kwargs):
 
     """ render HTML from a reStructuredText string 
@@ -61,6 +77,10 @@
 
         - 'output_encoding' - encoding of the rendered HTML output
         
+        - 'language_code' - docutils language
+        
+        - 'warnings' - will be overwritten with a string containing the warnings
+        
         - 'kwargs' - more keyword args which are applied to the parser
     """
     pub = Publisher(source_class=io.StringInput,
@@ -81,6 +101,7 @@
         'warning_stream'  : Warnings(),
         'input_encoding'  : input_encoding,
         'output_encoding' : output_encoding,
+        'language_code'   : language_code,
         })
     settings._update(kwargs, 'loose')
 
@@ -97,4 +118,82 @@
 
     # do the format
     return pub.writer.write(document, pub.destination)
+
+######################################################################
+# new style implementation
+# requires docutils >= 0.3.4
+
+from docutils.core import publish_parts
+
+def HTML_new(src,
+         writer='html4css1',
+         report_level=1,
+         stylesheet='default.css',
+         input_encoding=default_input_encoding,
+         output_encoding=default_output_encoding,
+         language_code=default_lang,
+         warnings = None,
+         settings=None):
+
+    """ render HTML from a reStructuredText string 
+
+        - 'src'  -- string containing a valid reST document
+
+        - 'writer' -- docutils writer 
+
+        - 'report_level' - verbosity of reST parser
+
+        - 'stylesheet' - Stylesheet to be used
+
+        - 'input_encoding' - encoding of the reST input string
+
+        - 'output_encoding' - encoding of the rendered HTML output
+        
+        - 'report_level' - verbosity of reST parser
+
+        - 'language_code' - docutils language
+        
+        - '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 or {}).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')
+
+    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
+
+# end of implementations
+######################################################################
+
+if docutils_version < '0.3.4':
+    HTML = HTML_old
+else:
+    HTML = HTML_new
+
+__all__ = ("HTML", ) 
 




More information about the Zope-Checkins mailing list