@@ -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
33583358class Numbering (Extra ):
0 commit comments