mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Allow setting Jinja environment arguments from settings
fix flake8 warnings Set jinja environment defaults within settings updating docs to remove JINJA_EXTENSIONS update logger warning and defaults documentation better way to grab jinja environment updating settings after refactor
This commit is contained in:
parent
7039668669
commit
335c40d23e
4 changed files with 53 additions and 7 deletions
|
|
@ -81,9 +81,11 @@ Basic settings
|
|||
|
||||
OUTPUT_RETENTION = [".hg", ".git", ".bzr"]
|
||||
|
||||
.. data:: JINJA_EXTENSIONS = []
|
||||
.. data:: JINJA_ENVIRONMENT = {'trim_blocks': True, 'lstrip_blocks': True}
|
||||
|
||||
A list of any Jinja2 extensions you want to use.
|
||||
A dictionary of custom Jinja2 environment variables you want to use. This
|
||||
also includes a list of extensions you may want to include.
|
||||
See `Jinja Environment documentation`_.
|
||||
|
||||
.. data:: JINJA_FILTERS = {}
|
||||
|
||||
|
|
@ -1215,4 +1217,5 @@ Example settings
|
|||
|
||||
|
||||
.. _Jinja custom filters documentation: http://jinja.pocoo.org/docs/api/#custom-filters
|
||||
.. _Jinja Environment documentation: http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment
|
||||
.. _Docutils Configuration: http://docutils.sourceforge.net/docs/user/config.html
|
||||
|
|
|
|||
|
|
@ -61,14 +61,12 @@ class Generator(object):
|
|||
simple_loader = FileSystemLoader(os.path.join(theme_path,
|
||||
"themes", "simple", "templates"))
|
||||
self.env = Environment(
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True,
|
||||
loader=ChoiceLoader([
|
||||
FileSystemLoader(self._templates_path),
|
||||
simple_loader, # implicit inheritance
|
||||
PrefixLoader({'!simple': simple_loader}) # explicit one
|
||||
]),
|
||||
extensions=self.settings['JINJA_EXTENSIONS'],
|
||||
**self.settings['JINJA_ENVIRONMENT']
|
||||
)
|
||||
|
||||
logger.debug('Template list: %s', self.env.list_templates())
|
||||
|
|
|
|||
|
|
@ -109,8 +109,12 @@ DEFAULT_CONFIG = {
|
|||
},
|
||||
'output_format': 'html5',
|
||||
},
|
||||
'JINJA_EXTENSIONS': [],
|
||||
'JINJA_FILTERS': {},
|
||||
'JINJA_ENVIRONMENT': {
|
||||
'trim_blocks': True,
|
||||
'lstrip_blocks': True,
|
||||
'extensions': [],
|
||||
},
|
||||
'LOG_FILTER': [],
|
||||
'LOCALE': [''], # defaults to user locale
|
||||
'DEFAULT_PAGINATION': False,
|
||||
|
|
@ -162,6 +166,12 @@ def read_settings(path=None, override=None):
|
|||
'PLUGIN_PATHS, moving it to the new setting name.')
|
||||
local_settings['PLUGIN_PATHS'] = local_settings['PLUGIN_PATH']
|
||||
del local_settings['PLUGIN_PATH']
|
||||
if 'JINJA_EXTENSIONS' in local_settings:
|
||||
logger.warning('JINJA_EXTENSIONS setting has been deprecated, '
|
||||
'moving it to JINJA_ENVIRONMENT setting.')
|
||||
local_settings['JINJA_ENVIRONMENT']['extensions'] = \
|
||||
local_settings['JINJA_EXTENSIONS']
|
||||
del local_settings['JINJA_EXTENSIONS']
|
||||
if isinstance(local_settings['PLUGIN_PATHS'], six.string_types):
|
||||
logger.warning("Defining PLUGIN_PATHS setting as string "
|
||||
"has been deprecated (should be a list)")
|
||||
|
|
@ -215,6 +225,20 @@ def get_settings_from_file(path, default_settings=DEFAULT_CONFIG):
|
|||
return get_settings_from_module(module, default_settings=default_settings)
|
||||
|
||||
|
||||
def get_jinja_environment(settings):
|
||||
"""Sets the environment for Jinja"""
|
||||
|
||||
jinja_env = settings.setdefault('JINJA_ENVIRONMENT',
|
||||
DEFAULT_CONFIG['JINJA_ENVIRONMENT'])
|
||||
|
||||
# Make sure we include the defaults if the user has set env variables
|
||||
for key, value in DEFAULT_CONFIG['JINJA_ENVIRONMENT'].items():
|
||||
if key not in jinja_env:
|
||||
jinja_env[key] = value
|
||||
|
||||
return settings
|
||||
|
||||
|
||||
def configure_settings(settings):
|
||||
"""Provide optimizations, error checking, and warnings for the given
|
||||
settings.
|
||||
|
|
@ -251,6 +275,9 @@ def configure_settings(settings):
|
|||
if key in settings:
|
||||
settings[key] = settings[key].lower()
|
||||
|
||||
# set defaults for Jinja environment
|
||||
settings = get_jinja_environment(settings)
|
||||
|
||||
# standardize strings to lists
|
||||
for key in ['LOCALE']:
|
||||
if key in settings and isinstance(settings[key], six.string_types):
|
||||
|
|
@ -355,7 +382,6 @@ def configure_settings(settings):
|
|||
'EXTRA_TEMPLATES_PATHS',
|
||||
'FILES_TO_COPY',
|
||||
'IGNORE_FILES',
|
||||
'JINJA_EXTENSIONS',
|
||||
'PAGINATED_DIRECT_TEMPLATES',
|
||||
'PLUGINS',
|
||||
'STATIC_EXCLUDES',
|
||||
|
|
|
|||
|
|
@ -98,6 +98,25 @@ class TestGenerator(unittest.TestCase):
|
|||
'subdir.md', found_files,
|
||||
"get_files() excluded a subdirectory by name, ignoring its path")
|
||||
|
||||
def test_custom_jinja_environment(self):
|
||||
"""
|
||||
Test that setting the JINJA_ENVIRONMENT
|
||||
properly gets set from the settings config
|
||||
"""
|
||||
settings = get_settings()
|
||||
comment_start_string = 'abc'
|
||||
comment_end_string = '/abc'
|
||||
settings['JINJA_ENVIRONMENT'] = {
|
||||
'comment_start_string': comment_start_string,
|
||||
'comment_end_string': comment_end_string
|
||||
}
|
||||
generator = Generator(settings.copy(), settings,
|
||||
CUR_DIR, settings['THEME'], None)
|
||||
self.assertEqual(comment_start_string,
|
||||
generator.env.comment_start_string)
|
||||
self.assertEqual(comment_end_string,
|
||||
generator.env.comment_end_string)
|
||||
|
||||
|
||||
class TestArticlesGenerator(unittest.TestCase):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue