[Zope3-checkins] SVN: Zope3/trunk/src/zope/structuredtext/ Port r28531 and r29927 from Zope 2's StructuredText package:

Philipp von Weitershausen philikon at philikon.de
Sun Jan 8 04:27:49 EST 2006


Log message for revision 41188:
  Port r28531 and r29927 from Zope 2's StructuredText package:
  - Fixed handling of image URLs
  - More unittests
  

Changed:
  U   Zope3/trunk/src/zope/structuredtext/document.py
  U   Zope3/trunk/src/zope/structuredtext/html.py
  U   Zope3/trunk/src/zope/structuredtext/tests.py

-=-
Modified: Zope3/trunk/src/zope/structuredtext/document.py
===================================================================
--- Zope3/trunk/src/zope/structuredtext/document.py	2006-01-07 16:37:35 UTC (rev 41187)
+++ Zope3/trunk/src/zope/structuredtext/document.py	2006-01-08 09:27:48 UTC (rev 41188)
@@ -693,32 +693,9 @@
 
     def doc_img(
         self, s,
-        expr1=re.compile('\"([ _a-zA-Z0-9*.:/;,\-\n\~]+)\":img:([a-zA-Z0-9\_\-.:/;,\n\~]+)').search,
-        expr2=re.compile('\"([ _a-zA-Z0-9*.:/;,\-\n\~]+)\":img:([a-zA-Z0-9\_\-.:/;,\n\~]+):([a-zA-Z0-9_\-.:/;,\n\~]+)').search
+        expr1=re.compile('\"([ _a-zA-Z0-9*.:/;,\[\]\'\-\n\~]+)\":img:([a-zA-Z0-9%\_\-.:/\?=;,\n\~]+)').search,
         ):
 
-        r = expr2(s)
-        if r:
-            # Warning: the regex are getting confused when the string after :img:
-            # is an URL containing ":" (Collector #2276)
-            # Ugly workaround: check if have an absolute URL here. Not a cool solution,
-            # but it works !
-
-            if not r.group(2) in ['http','file','ftp']:
-
-                startt, endt = r.span(1)
-                startk, endk = r.span(2)
-                starth, endh = r.span(3)
-                start, end = r.span()
-
-                key = s[startk:endk]
-
-                return (stng.StructuredTextImage(s[startt:endt],
-                                            href=s[starth:endh],
-                                            key=s[startk:endk]),
-                        start, end)
-
-
         r=expr1(s)
         if r:
             startt, endt = r.span(1)

Modified: Zope3/trunk/src/zope/structuredtext/html.py
===================================================================
--- Zope3/trunk/src/zope/structuredtext/html.py	2006-01-07 16:37:35 UTC (rev 41187)
+++ Zope3/trunk/src/zope/structuredtext/html.py	2006-01-08 09:27:48 UTC (rev 41188)
@@ -248,6 +248,6 @@
     def image(self, doc, level, output):
         if hasattr(doc, 'key'):
             output('<a name="%s"></a>\n' % doc.key)
-        output('<img src="%s" alt="%s">\n' % (doc.href, doc.getNodeValue()))
+        output('<img src="%s" alt="%s" />\n' % (doc.href, doc.getNodeValue()))
         if doc.getNodeValue() and hasattr(doc, 'key'):
             output('<p><b>Figure %s</b> %s</p>\n' % (doc.key, doc.getNodeValue()))

Modified: Zope3/trunk/src/zope/structuredtext/tests.py
===================================================================
--- Zope3/trunk/src/zope/structuredtext/tests.py	2006-01-07 16:37:35 UTC (rev 41187)
+++ Zope3/trunk/src/zope/structuredtext/tests.py	2006-01-08 09:27:48 UTC (rev 41188)
@@ -20,8 +20,8 @@
 import StringIO
 
 from zope.structuredtext import stng
