mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
utils: Add path_to_url() and generalize get_relative_path()
The old get_relative_path() implementation assumed os.sep == '/', which doesn't hold on MS Windows. The new implementation uses split_all() for a more general component count. I added path_to_url(), because the: '/'.join(split_all(path)) idiom was showing up in a number of cases, and it's easier to understand what's going on when that reads: path_to_url(path) This will fix a number of places where I think paths and URLs were conflated, and should improve MS Windows support.
This commit is contained in:
parent
b59da89e80
commit
ae4fc5a25e
5 changed files with 25 additions and 13 deletions
|
|
@ -304,11 +304,20 @@ def clean_output_dir(path):
|
|||
|
||||
def get_relative_path(path):
|
||||
"""Return the relative path from the given path to the root path."""
|
||||
nslashes = path.count('/')
|
||||
if nslashes == 0:
|
||||
components = split_all(path)
|
||||
if len(components) <= 1:
|
||||
return os.curdir
|
||||
else:
|
||||
return '/'.join([os.pardir] * nslashes)
|
||||
parents = [os.pardir] * (len(components) - 1)
|
||||
return os.path.join(*parents)
|
||||
|
||||
|
||||
def path_to_url(path):
|
||||
"""Return the URL corresponding to a given path."""
|
||||
if os.sep == '/':
|
||||
return path
|
||||
else:
|
||||
'/'.join(split_all(path))
|
||||
|
||||
|
||||
def truncate_html_words(s, num, end_text='...'):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue