aboutsummaryrefslogtreecommitdiffstats
path: root/markdown/__init__.py
diff options
context:
space:
mode:
authorWaylan Limberg <waylan.limberg@icloud.com>2014-08-27 14:59:40 -0400
committerWaylan Limberg <waylan.limberg@icloud.com>2014-08-27 14:59:40 -0400
commite98ac74737c1f28ec730fa5c645062e0e0d5702b (patch)
treeec4400643c52c1441c61d9fe994b574b48170006 /markdown/__init__.py
parent44e718ed82ed4c8e8e0f0fe1dbdb73d441747b19 (diff)
downloadmarkdown-e98ac74737c1f28ec730fa5c645062e0e0d5702b.tar.gz
markdown-e98ac74737c1f28ec730fa5c645062e0e0d5702b.tar.bz2
markdown-e98ac74737c1f28ec730fa5c645062e0e0d5702b.zip
Allow named extensions to specify the Class Name
If you were to import the class like this: from path.to.module import SomeExtensionClass Then the named extension would be the string: "path.to.module:SomeExtensionClass" This should simplify loading extensions from the command line or template filters -- expecially when multiple extensions are defined in a single python module. The docs still need updating. I'm waiting to update the docs after implementing #335 and #336 as that will require a major refactor of that section of the docs anyway.
Diffstat (limited to 'markdown/__init__.py')
-rw-r--r--markdown/__init__.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py
index 6190dd2..28f30c8 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -190,6 +190,9 @@ class Markdown(object):
pairs = [x.split("=") for x in ext_args.split(",")]
configs.update([(x.strip(), y.strip()) for (x, y) in pairs])
+ # Get class name (if provided): `path.to.module:ClassName`
+ ext_name, class_name = ext_name.split(':', 1) if ':' in ext_name else (ext_name, '')
+
# Try loading the extension first from one place, then another
try:
# Assume string uses dot syntax (`path.to.some.module`)
@@ -213,16 +216,19 @@ class Markdown(object):
e.args = (message,) + e.args[1:]
raise
- # If the module is loaded successfully, we expect it to define a
- # function called makeExtension()
- try:
- return module.makeExtension(configs.items())
- except AttributeError as e:
- message = e.args[0]
- message = "Failed to initiate extension " \
- "'%s': %s" % (ext_name, message)
- e.args = (message,) + e.args[1:]
- raise
+ if class_name:
+ # Load given class name from module.
+ return getattr(module, class_name)(configs.items())
+ else:
+ # Expect makeExtension() function to return a class.
+ try:
+ return module.makeExtension(configs.items())
+ except AttributeError as e:
+ message = e.args[0]
+ message = "Failed to initiate extension " \
+ "'%s': %s" % (ext_name, message)
+ e.args = (message,) + e.args[1:]
+ raise
def registerExtension(self, extension):
""" This gets called by the extension """