-from zope.structuredtext.document import Document
-from zope.structuredtext.html import HTML
+from zope.structuredtext.document import Document, DocumentWithImages
+from zope.structuredtext.html import HTML, HTMLWithImages
 
 package_dir = os.path.dirname(stng.__file__)
 regressions = os.path.join(package_dir, 'regressions')
@@ -64,9 +64,13 @@
 
     def _test(self, stxtxt, expected):
         doc = stng.structurize(stxtxt)
-        doc = Document()(doc)
-        output = HTML()(doc, level=1)
-        self.failIf(output.find(expected) == -1)
+        doc = DocumentWithImages()(doc)
+        output = HTMLWithImages()(doc, level=1)
+        if not expected in output:
+            print "Text:     ", stxtxt.encode('utf-8')
+            print "Converted:", output.encode('utf-8')
+            print "Expected: ", expected.encode('utf-8')
+            self.fail("'%s' not in result" % expected)        
 
     def testUnderline(self):
         self._test("xx _this is html_ xx",
@@ -112,9 +116,56 @@
         self._test("this is a '\"literal\":http://www.zope.org/.' eh",
         '<code>"literal":http://www.zope.org/.</code>')
 
-    # TODO need unicode tests
+    def testLink(self):
+        self._test('"foo":http://www.zope.org/foo/bar',
+                   '<p><a href="http://www.zope.org/foo/bar">foo</a></p>')
 
+        self._test('"foo":http://www.zope.org/foo/bar/%20x',
+                   '<p><a href="http://www.zope.org/foo/bar/%20x">foo</a></p>')
 
+        self._test('"foo":http://www.zope.org/foo/bar?arg1=1&arg2=2',
+                   '<p><a href="http://www.zope.org/foo/bar?arg1=1&arg2=2">foo</a></p>')
+
+        self._test('"foo bar":http://www.zope.org/foo/bar',
+                   '<p><a href="http://www.zope.org/foo/bar">foo bar</a></p>')
+
+        self._test('"[link goes here]":http://www.zope.org/foo/bar',
+                   '<p><a href="http://www.zope.org/foo/bar">[link goes here]</a></p>')
+
+        self._test('"[Dad\'s car]":http://www.zope.org/foo/bar',
+                   '<p><a href="http://www.zope.org/foo/bar">[Dad\'s car]</a></p>')
+
+     
+    def testImgLink(self):
+        self._test('"foo":img:http://www.zope.org/bar.gif',
+                   '<img src="http://www.zope.org/bar.gif" alt="foo" />')
+
+        self._test('"foo":img:http://www.zope.org:8080/bar.gif',
+                   '<img src="http://www.zope.org:8080/bar.gif" alt="foo" />')
+
+        self._test('"foo":img:http://www.zope.org:8080/foo/bar?arg=1',
+                   '<img src="http://www.zope.org:8080/foo/bar?arg=1" alt="foo" />')
+
+        self._test('"foo":img:http://www.zope.org:8080/foo/b%20ar?arg=1',
+                   '<img src="http://www.zope.org:8080/foo/b%20ar?arg=1" alt="foo" />')
+
+        self._test('"foo bar":img:http://www.zope.org:8080/foo/bar',
+                   '<img src="http://www.zope.org:8080/foo/bar" alt="foo bar" />')
+
+        self._test('"[link goes here]":img:http://www.zope.org:8080/foo/bar',
+                   '<img src="http://www.zope.org:8080/foo/bar" alt="[link goes here]" />')
+
+        self._test('"[Dad\'s new car]":img:http://www.zope.org:8080/foo/bar',
+                   '<img src="http://www.zope.org:8080/foo/bar" alt="[Dad\'s new car]" />')
+
+    def TODOtestUnicodeContent(self):
+        # This fails because ST uses the default locale to get "letters"
+        # whereas it should use \w+ and re.U if the string is Unicode.
+        #self._test(u"h\xe9 **y\xe9** xx",
+        #           u"h\xe9 <strong>y\xe9</strong> xx")
+        pass
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(StngTests))



More information about the Zope3-Checkins mailing list