Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix underscores within bold text getting emphasized (#589) #590

Merged
merged 3 commits into from
Jul 24, 2024
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.1 (not yet released)

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


## python-markdown2 2.5.0
Expand Down
29 changes: 20 additions & 9 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2643,9 +2643,12 @@ def run(self, text):
text = self.strong_re.sub(self.sub, text)
text = self.em_re.sub(self.sub, text)
else:
# put any hashed values back
for key, substr in self.hash_table.items():
text = text.replace(key, substr)
# push any hashed values back, using a while loop to deal with recursive hashes
orig_text = ''
while orig_text != text:
orig_text = text
for key, substr in self.hash_table.items():
text = text.replace(key, substr)
return text

@abstractmethod
Expand Down Expand Up @@ -2762,12 +2765,20 @@ class CodeFriendly(ItalicAndBoldProcessor):

def sub(self, match: re.Match) -> str:
syntax = match.group(1)
if '_' not in syntax:
return super().sub(match)
text = match.string[match.start(): match.end()]
key = _hash_text(text)
self.hash_table[key] = text
return key
text: str = match.string[match.start(): match.end()]
if '_' in syntax:
# if using _this_ syntax, hash the whole thing so that it doesn't get processed
key = _hash_text(text)
self.hash_table[key] = text
return key
elif '_' in text:
# if the text within the bold/em markers contains '_' then hash those contents to protect them from em_re
text = text[len(syntax): -len(syntax)]
key = _hash_text(text)
self.hash_table[key] = text
return syntax + key + syntax
# if no underscores are present, the text is fine and we can just leave it alone
return super().sub(match)


class FencedCodeBlocks(Extra):
Expand Down
1 change: 1 addition & 0 deletions test/tm-cases/code_friendly_bold.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><strong>bold_but_not_emphasized</strong></p>
1 change: 1 addition & 0 deletions test/tm-cases/code_friendly_bold.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["code-friendly"]}
1 change: 1 addition & 0 deletions test/tm-cases/code_friendly_bold.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**bold_but_not_emphasized**
Loading