You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.4 KiB

4 years ago
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from . import util
  4. from . import odict
  5. class State(list):
  6. """ Track the current and nested state of the parser.
  7. This utility class is used to track the state of the BlockParser and
  8. support multiple levels if nesting. It's just a simple API wrapped around
  9. a list. Each time a state is set, that state is appended to the end of the
  10. list. Each time a state is reset, that state is removed from the end of
  11. the list.
  12. Therefore, each time a state is set for a nested block, that state must be
  13. reset when we back out of that level of nesting or the state could be
  14. corrupted.
  15. While all the methods of a list object are available, only the three
  16. defined below need be used.
  17. """
  18. def set(self, state):
  19. """ Set a new state. """
  20. self.append(state)
  21. def reset(self):
  22. """ Step back one step in nested state. """
  23. self.pop()
  24. def isstate(self, state):
  25. """ Test that top (current) level is of given state. """
  26. if len(self):
  27. return self[-1] == state
  28. else:
  29. return False
  30. class BlockParser:
  31. """ Parse Markdown blocks into an ElementTree object.
  32. A wrapper class that stitches the various BlockProcessors together,
  33. looping through them and creating an ElementTree object.
  34. """
  35. def __init__(self, markdown):
  36. self.blockprocessors = odict.OrderedDict()
  37. self.state = State()
  38. self.markdown = markdown
  39. def parseDocument(self, lines):
  40. """ Parse a markdown document into an ElementTree.
  41. Given a list of lines, an ElementTree object (not just a parent
  42. Element) is created and the root element is passed to the parser
  43. as the parent. The ElementTree object is returned.
  44. This should only be called on an entire document, not pieces.
  45. """
  46. # Create a ElementTree from the lines
  47. self.root = util.etree.Element(self.markdown.doc_tag)
  48. self.parseChunk(self.root, '\n'.join(lines))
  49. return util.etree.ElementTree(self.root)
  50. def parseChunk(self, parent, text):
  51. """ Parse a chunk of markdown text and attach to given etree node.
  52. While the ``text`` argument is generally assumed to contain multiple
  53. blocks which will be split on blank lines, it could contain only one
  54. block. Generally, this method would be called by extensions when
  55. block parsing is required.
  56. The ``parent`` etree Element passed in is altered in place.
  57. Nothing is returned.
  58. """
  59. self.parseBlocks(parent, text.split('\n\n'))
  60. def parseBlocks(self, parent, blocks):
  61. """ Process blocks of markdown text and attach to given etree node.
  62. Given a list of ``blocks``, each blockprocessor is stepped through
  63. until there are no blocks left. While an extension could potentially
  64. call this method directly, it's generally expected to be used
  65. internally.
  66. This is a public method as an extension may need to add/alter
  67. additional BlockProcessors which call this method to recursively
  68. parse a nested block.
  69. """
  70. while blocks:
  71. for processor in self.blockprocessors.values():
  72. if processor.test(parent, blocks[0]):
  73. if processor.run(parent, blocks) is not False:
  74. # run returns True or None
  75. break