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

fijal at codespeak.net fijal at codespeak.net
Fri Oct 6 12:00:17 CEST 2006


Author: fijal
Date: Fri Oct  6 12:00:15 2006
New Revision: 32948

Modified:
   py/branch/apigen/py/rst/rst.py
   py/branch/apigen/py/rst/testing/test_rst.py
Log:
Added Link, fixed some issues with parentclass


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 12:00:15 2006
@@ -13,11 +13,18 @@
     def __new__(cls, *args):
         obj = super(AbstractMetaclass, cls).__new__(cls, *args)
         parent_cls = obj.parentclass
-        if parent_cls is not None:
-            if parent_cls.allowed_child:
-                parent_cls.allowed_child = {obj:True}
+        if parent_cls is None:
+            return obj
+        if not isinstance(parent_cls, list):
+            class_list = [parent_cls]
+        else:
+            class_list = parent_cls
+        
+        for _class in class_list:
+            if not _class.allowed_child:
+                _class.allowed_child = {obj:True}
             else:
-                parent_cls.allowed_child[obj] = True
+                _class.allowed_child[obj] = True
         return obj
 
 class AbstractNode(object):
@@ -32,7 +39,9 @@
     
     def __init__(self, *args, **kwargs):
         self.parent = None
-        self.childs = args
+        self.childs = []
+        for child in args:
+            self._add(child)
         for arg in kwargs:
             setattr(self, arg, kwargs[arg])
     
@@ -65,14 +74,28 @@
 
 class Rest(AbstractNode):
     sep = "\n\n"
+    def __init__(self, *args, **kwargs):
+        AbstractNode.__init__(self, *args, **kwargs)
+        self.links = {}
     
+    def render_links(self, check=False):
+        assert not check, "Link checking not implemented"
+        if not self.links:
+            return ""
+        link_texts = []
+        for link, target in self.links.iteritems():
+            link_texts.append(".. _`%s`: %s" % (link, target))
+        return "\n".join(link_texts) + "\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
+        
+        text = self.sep.join(outcome) + "\n" # trailing newline
+        return text + self.render_links()
 
 class Paragraph(AbstractNode):
     parentclass = Rest
@@ -128,8 +151,24 @@
         all_txts = all_txt.split("\n")
         return "\n".join([self.indent + i for i in all_txts])
 
+class Title(AbstractNode):
+    parentclass = Rest
+    belowchar = ""
+    abovechar = ""
+    previous_paragraph = None
+    
+    def text(self):
+        txt = AbstractNode.text(self)
+        lines = []
+        if self.abovechar:
+            lines.append(self.abovechar * len(txt))
+        lines.append(txt)
+        if self.belowchar:
+            lines.append(self.belowchar * len(txt))
+        return "\n".join(lines)
+
 class AbstractText(AbstractNode):
-    parentclass = Paragraph
+    parentclass = [Paragraph, Title]
     start = ""
     end = ""
     def __init__(self, _text):
@@ -146,16 +185,24 @@
     start = "*"
     end = "*"
 
-class Title(AbstractNode):
-    parentclass = Rest
-    belowchar = ""
-    abovechar = ""
+class Link(AbstractText):
+    start = '`'
+    end = '`_'
+
+    def __init__(self, _text, target):
+        self._text = _text
+        self.target = target
+        self.rest = None
     
     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))
+        if self.rest is None:
+            self.rest = self.find_rest()
+        self.rest.links[self._text] = self.target
+        return AbstractText.text(self)
+
+    def find_rest(self):
+        # XXX little overkill, but who cares...
+        next = self
+        while next.parent is not None:
+            next = next.parent
+        return next

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 12:00:15 2006
@@ -25,3 +25,13 @@
     txt = Rest(Paragraph("Text"), BlockQuote("def fun():\n some"), \
         Paragraph("Paragraph")).text()
     assert txt == expected
+
+def test_title():
+    assert Rest(Title(Text("Some title"), belowchar="=")).text() == \
+        "Some title\n==========\n"
+    assert Rest(Title(Text("Some title"), belowchar="#", abovechar="#")).text() \
+        == "##########\nSome title\n##########\n"
+
+def test_link():
+    expected = "`some link`_\n.. _`some link`: http://codespeak.net\n"
+    txt = Rest(Paragraph(Link("some link", "http://codespeak.net"))).text()



More information about the pytest-commit mailing list