| OLD | NEW |
| (Empty) |
| 1 """ | |
| 2 Extensions | |
| 3 ----------------------------------------------------------------------------- | |
| 4 """ | |
| 5 | |
| 6 from __future__ import unicode_literals | |
| 7 from ..util import parseBoolValue | |
| 8 import warnings | |
| 9 | |
| 10 | |
| 11 class Extension(object): | |
| 12 """ Base class for extensions to subclass. """ | |
| 13 | |
| 14 # Default config -- to be overriden by a subclass | |
| 15 # Must be of the following format: | |
| 16 # { | |
| 17 # 'key': ['value', 'description'] | |
| 18 # } | |
| 19 # Note that Extension.setConfig will raise a KeyError | |
| 20 # if a default is not set here. | |
| 21 config = {} | |
| 22 | |
| 23 def __init__(self, *args, **kwargs): | |
| 24 """ Initiate Extension and set up configs. """ | |
| 25 | |
| 26 # check for configs arg for backward compat. | |
| 27 # (there only ever used to be one so we use arg[0]) | |
| 28 if len(args): | |
| 29 if args[0] is not None: | |
| 30 self.setConfigs(args[0]) | |
| 31 warnings.warn('Extension classes accepting positional args is ' | |
| 32 'pending Deprecation. Each setting should be ' | |
| 33 'passed into the Class as a keyword. Positional ' | |
| 34 'args are deprecated and will raise ' | |
| 35 'an error in version 2.7. See the Release Notes for ' | |
| 36 'Python-Markdown version 2.6 for more info.', | |
| 37 DeprecationWarning) | |
| 38 # check for configs kwarg for backward compat. | |
| 39 if 'configs' in kwargs.keys(): | |
| 40 if kwargs['configs'] is not None: | |
| 41 self.setConfigs(kwargs.pop('configs', {})) | |
| 42 warnings.warn('Extension classes accepting a dict on the single ' | |
| 43 'keyword "config" is pending Deprecation. Each ' | |
| 44 'setting should be passed into the Class as a ' | |
| 45 'keyword directly. The "config" keyword is ' | |
| 46 'deprecated and raise an error in ' | |
| 47 'version 2.7. See the Release Notes for ' | |
| 48 'Python-Markdown version 2.6 for more info.', | |
| 49 DeprecationWarning) | |
| 50 # finally, use kwargs | |
| 51 self.setConfigs(kwargs) | |
| 52 | |
| 53 def getConfig(self, key, default=''): | |
| 54 """ Return a setting for the given key or an empty string. """ | |
| 55 if key in self.config: | |
| 56 return self.config[key][0] | |
| 57 else: | |
| 58 return default | |
| 59 | |
| 60 def getConfigs(self): | |
| 61 """ Return all configs settings as a dict. """ | |
| 62 return dict([(key, self.getConfig(key)) for key in self.config.keys()]) | |
| 63 | |
| 64 def getConfigInfo(self): | |
| 65 """ Return all config descriptions as a list of tuples. """ | |
| 66 return [(key, self.config[key][1]) for key in self.config.keys()] | |
| 67 | |
| 68 def setConfig(self, key, value): | |
| 69 """ Set a config setting for `key` with the given `value`. """ | |
| 70 if isinstance(self.config[key][0], bool): | |
| 71 value = parseBoolValue(value) | |
| 72 if self.config[key][0] is None: | |
| 73 value = parseBoolValue(value, preserve_none=True) | |
| 74 self.config[key][0] = value | |
| 75 | |
| 76 def setConfigs(self, items): | |
| 77 """ Set multiple config settings given a dict or list of tuples. """ | |
| 78 if hasattr(items, 'items'): | |
| 79 # it's a dict | |
| 80 items = items.items() | |
| 81 for key, value in items: | |
| 82 self.setConfig(key, value) | |
| 83 | |
| 84 def extendMarkdown(self, md, md_globals): | |
| 85 """ | |
| 86 Add the various proccesors and patterns to the Markdown Instance. | |
| 87 | |
| 88 This method must be overriden by every extension. | |
| 89 | |
| 90 Keyword arguments: | |
| 91 | |
| 92 * md: The Markdown instance. | |
| 93 | |
| 94 * md_globals: Global variables in the markdown module namespace. | |
| 95 | |
| 96 """ | |
| 97 raise NotImplementedError( | |
| 98 'Extension "%s.%s" must define an "extendMarkdown"' | |
| 99 'method.' % (self.__class__.__module__, self.__class__.__name__) | |
| 100 ) | |
| OLD | NEW |