[Zope-Checkins] CVS: Zope/lib/python/reStructuredText - __init__.py:1.2

Andreas Jung andreas@andreas-jung.com
Sat, 1 Feb 2003 04:26:25 -0500


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

Added Files:
	__init__.py 
Log Message:
merge from ajung-restructuredtext-integration-branch


=== Zope/lib/python/reStructuredText/__init__.py 1.1 => 1.2 ===
--- /dev/null	Sat Feb  1 04:26:24 2003
+++ Zope/lib/python/reStructuredText/__init__.py	Sat Feb  1 04:26:22 2003
@@ -0,0 +1,83 @@
+##############################################################################
+#
+# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE
+#
+##############################################################################
+
+""" Wrapper to integrate reStructuredText into Zope """
+
+__all__ = ["HTML" ]
+
+import docutils.core 
+from docutils.io import StringOutput, StringInput 
+import sys
+
+class Warnings:
+    def __init__(self):
+        self.messages = []
+
+    def write(self, message):
+        self.messages.append(message)
+
+
+def HTML(src, writer='html4zope', report_level=1, stylesheet='default.css',
+         input_encoding=None, output_encoding=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
+    """
+
+    pub = docutils.core.Publisher()
+    pub.set_reader('standalone', None, 'restructuredtext')
+    pub.set_writer(writer)
+
+    if input_encoding is None:
+        input_encoding = sys.getdefaultencoding()
+
+    if output_encoding is None:
+        output_encoding = sys.getdefaultencoding()
+
+    # go with the defaults
+    pub.get_settings()
+
+    pub.settings.stylesheet = stylesheet
+
+    # this is needed, but doesn't seem to do anything
+    pub.settings._destination = ''
+
+    # set the reporting level to something sane
+    pub.settings.report_level = report_level
+
+    # don't break if we get errors
+    pub.settings.halt_level = 6
+
+    # 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)
+    warnings = ''.join(pub.settings.warning_stream.messages)
+
+    # do the format
+    print pub.writer.write(document, pub.destination)
+    return pub.writer.write(document, pub.destination)
+