Skip to content

Commit d743c67

Browse files
authoredDec 7, 2016
Merge pull request #2585 from getnikola/kwargs-in-path-handlers
Keyword arguments in path handlers
2 parents 122380a + 2ae6dd1 commit d743c67

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed
 

‎CHANGES.txt

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Bugfixes
1212
Features
1313
--------
1414

15+
* Query strings in magic links are passed as keyword arguments to path
16+
handlers (via Issue #2580)
17+
* Accept arbitrary arguments to path handlers (via Issue #2580)
1518
* Added new ``typogrify_oldschool`` filter (Issue #2574)
1619
* Improving handling of .dep files, and allowing compilers to specify
1720
additional targets for the render_posts task (Issue #2536)

‎nikola/nikola.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
import natsort
4040
import mimetypes
4141
try:
42-
from urlparse import urlparse, urlsplit, urlunsplit, urljoin, unquote
42+
from urlparse import urlparse, urlsplit, urlunsplit, urljoin, unquote, parse_qs
4343
except ImportError:
44-
from urllib.parse import urlparse, urlsplit, urlunsplit, urljoin, unquote # NOQA
44+
from urllib.parse import urlparse, urlsplit, urlunsplit, urljoin, unquote, parse_qs # NOQA
4545

4646
try:
4747
import pyphen
@@ -1433,7 +1433,14 @@ def url_replacer(self, src, dst, lang=None, url_type=None):
14331433
# Refuse to replace links that are full URLs.
14341434
if dst_url.netloc:
14351435
if dst_url.scheme == 'link': # Magic link
1436-
dst = self.link(dst_url.netloc, dst_url.path.lstrip('/'), lang)
1436+
if dst_url.query:
1437+
# If query strings are used in magic link, they will be
1438+
# passed to the path handler as keyword arguments (strings)
1439+
link_kwargs = {k: v[-1] for k, v in parse_qs(dst_url.query).items()}
1440+
else:
1441+
link_kwargs = {}
1442+
1443+
dst = self.link(dst_url.netloc, dst_url.path.lstrip('/'), lang, **link_kwargs)
14371444
# Assuming the site is served over one of these, and
14381445
# since those are the only URLs we want to rewrite...
14391446
else:
@@ -1688,7 +1695,7 @@ def generic_rss_renderer(self, lang, title, link, description, timeline, output_
16881695
data = data.decode('utf-8')
16891696
rss_file.write(data)
16901697

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

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

1807-
def link(self, *args):
1814+
def link(self, *args, **kwargs):
18081815
"""Create a link."""
1809-
url = self.path(*args, is_link=True)
1816+
url = self.path(*args, is_link=True, **kwargs)
18101817
url = utils.encodelink(url)
18111818
return url
18121819

‎scripts/document_path_handlers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
.. slug: path-handlers
1010
.. author: The Nikola Team
1111
12-
Nikola supports special links with the syntax ``link://kind/name``. In the templates you can also
13-
use ``_link(kind, name)`` Here is the description for all the supported kinds.
12+
Nikola supports special links with the syntax ``link://kind/name``. In the templates you can also use ``_link(kind, name)``.
13+
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)
14+
15+
Here are the descriptions for all the supported kinds.
1416
1517
.. class:: dl-horizontal
1618
""")

0 commit comments

Comments
 (0)
Please sign in to comment.