MCPcopy Index your code
hub / github.com/Python-Markdown/markdown / BlockParser

Class BlockParser

markdown/blockparser.py:75–160  ·  view source on GitHub ↗

Parse Markdown blocks into an `ElementTree` object. A wrapper class that stitches the various `BlockProcessors` together, looping through them and creating an `ElementTree` object.

Source from the content-addressed store, hash-verified

73
74
75class BlockParser:
76 """ Parse Markdown blocks into an `ElementTree` object.
77
78 A wrapper class that stitches the various `BlockProcessors` together,
79 looping through them and creating an `ElementTree` object.
80
81 """
82
83 def __init__(self, md: Markdown):
84 """ Initialize the block parser.
85
86 Arguments:
87 md: A Markdown instance.
88
89 Attributes:
90 BlockParser.md (Markdown): A Markdown instance.
91 BlockParser.state (State): Tracks the nesting level of current location in document being parsed.
92 BlockParser.blockprocessors (util.Registry): A collection of
93 [`blockprocessors`][markdown.blockprocessors].
94
95 """
96 self.blockprocessors: util.Registry[BlockProcessor] = util.Registry()
97 self.state = State()
98 self.md = md
99
100 def parseDocument(self, lines: Iterable[str]) -> etree.ElementTree:
101 """ Parse a Markdown document into an `ElementTree`.
102
103 Given a list of lines, an `ElementTree` object (not just a parent
104 `Element`) is created and the root element is passed to the parser
105 as the parent. The `ElementTree` object is returned.
106
107 This should only be called on an entire document, not pieces.
108
109 Arguments:
110 lines: A list of lines (strings).
111
112 Returns:
113 An element tree.
114 """
115 # Create an `ElementTree` from the lines
116 self.root = etree.Element(self.md.doc_tag)
117 self.parseChunk(self.root, '\n'.join(lines))
118 return etree.ElementTree(self.root)
119
120 def parseChunk(self, parent: etree.Element, text: str) -> None:
121 """ Parse a chunk of Markdown text and attach to given `etree` node.
122
123 While the `text` argument is generally assumed to contain multiple
124 blocks which will be split on blank lines, it could contain only one
125 block. Generally, this method would be called by extensions when
126 block parsing is required.
127
128 The `parent` `etree` Element passed in is altered in place.
129 Nothing is returned.
130
131 Arguments:
132 parent: The parent element.

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…