[cpif] r430 - branches/alvaro/frontend-web

svn at argo.es svn at argo.es
Thu Oct 25 19:35:07 CEST 2007


Author: alvaro
Date: Thu Oct 25 19:35:06 2007
New Revision: 430

Log:
Seguimos adelante implementando el parseado de urls...


Modified:
   branches/alvaro/frontend-web/parser_bbcode.py
   branches/alvaro/frontend-web/parsers.py

Modified: branches/alvaro/frontend-web/parser_bbcode.py
==============================================================================
--- branches/alvaro/frontend-web/parser_bbcode.py	(original)
+++ branches/alvaro/frontend-web/parser_bbcode.py	Thu Oct 25 19:35:06 2007
@@ -36,7 +36,7 @@
     import xml.sax.saxutils
     return xml.sax.saxutils.escape(text, entities)
 
-def parse(text):
+def parse(text,context=None):
     before = []
     control = True
     
@@ -51,7 +51,7 @@
 
         before.append(ret[0])
         tag = ret[1]
-        after = ret[2]
+        after = "".join(i if i else "" for i in ret[2:])
         args = None
 
         for key,value in tags.iteritems():
@@ -76,7 +76,9 @@
             text = after
 
     str = "".join(before)
-
+    pos = len(str)
     str = escape(str)
-    return len(str), str, tag, args, after
+
+#    print "BBCODE->", pos, str, tag, args, after
+    return pos, str, tag, args, after
 

Modified: branches/alvaro/frontend-web/parsers.py
==============================================================================
--- branches/alvaro/frontend-web/parsers.py	(original)
+++ branches/alvaro/frontend-web/parsers.py	Thu Oct 25 19:35:06 2007
@@ -1,5 +1,8 @@
 # $Id$
 
+# TODO: Implementar tests negativos
+
+import parser_urls
 import parser_bbcode
 
 def gimme_the_tag(tag,args):
@@ -29,21 +32,22 @@
     """Converts the text into valid HTML."""
     
     parsers = [
+            (True, parser_urls),
             (True, parser_bbcode),
     ]
     
-    control = True
     
     stack = []
 
+    context = [None,]
+
+    control = True
     while control:
         results = {9999999: "foobar"}
         for allow, parser in parsers:
-            ret =  parser.parse(text)
+            ret =  parser.parse(text,context[-1])
             results[ret[0]] = ret[1:]
-
         before, tag, args, after = results[min(results.keys())]
-
         stack.append(('TEXT',before))
 
         # Tag has the format: (type, true if is an opening, the tag)
@@ -61,9 +65,11 @@
                     args.insert(-1,el[1])
                 elif el[0] == tag[0] and el[2] == tag[2]:
                     stack.append(('TEXT',gimme_the_tag(el[2],args)))
+                    context.pop()
                     break
         elif tag: 
             stack.append(tag)
+            context.append(tag[2])
             if args:
                 stack.append(('TEXT',args))
 
@@ -72,7 +78,7 @@
         else:
             control = False
             break
-#    print stack
+
     text = []
     while True:
         try:
@@ -150,7 +156,15 @@
             ('[url="http://example.org/uno&dos"]el &amp; texto[/url]', '<a href="http://example.org/uno&amp;dos">el &amp;amp; texto</a>'), 
             ('[url="http://example.org/uno&dos"]el "" texto[/url]', '<a href="http://example.org/uno&amp;dos">el &quot;&quot; texto</a>'), 
             ('[url="http://example.org/uno&dos"]el "" texto[/url]', '<a href="http://example.org/uno&amp;dos">el &quot;&quot; texto</a>'), 
-#            ('http://example.com [url="http://example.org/uno&dos"]el "" texto[/url]', '<a href="http://example.com" title="http://example.com">http://example.com</a> <a href="http://example.org/uno&amp;dos">el "" texto</a>'), 
+    )
+    correct_urls = (
+            ('http://example.com', ' <a href="http://example.com" title="http://example.com">http://example.com</a> '),
+            ('antes http://example.com', 'antes <a href="http://example.com" title="http://example.com">http://example.com</a> '),
+            ('http://example.com despues', ' <a href="http://example.com" title="http://example.com">http://example.com</a> despues'),
+            ('antes http://example.com despues', 'antes <a href="http://example.com" title="http://example.com">http://example.com</a> despues'),
+    )
+    correct = (
+            ('http://example.com [url="http://example.org/uno&dos"]el "" texto[/url]', ' <a href="http://example.com" title="http://example.com">http://example.com</a> <a href="http://example.org/uno&amp;dos">el &quot;&quot; texto</a>'), 
     )
     incorrect = (
             '<a>paco<a>luis</a>manolo</a>ringesvinto',
@@ -166,13 +180,27 @@
 #            retval, dummy = convert_to_html(code)
 #            assert retval
 #
+    def testCorrectURLSCode(self):
+        """Test if the translation goes well."""
+        global allow_errors
+        allow_errors = False
+        for code, html in self.correct_urls:
+            retval, result = convert_to_html(code)
+            assert not retval
+            self.assertEqual(html, result)
     def testCorrectBBCode(self):
         """Test if the translation goes well."""
         global allow_errors
         allow_errors = False
         for code, html in self.correct_bbcode:
-#            print "..."
-#            print "->",code
+            retval, result = convert_to_html(code)
+            assert not retval
+            self.assertEqual(html, result)
+    def testCorrectCode(self):
+        """Test if the translation goes well."""
+        global allow_errors
+        allow_errors = False
+        for code, html in self.correct:
             retval, result = convert_to_html(code)
             assert not retval
             self.assertEqual(html, result)



More information about the cpif mailing list