admonition.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Admonition extension for Python-Markdown
  3. ========================================
  4. Adds rST-style admonitions. Inspired by [rST][] feature with the same name.
  5. [rST]: http://docutils.sourceforge.net/docs/ref/rst/directives.html#specific-admonitions # noqa
  6. See <https://Python-Markdown.github.io/extensions/admonition>
  7. for documentation.
  8. Original code Copyright [Tiago Serafim](https://www.tiagoserafim.com/).
  9. All changes Copyright The Python Markdown Project
  10. License: [BSD](https://opensource.org/licenses/bsd-license.php)
  11. """
  12. from . import Extension
  13. from ..blockprocessors import BlockProcessor
  14. import xml.etree.ElementTree as etree
  15. import re
  16. class AdmonitionExtension(Extension):
  17. """ Admonition extension for Python-Markdown. """
  18. def extendMarkdown(self, md):
  19. """ Add Admonition to Markdown instance. """
  20. md.registerExtension(self)
  21. md.parser.blockprocessors.register(AdmonitionProcessor(md.parser), 'admonition', 105)
  22. class AdmonitionProcessor(BlockProcessor):
  23. CLASSNAME = 'admonition'
  24. CLASSNAME_TITLE = 'admonition-title'
  25. RE = re.compile(r'(?:^|\n)!!! ?([\w\-]+(?: +[\w\-]+)*)(?: +"(.*?)")? *(?:\n|$)')
  26. RE_SPACES = re.compile(' +')
  27. def test(self, parent, block):
  28. sibling = self.lastChild(parent)
  29. return self.RE.search(block) or \
  30. (block.startswith(' ' * self.tab_length) and sibling is not None and
  31. sibling.get('class', '').find(self.CLASSNAME) != -1)
  32. def run(self, parent, blocks):
  33. sibling = self.lastChild(parent)
  34. block = blocks.pop(0)
  35. m = self.RE.search(block)
  36. if m:
  37. block = block[m.end():] # removes the first line
  38. block, theRest = self.detab(block)
  39. if m:
  40. klass, title = self.get_class_and_title(m)
  41. div = etree.SubElement(parent, 'div')
  42. div.set('class', '{} {}'.format(self.CLASSNAME, klass))
  43. if title:
  44. p = etree.SubElement(div, 'p')
  45. p.text = title
  46. p.set('class', self.CLASSNAME_TITLE)
  47. else:
  48. div = sibling
  49. self.parser.parseChunk(div, block)
  50. if theRest:
  51. # This block contained unindented line(s) after the first indented
  52. # line. Insert these lines as the first block of the master blocks
  53. # list for future processing.
  54. blocks.insert(0, theRest)
  55. def get_class_and_title(self, match):
  56. klass, title = match.group(1).lower(), match.group(2)
  57. klass = self.RE_SPACES.sub(' ', klass)
  58. if title is None:
  59. # no title was provided, use the capitalized classname as title
  60. # e.g.: `!!! note` will render
  61. # `<p class="admonition-title">Note</p>`
  62. title = klass.split(' ', 1)[0].capitalize()
  63. elif title == '':
  64. # an explicit blank title should not be rendered
  65. # e.g.: `!!! warning ""` will *not* render `p` with a title
  66. title = None
  67. return klass, title
  68. def makeExtension(**kwargs): # pragma: no cover
  69. return AdmonitionExtension(**kwargs)