Skip to content

Commit

Permalink
Deprecate md_globals from extension API. (Python-Markdown#697)
Browse files Browse the repository at this point in the history
In the past, most of the config was defined using globals. Today all of 
the config is held on the class instance. Therefore, the `md_globals` 
parameter is no longer necessary.
  • Loading branch information
waylan authored Jul 31, 2018
1 parent f261ffd commit 7dad12c
Show file tree
Hide file tree
Showing 20 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion markdown/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def registerExtensions(self, extensions, configs):
if isinstance(ext, util.string_type):
ext = self.build_extension(ext, configs.get(ext, {}))
if isinstance(ext, Extension):
ext.extendMarkdown(self, globals())
ext._extendMarkdown(self)
logger.debug(
'Successfully loaded extension "%s.%s".'
% (ext.__class__.__module__, ext.__class__.__name__)
Expand Down
18 changes: 17 additions & 1 deletion markdown/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

from __future__ import unicode_literals
import warnings
from ..util import parseBoolValue


Expand Down Expand Up @@ -71,7 +72,22 @@ def setConfigs(self, items):
for key, value in items:
self.setConfig(key, value)

def extendMarkdown(self, md, md_globals):
def _extendMarkdown(self, *args):
""" Private wrapper around extendMarkdown. """
md = args[0]
try:
self.extendMarkdown(md)
except TypeError:
# Must be a 2.x extension. Pass in a dumby md_globals.
self.extendMarkdown(md, {})
warnings.warn(
"The 'md_globals' parameter of '{0}.{1}.extendMarkdown' is "
"deprecated.".format(self.__class__.__module__, self.__class__.__name__),
category=DeprecationWarning,
stacklevel=2
)

def extendMarkdown(self, md):
"""
Add the various proccesors and patterns to the Markdown Instance.
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/abbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class AbbrExtension(Extension):
""" Abbreviation Extension for Python-Markdown. """

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Insert AbbrPreprocessor before ReferencePreprocessor. """
md.preprocessors.register(AbbrPreprocessor(md), 'abbr', 12)

Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/admonition.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class AdmonitionExtension(Extension):
""" Admonition extension for Python-Markdown. """

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add Admonition to Markdown instance. """
md.registerExtension(self)

Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/attr_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def sanitize_name(self, name):


class AttrListExtension(Extension):
def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
md.treeprocessors.register(AttrListTreeprocessor(md), 'attr_list', 8)


Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def __init__(self, **kwargs):

super(CodeHiliteExtension, self).__init__(**kwargs)

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add HilitePostprocessor to Markdown instance. """
hiliter = HiliteTreeprocessor(md)
hiliter.config = self.getConfigs()
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/def_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def create_item(self, parent, block):
class DefListExtension(Extension):
""" Add definition lists to Markdown. """

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add an instance of DefListProcessor to BlockParser. """
md.parser.blockprocessors.register(DefListIndentProcessor(md.parser), 'defindent', 85)
md.parser.blockprocessors.register(DefListProcessor(md.parser), 'deflist', 25)
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, **kwargs):
""" config is a dumb holder which gets passed to actual ext later. """
self.config = kwargs

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Register extension instances. """
md.registerExtensions(extensions, self.config)
# Turn on processing of markdown text within raw html
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/fenced_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class FencedCodeExtension(Extension):

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add FencedBlockPreprocessor to the Markdown instance. """
md.registerExtension(self)

Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, **kwargs):

self.reset()

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add pieces to Markdown. """
md.registerExtension(self)
self.parser = md.parser
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/legacy_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def attributeCallback(match):


class LegacyAttrExtension(Extension):
def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
md.treeprocessors.register(LegacyAttrs(md), 'legacyattrs', 15)


Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/legacy_em.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class LegacyEmExtension(Extension):
""" Add legacy_em extension to Markdown class."""

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Modify inline patterns. """
md.inlinePatterns.register(SimpleTagInlineProcessor(STRONG_RE, 'strong'), 'strong', 40)
md.inlinePatterns.register(SimpleTagInlineProcessor(EMPHASIS_RE, 'em'), 'emphasis', 30)
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class MetaExtension (Extension):
""" Meta-Data extension for Python-Markdown. """

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add MetaPreprocessor to Markdown instance. """
md.registerExtension(self)
self.md = md
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/nl2br.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class Nl2BrExtension(Extension):

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
br_tag = SubstituteTagInlineProcessor(BR_RE, 'br')
md.inlinePatterns.register(br_tag, 'nl', 5)

Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/sane_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, parser):
class SaneListExtension(Extension):
""" Add sane lists to Markdown. """

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Override existing Processors. """
md.parser.blockprocessors.register(SaneOListProcessor(md.parser), 'olist', 40)
md.parser.blockprocessors.register(SaneUListProcessor(md.parser), 'ulist', 30)
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/smarty.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def educateQuotes(self, md):
)
self._addPatterns(md, patterns, 'quotes', 30)

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
configs = self.getConfigs()
self.inlinePatterns = Registry()
if configs['smart_ellipses']:
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def _split(self, row):
class TableExtension(Extension):
""" Add tables to Markdown. """

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
""" Add an instance of TableProcessor to BlockParser. """
if '|' not in md.ESCAPED_CHARS:
md.ESCAPED_CHARS.append('|')
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def __init__(self, **kwargs):

super(TocExtension, self).__init__(**kwargs)

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
md.registerExtension(self)
self.md = md
self.reset()
Expand Down
2 changes: 1 addition & 1 deletion markdown/extensions/wikilinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, **kwargs):

super(WikiLinkExtension, self).__init__(**kwargs)

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
self.md = md

# append to end of inline patterns
Expand Down
4 changes: 2 additions & 2 deletions tests/test_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def fakeSerializer(elem):
return '<div><p>foo</p></div>'

class registerFakeSerializer(markdown.extensions.Extension):
def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
md.output_formats['fake'] = fakeSerializer

return registerFakeSerializer()
Expand Down Expand Up @@ -962,7 +962,7 @@ def __init__(self, *args, **kwargs):

self.config = {}

def extendMarkdown(self, md, md_globals):
def extendMarkdown(self, md):
"""Modify inline patterns."""

pattern = r'(\+)([^\+]+)\1'
Expand Down

0 comments on commit 7dad12c

Please sign in to comment.