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)"
(ext_name, configs = [])
| 506 | |
| 507 | |
| 508 | def load_extension(ext_name, configs = []): |
| 509 | """Load extension by name, then return the module. |
| 510 | |
| 511 | The extension name may contain arguments as part of the string in the |
| 512 | following format: "extname(key1=value1,key2=value2)" |
| 513 | |
| 514 | """ |
| 515 | |
| 516 | # Parse extensions config params (ignore the order) |
| 517 | configs = dict(configs) |
| 518 | pos = ext_name.find("(") # find the first "(" |
| 519 | if pos > 0: |
| 520 | ext_args = ext_name[pos+1:-1] |
| 521 | ext_name = ext_name[:pos] |
| 522 | pairs = [x.split("=") for x in ext_args.split(",")] |
| 523 | configs.update([(x.strip(), y.strip()) for (x, y) in pairs]) |
| 524 | |
| 525 | # Setup the module names |
| 526 | ext_module = 'markdown.extensions' |
| 527 | module_name_new_style = '.'.join([ext_module, ext_name]) |
| 528 | module_name_old_style = '_'.join(['mdx', ext_name]) |
| 529 | |
| 530 | # Try loading the extention first from one place, then another |
| 531 | try: # New style (markdown.extensons.<extension>) |
| 532 | module = __import__(module_name_new_style, {}, {}, [ext_module]) |
| 533 | except ImportError: |
| 534 | try: # Old style (mdx.<extension>) |
| 535 | module = __import__(module_name_old_style) |
| 536 | except ImportError: |
| 537 | message(WARN, "Failed loading extension '%s' from '%s' or '%s'" |
| 538 | % (ext_name, module_name_new_style, module_name_old_style)) |
| 539 | # Return None so we don't try to initiate none-existant extension |
| 540 | return None |
| 541 | |
| 542 | # If the module is loaded successfully, we expect it to define a |
| 543 | # function called makeExtension() |
| 544 | try: |
| 545 | return module.makeExtension(list(configs.items())) |
| 546 | except AttributeError: |
| 547 | message(CRITICAL, "Failed to initiate extension '%s'" % ext_name) |
| 548 | |
| 549 | |
| 550 | def load_extensions(ext_names): |