forked from github/pelican
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
|
||||
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 = [
|
||||
(r'[^\\w\\s-]', ''), # remove non-alphabetical/whitespace/'-' chars
|
||||
(r'(?u)\\A\\s*', ''), # strip leading whitespace
|
||||
|
|
|
|||
|
|
@ -102,7 +102,8 @@ class Content(object):
|
|||
self.slug = slugify(
|
||||
value,
|
||||
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.relative_source_path = self.get_relative_source_path()
|
||||
|
|
|
|||
|
|
@ -156,6 +156,7 @@ DEFAULT_CONFIG = {
|
|||
'INTRASITE_LINK_REGEX': '[{|](?P<what>.*?)[|}]',
|
||||
'SLUGIFY_SOURCE': 'title',
|
||||
'SLUGIFY_USE_UNICODE': False,
|
||||
'SLUGIFY_PRESERVE_CASE': False,
|
||||
'CACHE_CONTENT': False,
|
||||
'CONTENT_CACHING_LAYER': 'reader',
|
||||
'CACHE_PATH': 'cache',
|
||||
|
|
|
|||
|
|
@ -135,16 +135,31 @@ class TestPage(LoggedTestCase):
|
|||
page = Page(**page_kwargs)
|
||||
self.assertEqual(page.slug, 'foo')
|
||||
|
||||
# test slug from unicode title
|
||||
# slug doesn't use unicode
|
||||
settings['SLUGIFY_SOURCE'] = "title"
|
||||
page_kwargs['metadata']['title'] = '指導書'
|
||||
# test slug from title with unicode and case
|
||||
|
||||
inputs = (
|
||||
# (title, expected, preserve_case, use_unicode)
|
||||
('指導書', 'zhi-dao-shu', False, False),
|
||||
('指導書', 'Zhi-Dao-Shu', True, False),
|
||||
('指導書', '指導書', False, True),
|
||||
('指導書', '指導書', True, True),
|
||||
('Çığ', 'cig', False, False),
|
||||
('Çığ', '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, 'zhi-dao-shu')
|
||||
# slug uses unicode
|
||||
settings['SLUGIFY_USE_UNICODE'] = True
|
||||
page = Page(**page_kwargs)
|
||||
self.assertEqual(page.slug, '指導書')
|
||||
self.assertEqual(page.slug, expected,
|
||||
(title, preserve_case, use_unicode))
|
||||
|
||||
def test_defaultlang(self):
|
||||
# If no lang is given, default to the default one.
|
||||
|
|
|
|||
|
|
@ -37,9 +37,11 @@ class URLWrapper(object):
|
|||
regex_subs = self.settings.get(
|
||||
class_key,
|
||||
self.settings.get('SLUG_REGEX_SUBSTITUTIONS', []))
|
||||
preserve_case = self.settings.get('SLUGIFY_PRESERVE_CASE', False)
|
||||
self._slug = slugify(
|
||||
self.name,
|
||||
regex_subs=regex_subs,
|
||||
preserve_case=preserve_case,
|
||||
use_unicode=self.settings.get('SLUGIFY_USE_UNICODE', False)
|
||||
)
|
||||
return self._slug
|
||||
|
|
@ -66,7 +68,12 @@ class URLWrapper(object):
|
|||
class_key,
|
||||
self.settings.get('SLUG_REGEX_SUBSTITUTIONS', []))
|
||||
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):
|
||||
if isinstance(other, self.__class__):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue