__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """
  2. Python Markdown
  3. A Python implementation of John Gruber's Markdown.
  4. Documentation: https://python-markdown.github.io/
  5. GitHub: https://github.com/Python-Markdown/markdown/
  6. PyPI: https://pypi.org/project/Markdown/
  7. Started by Manfred Stienstra (http://www.dwerg.net/).
  8. Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
  9. Currently maintained by Waylan Limberg (https://github.com/waylan),
  10. Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
  11. Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
  12. Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
  13. Copyright 2004 Manfred Stienstra (the original version)
  14. License: BSD (see LICENSE.md for details).
  15. """
  16. import warnings
  17. from ..util import parseBoolValue
  18. class Extension:
  19. """ Base class for extensions to subclass. """
  20. # Default config -- to be overriden by a subclass
  21. # Must be of the following format:
  22. # {
  23. # 'key': ['value', 'description']
  24. # }
  25. # Note that Extension.setConfig will raise a KeyError
  26. # if a default is not set here.
  27. config = {}
  28. def __init__(self, **kwargs):
  29. """ Initiate Extension and set up configs. """
  30. self.setConfigs(kwargs)
  31. def getConfig(self, key, default=''):
  32. """ Return a setting for the given key or an empty string. """
  33. if key in self.config:
  34. return self.config[key][0]
  35. else:
  36. return default
  37. def getConfigs(self):
  38. """ Return all configs settings as a dict. """
  39. return {key: self.getConfig(key) for key in self.config.keys()}
  40. def getConfigInfo(self):
  41. """ Return all config descriptions as a list of tuples. """
  42. return [(key, self.config[key][1]) for key in self.config.keys()]
  43. def setConfig(self, key, value):
  44. """ Set a config setting for `key` with the given `value`. """
  45. if isinstance(self.config[key][0], bool):
  46. value = parseBoolValue(value)
  47. if self.config[key][0] is None:
  48. value = parseBoolValue(value, preserve_none=True)
  49. self.config[key][0] = value
  50. def setConfigs(self, items):
  51. """ Set multiple config settings given a dict or list of tuples. """
  52. if hasattr(items, 'items'):
  53. # it's a dict
  54. items = items.items()
  55. for key, value in items:
  56. self.setConfig(key, value)
  57. def _extendMarkdown(self, *args):
  58. """ Private wrapper around extendMarkdown. """
  59. md = args[0]
  60. try:
  61. self.extendMarkdown(md)
  62. except TypeError as e:
  63. if "missing 1 required positional argument" in str(e):
  64. # Must be a 2.x extension. Pass in a dumby md_globals.
  65. self.extendMarkdown(md, {})
  66. warnings.warn(
  67. "The 'md_globals' parameter of '{}.{}.extendMarkdown' is "
  68. "deprecated.".format(self.__class__.__module__, self.__class__.__name__),
  69. category=DeprecationWarning,
  70. stacklevel=2
  71. )
  72. else:
  73. raise
  74. def extendMarkdown(self, md):
  75. """
  76. Add the various proccesors and patterns to the Markdown Instance.
  77. This method must be overriden by every extension.
  78. Keyword arguments:
  79. * md: The Markdown instance.
  80. * md_globals: Global variables in the markdown module namespace.
  81. """
  82. raise NotImplementedError(
  83. 'Extension "%s.%s" must define an "extendMarkdown"'
  84. 'method.' % (self.__class__.__module__, self.__class__.__name__)
  85. )