commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5324 - trunk/gnue-common/utils/helpers


From: jcater
Subject: r5324 - trunk/gnue-common/utils/helpers
Date: Mon, 15 Mar 2004 19:49:13 -0600 (CST)

Author: jcater
Date: 2004-03-15 19:49:08 -0600 (Mon, 15 Mar 2004)
New Revision: 5324

Added:
   trunk/gnue-common/utils/helpers/rst.py
Log:
added missing file (restructured-text processor)

Added: trunk/gnue-common/utils/helpers/rst.py
===================================================================
--- trunk/gnue-common/utils/helpers/rst.py      2004-03-16 01:01:24 UTC (rev 
5323)
+++ trunk/gnue-common/utils/helpers/rst.py      2004-03-16 01:49:08 UTC (rev 
5324)
@@ -0,0 +1,98 @@
+# reStructuredText calls
+
+# A lot of this was learned by studying epydoc.
+
+import re, string
+from xml.dom.minidom import *
+
+from docutils.core import publish_string
+from docutils.writers import Writer
+from docutils.writers.html4css1 import HTMLTranslator, Writer as HTMLWriter
+from docutils.readers.standalone import Reader as StandaloneReader
+from docutils.utils import new_document
+from docutils.nodes import NodeVisitor, Text, SkipChildren
+from docutils.nodes import SkipNode, TreeCopyVisitor
+
+def parse_string(source, errors, **options):
+    writer = _DocumentWriter()
+    writer.settings_spec = HTMLWriter.settings_spec
+    reader = _DocumentReader(errors) # Outputs errors to the list.
+    publish_string(source, writer=writer, reader=reader)
+    return _ParsedDocument(writer.document)
+
+class _ParsedDocument:
+    def __init__(self, document):
+        self._document = document
+
+    def concatenate(self, other):
+        result = self._document.copy()
+        for child in self._document.children + other._document.children:
+            visitor = TreeCopyVisitor(self._document)
+            child.walkabout(visitor)
+            result.append(visitor.get_tree_copy())
+        return ParsedRstDocstring(result)
+
+    def to_plaintext(self, linker, **options):
+        return self._document.astext()
+
+    def to_html(self, linker, **options):
+        visitor = _HTMLTranslator(self._document, linker)
+        self._document.walkabout(visitor)
+        return string.join(visitor.body)
+
+
+class _DocumentReader(StandaloneReader):
+    def __init__(self, errors):
+        self._errors = errors
+        StandaloneReader.__init__(self)
+
+    def new_document(self):
+        document = new_document(self.source.source_path, self.settings)
+        document.reporter.attach_observer(self.report)
+        document.reporter.set_conditions('', 10000, 10000, None)
+        self._encoding = document.reporter.encoding
+        self._error_handler = document.reporter.error_handler
+        return document
+
+    def report(self, error):
+        try: is_fatal = int(error['level']) > 2
+        except: is_fatal = 1
+        try: linenum = int(error['line'])
+        except: linenum = None
+
+        msg = string.join([c.astext().encode(self._encoding, 
self._error_handler)
+                       for c in error.children])
+
+        self._errors.append((msg, linenum, is_fatal))
+
+class _DocumentWriter(Writer):
+    def __init__(self):
+        self.document = None
+        Writer.__init__(self)
+
+    def translate(self):
+        self.output = ''
+
+
+class _HTMLTranslator(HTMLTranslator):
+    def __init__(self, document, docstring_linker):
+        HTMLTranslator.__init__(self, document)
+        self._linker = docstring_linker
+
+    # Handle interpreted text (crossreferences)
+    def visit_title_reference(self, node):
+        target = self.encode(node.astext())
+        xref = self._linker.translate_identifier_xref(target, target)
+        self.body.append(xref)
+        raise SkipNode
+
+    def visit_document(self, node): pass
+    def depart_document(self, node): pass
+
+    def starttag(self, node, tagname, suffix='\n', infix='', **attributes):
+        # Delete all CSS references
+        if attributes.has_key('class'):
+          del attributes['class']
+        return HTMLTranslator.starttag(self, node, tagname, suffix,
+                                       infix, **attributes)
+





reply via email to

[Prev in Thread] Current Thread [Next in Thread]