def_list.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """
  2. Definition List Extension for Python-Markdown
  3. =============================================
  4. Adds parsing of Definition Lists to Python-Markdown.
  5. See <https://Python-Markdown.github.io/extensions/definition_lists>
  6. for documentation.
  7. Original code Copyright 2008 [Waylan Limberg](http://achinghead.com)
  8. All changes Copyright 2008-2014 The Python Markdown Project
  9. License: [BSD](https://opensource.org/licenses/bsd-license.php)
  10. """
  11. from . import Extension
  12. from ..blockprocessors import BlockProcessor, ListIndentProcessor
  13. import xml.etree.ElementTree as etree
  14. import re
  15. class DefListProcessor(BlockProcessor):
  16. """ Process Definition Lists. """
  17. RE = re.compile(r'(^|\n)[ ]{0,3}:[ ]{1,3}(.*?)(\n|$)')
  18. NO_INDENT_RE = re.compile(r'^[ ]{0,3}[^ :]')
  19. def test(self, parent, block):
  20. return bool(self.RE.search(block))
  21. def run(self, parent, blocks):
  22. raw_block = blocks.pop(0)
  23. m = self.RE.search(raw_block)
  24. terms = [l.strip() for l in
  25. raw_block[:m.start()].split('\n') if l.strip()]
  26. block = raw_block[m.end():]
  27. no_indent = self.NO_INDENT_RE.match(block)
  28. if no_indent:
  29. d, theRest = (block, None)
  30. else:
  31. d, theRest = self.detab(block)
  32. if d:
  33. d = '{}\n{}'.format(m.group(2), d)
  34. else:
  35. d = m.group(2)
  36. sibling = self.lastChild(parent)
  37. if not terms and sibling is None:
  38. # This is not a definition item. Most likely a paragraph that
  39. # starts with a colon at the beginning of a document or list.
  40. blocks.insert(0, raw_block)
  41. return False
  42. if not terms and sibling.tag == 'p':
  43. # The previous paragraph contains the terms
  44. state = 'looselist'
  45. terms = sibling.text.split('\n')
  46. parent.remove(sibling)
  47. # Acquire new sibling
  48. sibling = self.lastChild(parent)
  49. else:
  50. state = 'list'
  51. if sibling is not None and sibling.tag == 'dl':
  52. # This is another item on an existing list
  53. dl = sibling
  54. if not terms and len(dl) and dl[-1].tag == 'dd' and len(dl[-1]):
  55. state = 'looselist'
  56. else:
  57. # This is a new list
  58. dl = etree.SubElement(parent, 'dl')
  59. # Add terms
  60. for term in terms:
  61. dt = etree.SubElement(dl, 'dt')
  62. dt.text = term
  63. # Add definition
  64. self.parser.state.set(state)
  65. dd = etree.SubElement(dl, 'dd')
  66. self.parser.parseBlocks(dd, [d])
  67. self.parser.state.reset()
  68. if theRest:
  69. blocks.insert(0, theRest)
  70. class DefListIndentProcessor(ListIndentProcessor):
  71. """ Process indented children of definition list items. """
  72. ITEM_TYPES = ['dd']
  73. LIST_TYPES = ['dl']
  74. def create_item(self, parent, block):
  75. """ Create a new dd and parse the block with it as the parent. """
  76. dd = etree.SubElement(parent, 'dd')
  77. self.parser.parseBlocks(dd, [block])
  78. class DefListExtension(Extension):
  79. """ Add definition lists to Markdown. """
  80. def extendMarkdown(self, md):
  81. """ Add an instance of DefListProcessor to BlockParser. """
  82. md.parser.blockprocessors.register(DefListIndentProcessor(md.parser), 'defindent', 85)
  83. md.parser.blockprocessors.register(DefListProcessor(md.parser), 'deflist', 25)
  84. def makeExtension(**kwargs): # pragma: no cover
  85. return DefListExtension(**kwargs)