[py-svn] r33554 - in py/dist/py/rst: . testing

guido at codespeak.net guido at codespeak.net
Mon Oct 23 14:24:24 CEST 2006


Author: guido
Date: Mon Oct 23 14:24:22 2006
New Revision: 33554

Modified:
   py/dist/py/rst/rst.py
   py/dist/py/rst/testing/test_rst.py
Log:
Literal block contents is no longer escaped.


Modified: py/dist/py/rst/rst.py
==============================================================================
--- py/dist/py/rst/rst.py	(original)
+++ py/dist/py/rst/rst.py	Mon Oct 23 14:24:22 2006
@@ -50,6 +50,8 @@
     allowed_child = {}
     defaults = {}
     
+    _reg_whitespace = py.std.re.compile('\s+')
+
     def __init__(self, *args, **kwargs):
         self.parent = None
         self.children = []
@@ -87,13 +89,13 @@
     def __setitem__(self, item, value):
         self.children[item] = value
 
-    def text(self):
+    def text(self, escape=True):
         """ return a ReST string representation of the node """
-        return self.sep.join([child.text() for child in self.children])
+        return self.sep.join([child.text(escape) for child in self.children])
     
-    def wordlist(self):
+    def wordlist(self, escape=True):
         """ return a list of ReST strings for this node and its children """ 
-        return [self.text()]
+        return [self.text(escape)]
 
 class Rest(AbstractNode):
     sep = "\n\n"
@@ -112,10 +114,10 @@
             link_texts.append(".. _`%s`: %s" % (escape(link), escape(target)))
         return "\n" + "\n".join(link_texts) + "\n\n"
 
-    def text(self):
+    def text(self, escape=True):
         outcome = []
         for child in self.children:
-            outcome.append(child.text())
+            outcome.append(child.text(escape))
         
         text = self.sep.join(outcome) + "\n" # trailing newline
         return text + self.render_links()
@@ -128,7 +130,7 @@
         self.width = width
         super(Transition, self).__init__(*args, **kwargs)
         
-    def text(self):
+    def text(self, escape=True):
         return (self.width - 1) * self.char
 
 class Paragraph(AbstractNode):
@@ -145,10 +147,10 @@
                 args[num] = Text(arg)
         super(Paragraph, self).__init__(*args, **kwargs)
     
-    def text(self):
+    def text(self, escape=True):
         texts = []
         for child in self.children:
-            texts += child.wordlist()
+            texts += child.wordlist(escape)
         
         buf = []
         outcome = []
@@ -181,9 +183,9 @@
     indent = " "
     sep = ""
     
-    def text(self):
-        all_txt = AbstractNode.text(self)
-        all_txts = all_txt.split("\n")
+    def text(self, escape=True):
+        all_txt = AbstractNode.text(self, escape=False)
+        all_txts = all_txt.split('\n')
         return '::\n\n%s' % ("\n".join([self.indent + i for i in
                                         all_txts]),)
 
@@ -192,8 +194,8 @@
     belowchar = ""
     abovechar = ""
     
-    def text(self):
-        txt = Paragraph.text(self)
+    def text(self, escape=True):
+        txt = Paragraph.text(self, escape)
         lines = []
         if self.abovechar:
             lines.append(self.abovechar * len(txt))
@@ -209,13 +211,18 @@
     def __init__(self, _text):
         self._text = _text
     
-    def text(self):
-        return self.start + escape(self._text) + self.end
+    def text(self, escape=True):
+        text = self._text
+        if escape:
+            text = globals()['escape'](text)
+        return self.start + text + self.end
     
 class Text(AbstractText):
-    _reg_whitespace = py.std.re.compile('\s+')
-    def wordlist(self):
-        return self._reg_whitespace.split(escape(self._text))
+    def wordlist(self, escape=True):
+        text = self._text
+        if escape:
+            text = globals()['escape'](text)
+        return self._reg_whitespace.split(text)
 
 class Em(AbstractText):
     start = "*"
@@ -244,11 +251,11 @@
 class ListItem(Paragraph):
     item_char = "*"
     
-    def text(self):
+    def text(self, escape=True):
         oldindent = self.indent
         self.indent = oldindent + '  '
         try:
-            txt = Paragraph.text(self)
+            txt = Paragraph.text(self, escape)
         finally:
             self.indent = oldindent
         txt = self.item_char + txt[1:]
@@ -272,11 +279,11 @@
         self.target = target
         self.rest = None
     
-    def text(self):
+    def text(self, escape=True):
         if self.rest is None:
             self.rest = self.find_rest()
         self.rest.links[self._text] = self.target
-        return AbstractText.text(self)
+        return AbstractText.text(self, escape)
 
     def find_rest(self):
         # XXX little overkill, but who cares...

Modified: py/dist/py/rst/testing/test_rst.py
==============================================================================
--- py/dist/py/rst/testing/test_rst.py	(original)
+++ py/dist/py/rst/testing/test_rst.py	Mon Oct 23 14:24:22 2006
@@ -7,11 +7,15 @@
 def test_escape():
     txt = Paragraph('*escape* ``test``').text()
     assert txt == '\\*escape\\* \\`\\`test\\`\\`'
-    txt = Strong('*strong*').text()
+    txt = Paragraph(Strong('*strong*')).text()
     assert txt == '**\\*strong\\***'
     txt = Rest(Paragraph(Link('foo[bar]', 'foo[bar]'))).text()
     assert txt == "`foo\\[bar]`_\n\n.. _`foo\\[bar]`: foo\\[bar]\n\n"
 
+def test_escape_literal():
+    txt = LiteralBlock('*escape* ``test``').text()
+    assert txt == '::\n\n *escape* ``test``'
+
 def test_illegal_parent():
     Rest(Paragraph(Text('spam')))
     py.test.raises(RestError, 'Rest(Text("spam"))')
@@ -81,7 +85,7 @@
 
 ::
 
- def fun()\\:
+ def fun():
   some
 
 Paragraph



More information about the pytest-commit mailing list