Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute paths in path handlers #2879

Merged
merged 4 commits into from Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -12,6 +12,7 @@ Features
full tracebacks instead of one-line summaries
* Use ``PRETTY_URLS`` by default on all sites (Issue #1838)
* Feed link generation is completely refactored (Issue #2844)
* Let path handlers return absolute URLs (Issue #2876)

Bugfixes
--------
Expand Down
41 changes: 25 additions & 16 deletions nikola/nikola.py
Expand Up @@ -1736,38 +1736,47 @@ def path(self, kind, name, lang=None, is_link=False, **kwargs):
* slug (name is the slug of a post or page)
* filename (name is the source filename of a post/page, in DEFAULT_LANG, relative to conf.py)

The returned value is always a path relative to output, like
"categories/whatever.html"
The returned value is either a path relative to output, like "categories/whatever.html", or
an absolute URL ("https://getnikola.com/"), if path handler returns a string.

If is_link is True, the path is absolute and uses "/" as separator
(ex: "/archive/index.html").
If is_link is False, the path is relative to output and uses the
platform's separator.
(ex: "archive\index.html")
If the registered path handler returns a string instead of path component list - it's
considered to be an absolute URL and returned as is.

"""
if lang is None:
lang = utils.LocaleBorg().current_lang

try:
path = self.path_handlers[kind](name, lang, **kwargs)
if path is None:
path = "#"
else:
path = [os.path.normpath(p) for p in path if p != '.'] # Fix Issue #1028
if is_link:
link = '/' + ('/'.join(path))
index_len = len(self.config['INDEX_FILE'])
if self.config['STRIP_INDEXES'] and \
link[-(1 + index_len):] == '/' + self.config['INDEX_FILE']:
return link[:-index_len]
else:
return link
else:
return os.path.join(*path)
except KeyError:
utils.LOGGER.warn("Unknown path request of kind: {0}".format(kind))
return ""

# If path handler returns a string we consider it to be an absolute URL not requiring any
# further processing, i.e 'https://getnikola.com/'. See Issue #2876.
if isinstance(path, str):
return path

if path is None:
path = "#"
else:
path = [os.path.normpath(p) for p in path if p != '.'] # Fix Issue #1028
if is_link:
link = '/' + ('/'.join(path))
index_len = len(self.config['INDEX_FILE'])
if self.config['STRIP_INDEXES'] and \
link[-(1 + index_len):] == '/' + self.config['INDEX_FILE']:
return link[:-index_len]
else:
return link
else:
return os.path.join(*path)

def post_path(self, name, lang):
"""Link to the destination of an element in the POSTS/PAGES settings.

Expand Down