Skip to content

Commit 1d37310

Browse files
Merge pull request #607 from Crozzers/fix-middle-word-ems
Fix `middle-word-em` extra preventing strongs from being recognized (#606)
2 parents 777988e + 89c931a commit 1d37310

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## python-markdown2 2.5.2 (not yet released)
44

55
- [pull #605] Add support for Python 3.13, drop EOL 3.8
6+
- [pull #607] Fix `middle-word-em` extra preventing strongs from being recognized (#606)
67

78

89
## python-markdown2 2.5.1

lib/markdown2.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3144,9 +3144,26 @@ def __init__(self, md: Markdown, options: Union[dict, bool]):
31443144

31453145
self.liberal_em_re = self.em_re
31463146
if not options['allowed']:
3147-
self.em_re = re.compile(r'(?<=\b)%s(?=\b)' % self.liberal_em_re.pattern, self.liberal_em_re.flags)
3147+
self.em_re = re.compile(r'(?<=\b)%s(?=\b)' % self.em_re.pattern, self.em_re.flags)
3148+
self.liberal_em_re = re.compile(
3149+
r'''
3150+
( # \1 - must be a single em char in the middle of a word
3151+
(?<![*_\s]) # cannot be preceeded by em character or whitespace (must be in middle of word)
3152+
[*_] # em character
3153+
(?![*_]) # cannot be followed by another em char
3154+
)
3155+
(?=\S) # em opening must be followed by non-whitespace text
3156+
(.*?\S) # the emphasized text
3157+
\1 # closing char
3158+
(?!\s|$) # must not be followed by whitespace (middle of word) or EOF
3159+
'''
3160+
, re.S | re.X)
31483161

31493162
def run(self, text):
3163+
if self.options['allowed']:
3164+
# if middle word em is allowed, do nothing. This extra's only use is to prevent them
3165+
return text
3166+
31503167
# run strong and whatnot first
31513168
# this also will process all strict ems
31523169
text = super().run(text)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<p>Double variants:</p>
2+
3+
<p><strong>one_two_three</strong></p>
4+
5+
<p><strong><em>one_two_three</em></strong></p>
6+
7+
<p><em><strong>one_two_three</strong></em></p>
8+
9+
<p><strong><em>one_two_three</em></strong></p>
10+
11+
<p>Single variants:</p>
12+
13+
<p><em>one_two_three</em></p>
14+
15+
<p><em>one_two_three</em></p>
16+
17+
<p><em>one*two*three</em></p>
18+
19+
<p><em>one<em>two</em>three</em></p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": {"middle-word-em": False}}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Double variants:
2+
3+
**one_two_three**
4+
5+
***one_two_three***
6+
7+
_**one_two_three**_
8+
9+
**_one_two_three_**
10+
11+
Single variants:
12+
13+
*one_two_three*
14+
15+
_one_two_three_
16+
17+
_one*two*three_
18+
19+
*one*two*three*

0 commit comments

Comments
 (0)