MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / ListIndentProcessor

Class ListIndentProcessor

markdown/blockprocessors.py:153–252  ·  view source on GitHub ↗

Process children of list items. Example * a list item process this part or this part

Source from the content-addressed store, hash-verified

151
152
153class ListIndentProcessor(BlockProcessor):
154 """ Process children of list items.
155
156 Example
157
158 * a list item
159 process this part
160
161 or this part
162
163 """
164
165 ITEM_TYPES = ['li']
166 """ List of tags used for list items. """
167 LIST_TYPES = ['ul', 'ol']
168 """ Types of lists this processor can operate on. """
169
170 def __init__(self, *args):
171 super().__init__(*args)
172 self.INDENT_RE = re.compile(r'^(([ ]{%s})+)' % self.tab_length)
173
174 def test(self, parent: etree.Element, block: str) -> bool:
175 # A list calls `parseBlocks` once per level of nesting, so the
176 # nesting has to stop short of the interpreter's limit. The markers which no
177 # longer nest are kept as text, as they are for a blockquote.
178 return block.startswith(' '*self.tab_length) and \
179 not self.parser.state.isstate('detabbed') and \
180 not util.nearing_recursion_limit() and \
181 (parent.tag in self.ITEM_TYPES or
182 (len(parent) and parent[-1] is not None and
183 (parent[-1].tag in self.LIST_TYPES)))
184
185 def run(self, parent: etree.Element, blocks: list[str]) -> None:
186 block = blocks.pop(0)
187 level, sibling = self.get_level(parent, block)
188 block = self.looseDetab(block, level)
189
190 self.parser.state.set('detabbed')
191 if parent.tag in self.ITEM_TYPES:
192 # It's possible that this parent has a `ul` or `ol` child list
193 # with a member. If that is the case, then that should be the
194 # parent. This is intended to catch the edge case of an indented
195 # list whose first member was parsed previous to this point
196 # see `OListProcessor`
197 if len(parent) and parent[-1].tag in self.LIST_TYPES:
198 self.parser.parseBlocks(parent[-1], [block])
199 else:
200 # The parent is already a `li`. Just parse the child block.
201 self.parser.parseBlocks(parent, [block])
202 elif sibling.tag in self.ITEM_TYPES:
203 # The sibling is a `li`. Use it as parent.
204 self.parser.parseBlocks(sibling, [block])
205 elif len(sibling) and sibling[-1].tag in self.ITEM_TYPES:
206 # The parent is a list (`ol` or `ul`) which has children.
207 # Assume the last child `li` is the parent of this block.
208 if sibling[-1].text:
209 # If the parent `li` has text, that text needs to be moved to a `p`
210 # The `p` must be 'inserted' at beginning of list in the event

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected