def_list.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # markdown is released under the BSD license
  2. # Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later)
  3. # Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
  4. # Copyright 2004 Manfred Stienstra (the original version)
  5. #
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above copyright
  14. # notice, this list of conditions and the following disclaimer in the
  15. # documentation and/or other materials provided with the distribution.
  16. # * Neither the name of the <organization> nor the
  17. # names of its contributors may be used to endorse or promote products
  18. # derived from this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY
  21. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. # DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT
  24. # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. # POSSIBILITY OF SUCH DAMAGE.
  31. """
  32. Definition List Extension for Python-Markdown
  33. =============================================
  34. Added parsing of Definition Lists to Python-Markdown.
  35. A simple example:
  36. Apple
  37. : Pomaceous fruit of plants of the genus Malus in
  38. the family Rosaceae.
  39. : An american computer company.
  40. Orange
  41. : The fruit of an evergreen tree of the genus Citrus.
  42. Copyright 2008 - [Waylan Limberg](http://achinghead.com)
  43. """
  44. from __future__ import absolute_import
  45. from __future__ import unicode_literals
  46. from . import Extension
  47. from ..blockprocessors import BlockProcessor, ListIndentProcessor
  48. from ..util import etree
  49. import re
  50. class DefListProcessor(BlockProcessor):
  51. """ Process Definition Lists. """
  52. RE = re.compile(r'(^|\n)[ ]{0,3}:[ ]{1,3}(.*?)(\n|$)')
  53. NO_INDENT_RE = re.compile(r'^[ ]{0,3}[^ :]')
  54. def test(self, parent, block):
  55. return bool(self.RE.search(block))
  56. def run(self, parent, blocks):
  57. raw_block = blocks.pop(0)
  58. m = self.RE.search(raw_block)
  59. terms = [l.strip() for l in raw_block[:m.start()].split('\n') if l.strip()]
  60. block = raw_block[m.end():]
  61. no_indent = self.NO_INDENT_RE.match(block)
  62. if no_indent:
  63. d, theRest = (block, None)
  64. else:
  65. d, theRest = self.detab(block)
  66. if d:
  67. d = '%s\n%s' % (m.group(2), d)
  68. else:
  69. d = m.group(2)
  70. sibling = self.lastChild(parent)
  71. if not terms and sibling is None:
  72. # This is not a definition item. Most likely a paragraph that
  73. # starts with a colon at the begining of a document or list.
  74. blocks.insert(0, raw_block)
  75. return False
  76. if not terms and sibling.tag == 'p':
  77. # The previous paragraph contains the terms
  78. state = 'looselist'
  79. terms = sibling.text.split('\n')
  80. parent.remove(sibling)
  81. # Aquire new sibling
  82. sibling = self.lastChild(parent)
  83. else:
  84. state = 'list'
  85. if sibling and sibling.tag == 'dl':
  86. # This is another item on an existing list
  87. dl = sibling
  88. if len(dl) and dl[-1].tag == 'dd' and len(dl[-1]):
  89. state = 'looselist'
  90. else:
  91. # This is a new list
  92. dl = etree.SubElement(parent, 'dl')
  93. # Add terms
  94. for term in terms:
  95. dt = etree.SubElement(dl, 'dt')
  96. dt.text = term
  97. # Add definition
  98. self.parser.state.set(state)
  99. dd = etree.SubElement(dl, 'dd')
  100. self.parser.parseBlocks(dd, [d])
  101. self.parser.state.reset()
  102. if theRest:
  103. blocks.insert(0, theRest)
  104. class DefListIndentProcessor(ListIndentProcessor):
  105. """ Process indented children of definition list items. """
  106. ITEM_TYPES = ['dd']
  107. LIST_TYPES = ['dl']
  108. def create_item(self, parent, block):
  109. """ Create a new dd and parse the block with it as the parent. """
  110. dd = etree.SubElement(parent, 'dd')
  111. self.parser.parseBlocks(dd, [block])
  112. class DefListExtension(Extension):
  113. """ Add definition lists to Markdown. """
  114. def extendMarkdown(self, md, md_globals):
  115. """ Add an instance of DefListProcessor to BlockParser. """
  116. md.parser.blockprocessors.add('defindent',
  117. DefListIndentProcessor(md.parser),
  118. '>indent')
  119. md.parser.blockprocessors.add('deflist',
  120. DefListProcessor(md.parser),
  121. '>ulist')
  122. def makeExtension(configs={}):
  123. return DefListExtension(configs=configs)