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