• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2import markdown
3
4class State(list):
5    """ Track the current and nested state of the parser.
6
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
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
15    corrupted.
16
17    While all the methods of a list object are available, only the three
18    defined below need be used.
19
20    """
21
22    def set(self, state):
23        """ Set a new state. """
24        self.append(state)
25
26    def reset(self):
27        """ Step back one step in nested state. """
28        self.pop()
29
30    def isstate(self, state):
31        """ Test that top (current) level is of given state. """
32        if len(self):
33            return self[-1] == state
34        else:
35            return False
36
37class BlockParser:
38    """ Parse Markdown blocks into an ElementTree object.
39
40    A wrapper class that stitches the various BlockProcessors together,
41    looping through them and creating an ElementTree object.
42    """
43
44    def __init__(self):
45        self.blockprocessors = markdown.odict.OrderedDict()
46        self.state = State()
47
48    def parseDocument(self, lines):
49        """ Parse a markdown document into an ElementTree.
50
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.
54
55        This should only be called on an entire document, not pieces.
56
57        """
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)
62
63    def parseChunk(self, parent, text):
64        """ Parse a chunk of markdown text and attach to given etree node.
65
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.
70
71        The ``parent`` etree Element passed in is altered in place.
72        Nothing is returned.
73
74        """
75        self.parseBlocks(parent, text.split('\n\n'))
76
77    def parseBlocks(self, parent, blocks):
78        """ Process blocks of markdown text and attach to given etree node.
79
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.
83
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
86        block.
87
88        """
89        while blocks:
90           for processor in self.blockprocessors.values():
91               if processor.test(parent, blocks[0]):
92                   processor.run(parent, blocks)
93                   break
94
95
96