[cpif] r209 - trunk/frontend-web

svn at argo.es svn at argo.es
Sat Jun 30 22:59:13 CEST 2007


Author: alvaro
Date: Sat Jun 30 22:59:12 2007
New Revision: 209

Log:
Aniadio el parser para las entidades.

Ha cambiado la interfaz del parser, ahora cada
parser devuelve una lista de tuplas, de la forma
[(Boolean, "text")]. El primer argumento esta
a True si no se ha tocado "text". Text es el texto
analizado o sin analizar.


Added:
   trunk/frontend-web/parser_entities.py
      - copied, changed from r204, /trunk/frontend-web/parser_eol.py
Modified:
   trunk/frontend-web/parser_bbcode.py
   trunk/frontend-web/parser_eol.py
   trunk/frontend-web/parser_html.py
   trunk/frontend-web/parsers.py

Modified: trunk/frontend-web/parser_bbcode.py
==============================================================================
--- trunk/frontend-web/parser_bbcode.py	(original)
+++ trunk/frontend-web/parser_bbcode.py	Sat Jun 30 22:59:12 2007
@@ -117,7 +117,7 @@
     """Do the parsing.
     Should be called after _reset, and _tokenize."""
     stack = []
-    self._parsed.append((True, escape(self._tokens.pop(0))))
+    self._parsed.append((True, self._tokens.pop(0)))
     while self._tags:
       tag = self._tags.pop(0)
       tag = tag.split("=", 1)
@@ -131,7 +131,7 @@
               # Esto es una chapuza para usuarios estupidos y chapuceros
           self._parsed.append((False, "%s%s%s" % (self._open, tag[0], self._close)))
         if self._tokens and self._tag_closes(tag):
-          self._parsed.append((True, escape(self._tokens.pop(0))))
+          self._parsed.append((True, self._tokens.pop(0)))
 
       elif tag[0] in allowed_tags.keys():
         tag.append(None)
@@ -144,12 +144,12 @@
         if not self._tag_closes(tag) and self._tokens:
           self._tokens.pop(0)
         if self._tokens: 
-          self._parsed.append((True, escape(self._tokens.pop(0))))
+          self._parsed.append((True, self._tokens.pop(0)))
 
       else: # Si no conozco el tag se lo paso tal cual
         self._parsed.append((True, "%s%s%s" % (self._open, tag[0], self._close)))
         if self._tokens:
-          self._parsed.append((True, escape(self._tokens.pop(0))))
+          self._parsed.append((True, self._tokens.pop(0)))
     
     if stack and allow_errors:
       while stack: # No me responsabilizo de lo que salga aqui...
@@ -159,7 +159,9 @@
     return False
 
 def parse(text):
-  """Parses the text w/ BBCode into HTML."""
+  """Parses the text into BBCode. It returns the exit code and a list of tuples
+in the form [(Boolean,"text")]. The first element tells whether "text" was
+untouched or analysed."""
   parser = BBCodeParser()
   parser.feed(text)
   return parser.get_tokens()

Copied: trunk/frontend-web/parser_entities.py (from r204, /trunk/frontend-web/parser_eol.py)
==============================================================================
--- /trunk/frontend-web/parser_eol.py	(original)
+++ trunk/frontend-web/parser_entities.py	Sat Jun 30 22:59:12 2007
@@ -1,5 +1,6 @@
 # $Id$
 
 def parse(text):
-  return False,text.replace("\r","").replace("\n","<br>\r\n")
+  import cgi
+  return False,[(True, cgi.escape(text))]
 

Modified: trunk/frontend-web/parser_eol.py
==============================================================================
--- trunk/frontend-web/parser_eol.py	(original)
+++ trunk/frontend-web/parser_eol.py	Sat Jun 30 22:59:12 2007
@@ -1,5 +1,5 @@
 # $Id$
 
 def parse(text):
-  return False,text.replace("\r","").replace("\n","<br>\r\n")
+  return False,[(True, text.replace("\r","").replace("\n","<br>\r\n"))]
 

Modified: trunk/frontend-web/parser_html.py
==============================================================================
--- trunk/frontend-web/parser_html.py	(original)
+++ trunk/frontend-web/parser_html.py	Sat Jun 30 22:59:12 2007
@@ -136,6 +136,9 @@
 
 
 def parse(text):
+  """Parses the text into HTML. It returns the exit code and a list of tuples
+in the form [(Boolean,"text")]. The first element tells whether "text" was
+untouched or analysed."""
   parser = HTMLParser()
   parser.reset()
   try:

Modified: trunk/frontend-web/parsers.py
==============================================================================
--- trunk/frontend-web/parsers.py	(original)
+++ trunk/frontend-web/parsers.py	Sat Jun 30 22:59:12 2007
@@ -3,38 +3,36 @@
 import parser_bbcode
 import parser_html
 import parser_eol
+import parser_entities
 
 def convert_to_html(text):
-  """Converts the text into valid HTML"""
+  """Converts the text into valid HTML."""
   from globales import allow_bbcode, allow_html
   
   tokens = [(True, text)]
 
-  if allow_html:
-    aux = []
-    for token in tokens:
-      if token[0]:
-        retval, token = parser_html.parse(token[1])
-        if retval:
-          return retval, None
-        aux += token
-      else:
-        aux.append(token)
-    tokens = aux[:]
-
-  if allow_bbcode:
-    aux = []
-    for token in tokens:
-      if token[0]:
-        retval, token=parser_bbcode.parse(token[1])
-        if retval:
-          return retval, None
-        aux += token
-      else:
-        aux.append(token)
-    tokens = aux[:]
-
-  return parser_eol.parse("".join([text for dummy, text in tokens]))
+  parsers = [
+      (allow_html, parser_html),
+      (allow_bbcode, parser_bbcode),
+      (True, parser_entities),
+      (True, parser_eol),
+  ]
+
+  for allow, parser in parsers:
+    if allow:
+      aux = []
+      for token in tokens:
+        if token[0]:
+          # Me espero el codigo de retorno y la lista de tuplas
+          retval, token = parser.parse(token[1])
+          if retval:
+            return retval, None
+          aux += token
+        else:
+          aux.append(token)
+      tokens = aux[:]
+  
+  return False, "".join([text for dummy, text in tokens])
 
 import unittest
 class TestBBCodeHTML(unittest.TestCase):
@@ -43,7 +41,7 @@
       ('[url="http://example.org/uno&dos"]el texto[/url]', '<a href="http://example.org/uno&amp;dos">el texto</a>'), 
       ('[url="http://example.org/uno&dos"]el & texto[/url]', '<a href="http://example.org/uno&amp;dos">el &amp; texto</a>'), 
       ('[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 "" texto</a>'), 
 )
 
   def testCorrectBBCode(self):
@@ -51,9 +49,6 @@
     global allow_errors
     allow_errors = False
     for bbcode, html in self.correct_bbcode:
-      retval, result = convert_to_html(bbcode)
-      assert not retval
-      self.assertEqual(html, result)
       retval, result = convert_to_html(bbcode)
       assert not retval
       self.assertEqual(html, result)



More information about the cpif mailing list