From 56b0061393d682f2487901b27bcafd5f0fbbd4a7 Mon Sep 17 00:00:00 2001 From: th3aftermath Date: Fri, 31 Jan 2014 19:42:20 -0500 Subject: [PATCH] Add the setting SLUGIFY_ATTRIBUTE --- docs/settings.rst | 4 ++++ pelican/contents.py | 12 +++++++++--- pelican/settings.py | 1 + pelican/tests/test_contents.py | 11 +++++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index b75ce53d..3873a479 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -166,6 +166,10 @@ Setting name (default value) `PYGMENTS_RST_OPTIONS` (``[]``) A list of default Pygments settings for your reStructuredText code blocks. See :ref:`internal_pygments_options` for a list of supported options. + +`SLUGIFY_SOURCE` (``'input'``) Specifies where you want the slug to be automatically generated + from. Can be set to 'title' to use the Title: metadata tag or + 'basename' to use the articles basename to make a slug. =============================================================================== ===================================================================== .. [#] Default is the system locale. diff --git a/pelican/contents.py b/pelican/contents.py index 2ba81c1d..66602666 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -96,9 +96,15 @@ class Content(object): self.in_default_lang = (self.lang == default_lang) - # create the slug if not existing, from the title - if not hasattr(self, 'slug') and hasattr(self, 'title'): - self.slug = slugify(self.title, + # create the slug if not existing, generate slug according to + # setting of SLUG_ATTRIBUTE + if not hasattr(self, 'slug'): + if settings['SLUGIFY_SOURCE'] == 'title' and hasattr(self, 'title'): + self.slug = slugify(self.title, + settings.get('SLUG_SUBSTITUTIONS', ())) + elif settings['SLUGIFY_SOURCE'] == 'basename' and source_path != None: + basename = os.path.basename(os.path.splitext(source_path)[0]) + self.slug = slugify(basename, settings.get('SLUG_SUBSTITUTIONS', ())) self.source_path = source_path diff --git a/pelican/settings.py b/pelican/settings.py index 225a1e9d..7ab43a82 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -116,6 +116,7 @@ DEFAULT_CONFIG = { 'IGNORE_FILES': ['.#*'], 'SLUG_SUBSTITUTIONS': (), 'INTRASITE_LINK_REGEX': '[{|](?P.*?)[|}]', + 'SLUGIFY_SOURCE': 'title' } PYGMENTS_RST_OPTIONS = None diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py index f831f061..4c6f8ed1 100644 --- a/pelican/tests/test_contents.py +++ b/pelican/tests/test_contents.py @@ -32,6 +32,7 @@ class TestPage(unittest.TestCase): 'title': 'foo bar', 'author': 'Blogger', }, + 'source_path': '/path/to/file/foo.ext' } def test_use_args(self): @@ -77,9 +78,15 @@ class TestPage(unittest.TestCase): self.assertEqual(page.summary, '') def test_slug(self): - # If a title is given, it should be used to generate the slug. - page = Page(**self.page_kwargs) + page_kwargs = self._copy_page_kwargs() + settings = get_settings() + page_kwargs['settings'] = settings + settings['SLUGIFY_SOURCE'] = "title" + page = Page(**page_kwargs) self.assertEqual(page.slug, 'foo-bar') + settings['SLUGIFY_SOURCE'] = "basename" + page = Page(**page_kwargs) + self.assertEqual(page.slug, 'foo') def test_defaultlang(self): # If no lang is given, default to the default one.