/bin/markdown/extensions/__init__.py

https://github.com/straup/recipes · Python · 93 lines · 45 code · 13 blank · 35 comment · 10 complexity · daafb13bb41eb5d18441bea58bc67422 MD5 · raw file

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