Skip to content

Commit

Permalink
Merge pull request #607 from Crozzers/fix-middle-word-ems
Browse files Browse the repository at this point in the history
Fix `middle-word-em` extra preventing strongs from being recognized (#606)
  • Loading branch information
nicholasserra authored Oct 28, 2024
2 parents 777988e + 89c931a commit 1d37310
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## python-markdown2 2.5.2 (not yet released)

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


## python-markdown2 2.5.1
Expand Down
19 changes: 18 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3144,9 +3144,26 @@ def __init__(self, md: Markdown, options: Union[dict, bool]):

self.liberal_em_re = self.em_re
if not options['allowed']:
self.em_re = re.compile(r'(?<=\b)%s(?=\b)' % self.liberal_em_re.pattern, self.liberal_em_re.flags)
self.em_re = re.compile(r'(?<=\b)%s(?=\b)' % self.em_re.pattern, self.em_re.flags)
self.liberal_em_re = re.compile(
r'''
( # \1 - must be a single em char in the middle of a word
(?<![*_\s]) # cannot be preceeded by em character or whitespace (must be in middle of word)
[*_] # em character
(?![*_]) # cannot be followed by another em char
)
(?=\S) # em opening must be followed by non-whitespace text
(.*?\S) # the emphasized text
\1 # closing char
(?!\s|$) # must not be followed by whitespace (middle of word) or EOF
'''
, re.S | re.X)

def run(self, text):
if self.options['allowed']:
# if middle word em is allowed, do nothing. This extra's only use is to prevent them
return text

# run strong and whatnot first
# this also will process all strict ems
text = super().run(text)
Expand Down
19 changes: 19 additions & 0 deletions test/tm-cases/middle_word_em_with_extra_ems.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p>Double variants:</p>

<p><strong>one_two_three</strong></p>

<p><strong><em>one_two_three</em></strong></p>

<p><em><strong>one_two_three</strong></em></p>

<p><strong><em>one_two_three</em></strong></p>

<p>Single variants:</p>

<p><em>one_two_three</em></p>

<p><em>one_two_three</em></p>

<p><em>one*two*three</em></p>

<p><em>one<em>two</em>three</em></p>
1 change: 1 addition & 0 deletions test/tm-cases/middle_word_em_with_extra_ems.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": {"middle-word-em": False}}
19 changes: 19 additions & 0 deletions test/tm-cases/middle_word_em_with_extra_ems.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Double variants:

**one_two_three**

***one_two_three***

_**one_two_three**_

**_one_two_three_**

Single variants:

*one_two_three*

_one_two_three_

_one*two*three_

*one*two*three*

0 comments on commit 1d37310

Please sign in to comment.