A `Treeprocessor` that traverses a tree, applying inline patterns.
| 73 | |
| 74 | |
| 75 | class InlineProcessor(Treeprocessor): |
| 76 | """ |
| 77 | A `Treeprocessor` that traverses a tree, applying inline patterns. |
| 78 | """ |
| 79 | |
| 80 | def __init__(self, md: Markdown): |
| 81 | self.__placeholder_prefix = util.INLINE_PLACEHOLDER_PREFIX |
| 82 | self.__placeholder_suffix = util.ETX |
| 83 | self.__placeholder_length = 4 + len(self.__placeholder_prefix) \ |
| 84 | + len(self.__placeholder_suffix) |
| 85 | self.__placeholder_re = util.INLINE_PLACEHOLDER_RE |
| 86 | self.md = md |
| 87 | self.inlinePatterns = md.inlinePatterns |
| 88 | self.ancestors: list[str] = [] |
| 89 | |
| 90 | def __makePlaceholder(self, type: str) -> tuple[str, str]: |
| 91 | """ Generate a placeholder """ |
| 92 | id = "%04d" % len(self.stashed_nodes) |
| 93 | hash = util.INLINE_PLACEHOLDER % id |
| 94 | return hash, id |
| 95 | |
| 96 | def __findPlaceholder(self, data: str, index: int) -> tuple[str | None, int]: |
| 97 | """ |
| 98 | Extract id from data string, start from index. |
| 99 | |
| 100 | Arguments: |
| 101 | data: String. |
| 102 | index: Index, from which we start search. |
| 103 | |
| 104 | Returns: |
| 105 | Placeholder id and string index, after the found placeholder. |
| 106 | |
| 107 | """ |
| 108 | m = self.__placeholder_re.search(data, index) |
| 109 | if m: |
| 110 | return m.group(1), m.end() |
| 111 | else: |
| 112 | return None, index + 1 |
| 113 | |
| 114 | def __stashNode(self, node: etree.Element | str, type: str) -> str: |
| 115 | """ Add node to stash. """ |
| 116 | placeholder, id = self.__makePlaceholder(type) |
| 117 | self.stashed_nodes[id] = node |
| 118 | return placeholder |
| 119 | |
| 120 | def __handleInline(self, data: str, patternIndex: int = 0) -> str: |
| 121 | """ |
| 122 | Process string with inline patterns and replace it with placeholders. |
| 123 | |
| 124 | Arguments: |
| 125 | data: A line of Markdown text. |
| 126 | patternIndex: The index of the `inlinePattern` to start with. |
| 127 | |
| 128 | Returns: |
| 129 | String with placeholders. |
| 130 | |
| 131 | """ |
| 132 | if not isinstance(data, util.AtomicString): |
no outgoing calls
no test coverage detected