1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-09-11 12:46:15 +03:00

Merge pull request #595 from Tontyna/fix-path2url

Ensure 'str' and trailing slash in path2url
This commit is contained in:
Guillaume Ayoub 2018-03-20 10:38:31 +01:00 committed by GitHub
commit 5269a65322
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,13 +81,24 @@ def iri_to_uri(url):
def path2url(path):
"""Return file URL of `path`"""
"""Return file URL of `path`.
Accepts 'str' or 'bytes', returns 'str'
"""
# Ensure 'str'
if isinstance(path, bytes):
path = path.decode(sys.getfilesystemencoding())
# if a trailing path.sep is given -- keep it
wants_trailing_slash = path.endswith(os.path.sep) or path.endswith('/')
path = os.path.abspath(path)
if os.path.isdir(path):
if wants_trailing_slash or os.path.isdir(path):
# Make sure directory names have a trailing slash.
# Otherwise relative URIs are resolved from the parent directory.
path += os.path.sep
wants_trailing_slash = True
path = pathname2url(path)
# on Windows pathname2url cuts off trailing slash
if wants_trailing_slash and not path.endswith('/'):
path += '/'
if path.startswith('///'):
# On Windows pathname2url(r'C:\foo') is apparently '///C:/foo'
# That enough slashes already.