Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #103: Added safe parameter to url functions

  • Loading branch information...
commit 68597ca925c3479b847c7410571b29a5f36f3440 1 parent 42d30a6
@lakshmivyas lakshmivyas authored
View
6 CHANGELOG.rst
@@ -1,3 +1,9 @@
+Version 0.8.5a1
+============================================================
+
+* Added ability to specify safe characters in `content_url`,
+ `media_url` functions and `urlencode` filter. (Issue #103)
+
Version 0.8.4
============================================================
View
2  README.rst
@@ -1,4 +1,4 @@
-Version 0.8.4
+Version 0.8.5a1
A brand new **hyde**
====================
View
4 hyde/ext/plugins/urls.py
@@ -53,8 +53,8 @@ def begin_site(self):
def clean_url(urlgetter):
@wraps(urlgetter)
- def wrapper(site, path):
- url = urlgetter(site, path)
+ def wrapper(site, path, safe=None):
+ url = urlgetter(site, path, safe)
index_file_names = getattr(settings,
'index_file_names',
['index.html'])
View
19 hyde/ext/templates/jinja.py
@@ -36,29 +36,32 @@ def __call__(self, *args, **kwargs):
return self
@contextfunction
-def media_url(context, path):
+def media_url(context, path, safe=None):
"""
Returns the media url given a partial path.
"""
- return context['site'].media_url(path)
+ return context['site'].media_url(path, safe)
@contextfunction
-def content_url(context, path):
+def content_url(context, path, safe=None):
"""
Returns the content url given a partial path.
"""
- return context['site'].content_url(path)
+ return context['site'].content_url(path, safe)
@contextfunction
-def full_url(context, path):
+def full_url(context, path, safe=None):
"""
Returns the full url given a partial path.
"""
- return context['site'].full_url(path)
+ return context['site'].full_url(path, safe)
@contextfilter
-def urlencode(ctx, url):
- return quote(url.encode('utf8'))
+def urlencode(ctx, url, safe=None):
+ if safe is not None:
+ return quote(url.encode('utf8'), safe)
+ else:
+ return quote(url.encode('utf8'))
@contextfilter
def urldecode(ctx, url):
View
34 hyde/site.py
@@ -51,7 +51,6 @@ def path(self):
"""
return self.source.path
-
class Resource(Processable):
"""
Represents any file that is processed by hyde
@@ -412,33 +411,46 @@ def load(self):
"""
self.content.load()
- def content_url(self, path):
+ def content_url(self, path, safe=None):
"""
Returns the content url by appending the base url from the config
- with the given path.
+ with the given path. The return value is url encoded.
"""
- return quote(Folder(self.config.base_url).child(path).replace(os.sep, '/').encode("utf-8"))
+ fpath = Folder(self.config.base_url) \
+ .child(path) \
+ .replace(os.sep, '/').encode("utf-8")
+ if safe is not None:
+ return quote(fpath, safe)
+ else:
+ return quote(fpath)
- def media_url(self, path):
+ def media_url(self, path, safe=None):
"""
Returns the media url by appending the media base url from the config
- with the given path.
+ with the given path. The return value is url encoded.
"""
- return quote(Folder(self.config.media_url).child(path).replace(os.sep, '/').encode("utf-8"))
+ fpath = Folder(self.config.media_url) \
+ .child(path) \
+ .replace(os.sep, '/').encode("utf-8")
+ if safe is not None:
+ return quote(fpath, safe)
+ else:
+ return quote(fpath)
- def full_url(self, path):
+ def full_url(self, path, safe=None):
"""
Determines if the given path is media or content based on the
- configuration and returns the appropriate url.
+ configuration and returns the appropriate url. The return value
+ is url encoded.
"""
if urlparse.urlparse(path)[:2] != ("",""):
return path
if self.is_media(path):
relative_path = File(path).get_relative_path(
Folder(self.config.media_root))
- return self.media_url(relative_path)
+ return self.media_url(relative_path, safe)
else:
- return self.content_url(path)
+ return self.content_url(path, safe)
def is_media(self, path):
"""
View
12 hyde/tests/test_site.py
@@ -197,6 +197,18 @@ def test_content_url(self):
path = 'blog/2010/december'
assert s.content_url(path) == "/" + path
+ def test_content_url_encoding(self):
+ s = Site(self.SITE_PATH, config=self.config)
+ s.load()
+ path = '".jpg'
+ assert s.content_url(path) == quote("/" + path)
+
+ def test_content_url_encoding_safe(self):
+ s = Site(self.SITE_PATH, config=self.config)
+ s.load()
+ path = '".jpg/abc'
+ assert s.content_url(path, "") == quote("/" + path, "")
+
def test_media_url(self):
s = Site(self.SITE_PATH, config=self.config)
s.load()
View
2  hyde/version.py
@@ -3,4 +3,4 @@
Handles hyde version
TODO: Use fabric like versioning scheme
"""
-__version__ = '0.8.4'
+__version__ = '0.8.5a1'
Please sign in to comment.
Something went wrong with that request. Please try again.