url()

David Binger <dbinger-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Mon, 21 Feb 2005 15:01:25 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
--Apple-Mail-5--287616565
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	x-unix-mode=0664;
	name="url.txt"
Content-Disposition: attachment;
	filename=url.txt

- Add an 'html_url' function that constructs an appropriately url_quoted URL.
  keyword args are appended to the URL as a querystring with the keys and 
  values url_quoted as well.

- Add new test for html_url and others.
Changed:
html.py
test/utest_html.py


Modified: trunk/quixote/html.py
===================================================================
--- trunk/quixote/html.py	2005-02-21 14:31:42 UTC (rev 26201)
+++ trunk/quixote/html.py	2005-02-21 19:11:12 UTC (rev 26202)
@@ -74,6 +74,12 @@
             htmlescape(text) +
             htmltext("</a>"))

+def html_url(path, **attrs):
+    result = htmltext(url_quote(path))
+    if attrs:
+        result += "?" + "&".join([url_quote(key) + "=" + url_quote(value)
+                                  for key, value in attrs.items()])
+    return result

 def nl2br(value):
     """nl2br(value : any) -> htmltext

Modified: trunk/quixote/test/utest_html.py
===================================================================
--- trunk/quixote/test/utest_html.py	2005-02-21 14:31:42 UTC (rev 26201)
+++ trunk/quixote/test/utest_html.py	2005-02-21 19:11:12 UTC (rev 26202)
@@ -1,6 +1,7 @@
 import sys
 from sancho.utest import UTest
 from quixote import _py_htmltext
+from quixote.html import href, html_url, url_quote, nl2br, htmltag

 markupchars = '<>&"'
 quotedchars = '&lt;&gt;&amp;&quot;'
@@ -33,6 +34,24 @@

 class HTMLTest (UTest):

+    def check_href(self):
+        assert str(href('/foo/bar', 'bar')) == '<a href="/foo/bar">bar</a>'
+
+    def check_html_url(self):
+        assert str(html_url('/f/b', a='1')) == '/f/b?a=1'
+        assert str(html_url('/f/b', a='1', b='3 4')) == '/f/b?a=1&amp;b=3%204'
+
+    def check_nl2br(self):
+        assert str(nl2br('a\nb\nc')) == 'a<br />\nb<br />\nc'
+
+    def check_url_quote(self):
+        assert url_quote('abc') == 'abc'
+        assert url_quote('a b c') == 'a%20b%20c'
+        assert url_quote(None, fallback='abc') == 'abc'
+
+
+class HTMLTextTest (UTest):
+
     def _pre(self):
         global htmltext, escape, htmlescape, TemplateIO, stringify
         htmltext = _py_htmltext.htmltext

--Apple-Mail-5--287616565--