5
""" 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
13
Therefore, each time a state is set for a nested block, that state must be
14
reset when we back out of that level of nesting or the state could be
17
While all the methods of a list object are available, only the three
18
defined below need be used.
23
""" Set a new state. """
27
""" Step back one step in nested state. """
30
def isstate(self, state):
31
""" Test that top (current) level is of given state. """
33
return self[-1] == state
38
""" Parse Markdown blocks into an ElementTree object.
40
A wrapper class that stitches the various BlockProcessors together,
41
looping through them and creating an ElementTree object.
45
self.blockprocessors = markdown.odict.OrderedDict()
48
def parseDocument(self, lines):
49
""" Parse a markdown document into an ElementTree.
51
Given a list of lines, an ElementTree object (not just a parent Element)
52
is created and the root element is passed to the parser as the parent.
53
The ElementTree object is returned.
55
This should only be called on an entire document, not pieces.
58
# Create a ElementTree from the lines
59
self.root = markdown.etree.Element(markdown.DOC_TAG)
60
self.parseChunk(self.root, '\n'.join(lines))
61
return markdown.etree.ElementTree(self.root)
63
def parseChunk(self, parent, text):
64
""" Parse a chunk of markdown text and attach to given etree node.
66
While the ``text`` argument is generally assumed to contain multiple
67
blocks which will be split on blank lines, it could contain only one
68
block. Generally, this method would be called by extensions when
69
block parsing is required.
71
The ``parent`` etree Element passed in is altered in place.
75
self.parseBlocks(parent, text.split('\n\n'))
77
def parseBlocks(self, parent, blocks):
78
""" Process blocks of markdown text and attach to given etree node.
80
Given a list of ``blocks``, each blockprocessor is stepped through
81
until there are no blocks left. While an extension could potentially
82
call this method directly, it's generally expected to be used internally.
84
This is a public method as an extension may need to add/alter additional
85
BlockProcessors which call this method to recursively parse a nested
90
for processor in self.blockprocessors.values():
91
if processor.test(parent, blocks[0]):
92
processor.run(parent, blocks)