[Zope-Checkins] SVN: Zope/trunk/lib/python/reStructuredText/ Merging from tiran-restfixing-brnach

Christian 'Tiran' Heimes heimes at faho.rwth-aachen.de
Thu May 13 11:32:07 EDT 2004


Log message for revision 24626:
Merging from tiran-restfixing-brnach


-=-
Modified: Zope/trunk/lib/python/reStructuredText/__init__.py
===================================================================
--- Zope/trunk/lib/python/reStructuredText/__init__.py	2004-05-13 15:25:53 UTC (rev 24625)
+++ Zope/trunk/lib/python/reStructuredText/__init__.py	2004-05-13 15:32:07 UTC (rev 24626)
@@ -10,20 +10,30 @@
 # FOR A PARTICULAR PURPOSE
 #
 ##############################################################################
+"""Wrapper to integrate reStructuredText into Zope
 
-""" Wrapper to integrate reStructuredText into Zope """
+This implementation requires docutils 0.3.4+ from http://docutils.sf.net/
+"""
 
-__all__ = ("HTML", ) 
+import sys, os, locale
+from App.config import getConfiguration
+from docutils.core import publish_parts
 
-import sys, os
-import docutils.core 
-from docutils.io import StringOutput, StringInput
-from App.config import getConfiguration 
-
+# 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
 
+# 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:
 
     def __init__(self):
@@ -32,12 +42,15 @@
     def write(self, message):
         self.messages.append(message)
 
-def HTML(src, 
-         writer='html4zope', 
-         report_level=1, 
+def HTML(src,
+         writer='html4css1',
+         report_level=1,
          stylesheet='default.css',
-         input_encoding=default_input_encoding, 
-         output_encoding=default_output_encoding):
+         input_encoding=default_input_encoding,
+         output_encoding=default_output_encoding,
+         language_code=default_lang,
+         warnings = None,
+         settings = {}):
 
     """ render HTML from a reStructuredText string 
 
@@ -52,48 +65,44 @@
         - 'input_encoding' - encoding of the reST input string
 
         - 'output_encoding' - encoding of the rendered HTML output
-    """
+        
+        - 'report_level' - verbosity of reST parser
 
-    pub = docutils.core.Publisher()
-    pub.set_reader('standalone', None, 'restructuredtext')
-    pub.set_writer(writer)
+        - '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
 
-    # go with the defaults
-    pub.get_settings()
+    """
+    # 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()
 
-    pub.settings.stylesheet = stylesheet
+    parts = publish_parts(source=src, writer_name=writer,
+                          settings_overrides=settings,
+                          config_section='zope application')
 
-    # this is needed, but doesn't seem to do anything
-    pub.settings._destination = ''
+    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)
 
-    # set the reporting level to something sane
-    pub.settings.report_level = report_level
+    # what to do with this? (not used in the original code)
+    warnings = ''.join(warning_stream.messages)
 
-    # don't break if we get errors
-    pub.settings.halt_level = 6
+    return output
 
-    # remember warnings
-    pub.settings.warning_stream = Warnings()
 
-    # input
-    pub.source = StringInput(source=src, encoding=input_encoding)
-
-    # output - not that it's needed
-    pub.destination = StringOutput(encoding=output_encoding)
-
-    # parse!
-    document = pub.reader.read(pub.source, pub.parser, pub.settings)
-
-    # transform
-    pub.apply_transforms(document)
-    
-    warnings = ''.join(pub.settings.warning_stream.messages)
-
-    # do the format
-    return pub.writer.write(document, pub.destination)
-
-from docutils import writers
-import html4zope
-
-writers.html4zope = html4zope
-sys.modules['docutils.writers.html4zope'] = html4zope
+__all__ = ("HTML", )

Deleted: Zope/trunk/lib/python/reStructuredText/html4zope.py
===================================================================
--- Zope/trunk/lib/python/reStructuredText/html4zope.py	2004-05-13 15:25:53 UTC (rev 24625)
+++ Zope/trunk/lib/python/reStructuredText/html4zope.py	2004-05-13 15:32:07 UTC (rev 24626)
@@ -1,59 +0,0 @@
-# Author: Andreas Jung
-# Contact: andreas at andreas-jung.com
-# Revision: $Revision: 1.1 $
-# Date: $Date: 2003/11/30 16:16:06 $
-# Copyright: This module has been placed in the public domain.
-
-"""
-Writer module to integrate reST into Zope.  This writer subclasses the standard
-html4css1 writer and changes the starting level for <H> elements from 1 to 3
-(default behaviour inside Zope.
-"""
-
-__docformat__ = 'reStructuredText'
-
-from docutils import nodes
-from docutils.writers.html4css1 import Writer  as CSS1Writer, HTMLTranslator as CSS1HTMLTranslator
-import os
-
-default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3))
-
-class Writer(CSS1Writer):
-    def __init__(self):
-        CSS1Writer.__init__(self)
-        self.translator_class = HTMLTranslator
-
-class HTMLTranslator(CSS1HTMLTranslator):
-
-    def astext(self):
-        return ''.join(self.body)
-
-    def visit_title(self, node):
-        """Only 6 section levels are supported by HTML."""
-
-        if isinstance(node.parent, nodes.topic):
-            self.body.append(
-                  self.starttag(node, 'p', '', CLASS='topic-title'))
-            if node.parent.hasattr('id'):
-                self.body.append(
-                    self.starttag({}, 'a', '', name=node.parent['id']))
-                self.context.append('</a></p>\n')
-            else:
-                self.context.append('</p>\n')
-        elif self.section_level == 0:
-            # 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)
-        else:
-            self.body.append(
-                  self.starttag(node, 'h%s' % (default_level+self.section_level-1), ''))
-            atts = {}
-            if node.parent.hasattr('id'):
-                atts['name'] = node.parent['id']
-            if node.hasattr('refid'):
-                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)))




More information about the Zope-Checkins mailing list