Skip to content

Commit c35e704

Browse files
Merge pull request #590 from Crozzers/code-friendly-bold-italic
Fix underscores within bold text getting emphasized (#589)
2 parents 2b0a14d + 3f0668e commit c35e704

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

CHANGES.md

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

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

5-
(nothing yet)
5+
- [pull 590] Fix underscores within bold text getting emphasized (#589)
66

77

88
## python-markdown2 2.5.0

lib/markdown2.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,9 +2643,12 @@ def run(self, text):
26432643
text = self.strong_re.sub(self.sub, text)
26442644
text = self.em_re.sub(self.sub, text)
26452645
else:
2646-
# put any hashed values back
2647-
for key, substr in self.hash_table.items():
2648-
text = text.replace(key, substr)
2646+
# push any hashed values back, using a while loop to deal with recursive hashes
2647+
orig_text = ''
2648+
while orig_text != text:
2649+
orig_text = text
2650+
for key, substr in self.hash_table.items():
2651+
text = text.replace(key, substr)
26492652
return text
26502653

26512654
@abstractmethod
@@ -2762,12 +2765,20 @@ class CodeFriendly(ItalicAndBoldProcessor):
27622765

27632766
def sub(self, match: re.Match) -> str:
27642767
syntax = match.group(1)
2765-
if '_' not in syntax:
2766-
return super().sub(match)
2767-
text = match.string[match.start(): match.end()]
2768-
key = _hash_text(text)
2769-
self.hash_table[key] = text
2770-
return key
2768+
text: str = match.string[match.start(): match.end()]
2769+
if '_' in syntax:
2770+
# if using _this_ syntax, hash the whole thing so that it doesn't get processed
2771+
key = _hash_text(text)
2772+
self.hash_table[key] = text
2773+
return key
2774+
elif '_' in text:
2775+
# if the text within the bold/em markers contains '_' then hash those contents to protect them from em_re
2776+
text = text[len(syntax): -len(syntax)]
2777+
key = _hash_text(text)
2778+
self.hash_table[key] = text
2779+
return syntax + key + syntax
2780+
# if no underscores are present, the text is fine and we can just leave it alone
2781+
return super().sub(match)
27712782

27722783

27732784
class FencedCodeBlocks(Extra):

test/tm-cases/code_friendly_bold.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><strong>bold_but_not_emphasized</strong></p>

test/tm-cases/code_friendly_bold.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["code-friendly"]}

test/tm-cases/code_friendly_bold.text

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**bold_but_not_emphasized**

0 commit comments

Comments
 (0)