[Zope-Checkins] CVS: Zope/lib/python/DocumentTemplate - DT_Var.py:1.58.10.1

Andreas Jung andreas@digicool.com
Mon, 4 Nov 2002 11:50:37 -0500


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

Modified Files:
      Tag: ajung-restructuredtext-integration-branch
	DT_Var.py 
Log Message:
first cut of the integration into DTML

=== Zope/lib/python/DocumentTemplate/DT_Var.py 1.58 => 1.58.10.1 ===
--- Zope/lib/python/DocumentTemplate/DT_Var.py:1.58	Mon Sep 16 06:09:11 2002
+++ Zope/lib/python/DocumentTemplate/DT_Var.py	Mon Nov  4 11:50:37 2002
@@ -162,6 +162,7 @@
 from types import StringType
 from Acquisition import aq_base
 from ZPublisher.TaintedString import TaintedString
+import docutils.core
 
 class Var:
     name='var'
@@ -396,6 +397,65 @@
 def len_comma(v, name='(Unknown name)', md={}):
     return thousands_commas(str(len(v)))
 
+def restructured_text(v, name='(Unknown name)', md={}):
+
+    class Warnings:
+        def __init__(self):
+            self.messages = []
+        def write(self, message):
+            self.messages.append(message)
+
+
+    if isinstance(v,StringType): txt = v
+    elif aq_base(v).meta_type in ['DTML Document','DTML Method']:
+        txt = aq_base(v).read_raw()
+    else: txt = str(v)
+
+    pub = docutils.core.Publisher()
+    pub.set_reader('restructuredtext', None, 'restructuredtext')
+    pub.set_writer('html')
+
+    # go with the defaults
+    pub.get_settings()
+
+    # this is needed, but doesn't seem to do anything
+    pub.settings._destination = ''
+
+    # use the stylesheet chosen by the user
+#    pub.settings.stylesheet = self.stylesheet
+
+    # set the reporting level to something sane
+    pub.settings.report_level = 5
+
+    # don't break if we get errors
+    pub.settings.halt_level = 6
+
+    # remember warnings
+    pub.settings.warning_stream = Warnings()
+
+    # input
+    pub.source = docutils.io.StringInput(pub.settings)
+#    pub.source.source = self.source
+    pub.source.source = txt
+
+    # output - not that it's needed
+    pub.destination = docutils.io.StringOutput(pub.settings)
+
+    # parse!
+    document = pub.reader.read(pub.source, pub.parser, pub.settings)
+
+#    self.warnings = ''.join(pub.settings.warning_stream.messages)
+
+#    if document.children:
+#        item = document.children[0]
+#        if item.tagname == 'title':
+#            self.title = str(item.children[0])
+
+    # do the format
+    formatted = pub.writer.write(document, pub.destination)
+    return formatted
+
+
 StructuredText=None
 def structured_text(v, name='(Unknown name)', md={}):
     global StructuredText
@@ -428,6 +488,7 @@
     'dollars-and-cents': dollars_and_cents,
     'collection-length': len_format,
     'structured-text': structured_text,
+    'restructured-text': restructured_text,
 
     # The rest are deprecated:
     'sql-quote': sql_quote,