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