Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eeb509d

Browse files
Fix DRY by adding create_element method in abbr extension
1 parent d312609 commit eeb509d

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

‎docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file.
88
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
99
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). See the [Contributing Guide](contributing.md) for details.
1010

11+
## [Unreleased]
12+
13+
### Changed
14+
15+
* DRY fix in `abbr` extension by introducing method `create_element` (#1483).
16+
1117
## [3.7] -- 2024年08月16日
1218

1319
### Changed

‎markdown/extensions/abbr.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,29 @@ def __init__(self, md: Markdown | None = None, abbrs: dict | None = None):
8686
self.RE: re.RegexObject | None = None
8787
super().__init__(md)
8888

89+
def create_element(self, title: str, text: str, tail: str) -> etree.Element:
90+
''' Create an `abbr` element. '''
91+
abbr = etree.Element('abbr', {'title': title})
92+
abbr.text = AtomicString(text)
93+
abbr.tail = tail
94+
return abbr
95+
8996
def iter_element(self, el: etree.Element, parent: etree.Element | None = None) -> None:
9097
''' Recursively iterate over elements, run regex on text and wrap matches in `abbr` tags. '''
9198
for child in reversed(el):
9299
self.iter_element(child, el)
93100
if text := el.text:
94101
for m in reversed(list(self.RE.finditer(text))):
95102
if self.abbrs[m.group(0)]:
96-
abbr = etree.Element('abbr', {'title': self.abbrs[m.group(0)]})
97-
abbr.text = AtomicString(m.group(0))
98-
abbr.tail = text[m.end():]
103+
abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), text[m.end():])
99104
el.insert(0, abbr)
100105
text = text[:m.start()]
101106
el.text = text
102107
if parent is not None and el.tail:
103108
tail = el.tail
104109
index = list(parent).index(el) + 1
105110
for m in reversed(list(self.RE.finditer(tail))):
106-
abbr = etree.Element('abbr', {'title': self.abbrs[m.group(0)]})
107-
abbr.text = AtomicString(m.group(0))
108-
abbr.tail = tail[m.end():]
111+
abbr = self.create_element(self.abbrs[m.group(0)], m.group(0), tail[m.end():])
109112
parent.insert(index, abbr)
110113
tail = tail[:m.start()]
111114
el.tail = tail

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /