Skip to content

Commit 0308985

Browse files
Merge branch 'master' into fix-code-friendly-issue638
2 parents ea0c337 + f44849c commit 0308985

File tree

6 files changed

+33
-27
lines changed

6 files changed

+33
-27
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## python-markdown2 2.5.5 (not yet released)
44

5+
- [pull #639] Fix middle-word-em interfering with strongs (#637)
56
- [pull #640] Fix code friendly extra stopping other syntax being processed (#638)
67

78

lib/markdown2.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,42 +3317,42 @@ def __init__(self, md: Markdown, options: Union[dict, bool, None]):
33173317
options.setdefault('allowed', True)
33183318
super().__init__(md, options)
33193319

3320-
self.liberal_em_re = self.em_re
3321-
if not options['allowed']:
3322-
self.em_re = re.compile(r'(?<=\b)%s(?=\b)' % self.em_re.pattern, self.em_re.flags)
3323-
self.liberal_em_re = re.compile(
3324-
r'''
3325-
( # \1 - must be a single em char in the middle of a word
3326-
(?<![*_\s]) # cannot be preceeded by em character or whitespace (must be in middle of word)
3327-
[*_] # em character
3328-
(?![*_]) # cannot be followed by another em char
3329-
)
3330-
(?=\S) # em opening must be followed by non-whitespace text
3331-
(.*?\S) # the emphasized text
3332-
\1 # closing char
3333-
(?!\s|$) # must not be followed by whitespace (middle of word) or EOF
3334-
'''
3335-
, re.S | re.X)
3320+
self.middle_word_em_re = re.compile(
3321+
r'''
3322+
(?<!^) # To be middle of a word, it cannot be at the start of the input
3323+
(?<![*_\s]) # cannot be preceeded by em character or whitespace (must be in middle of word)
3324+
([*_]) # em char
3325+
(?=\S) # must be followed by non-whitespace char
3326+
(?![*_]|$|\W) # cannot be followed by another em char, EOF or a non-word char
3327+
''', re.X | re.M
3328+
)
3329+
3330+
# add a prefix to it so we don't interfere with escaped/hashed chars from other stages
3331+
self.hash_table['_'] = _hash_text(self.name + '_')
3332+
self.hash_table['*'] = _hash_text(self.name + '*')
33363333

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

3342-
# run strong and whatnot first
3343-
# this also will process all strict ems
3344-
text = super().run(text)
3339+
# hash all em chars in the middle of words to prevent em_re from picking up on them
33453340
if self.md.order < self.md.stage:
3346-
# hash all non-valid ems
3347-
text = self.liberal_em_re.sub(self.sub_hash, text)
3341+
text = self.middle_word_em_re.sub(self.sub, text)
3342+
3343+
# put all the em chars back
3344+
if self.md.order > self.md.stage:
3345+
text = text.replace(self.hash_table['_'], '_')
3346+
text = text.replace(self.hash_table['*'], '*')
3347+
33483348
return text
33493349

3350-
def sub(self, match: re.Match) -> str:
3351-
syntax = match.group(1)
3352-
if len(syntax) != 1:
3353-
# strong syntax
3350+
def sub(self, match: re.Match):
3351+
if match.re != self.middle_word_em_re:
33543352
return super().sub(match)
3355-
return '<em>%s</em>' % match.group(2)
3353+
3354+
syntax = match.group(1)
3355+
return self.hash_table[syntax]
33563356

33573357

33583358
class Numbering(Extra):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Visit <a href="https://github.com"><strong>GitHub</strong></a> for code repositories and
2+
<a href="https://stackoverflow.com"><strong>Stack Overflow</strong></a> for programming help.</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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Visit [**GitHub**](https://github.com) for code repositories and
2+
[**Stack Overflow**](https://stackoverflow.com) for programming help.

test/tm-cases/middle_word_em_with_extra_ems.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616

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

19-
<p><em>one<em>two</em>three</em></p>
19+
<p><em>one*two*three</em></p>

0 commit comments

Comments
 (0)