Skip to content
Permalink
Browse files

Merge pull request #2585 from getnikola/kwargs-in-path-handlers

Keyword arguments in path handlers
  • Loading branch information
Kwpolska committed Dec 7, 2016
2 parents 122380a + 2ae6dd1 commit d743c673344f94e2fb17d131f18fc98bfc5b6f7e
Showing with 21 additions and 9 deletions.
  1. +3 −0 CHANGES.txt
  2. +14 −7 nikola/nikola.py
  3. +4 −2 scripts/document_path_handlers.py
@@ -12,6 +12,9 @@ Bugfixes
Features
--------

* Query strings in magic links are passed as keyword arguments to path
handlers (via Issue #2580)
* Accept arbitrary arguments to path handlers (via Issue #2580)
* Added new ``typogrify_oldschool`` filter (Issue #2574)
* Improving handling of .dep files, and allowing compilers to specify
additional targets for the render_posts task (Issue #2536)
@@ -39,9 +39,9 @@
import natsort
import mimetypes
try:
from urlparse import urlparse, urlsplit, urlunsplit, urljoin, unquote
from urlparse import urlparse, urlsplit, urlunsplit, urljoin, unquote, parse_qs
except ImportError:
from urllib.parse import urlparse, urlsplit, urlunsplit, urljoin, unquote # NOQA
from urllib.parse import urlparse, urlsplit, urlunsplit, urljoin, unquote, parse_qs # NOQA

try:
import pyphen
@@ -1433,7 +1433,14 @@ def url_replacer(self, src, dst, lang=None, url_type=None):
# Refuse to replace links that are full URLs.
if dst_url.netloc:
if dst_url.scheme == 'link': # Magic link
dst = self.link(dst_url.netloc, dst_url.path.lstrip('/'), lang)
if dst_url.query:
# If query strings are used in magic link, they will be
# passed to the path handler as keyword arguments (strings)
link_kwargs = {k: v[-1] for k, v in parse_qs(dst_url.query).items()}
else:
link_kwargs = {}

dst = self.link(dst_url.netloc, dst_url.path.lstrip('/'), lang, **link_kwargs)
# Assuming the site is served over one of these, and
# since those are the only URLs we want to rewrite...
else:
@@ -1688,7 +1695,7 @@ def generic_rss_renderer(self, lang, title, link, description, timeline, output_
data = data.decode('utf-8')
rss_file.write(data)

def path(self, kind, name, lang=None, is_link=False):
def path(self, kind, name, lang=None, is_link=False, **kwargs):
r"""Build the path to a certain kind of page.
These are mostly defined by plugins by registering via the
@@ -1725,7 +1732,7 @@ def path(self, kind, name, lang=None, is_link=False):
lang = utils.LocaleBorg().current_lang

try:
path = self.path_handlers[kind](name, lang)
path = self.path_handlers[kind](name, lang, **kwargs)
path = [os.path.normpath(p) for p in path if p != '.'] # Fix Issue #1028
if is_link:
link = '/' + ('/'.join(path))
@@ -1804,9 +1811,9 @@ def register_path_handler(self, kind, f):
else:
self.path_handlers[kind] = f

def link(self, *args):
def link(self, *args, **kwargs):
"""Create a link."""
url = self.path(*args, is_link=True)
url = self.path(*args, is_link=True, **kwargs)
url = utils.encodelink(url)
return url

@@ -9,8 +9,10 @@
.. slug: path-handlers
.. author: The Nikola Team
Nikola supports special links with the syntax ``link://kind/name``. In the templates you can also
use ``_link(kind, name)`` Here is the description for all the supported kinds.
Nikola supports special links with the syntax ``link://kind/name``. In the templates you can also use ``_link(kind, name)``.
You can also add query strings (``?key=value``) for extra arguments, or pass keyword arguments to ``_link`` in templates (support and behavior depends on path handlers themselves)
Here are the descriptions for all the supported kinds.
.. class:: dl-horizontal
""")

0 comments on commit d743c67

Please sign in to comment.
You can’t perform that action at this time.