Skip to content

Commit

Permalink
Merge pull request #2585 from getnikola/kwargs-in-path-handlers
Browse files Browse the repository at this point in the history
Keyword arguments in path handlers
  • Loading branch information
Kwpolska committed Dec 7, 2016
2 parents 122380a + 2ae6dd1 commit d743c67
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -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)
Expand Down
21 changes: 14 additions & 7 deletions nikola/nikola.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions scripts/document_path_handlers.py
Expand Up @@ -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
""")
Expand Down

0 comments on commit d743c67

Please sign in to comment.