OLD | NEW |
1 # markdown is released under the BSD license | |
2 # Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later) | |
3 # Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) | |
4 # Copyright 2004 Manfred Stienstra (the original version) | |
5 # | |
6 # All rights reserved. | |
7 # | |
8 # Redistribution and use in source and binary forms, with or without | |
9 # modification, are permitted provided that the following conditions are met: | |
10 # | |
11 # * Redistributions of source code must retain the above copyright | |
12 # notice, this list of conditions and the following disclaimer. | |
13 # * Redistributions in binary form must reproduce the above copyright | |
14 # notice, this list of conditions and the following disclaimer in the | |
15 # documentation and/or other materials provided with the distribution. | |
16 # * Neither the name of the <organization> nor the | |
17 # names of its contributors may be used to endorse or promote products | |
18 # derived from this software without specific prior written permission. | |
19 # | |
20 # THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY | |
21 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
23 # DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT | |
24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
30 # POSSIBILITY OF SUCH DAMAGE. | |
31 | |
32 | |
33 from __future__ import unicode_literals | 1 from __future__ import unicode_literals |
34 from __future__ import absolute_import | 2 from __future__ import absolute_import |
35 from . import util | 3 from . import util |
36 from . import odict | 4 from . import odict |
37 | 5 |
| 6 |
38 class State(list): | 7 class State(list): |
39 """ Track the current and nested state of the parser. | 8 """ Track the current and nested state of the parser. |
40 | 9 |
41 This utility class is used to track the state of the BlockParser and | 10 This utility class is used to track the state of the BlockParser and |
42 support multiple levels if nesting. It's just a simple API wrapped around | 11 support multiple levels if nesting. It's just a simple API wrapped around |
43 a list. Each time a state is set, that state is appended to the end of the | 12 a list. Each time a state is set, that state is appended to the end of the |
44 list. Each time a state is reset, that state is removed from the end of | 13 list. Each time a state is reset, that state is removed from the end of |
45 the list. | 14 the list. |
46 | 15 |
47 Therefore, each time a state is set for a nested block, that state must be | 16 Therefore, each time a state is set for a nested block, that state must be |
48 reset when we back out of that level of nesting or the state could be | 17 reset when we back out of that level of nesting or the state could be |
49 corrupted. | 18 corrupted. |
50 | 19 |
51 While all the methods of a list object are available, only the three | 20 While all the methods of a list object are available, only the three |
52 defined below need be used. | 21 defined below need be used. |
53 | 22 |
54 """ | 23 """ |
55 | 24 |
56 def set(self, state): | 25 def set(self, state): |
57 """ Set a new state. """ | 26 """ Set a new state. """ |
58 self.append(state) | 27 self.append(state) |
59 | 28 |
60 def reset(self): | 29 def reset(self): |
61 """ Step back one step in nested state. """ | 30 """ Step back one step in nested state. """ |
62 self.pop() | 31 self.pop() |
63 | 32 |
64 def isstate(self, state): | 33 def isstate(self, state): |
65 """ Test that top (current) level is of given state. """ | 34 """ Test that top (current) level is of given state. """ |
66 if len(self): | 35 if len(self): |
67 return self[-1] == state | 36 return self[-1] == state |
68 else: | 37 else: |
69 return False | 38 return False |
70 | 39 |
| 40 |
71 class BlockParser: | 41 class BlockParser: |
72 """ Parse Markdown blocks into an ElementTree object. | 42 """ Parse Markdown blocks into an ElementTree object. |
73 | 43 |
74 A wrapper class that stitches the various BlockProcessors together, | 44 A wrapper class that stitches the various BlockProcessors together, |
75 looping through them and creating an ElementTree object. | 45 looping through them and creating an ElementTree object. |
76 """ | 46 """ |
77 | 47 |
78 def __init__(self, markdown): | 48 def __init__(self, markdown): |
79 self.blockprocessors = odict.OrderedDict() | 49 self.blockprocessors = odict.OrderedDict() |
80 self.state = State() | 50 self.state = State() |
81 self.markdown = markdown | 51 self.markdown = markdown |
82 | 52 |
83 def parseDocument(self, lines): | 53 def parseDocument(self, lines): |
84 """ Parse a markdown document into an ElementTree. | 54 """ Parse a markdown document into an ElementTree. |
85 | 55 |
86 Given a list of lines, an ElementTree object (not just a parent Element) | 56 Given a list of lines, an ElementTree object (not just a parent |
87 is created and the root element is passed to the parser as the parent. | 57 Element) is created and the root element is passed to the parser |
88 The ElementTree object is returned. | 58 as the parent. The ElementTree object is returned. |
89 | 59 |
90 This should only be called on an entire document, not pieces. | 60 This should only be called on an entire document, not pieces. |
91 | 61 |
92 """ | 62 """ |
93 # Create a ElementTree from the lines | 63 # Create a ElementTree from the lines |
94 self.root = util.etree.Element(self.markdown.doc_tag) | 64 self.root = util.etree.Element(self.markdown.doc_tag) |
95 self.parseChunk(self.root, '\n'.join(lines)) | 65 self.parseChunk(self.root, '\n'.join(lines)) |
96 return util.etree.ElementTree(self.root) | 66 return util.etree.ElementTree(self.root) |
97 | 67 |
98 def parseChunk(self, parent, text): | 68 def parseChunk(self, parent, text): |
99 """ Parse a chunk of markdown text and attach to given etree node. | 69 """ Parse a chunk of markdown text and attach to given etree node. |
100 | 70 |
101 While the ``text`` argument is generally assumed to contain multiple | 71 While the ``text`` argument is generally assumed to contain multiple |
102 blocks which will be split on blank lines, it could contain only one | 72 blocks which will be split on blank lines, it could contain only one |
103 block. Generally, this method would be called by extensions when | 73 block. Generally, this method would be called by extensions when |
104 block parsing is required. | 74 block parsing is required. |
105 | 75 |
106 The ``parent`` etree Element passed in is altered in place. | 76 The ``parent`` etree Element passed in is altered in place. |
107 Nothing is returned. | 77 Nothing is returned. |
108 | 78 |
109 """ | 79 """ |
110 self.parseBlocks(parent, text.split('\n\n')) | 80 self.parseBlocks(parent, text.split('\n\n')) |
111 | 81 |
112 def parseBlocks(self, parent, blocks): | 82 def parseBlocks(self, parent, blocks): |
113 """ Process blocks of markdown text and attach to given etree node. | 83 """ Process blocks of markdown text and attach to given etree node. |
114 | 84 |
115 Given a list of ``blocks``, each blockprocessor is stepped through | 85 Given a list of ``blocks``, each blockprocessor is stepped through |
116 until there are no blocks left. While an extension could potentially | 86 until there are no blocks left. While an extension could potentially |
117 call this method directly, it's generally expected to be used internally
. | 87 call this method directly, it's generally expected to be used |
| 88 internally. |
118 | 89 |
119 This is a public method as an extension may need to add/alter additional | 90 This is a public method as an extension may need to add/alter |
120 BlockProcessors which call this method to recursively parse a nested | 91 additional BlockProcessors which call this method to recursively |
121 block. | 92 parse a nested block. |
122 | 93 |
123 """ | 94 """ |
124 while blocks: | 95 while blocks: |
125 for processor in self.blockprocessors.values(): | 96 for processor in self.blockprocessors.values(): |
126 if processor.test(parent, blocks[0]): | 97 if processor.test(parent, blocks[0]): |
127 if processor.run(parent, blocks) is not False: | 98 if processor.run(parent, blocks) is not False: |
128 # run returns True or None | 99 # run returns True or None |
129 break | 100 break |
130 | |
131 | |
OLD | NEW |