From fcd4f9aa0d669b502087b77ba9a1214a16d780b7 Mon Sep 17 00:00:00 2001 From: "Mr. Senko" Date: Fri, 1 Apr 2016 23:01:57 +0300 Subject: [PATCH] Add AUTHOR_SUBSTITUTIONS --- docs/changelog.rst | 3 +++ docs/settings.rst | 2 ++ pelican/tests/test_contents.py | 17 +++++++++++++++++ pelican/tests/test_urlwrappers.py | 16 ++++++++++++++++ pelican/urlwrappers.py | 7 ++++++- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 315a6b2b..6a4d65a4 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -12,6 +12,9 @@ Next release ``TAG_SUBSTITUTIONS`` and ``CATEGORY_SUBSTITUTIONS`` settings. These also allow for keeping non-alphanum characters for backward compatibility with existing URLs. +* Author slugs can be controlled with greater precision using the + ``AUTHOR_SUBSTITUTIONS`` setting. Keeping non-alphanum characters is supported + as well but discouraged. 3.6.3 (2015-08-14) ================== diff --git a/docs/settings.rst b/docs/settings.rst index 3a8511bc..1de28c12 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -310,6 +310,8 @@ Setting name (followed by default value, if any) What does it do? applied in order. ``skip`` is a boolean indicating whether or not to skip replacement of non-alphanumeric characters. Useful for backward compatibility with existing URLs. +``AUTHOR_SUBSTITUTIONS = ()`` Substitutions for authors. ``SLUG_SUBSTITUTIONS`` is not + taken into account here! ``CATEGORY_SUBSTITUTIONS = ()`` Added to ``SLUG_SUBSTITUTIONS`` for categories. ``TAG_SUBSTITUTIONS = ()`` Added to ``SLUG_SUBSTITUTIONS`` for tags. ====================================================== ============================================================== diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index d62d0ed6..2f774a6e 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -457,6 +457,23 @@ class TestArticle(TestPage): self.assertEqual( article.save_as, 'obrien/csharp-stuff/fnord/index.html') + def test_slugify_with_author_substitutions(self): + settings = get_settings() + settings['AUTHOR_SUBSTITUTIONS'] = [ + ('Alexander Todorov', 'atodorov', False), + ('Krasimir Tsonev', 'krasimir', False), + ] + settings['ARTICLE_URL'] = 'blog/{author}/{slug}/' + settings['ARTICLE_SAVE_AS'] = 'blog/{author}/{slug}/index.html' + article_kwargs = self._copy_page_kwargs() + article_kwargs['metadata']['author'] = Author('Alexander Todorov', + settings) + article_kwargs['metadata']['title'] = 'fnord' + article_kwargs['settings'] = settings + article = Article(**article_kwargs) + self.assertEqual(article.url, 'blog/atodorov/fnord/') + self.assertEqual(article.save_as, 'blog/atodorov/fnord/index.html') + def test_slugify_category_with_dots(self): settings = get_settings() settings['CATEGORY_SUBSTITUTIONS'] = [('Fedora QA', 'fedora.qa', True)] diff --git a/pelican/tests/test_urlwrappers.py b/pelican/tests/test_urlwrappers.py index db194776..21a7d98f 100644 --- a/pelican/tests/test_urlwrappers.py +++ b/pelican/tests/test_urlwrappers.py @@ -71,3 +71,19 @@ class TestURLWrapper(unittest.TestCase): self.assertEqual(tag.slug, 'tag.dot') self.assertEqual(cat.slug, 'cat.dot') + + def test_author_slug_substitutions(self): + settings = { + 'AUTHOR_SUBSTITUTIONS': [ + ('Alexander Todorov', 'atodorov', False), + ('Krasimir Tsonev', 'krasimir', False), + ] + } + + author1 = Author('Mr. Senko', settings=settings) + author2 = Author('Alexander Todorov', settings=settings) + author3 = Author('Krasimir Tsonev', settings=settings) + + self.assertEqual(author1.slug, 'mr-senko') + self.assertEqual(author2.slug, 'atodorov') + self.assertEqual(author3.slug, 'krasimir') diff --git a/pelican/urlwrappers.py b/pelican/urlwrappers.py index e56fea8f..7659471d 100644 --- a/pelican/urlwrappers.py +++ b/pelican/urlwrappers.py @@ -136,4 +136,9 @@ class Tag(URLWrapper): class Author(URLWrapper): - pass + @property + def slug(self): + if self._slug is None: + self._slug = slugify(self.name, + self.settings.get('AUTHOR_SUBSTITUTIONS', ())) + return self._slug