From db95e0a240930bd615414cbf5ce923136d947124 Mon Sep 17 00:00:00 2001 From: Vladimir Kazanov Date: Mon, 24 Jul 2017 08:19:22 +0100 Subject: [PATCH 1/4] do not wrap extra code in try..except --- nikola/nikola.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index b630f935c1..4a2d3a342f 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1750,23 +1750,23 @@ def path(self, kind, name, lang=None, is_link=False, **kwargs): 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 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. From cc53e6c22efe26843dfcf1ced7cb8d363b3bd2fc Mon Sep 17 00:00:00 2001 From: Vladimir Kazanov Date: Mon, 24 Jul 2017 08:29:26 +0100 Subject: [PATCH 2/4] If path handler returns a string - use it without further processing --- nikola/nikola.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index 4a2d3a342f..2b9975f758 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1736,14 +1736,17 @@ 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://en.wikipedia.org/wiki/Nikola"), 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 @@ -1753,6 +1756,12 @@ def path(self, kind, name, lang=None, is_link=False, **kwargs): 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://en.wikipedia.org/wiki/Nikola'. See Issue #2876. + if isinstance(path, str): + return path + if path is None: path = "#" else: From f70e993955aa59a153b2a19f81ee985d99aa7a29 Mon Sep 17 00:00:00 2001 From: Vladimir Kazanov Date: Mon, 24 Jul 2017 08:47:56 +0100 Subject: [PATCH 3/4] Mention the path handler change in CHANGES.txt --- CHANGES.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.txt b/CHANGES.txt index bee2f42125..58aa0ad4da 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 -------- From 6e7a7f3797cb4cdefa0c2c4099619837301c8eab Mon Sep 17 00:00:00 2001 From: Vladimir Kazanov Date: Mon, 24 Jul 2017 11:00:20 +0100 Subject: [PATCH 4/4] better example URLs --- nikola/nikola.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nikola/nikola.py b/nikola/nikola.py index 2b9975f758..719188a49a 100644 --- a/nikola/nikola.py +++ b/nikola/nikola.py @@ -1737,7 +1737,7 @@ def path(self, kind, name, lang=None, is_link=False, **kwargs): * filename (name is the source filename of a post/page, in DEFAULT_LANG, relative to conf.py) The returned value is either a path relative to output, like "categories/whatever.html", or - an absolute URL ("https://en.wikipedia.org/wiki/Nikola"), if path handler returns a string. + 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"). @@ -1758,7 +1758,7 @@ def path(self, kind, name, lang=None, is_link=False, **kwargs): return "" # If path handler returns a string we consider it to be an absolute URL not requiring any - # further processing, i.e 'https://en.wikipedia.org/wiki/Nikola'. See Issue #2876. + # further processing, i.e 'https://getnikola.com/'. See Issue #2876. if isinstance(path, str): return path