Skip to content

Commit

Permalink
Issue hyde#103: Added safe parameter to url functions
Browse files Browse the repository at this point in the history
  • Loading branch information
navilan committed Nov 10, 2011
1 parent 42d30a6 commit 68597ca
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
6 changes: 6 additions & 0 deletions 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
============================================================

Expand Down
2 changes: 1 addition & 1 deletion README.rst
@@ -1,4 +1,4 @@
Version 0.8.4
Version 0.8.5a1

A brand new **hyde**
====================
Expand Down
4 changes: 2 additions & 2 deletions hyde/ext/plugins/urls.py
Expand Up @@ -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'])
Expand Down
19 changes: 11 additions & 8 deletions hyde/ext/templates/jinja.py
Expand Up @@ -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):
Expand Down
34 changes: 23 additions & 11 deletions hyde/site.py
Expand Up @@ -51,7 +51,6 @@ def path(self):
"""
return self.source.path


class Resource(Processable):
"""
Represents any file that is processed by hyde
Expand Down Expand Up @@ -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):
"""
Expand Down
12 changes: 12 additions & 0 deletions hyde/tests/test_site.py
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion hyde/version.py
Expand Up @@ -3,4 +3,4 @@
Handles hyde version
TODO: Use fabric like versioning scheme
"""
__version__ = '0.8.4'
__version__ = '0.8.5a1'

0 comments on commit 68597ca

Please sign in to comment.