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