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

Class HtmlStash

markdown/util.py:213–259  ·  view source on GitHub ↗

This class is used for stashing HTML objects that we extract in the beginning and replace with place-holders.

Source from the content-addressed store, hash-verified

211
212
213class HtmlStash:
214 """
215 This class is used for stashing HTML objects that we extract
216 in the beginning and replace with place-holders.
217 """
218
219 def __init__(self):
220 """ Create an `HtmlStash`. """
221 self.html_counter = 0 # for counting inline html segments
222 self.rawHtmlBlocks: list[str | etree.Element] = []
223 self.tag_counter = 0
224 self.tag_data: list[TagData] = [] # list of dictionaries in the order tags appear
225
226 def store(self, html: str | etree.Element) -> str:
227 """
228 Saves an HTML segment for later reinsertion. Returns a
229 placeholder string that needs to be inserted into the
230 document.
231
232 Keyword arguments:
233 html: An html segment.
234
235 Returns:
236 A placeholder string.
237
238 """
239 self.rawHtmlBlocks.append(html)
240 placeholder = self.get_placeholder(self.html_counter)
241 self.html_counter += 1
242 return placeholder
243
244 def reset(self) -> None:
245 """ Clear the stash. """
246 self.html_counter = 0
247 self.rawHtmlBlocks = []
248
249 def get_placeholder(self, key: int) -> str:
250 return HTML_PLACEHOLDER % key
251
252 def store_tag(self, tag: str, attrs: dict[str, str], left_index: int, right_index: int) -> str:
253 """Store tag data and return a placeholder."""
254 self.tag_data.append({'tag': tag, 'attrs': attrs,
255 'left_index': left_index,
256 'right_index': right_index})
257 placeholder = TAG_PLACEHOLDER % str(self.tag_counter)
258 self.tag_counter += 1 # equal to the tag's index in `self.tag_data`
259 return placeholder
260
261
262# Used internally by `Registry` for each item in its sorted list.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected