Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
waylan committed Apr 24, 2024
1 parent c03724b commit 3110977
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions markdown/extensions/abbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
from ..inlinepatterns import InlineProcessor
from ..treeprocessors import Treeprocessor
from ..util import AtomicString, deprecated
from typing import TYPE_CHECKING
import re
import xml.etree.ElementTree as etree

if TYPE_CHECKING: # pragma: no cover
from .. import Markdown
from ..blockparsers import BlockParser


class AbbrExtension(Extension):
""" Abbreviation Extension for Python-Markdown. """
Expand All @@ -42,15 +47,15 @@ def extendMarkdown(self, md):


class AbbrTreeprocessor(Treeprocessor):
""" Replace abbr text with `<abbr>` elements. """
""" Replace abbreviation text with `<abbr>` elements. """

def __init__(self, md: Markdown | None=None):
def __init__(self, md: Markdown | None = None):
self.abbrs = {}
self.RE = None
super().__init__(md)

def iter_element(self, el, parent=None):
''' Resursively iterate over elements, run regex on text and wrap matches in `abbr` tags. '''
def iter_element(self, el: etree.Element, parent: etree.Element | None = None) -> None:
''' Recursively iterate over elements, run regex on text and wrap matches in `abbr` tags. '''
for child in reversed(el):
self.iter_element(child, el)
if text := el.text:
Expand Down Expand Up @@ -89,7 +94,7 @@ class AbbrBlockprocessor(BlockProcessor):

RE = re.compile(r'^[*]\[(?P<abbr>[^\\]*?)\][ ]?:[ ]*\n?[ ]*(?P<title>.*)$', re.MULTILINE)

def __init__(self, parser, abbrs):
def __init__(self, parser: BlockParser, abbrs: dict):
self.abbrs = abbrs
super().__init__(parser)

Expand All @@ -98,8 +103,8 @@ def test(self, parent: etree.Element, block: str) -> bool:

def run(self, parent: etree.Element, blocks: list[str]) -> bool:
"""
Find and remove all Abbreviation references from the text.
Each reference is added to the abbrs collection.
Find and remove all abbreviation references from the text.
Each reference is added to the abbreviation collection.
"""
block = blocks.pop(0)
Expand Down

0 comments on commit 3110977

Please sign in to comment.