Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## python-markdown2 2.5.5 (not yet released)

(nothing yet)
- [pull #639] Fix middle-word-em interfering with strongs (#637)


## python-markdown2 2.5.4
Expand Down
52 changes: 26 additions & 26 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3309,42 +3309,42 @@ def __init__(self, md: Markdown, options: Union[dict, bool, None]):
options.setdefault('allowed', True)
super().__init__(md, options)

self.liberal_em_re = self.em_re
if not options['allowed']:
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)
self.middle_word_em_re = re.compile(
r'''
(?<!^) # To be middle of a word, it cannot be at the start of the input
(?<![*_\s]) # cannot be preceeded by em character or whitespace (must be in middle of word)
([*_]) # em char
(?=\S) # must be followed by non-whitespace char
(?![*_]|$|\W) # cannot be followed by another em char, EOF or a non-word char
''', re.X | re.M
)

# add a prefix to it so we don't interfere with escaped/hashed chars from other stages
self.hash_table['_'] = _hash_text(self.name + '_')
self.hash_table['*'] = _hash_text(self.name + '*')

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)
# hash all em chars in the middle of words to prevent em_re from picking up on them
if self.md.order < self.md.stage:
# hash all non-valid ems
text = self.liberal_em_re.sub(self.sub_hash, text)
text = self.middle_word_em_re.sub(self.sub, text)

# put all the em chars back
if self.md.order > self.md.stage:
text = text.replace(self.hash_table['_'], '_')
text = text.replace(self.hash_table['*'], '*')

return text

def sub(self, match: re.Match) -> str:
syntax = match.group(1)
if len(syntax) != 1:
# strong syntax
def sub(self, match: re.Match):
if match.re != self.middle_word_em_re:
return super().sub(match)
return '<em>%s</em>' % match.group(2)

syntax = match.group(1)
return self.hash_table[syntax]


class Numbering(Extra):
Expand Down
2 changes: 2 additions & 0 deletions test/tm-cases/middle_word_em_issue637.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Visit <a href="https://github.com"><strong>GitHub</strong></a> for code repositories and
<a href="https://stackoverflow.com"><strong>Stack Overflow</strong></a> for programming help.</p>
1 change: 1 addition & 0 deletions test/tm-cases/middle_word_em_issue637.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'extras': {'middle-word-em': False}}
2 changes: 2 additions & 0 deletions test/tm-cases/middle_word_em_issue637.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Visit [**GitHub**](https://github.com) for code repositories and
[**Stack Overflow**](https://stackoverflow.com) for programming help.
2 changes: 1 addition & 1 deletion test/tm-cases/middle_word_em_with_extra_ems.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

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

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