diff --git a/IPython/html/utils.py b/IPython/html/utils.py index 6bb2ae4..500d152 100644 --- a/IPython/html/utils.py +++ b/IPython/html/utils.py @@ -12,6 +12,9 @@ Authors: # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- +import os +from urllib import quote, unquote + #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- @@ -24,9 +27,25 @@ def url_path_join(*pieces): """ initial = pieces[0].startswith('/') final = pieces[-1].endswith('/') - striped = [s.strip('/') for s in pieces] - result = '/'.join(s for s in striped if s) + stripped = [s.strip('/') for s in pieces] + result = '/'.join(s for s in stripped if s) if initial: result = '/' + result if final: result = result + '/' if result == '//': result = '/' return result + +def path2url(path): + """Convert a local file path to a URL""" + pieces = [ quote(p) for p in path.split(os.path.sep) ] + # preserve trailing / + if pieces[-1] == '': + pieces[-1] = '/' + url = url_path_join(*pieces) + return url + +def url2path(url): + """Convert a URL to a local file path""" + pieces = [ unquote(p) for p in url.split('/') ] + path = os.path.join(*pieces) + return path +