[py-svn] r32943 - in py/branch/apigen/py/rst: . testing

fijal at codespeak.net fijal at codespeak.net
Fri Oct 6 11:20:54 CEST 2006


Author: fijal
Date: Fri Oct  6 11:20:51 2006
New Revision: 32943

Modified:
   py/branch/apigen/py/rst/rst.py
   py/branch/apigen/py/rst/testing/test_rst.py
Log:
Added blockquote.


Modified: py/branch/apigen/py/rst/rst.py
==============================================================================
--- py/branch/apigen/py/rst/rst.py	(original)
+++ py/branch/apigen/py/rst/rst.py	Fri Oct  6 11:20:51 2006
@@ -65,12 +65,29 @@
 
 class Rest(AbstractNode):
     sep = "\n\n"
+    
+    def text(self):
+        outcome = []
+        for child in self.childs:
+            if child.previous_paragraph and len(outcome):
+                outcome[-1] += child.previous_paragraph
+            outcome.append(child.text())
+        return self.sep.join(outcome) + "\n" # trailing newline
 
 class Paragraph(AbstractNode):
     parentclass = Rest
     sep = " "
     indent = ""
     width = 80
+    previous_paragraph = ""
+    
+    def __init__(self, *args, **kwargs):
+        # make shortcut
+        args = list(args)
+        for num, arg in enumerate(args):
+            if isinstance(arg, str):
+                args[num] = Text(arg)
+        super(Paragraph, self).__init__(*args, **kwargs)
     
     def text(self):
         texts = []
@@ -97,17 +114,48 @@
         grab(buf)
         outcome.reverse()
         return "\n".join(outcome)
-##
+
 class SubParagraph(Paragraph):
     indent = " "
+    
+class BlockQuote(Paragraph):
+    indent = " "
+    previous_paragraph = "::"
+    sep = ""
+    
+    def text(self):
+        all_txt = AbstractNode.text(self)
+        all_txts = all_txt.split("\n")
+        return "\n".join([self.indent + i for i in all_txts])
 
-class Text(AbstractNode):
+class AbstractText(AbstractNode):
     parentclass = Paragraph
+    start = ""
+    end = ""
     def __init__(self, _text):
         self._text = _text
     
     def text(self):
-        return self._text
+        return self.start + self._text + self.end
     
+class Text(AbstractText):
     def wordlist(self):
         return self._text.split(" ")
+
+class Emph(AbstractText):
+    start = "*"
+    end = "*"
+
+class Title(AbstractNode):
+    parentclass = Rest
+    belowchar = ""
+    abovechar = ""
+    
+    def text(self):
+        txt = AbstractNode.text(self)
+        lines = []
+        if abovechar:
+            lines.append(abovechar * len(txt))
+        lines.append(txt)
+        if belowchar:
+            lines.append(belowchar * len(txt))

Modified: py/branch/apigen/py/rst/testing/test_rst.py
==============================================================================
--- py/branch/apigen/py/rst/testing/test_rst.py	(original)
+++ py/branch/apigen/py/rst/testing/test_rst.py	Fri Oct  6 11:20:51 2006
@@ -5,11 +5,23 @@
 from py.__.rst.rst import *
 
 def test_textgen():
-    assert Rest(Paragraph(Text("dupa"))).text() == "dupa"
-    assert Rest(Paragraph(Text("dupa"), Text("dupa"))).text() == "dupa dupa"
-    assert Rest(Paragraph(Text("a")), Paragraph(Text("b"))).text() == "a\n\nb"
-    assert Rest(Paragraph(Text("a"), indent=" ")).text() == " a"
+    assert Rest(Paragraph(Text("dupa"))).text() == "dupa\n"
+    assert Rest(Paragraph(Text("dupa"), Text("dupa"))).text() == "dupa dupa\n"
+    assert Rest(Paragraph(Text("a")), Paragraph(Text("b"))).text() == "a\n\nb\n"
+    assert Rest(Paragraph(Text("a"), indent=" ")).text() == " a\n"
 
 def test_join():
     txt = Rest(Paragraph(Text("a b c d e f"), width=3, indent=" ")).text()
-    assert txt == ' a\n b\n c\n d\n e\n f'
+    assert txt == ' a\n b\n c\n d\n e\n f\n'
+
+def test_blockquote():
+    expected = """Text::
+
+ def fun():
+  some
+
+Paragraph
+"""
+    txt = Rest(Paragraph("Text"), BlockQuote("def fun():\n some"), \
+        Paragraph("Paragraph")).text()
+    assert txt == expected


More information about the py-svn mailing list