mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
control slug substitutions from settings with regex
This commit is contained in:
parent
6af825eae6
commit
5199fa51ea
12 changed files with 409 additions and 235 deletions
|
|
@ -497,7 +497,13 @@ class TestArticle(TestPage):
|
|||
|
||||
def test_slugify_category_author(self):
|
||||
settings = get_settings()
|
||||
settings['SLUG_SUBSTITUTIONS'] = [('C#', 'csharp')]
|
||||
settings['SLUG_REGEX_SUBSTITUTIONS'] = [
|
||||
(r'C#', 'csharp'),
|
||||
(r'[^\w\s-]', ''),
|
||||
(r'(?u)\A\s*', ''),
|
||||
(r'(?u)\s*\Z', ''),
|
||||
(r'[-\s]+', '-'),
|
||||
]
|
||||
settings['ARTICLE_URL'] = '{author}/{category}/{slug}/'
|
||||
settings['ARTICLE_SAVE_AS'] = '{author}/{category}/{slug}/index.html'
|
||||
article_kwargs = self._copy_page_kwargs()
|
||||
|
|
@ -513,9 +519,13 @@ class TestArticle(TestPage):
|
|||
|
||||
def test_slugify_with_author_substitutions(self):
|
||||
settings = get_settings()
|
||||
settings['AUTHOR_SUBSTITUTIONS'] = [
|
||||
('Alexander Todorov', 'atodorov', False),
|
||||
('Krasimir Tsonev', 'krasimir', False),
|
||||
settings['AUTHOR_REGEX_SUBSTITUTIONS'] = [
|
||||
('Alexander Todorov', 'atodorov'),
|
||||
('Krasimir Tsonev', 'krasimir'),
|
||||
(r'[^\w\s-]', ''),
|
||||
(r'(?u)\A\s*', ''),
|
||||
(r'(?u)\s*\Z', ''),
|
||||
(r'[-\s]+', '-'),
|
||||
]
|
||||
settings['ARTICLE_URL'] = 'blog/{author}/{slug}/'
|
||||
settings['ARTICLE_SAVE_AS'] = 'blog/{author}/{slug}/index.html'
|
||||
|
|
@ -530,7 +540,9 @@ class TestArticle(TestPage):
|
|||
|
||||
def test_slugify_category_with_dots(self):
|
||||
settings = get_settings()
|
||||
settings['CATEGORY_SUBSTITUTIONS'] = [('Fedora QA', 'fedora.qa', True)]
|
||||
settings['CATEGORY_REGEX_SUBSTITUTIONS'] = [
|
||||
('Fedora QA', 'fedora.qa'),
|
||||
]
|
||||
settings['ARTICLE_URL'] = '{category}/{slug}/'
|
||||
article_kwargs = self._copy_page_kwargs()
|
||||
article_kwargs['metadata']['category'] = Category('Fedora QA',
|
||||
|
|
@ -542,7 +554,9 @@ class TestArticle(TestPage):
|
|||
|
||||
def test_slugify_tags_with_dots(self):
|
||||
settings = get_settings()
|
||||
settings['TAG_SUBSTITUTIONS'] = [('Fedora QA', 'fedora.qa', True)]
|
||||
settings['TAG_REGEX_SUBSTITUTIONS'] = [
|
||||
('Fedora QA', 'fedora.qa'),
|
||||
]
|
||||
settings['ARTICLE_URL'] = '{tag}/{slug}/'
|
||||
article_kwargs = self._copy_page_kwargs()
|
||||
article_kwargs['metadata']['tag'] = Tag('Fedora QA', settings)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import os
|
|||
import re
|
||||
from codecs import open
|
||||
|
||||
from pelican.settings import DEFAULT_CONFIG
|
||||
from pelican.tests.support import (mute, skipIfNoExecutable, temporary_folder,
|
||||
unittest)
|
||||
from pelican.tools.pelican_import import (blogger2fields, build_header,
|
||||
|
|
@ -133,10 +134,11 @@ class TestWordpressXmlImporter(unittest.TestCase):
|
|||
with temporary_folder() as temp:
|
||||
fnames = list(silent_f2p(test_posts, 'markdown',
|
||||
temp, dircat=True))
|
||||
subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS']
|
||||
index = 0
|
||||
for post in test_posts:
|
||||
name = post[2]
|
||||
category = slugify(post[5][0])
|
||||
category = slugify(post[5][0], regex_subs=subs)
|
||||
name += '.md'
|
||||
filename = os.path.join(category, name)
|
||||
out_name = fnames[index]
|
||||
|
|
@ -208,11 +210,12 @@ class TestWordpressXmlImporter(unittest.TestCase):
|
|||
with temporary_folder() as temp:
|
||||
fnames = list(silent_f2p(test_posts, 'markdown', temp,
|
||||
wp_custpost=True, dircat=True))
|
||||
subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS']
|
||||
index = 0
|
||||
for post in test_posts:
|
||||
name = post[2]
|
||||
kind = post[8]
|
||||
category = slugify(post[5][0])
|
||||
category = slugify(post[5][0], regex_subs=subs)
|
||||
name += '.md'
|
||||
filename = os.path.join(kind, category, name)
|
||||
out_name = fnames[index]
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ from sys import platform
|
|||
|
||||
|
||||
from pelican.settings import (DEFAULT_CONFIG, DEFAULT_THEME,
|
||||
configure_settings, read_settings)
|
||||
configure_settings, handle_deprecated_settings,
|
||||
read_settings)
|
||||
from pelican.tests.support import unittest
|
||||
|
||||
|
||||
|
|
@ -128,7 +129,7 @@ class TestSettingsConfiguration(unittest.TestCase):
|
|||
settings['ARTICLE_DIR'] = 'foo'
|
||||
settings['PAGE_DIR'] = 'bar'
|
||||
|
||||
configure_settings(settings)
|
||||
settings = handle_deprecated_settings(settings)
|
||||
|
||||
self.assertEqual(settings['ARTICLE_PATHS'], ['foo'])
|
||||
self.assertEqual(settings['PAGE_PATHS'], ['bar'])
|
||||
|
|
@ -171,7 +172,7 @@ class TestSettingsConfiguration(unittest.TestCase):
|
|||
settings = self.settings
|
||||
settings['EXTRA_TEMPLATES_PATHS'] = ['/foo/bar', '/ha']
|
||||
|
||||
configure_settings(settings)
|
||||
settings = handle_deprecated_settings(settings)
|
||||
|
||||
self.assertEqual(settings['THEME_TEMPLATES_OVERRIDES'],
|
||||
['/foo/bar', '/ha'])
|
||||
|
|
@ -181,7 +182,7 @@ class TestSettingsConfiguration(unittest.TestCase):
|
|||
settings = self.settings
|
||||
settings['PAGINATED_DIRECT_TEMPLATES'] = ['index', 'archives']
|
||||
settings['PAGINATED_TEMPLATES'] = {'index': 10, 'category': None}
|
||||
settings = configure_settings(settings)
|
||||
settings = handle_deprecated_settings(settings)
|
||||
self.assertEqual(settings['PAGINATED_TEMPLATES'],
|
||||
{'index': 10, 'category': None, 'archives': None})
|
||||
self.assertNotIn('PAGINATED_DIRECT_TEMPLATES', settings)
|
||||
|
|
@ -191,4 +192,82 @@ class TestSettingsConfiguration(unittest.TestCase):
|
|||
settings['EXTRA_TEMPLATES_PATHS'] = ['/ha']
|
||||
settings['THEME_TEMPLATES_OVERRIDES'] = ['/foo/bar']
|
||||
|
||||
self.assertRaises(Exception, configure_settings, settings)
|
||||
self.assertRaises(Exception, handle_deprecated_settings, settings)
|
||||
|
||||
def test_slug_and_slug_regex_substitutions_exception(self):
|
||||
settings = {}
|
||||
settings['SLUG_REGEX_SUBSTITUTIONS'] = [('C++', 'cpp')]
|
||||
settings['TAG_SUBSTITUTIONS'] = [('C#', 'csharp')]
|
||||
|
||||
self.assertRaises(Exception, handle_deprecated_settings, settings)
|
||||
|
||||
def test_deprecated_slug_substitutions(self):
|
||||
default_slug_regex_subs = self.settings['SLUG_REGEX_SUBSTITUTIONS']
|
||||
|
||||
# If no deprecated setting is set, don't set new ones
|
||||
settings = {}
|
||||
settings = handle_deprecated_settings(settings)
|
||||
self.assertNotIn('SLUG_REGEX_SUBSTITUTIONS', settings)
|
||||
self.assertNotIn('TAG_REGEX_SUBSTITUTIONS', settings)
|
||||
self.assertNotIn('CATEGORY_REGEX_SUBSTITUTIONS', settings)
|
||||
self.assertNotIn('AUTHOR_REGEX_SUBSTITUTIONS', settings)
|
||||
|
||||
# If SLUG_SUBSTITUTIONS is set, set {SLUG, AUTHOR}_REGEX_SUBSTITUTIONS
|
||||
# correctly, don't set {CATEGORY, TAG}_REGEX_SUBSTITUTIONS
|
||||
settings = {}
|
||||
settings['SLUG_SUBSTITUTIONS'] = [('C++', 'cpp')]
|
||||
settings = handle_deprecated_settings(settings)
|
||||
self.assertEqual(settings.get('SLUG_REGEX_SUBSTITUTIONS'),
|
||||
[(r'C\+\+', 'cpp')] + default_slug_regex_subs)
|
||||
self.assertNotIn('TAG_REGEX_SUBSTITUTIONS', settings)
|
||||
self.assertNotIn('CATEGORY_REGEX_SUBSTITUTIONS', settings)
|
||||
self.assertEqual(settings.get('AUTHOR_REGEX_SUBSTITUTIONS'),
|
||||
default_slug_regex_subs)
|
||||
|
||||
# If {CATEGORY, TAG, AUTHOR}_SUBSTITUTIONS are set, set
|
||||
# {CATEGORY, TAG, AUTHOR}_REGEX_SUBSTITUTIONS correctly, don't set
|
||||
# SLUG_REGEX_SUBSTITUTIONS
|
||||
settings = {}
|
||||
settings['TAG_SUBSTITUTIONS'] = [('C#', 'csharp')]
|
||||
settings['CATEGORY_SUBSTITUTIONS'] = [('C#', 'csharp')]
|
||||
settings['AUTHOR_SUBSTITUTIONS'] = [('Alexander Todorov', 'atodorov')]
|
||||
settings = handle_deprecated_settings(settings)
|
||||
self.assertNotIn('SLUG_REGEX_SUBSTITUTIONS', settings)
|
||||
self.assertEqual(settings['TAG_REGEX_SUBSTITUTIONS'],
|
||||
[(r'C\#', 'csharp')] + default_slug_regex_subs)
|
||||
self.assertEqual(settings['CATEGORY_REGEX_SUBSTITUTIONS'],
|
||||
[(r'C\#', 'csharp')] + default_slug_regex_subs)
|
||||
self.assertEqual(settings['AUTHOR_REGEX_SUBSTITUTIONS'],
|
||||
[(r'Alexander\ Todorov', 'atodorov')] +
|
||||
default_slug_regex_subs)
|
||||
|
||||
# If {SLUG, CATEGORY, TAG, AUTHOR}_SUBSTITUTIONS are set, set
|
||||
# {SLUG, CATEGORY, TAG, AUTHOR}_REGEX_SUBSTITUTIONS correctly
|
||||
settings = {}
|
||||
settings['SLUG_SUBSTITUTIONS'] = [('C++', 'cpp')]
|
||||
settings['TAG_SUBSTITUTIONS'] = [('C#', 'csharp')]
|
||||
settings['CATEGORY_SUBSTITUTIONS'] = [('C#', 'csharp')]
|
||||
settings['AUTHOR_SUBSTITUTIONS'] = [('Alexander Todorov', 'atodorov')]
|
||||
settings = handle_deprecated_settings(settings)
|
||||
self.assertEqual(settings['TAG_REGEX_SUBSTITUTIONS'],
|
||||
[(r'C\+\+', 'cpp')] + [(r'C\#', 'csharp')] +
|
||||
default_slug_regex_subs)
|
||||
self.assertEqual(settings['CATEGORY_REGEX_SUBSTITUTIONS'],
|
||||
[(r'C\+\+', 'cpp')] + [(r'C\#', 'csharp')] +
|
||||
default_slug_regex_subs)
|
||||
self.assertEqual(settings['AUTHOR_REGEX_SUBSTITUTIONS'],
|
||||
[(r'Alexander\ Todorov', 'atodorov')] +
|
||||
default_slug_regex_subs)
|
||||
|
||||
# Handle old 'skip' flags correctly
|
||||
settings = {}
|
||||
settings['SLUG_SUBSTITUTIONS'] = [('C++', 'cpp', True)]
|
||||
settings['AUTHOR_SUBSTITUTIONS'] = [('Alexander Todorov', 'atodorov',
|
||||
False)]
|
||||
settings = handle_deprecated_settings(settings)
|
||||
self.assertEqual(settings.get('SLUG_REGEX_SUBSTITUTIONS'),
|
||||
[(r'C\+\+', 'cpp')] +
|
||||
[(r'(?u)\A\s*', ''), (r'(?u)\s*\Z', '')])
|
||||
self.assertEqual(settings['AUTHOR_REGEX_SUBSTITUTIONS'],
|
||||
[(r'Alexander\ Todorov', 'atodorov')] +
|
||||
default_slug_regex_subs)
|
||||
|
|
|
|||
|
|
@ -55,30 +55,29 @@ class TestURLWrapper(unittest.TestCase):
|
|||
self.assertEqual(author, author_equal)
|
||||
|
||||
cat_ascii = Category('指導書', settings={})
|
||||
self.assertEqual(cat_ascii, u'zhi-dao-shu')
|
||||
self.assertEqual(cat_ascii, u'zhi dao shu')
|
||||
|
||||
def test_slugify_with_substitutions_and_dots(self):
|
||||
tag = Tag('Tag Dot',
|
||||
settings={
|
||||
'TAG_SUBSTITUTIONS': [('Tag Dot', 'tag.dot', True)]
|
||||
})
|
||||
tag = Tag('Tag Dot', settings={'TAG_REGEX_SUBSTITUTIONS': [
|
||||
('Tag Dot', 'tag.dot'),
|
||||
]})
|
||||
cat = Category('Category Dot',
|
||||
settings={
|
||||
'CATEGORY_SUBSTITUTIONS': (('Category Dot',
|
||||
'cat.dot',
|
||||
True),)
|
||||
})
|
||||
settings={'CATEGORY_REGEX_SUBSTITUTIONS': [
|
||||
('Category Dot', 'cat.dot'),
|
||||
]})
|
||||
|
||||
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),
|
||||
]
|
||||
}
|
||||
settings = {'AUTHOR_REGEX_SUBSTITUTIONS': [
|
||||
('Alexander Todorov', 'atodorov'),
|
||||
('Krasimir Tsonev', 'krasimir'),
|
||||
(r'[^\w\s-]', ''),
|
||||
(r'(?u)\A\s*', ''),
|
||||
(r'(?u)\s*\Z', ''),
|
||||
(r'[-\s]+', '-'),
|
||||
]}
|
||||
|
||||
author1 = Author('Mr. Senko', settings=settings)
|
||||
author2 = Author('Alexander Todorov', settings=settings)
|
||||
|
|
|
|||
|
|
@ -119,8 +119,11 @@ class TestUtils(LoggedTestCase):
|
|||
('大飯原発4号機、18日夜起動へ',
|
||||
'da-fan-yuan-fa-4hao-ji-18ri-ye-qi-dong-he'),)
|
||||
|
||||
settings = read_settings()
|
||||
subs = settings['SLUG_REGEX_SUBSTITUTIONS']
|
||||
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.slugify(value), expected)
|
||||
self.assertEqual(utils.slugify(value, regex_subs=subs), expected)
|
||||
|
||||
def test_slugify_substitute(self):
|
||||
|
||||
|
|
@ -129,21 +132,27 @@ class TestUtils(LoggedTestCase):
|
|||
('c++, c#, C#, C++', 'cpp-c-sharp-c-sharp-cpp'),
|
||||
('c++-streams', 'cpp-streams'),)
|
||||
|
||||
subs = (('C++', 'CPP'), ('C#', 'C-SHARP'))
|
||||
settings = read_settings()
|
||||
subs = [
|
||||
(r'C\+\+', 'CPP'),
|
||||
(r'C#', 'C-SHARP'),
|
||||
] + settings['SLUG_REGEX_SUBSTITUTIONS']
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.slugify(value, subs), expected)
|
||||
self.assertEqual(utils.slugify(value, regex_subs=subs), expected)
|
||||
|
||||
def test_slugify_substitute_and_keeping_non_alphanum(self):
|
||||
|
||||
samples = (('Fedora QA', 'fedora.qa'),
|
||||
('C++ is used by Fedora QA', 'cpp is used by fedora.qa'),
|
||||
('C++ is based on C', 'cpp-is-based-on-c'),
|
||||
('C+++ test C+ test', 'cpp-test-c-test'),)
|
||||
('C++ is based on C', 'cpp is based on c'),
|
||||
('C+++ test C+ test', 'cpp+ test c+ test'),)
|
||||
|
||||
subs = (('Fedora QA', 'fedora.qa', True),
|
||||
('c++', 'cpp'),)
|
||||
subs = [
|
||||
(r'Fedora QA', 'fedora.qa'),
|
||||
(r'c\+\+', 'cpp'),
|
||||
]
|
||||
for value, expected in samples:
|
||||
self.assertEqual(utils.slugify(value, subs), expected)
|
||||
self.assertEqual(utils.slugify(value, regex_subs=subs), expected)
|
||||
|
||||
def test_get_relative_path(self):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue