mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Expose preserve_case option from slugify
This commit is contained in:
parent
97fe235e60
commit
bd699d34e8
5 changed files with 42 additions and 12 deletions
|
|
@ -628,6 +628,12 @@ corresponding ``*_URL`` setting as string, while others hard-code them:
|
||||||
in auto-generated slugs. Otherwise, unicode characters will be replaced
|
in auto-generated slugs. Otherwise, unicode characters will be replaced
|
||||||
with ASCII equivalents.
|
with ASCII equivalents.
|
||||||
|
|
||||||
|
|
||||||
|
.. data:: SLUGIFY_PRESERVE_CASE = False
|
||||||
|
|
||||||
|
Preserve uppercase characters in the slugs. Set ``True`` to keep the
|
||||||
|
uppercase characters in the ``SLUGIFY_SOURCE`` as is.
|
||||||
|
|
||||||
.. data:: SLUG_REGEX_SUBSTITUTIONS = [
|
.. data:: SLUG_REGEX_SUBSTITUTIONS = [
|
||||||
(r'[^\\w\\s-]', ''), # remove non-alphabetical/whitespace/'-' chars
|
(r'[^\\w\\s-]', ''), # remove non-alphabetical/whitespace/'-' chars
|
||||||
(r'(?u)\\A\\s*', ''), # strip leading whitespace
|
(r'(?u)\\A\\s*', ''), # strip leading whitespace
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,8 @@ class Content(object):
|
||||||
self.slug = slugify(
|
self.slug = slugify(
|
||||||
value,
|
value,
|
||||||
regex_subs=settings.get('SLUG_REGEX_SUBSTITUTIONS', []),
|
regex_subs=settings.get('SLUG_REGEX_SUBSTITUTIONS', []),
|
||||||
use_unicode=settings['SLUGIFY_USE_UNICODE'])
|
preserve_case=settings.get('SLUGIFY_PRESERVE_CASE', False),
|
||||||
|
use_unicode=settings.get('SLUGIFY_USE_UNICODE', False))
|
||||||
|
|
||||||
self.source_path = source_path
|
self.source_path = source_path
|
||||||
self.relative_source_path = self.get_relative_source_path()
|
self.relative_source_path = self.get_relative_source_path()
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,7 @@ DEFAULT_CONFIG = {
|
||||||
'INTRASITE_LINK_REGEX': '[{|](?P<what>.*?)[|}]',
|
'INTRASITE_LINK_REGEX': '[{|](?P<what>.*?)[|}]',
|
||||||
'SLUGIFY_SOURCE': 'title',
|
'SLUGIFY_SOURCE': 'title',
|
||||||
'SLUGIFY_USE_UNICODE': False,
|
'SLUGIFY_USE_UNICODE': False,
|
||||||
|
'SLUGIFY_PRESERVE_CASE': False,
|
||||||
'CACHE_CONTENT': False,
|
'CACHE_CONTENT': False,
|
||||||
'CONTENT_CACHING_LAYER': 'reader',
|
'CONTENT_CACHING_LAYER': 'reader',
|
||||||
'CACHE_PATH': 'cache',
|
'CACHE_PATH': 'cache',
|
||||||
|
|
|
||||||
|
|
@ -135,16 +135,31 @@ class TestPage(LoggedTestCase):
|
||||||
page = Page(**page_kwargs)
|
page = Page(**page_kwargs)
|
||||||
self.assertEqual(page.slug, 'foo')
|
self.assertEqual(page.slug, 'foo')
|
||||||
|
|
||||||
# test slug from unicode title
|
# test slug from title with unicode and case
|
||||||
# slug doesn't use unicode
|
|
||||||
settings['SLUGIFY_SOURCE'] = "title"
|
inputs = (
|
||||||
page_kwargs['metadata']['title'] = '指導書'
|
# (title, expected, preserve_case, use_unicode)
|
||||||
page = Page(**page_kwargs)
|
('指導書', 'zhi-dao-shu', False, False),
|
||||||
self.assertEqual(page.slug, 'zhi-dao-shu')
|
('指導書', 'Zhi-Dao-Shu', True, False),
|
||||||
# slug uses unicode
|
('指導書', '指導書', False, True),
|
||||||
settings['SLUGIFY_USE_UNICODE'] = True
|
('指導書', '指導書', True, True),
|
||||||
page = Page(**page_kwargs)
|
('Çığ', 'cig', False, False),
|
||||||
self.assertEqual(page.slug, '指導書')
|
('Çığ', 'Cig', True, False),
|
||||||
|
('Çığ', 'çığ', False, True),
|
||||||
|
('Çığ', 'Çığ', True, True),
|
||||||
|
)
|
||||||
|
|
||||||
|
settings = get_settings()
|
||||||
|
page_kwargs = self._copy_page_kwargs()
|
||||||
|
page_kwargs['settings'] = settings
|
||||||
|
|
||||||
|
for title, expected, preserve_case, use_unicode in inputs:
|
||||||
|
settings['SLUGIFY_PRESERVE_CASE'] = preserve_case
|
||||||
|
settings['SLUGIFY_USE_UNICODE'] = use_unicode
|
||||||
|
page_kwargs['metadata']['title'] = title
|
||||||
|
page = Page(**page_kwargs)
|
||||||
|
self.assertEqual(page.slug, expected,
|
||||||
|
(title, preserve_case, use_unicode))
|
||||||
|
|
||||||
def test_defaultlang(self):
|
def test_defaultlang(self):
|
||||||
# If no lang is given, default to the default one.
|
# If no lang is given, default to the default one.
|
||||||
|
|
|
||||||
|
|
@ -37,9 +37,11 @@ class URLWrapper(object):
|
||||||
regex_subs = self.settings.get(
|
regex_subs = self.settings.get(
|
||||||
class_key,
|
class_key,
|
||||||
self.settings.get('SLUG_REGEX_SUBSTITUTIONS', []))
|
self.settings.get('SLUG_REGEX_SUBSTITUTIONS', []))
|
||||||
|
preserve_case = self.settings.get('SLUGIFY_PRESERVE_CASE', False)
|
||||||
self._slug = slugify(
|
self._slug = slugify(
|
||||||
self.name,
|
self.name,
|
||||||
regex_subs=regex_subs,
|
regex_subs=regex_subs,
|
||||||
|
preserve_case=preserve_case,
|
||||||
use_unicode=self.settings.get('SLUGIFY_USE_UNICODE', False)
|
use_unicode=self.settings.get('SLUGIFY_USE_UNICODE', False)
|
||||||
)
|
)
|
||||||
return self._slug
|
return self._slug
|
||||||
|
|
@ -66,7 +68,12 @@ class URLWrapper(object):
|
||||||
class_key,
|
class_key,
|
||||||
self.settings.get('SLUG_REGEX_SUBSTITUTIONS', []))
|
self.settings.get('SLUG_REGEX_SUBSTITUTIONS', []))
|
||||||
use_unicode = self.settings.get('SLUGIFY_USE_UNICODE', False)
|
use_unicode = self.settings.get('SLUGIFY_USE_UNICODE', False)
|
||||||
return slugify(key, regex_subs=regex_subs, use_unicode=use_unicode)
|
preserve_case = self.settings.get('SLUGIFY_PRESERVE_CASE', False)
|
||||||
|
return slugify(
|
||||||
|
key,
|
||||||
|
regex_subs=regex_subs,
|
||||||
|
preserve_case=preserve_case,
|
||||||
|
use_unicode=use_unicode)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, self.__class__):
|
if isinstance(other, self.__class__):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue