From 3ef99c9d19267c542316906eb5157a781f7597ec Mon Sep 17 00:00:00 2001 From: Kernc Date: Sun, 15 Nov 2015 13:13:12 +0100 Subject: [PATCH] Make MD_EXTENSIONS setting a dict As proposed by in https://github.com/getpelican/pelican/pull/1638 --- docs/internals.rst | 6 ++++-- docs/settings.rst | 17 ++++++----------- pelican/__init__.py | 30 ------------------------------ pelican/readers.py | 8 +++----- pelican/settings.py | 18 ++++++++++++------ pelican/tests/test_readers.py | 5 ++++- 6 files changed, 29 insertions(+), 55 deletions(-) diff --git a/docs/internals.rst b/docs/internals.rst index 52eede89c..b267df76a 100644 --- a/docs/internals.rst +++ b/docs/internals.rst @@ -50,8 +50,10 @@ Take a look at the Markdown reader:: def read(self, source_path): """Parse content and metadata of markdown files""" text = pelican_open(source_path) - md = Markdown(extensions = ['markdown.extensions.meta', - 'markdown.extensions.codehilite']) + md_extensions = {'markdown.extensions.meta': {}, + 'markdown.extensions.codehilite': {}} + md = Markdown(extensions=md_extensions.keys(), + extension_configs=md_extensions) content = md.convert(text) metadata = {} diff --git a/docs/settings.rst b/docs/settings.rst index e27cde426..0f7280fa4 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -112,18 +112,13 @@ Setting name (followed by default value, if any) of these patterns will be ignored by the processor. For example, the default ``['.#*']`` will ignore emacs lock files, and ``['__pycache__']`` would ignore Python 3's bytecode caches. -``MD_EXTENSIONS =`` ``[...]`` A list of the extensions that the Markdown processor - will use. Refer to the Python Markdown documentation's +``MD_EXTENSIONS =`` ``{...}`` A dict of the extensions that the Markdown processor + will use, with extensions' settings as the values. + Refer to the Python Markdown documentation's `Extensions section `_ - for a complete list of supported extensions. Default is - ``['markdown.extensions.codehilite', 'markdown.extensions.extra']``. - (Note that defining this in your settings file will override and - replace the default values. If your goal is to *add* - to the default values for this setting, you'll need to - include them explicitly and enumerate the full list of - desired Markdown extensions.) -``MD_EXTENSION_CONFIGS =`` ``{...}`` Configuration for Markdown plugins. Default is - ``{'markdown.extensions.codehilite': {'css_class': 'highlight'}}`` + for a complete list of supported extensions and their options. + Default is ``{'markdown.extensions.codehilite' : {'css_class': 'highlight'}, + 'markdown.extensions.extra': {}, 'markdown.extensions.meta': {}}``. (Note that the dictionary defined in your settings file will update this default one.) ``OUTPUT_PATH = 'output/'`` Where to output the generated files. diff --git a/pelican/__init__.py b/pelican/__init__.py index eea723653..48cb59809 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -141,36 +141,6 @@ def _handle_deprecation(self): ) self.settings[old] = self.settings[new] - # Markdown 2.6 deprecations - for i, extension in enumerate(self.settings['MD_EXTENSIONS']): - try: name, params = extension.split('(') - except (ValueError, AttributeError): - if (isinstance(extension, six.string_types) and - not extension.startswith('markdown.extensions.')): - logger.warning('Found deprecated short declaration of ' - 'Markdown extension "%(name)s". Specify it ' - 'by its fully-qualified name ' - '"markdown.extensions.%(name)s".', - {'name': extension}) - extension = 'markdown.extensions.' + extension - self.settings['MD_EXTENSIONS'][i] = extension - # The extension is a fully-qualified string without config - # parameters, or an object instance, both of which are fine. - continue - logger.warning('Found deprecated configuration of Markdown ' - 'extensions in MD_EXTENSIONS. Use ' - 'MD_EXTENSION_CONFIGS instead.') - name = name.strip() - self.settings['MD_EXTENSIONS'][i] = name - - config = {} - for key, value in re.findall(r'([\w_]+?)\s*=\s*([^,\)]*)', params): - if value in ('True', 'False', 'None') or value.isdigit(): - value = eval(value) - config[key] = value - self.settings['MD_EXTENSION_CONFIGS'][name] = config - - def run(self): """Run the generators and return""" start_time = time.time() diff --git a/pelican/readers.py b/pelican/readers.py index e633f204d..e3b1012a7 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -246,10 +246,8 @@ class MarkdownReader(BaseReader): def __init__(self, *args, **kwargs): super(MarkdownReader, self).__init__(*args, **kwargs) - self.extension_configs = self.settings['MD_EXTENSION_CONFIGS'] self.extensions = self.settings['MD_EXTENSIONS'] - if 'meta' not in self.extensions: - self.extensions.append('meta') + self.extensions.setdefault('markdown.extensions.meta', {}) self._source_path = None def _parse_metadata(self, meta): @@ -285,8 +283,8 @@ def read(self, source_path): """Parse content and metadata of markdown files""" self._source_path = source_path - self._md = Markdown(extensions=self.extensions, - extension_configs=self.extension_configs) + self._md = Markdown(extensions=self.extensions.keys(), + extension_configs=self.extensions) with pelican_open(source_path) as text: content = self._md.convert(text) diff --git a/pelican/settings.py b/pelican/settings.py index f7db70ff9..83054ddc3 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -101,12 +101,10 @@ def load_source(name, path): 'PELICAN_CLASS': 'pelican.Pelican', 'DEFAULT_DATE_FORMAT': '%a %d %B %Y', 'DATE_FORMATS': {}, - 'MD_EXTENSIONS': [ - 'markdown.extensions.codehilite', - 'markdown.extensions.extra', - ], - 'MD_EXTENSION_CONFIGS': { - 'markdown.extensions.codehilite' : {'css_class': 'highlight'}, + 'MD_EXTENSIONS': { + 'markdown.extensions.codehilite': {'css_class': 'highlight'}, + 'markdown.extensions.extra': {}, + 'markdown.extensions.meta': {}, }, 'JINJA_EXTENSIONS': [], 'JINJA_FILTERS': {}, @@ -368,6 +366,14 @@ def configure_settings(settings): PATH_KEY) settings[PATH_KEY] = DEFAULT_CONFIG[PATH_KEY] + # Save people from declaring MD_EXTENSIONS as a list rather than a dict + if not isinstance(settings['MD_EXTENSIONS'], dict): + logger.warning('The format of the MD_EXTENSIONS setting has ' + 'changed. It should now be a dict mapping ' + 'fully-qualified extension names to their ' + 'configurations. Falling back to the default') + settings['MD_EXTENSIONS'] = DEFAULT_CONFIG['MD_EXTENSIONS'] + # Add {PAGE,ARTICLE}_PATHS to {ARTICLE,PAGE}_EXCLUDES mutually_exclusive = ('ARTICLE', 'PAGE') for type_1, type_2 in [mutually_exclusive, mutually_exclusive[::-1]]: diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py index db4aa44ff..d3a9a6ffa 100644 --- a/pelican/tests/test_readers.py +++ b/pelican/tests/test_readers.py @@ -471,7 +471,10 @@ def test_article_with_markdown_markup_extension(self): # expected page = self.read_file( path='article_with_markdown_markup_extensions.md', - MD_EXTENSIONS=['toc', 'codehilite', 'extra']) + MD_EXTENSIONS={ + 'markdown.extensions.toc': {}, + 'markdown.extensions.codehilite': {}, + 'markdown.extensions.extra': {}}) expected = ('
\n' '