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

Class HTMLExtractor

markdown/htmlparser.py:130–469  ·  view source on GitHub ↗

Extract raw HTML from text. The raw HTML is stored in the [`htmlStash`][markdown.util.HtmlStash] of the [`Markdown`][markdown.Markdown] instance passed to `md` and the remaining text is stored in `cleandoc` as a list of strings.

Source from the content-addressed store, hash-verified

128
129
130class HTMLExtractor(htmlparser.HTMLParser):
131 """
132 Extract raw HTML from text.
133
134 The raw HTML is stored in the [`htmlStash`][markdown.util.HtmlStash] of the
135 [`Markdown`][markdown.Markdown] instance passed to `md` and the remaining text
136 is stored in `cleandoc` as a list of strings.
137 """
138
139 def __init__(self, md: Markdown, *args, **kwargs):
140 if 'convert_charrefs' not in kwargs:
141 kwargs['convert_charrefs'] = False
142
143 # Block tags that should contain no content (self closing)
144 self.empty_tags = set(['hr'])
145
146 self.lineno_start_cache = [0]
147
148 # This calls self.reset
149 super().__init__(*args, **kwargs)
150 self.md = md
151
152 def reset(self):
153 """Reset this instance. Loses all unprocessed data."""
154 self.inraw = False
155 self.intail = False
156 self.stack: list[str] = [] # When `inraw==True`, stack contains a list of tags
157 self.inline_stack: list[str] = [] # Unclosed raw inline tags in the current paragraph
158 self._cache: list[str] = []
159 self.cleandoc: list[str] = []
160 self.lineno_start_cache = [0]
161 # The `rawdata` and position of the last failed search for a comment close.
162 self._no_commentclose: tuple[str | None, int] = (None, 0)
163
164 super().reset()
165
166 def close(self):
167 """Handle any buffered data."""
168 super().close()
169 if len(self.rawdata):
170 # Temp fix for https://bugs.python.org/issue41989
171 # TODO: remove this when the bug is fixed in all supported Python versions.
172 if self.convert_charrefs and not self.cdata_elem: # pragma: no cover
173 self.handle_data(htmlparser.unescape(self.rawdata))
174 else:
175 self.handle_data(self.rawdata)
176 # Handle any unclosed tags.
177 if len(self._cache):
178 self.cleandoc.append(self.md.htmlStash.store(''.join(self._cache)))
179 self._cache = []
180
181 @property
182 def line_offset(self) -> int:
183 """Returns char index in `self.rawdata` for the start of the current line. """
184 for ii in range(len(self.lineno_start_cache)-1, self.lineno-1):
185 last_line_start_pos = self.lineno_start_cache[ii]
186 lf_pos = self.rawdata.find('\n', last_line_start_pos)
187 if lf_pos == -1:

Callers 1

runMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected