Skip to content

Commit b1b9bb0

Browse files
authored
Add 'slug_source' path handler (#3831)
This allows the creation of links to the source for a blog post. Note that this also works for translations, so the link will always point to the correct source even in translations.
1 parent b1c0e92 commit b1b9bb0

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

AUTHORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* `dastagg <https://github.com/dastagg>`_
3636
* `David Barragán Merino <https://github.com/bameda>`_
3737
* `David Beath <https://github.com/DBeath>`_
38+
* `Detlev Zundel <https://github.com/laodzu>`_
3839
* `Dhruv Baldawa <https://github.com/dhruvbaldawa>`_
3940
* `Dirk Engling <https://github.com/erdgeist>`_
4041
* `Dmitry Verkhoturov <https://github.com/paskal>`_

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Features
1010
(either ``mako.lookup.TemplateLookup`` or ``jinja2.Environment``)
1111
via an optional ``conf.py`` method ``TEMPLATE_ENGINE_FACTORY``.
1212
* Switch to pyproject.toml
13+
* Add path handler ``slug_source`` linking to source of post.
1314

1415
Bugfixes
1516
--------

docs/path_handlers.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ slug
197197
link://slug/yellow-camaro => /posts/cars/awful/yellow-camaro/index.html
198198

199199

200+
slug_source
201+
Return a link to source for a post with given slug, if not ambiguous.
202+
203+
Example:
204+
205+
link://slug_source/yellow-camaro => /posts/cars/awful/yellow-camaro.rst
206+
207+
200208
tag
201209
A link to a tag's page. Takes page number as optional keyword argument.
202210

nikola/nikola.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ def __init__(self, **config) -> None:
379379
# Register our own path handlers
380380
self.path_handlers = {
381381
'slug': self.slug_path,
382+
'slug_source': self.slug_source,
382383
'post_path': self.post_path,
383384
'root': self.root_path,
384385
'filename': self.filename_path,
@@ -1970,6 +1971,21 @@ def slug_path(self, name, lang):
19701971
utils.LOGGER.warning('Ambiguous path request for slug: {0}'.format(name))
19711972
return [_f for _f in results[0].permalink(lang).split('/')]
19721973

1974+
def slug_source(self, name, lang):
1975+
"""Return a link to source for a post with given slug, if not ambiguous.
1976+
1977+
Example:
1978+
1979+
link://slug_source/yellow-camaro => /posts/cars/awful/yellow-camaro.rst
1980+
"""
1981+
results = [p for p in self.timeline if p.meta('slug') == name]
1982+
if not results:
1983+
utils.LOGGER.warning("Cannot resolve path request for slug: {0}".format(name))
1984+
else:
1985+
if len(results) > 1:
1986+
utils.LOGGER.warning('Ambiguous path request for slug: {0}'.format(name))
1987+
return [_f for _f in results[0].translated_source_path(lang).split('/')]
1988+
19731989
def filename_path(self, name, lang):
19741990
"""Link to post or page by source filename.
19751991

0 commit comments

Comments
 (0)