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

Class AbbrBlockprocessor

markdown/extensions/abbr.py:141–178  ·  view source on GitHub ↗

Parse text for abbreviation references.

Source from the content-addressed store, hash-verified

139
140
141class AbbrBlockprocessor(BlockProcessor):
142 """ Parse text for abbreviation references. """
143
144 RE = re.compile(r'^[*]\[(?P<abbr>[^\\]*?)\][ ]?:[ ]*\n?[ ]*(?P<title>.*)$', re.MULTILINE)
145
146 def __init__(self, parser: BlockParser, abbrs: dict):
147 self.abbrs: dict = abbrs
148 super().__init__(parser)
149
150 def test(self, parent: etree.Element, block: str) -> bool:
151 return True
152
153 def run(self, parent: etree.Element, blocks: list[str]) -> bool:
154 """
155 Find and remove all abbreviation references from the text.
156 Each reference is added to the abbreviation collection.
157
158 """
159 block = blocks.pop(0)
160 m = self.RE.search(block)
161 if m:
162 abbr = m.group('abbr').strip()
163 title = m.group('title').strip()
164 if title and abbr:
165 if title == "''" or title == '""':
166 self.abbrs.pop(abbr, None)
167 else:
168 self.abbrs[abbr] = title
169 if block[m.end():].strip():
170 # Add any content after match back to blocks as separate block
171 blocks.insert(0, block[m.end():].lstrip('\n'))
172 if block[:m.start()].strip():
173 # Add any content before match back to blocks as separate block
174 blocks.insert(0, block[:m.start()].rstrip('\n'))
175 return True
176 # No match. Restore block.
177 blocks.insert(0, block)
178 return False
179
180
181AbbrPreprocessor = deprecated("This class has been renamed to `AbbrBlockprocessor`.")(AbbrBlockprocessor)

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected