aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/__init__.py49
-rw-r--r--markdown/extensions/__init__.py51
-rw-r--r--tests/test_apis.py4
3 files changed, 47 insertions, 57 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py
index 5375b7d..57708fe 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -43,7 +43,7 @@ from blockprocessors import build_block_parser
from treeprocessors import build_treeprocessors
from inlinepatterns import build_inlinepatterns
from postprocessors import build_postprocessors
-from extensions import Extension, load_extension, load_extensions
+from extensions import Extension
import html4
# For backwards compatibility in the 2.0.x series
@@ -136,7 +136,7 @@ class Markdown:
"""
for ext in extensions:
if isinstance(ext, basestring):
- ext = load_extension(ext, configs.get(ext, []))
+ ext = self.build_extension(ext, configs.get(ext, []))
if isinstance(ext, Extension):
try:
ext.extendMarkdown(self, globals())
@@ -147,6 +147,47 @@ class Markdown:
'Extension "%s.%s" must be of type: "markdown.Extension".' \
% (ext.__class__.__module__, ext.__class__.__name__))
+ def build_extension(self, ext_name, configs = []):
+ """Build extension by name, then return the module.
+
+ The extension name may contain arguments as part of the string in the
+ following format: "extname(key1=value1,key2=value2)"
+
+ """
+
+ # Parse extensions config params (ignore the order)
+ configs = dict(configs)
+ pos = ext_name.find("(") # find the first "("
+ if pos > 0:
+ ext_args = ext_name[pos+1:-1]
+ ext_name = ext_name[:pos]
+ pairs = [x.split("=") for x in ext_args.split(",")]
+ configs.update([(x.strip(), y.strip()) for (x, y) in pairs])
+
+ # Setup the module names
+ ext_module = 'markdown.extensions'
+ module_name_new_style = '.'.join([ext_module, ext_name])
+ module_name_old_style = '_'.join(['mdx', ext_name])
+
+ # Try loading the extention first from one place, then another
+ try: # New style (markdown.extensons.<extension>)
+ module = __import__(module_name_new_style, {}, {}, [ext_module])
+ except ImportError:
+ try: # Old style (mdx_<extension>)
+ module = __import__(module_name_old_style)
+ except ImportError:
+ message(WARN, "Failed loading extension '%s' from '%s' or '%s'"
+ % (ext_name, module_name_new_style, module_name_old_style))
+ # Return None so we don't try to initiate none-existant extension
+ return None
+
+ # If the module is loaded successfully, we expect it to define a
+ # function called makeExtension()
+ try:
+ return module.makeExtension(configs.items())
+ except AttributeError, e:
+ message(CRITICAL, "Failed to initiate extension '%s': %s" % (ext_name, e))
+
def registerExtension(self, extension):
""" This gets called by the extension """
self.registeredExtensions.append(extension)
@@ -321,7 +362,7 @@ def markdown(text,
Returns: An HTML document as a string.
"""
- md = Markdown(extensions=load_extensions(extensions),
+ md = Markdown(extensions=extensions,
safe_mode=safe_mode,
output_format=output_format)
return md.convert(text)
@@ -334,7 +375,7 @@ def markdownFromFile(input = None,
safe_mode = False,
output_format = 'xhtml1'):
"""Read markdown code from a file and write it to a file or a stream."""
- md = Markdown(extensions=load_extensions(extensions),
+ md = Markdown(extensions=extensions,
safe_mode=safe_mode,
output_format=output_format)
md.convertFile(input, output, encoding)
diff --git a/markdown/extensions/__init__.py b/markdown/extensions/__init__.py
index 14e0911..1cc4762 100644
--- a/markdown/extensions/__init__.py
+++ b/markdown/extensions/__init__.py
@@ -48,54 +48,3 @@ class Extension:
raise NotImplementedError, 'Extension "%s.%s" must define an "extendMarkdown"' \
'method.' % (self.__class__.__module__, self.__class__.__name__)
-
-def load_extension(ext_name, configs = []):
- """Load extension by name, then return the module.
-
- The extension name may contain arguments as part of the string in the
- following format: "extname(key1=value1,key2=value2)"
-
- """
-
- # Parse extensions config params (ignore the order)
- configs = dict(configs)
- pos = ext_name.find("(") # find the first "("
- if pos > 0:
- ext_args = ext_name[pos+1:-1]
- ext_name = ext_name[:pos]
- pairs = [x.split("=") for x in ext_args.split(",")]
- configs.update([(x.strip(), y.strip()) for (x, y) in pairs])
-
- # Setup the module names
- ext_module = 'markdown.extensions'
- module_name_new_style = '.'.join([ext_module, ext_name])
- module_name_old_style = '_'.join(['mdx', ext_name])
-
- # Try loading the extention first from one place, then another
- try: # New style (markdown.extensons.<extension>)
- module = __import__(module_name_new_style, {}, {}, [ext_module])
- except ImportError:
- try: # Old style (mdx.<extension>)
- module = __import__(module_name_old_style)
- except ImportError:
- message(WARN, "Failed loading extension '%s' from '%s' or '%s'"
- % (ext_name, module_name_new_style, module_name_old_style))
- # Return None so we don't try to initiate none-existant extension
- return None
-
- # If the module is loaded successfully, we expect it to define a
- # function called makeExtension()
- try:
- return module.makeExtension(configs.items())
- except AttributeError, e:
- message(CRITICAL, "Failed to initiate extension '%s': %s" % (ext_name, e))
-
-
-def load_extensions(ext_names):
- """Loads multiple extensions"""
- extensions = []
- for ext_name in ext_names:
- extension = load_extension(ext_name)
- if extension:
- extensions.append(extension)
- return extensions
diff --git a/tests/test_apis.py b/tests/test_apis.py
index e420c2a..51f4e73 100644
--- a/tests/test_apis.py
+++ b/tests/test_apis.py
@@ -249,13 +249,13 @@ class TestErrors(unittest.TestCase):
def testLoadExtensionFailure(self):
""" Test failure of an extension to load. """
self.assertRaises(MarkdownWarning,
- markdown.extensions.load_extension, 'non_existant_ext')
+ markdown.Markdown, extensions=['non_existant_ext'])
def testLoadBadExtension(self):
""" Test loading of an Extension with no makeExtension function. """
_create_fake_extension(name='fake', has_factory_func=False)
self.assertRaises(MarkdownException,
- markdown.extensions.load_extension, 'fake')
+ markdown.Markdown, extensions=['fake'])
def testNonExtension(self):
""" Test loading a non Extension object as an extension. """