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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## python-markdown2 2.5.5 (not yet released)

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


## python-markdown2 2.5.4
Expand Down
26 changes: 17 additions & 9 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2960,20 +2960,28 @@ class CodeFriendly(ItalicAndBoldProcessor):
'''
name = 'code-friendly'

def __init__(self, md, options):
super().__init__(md, options)

# 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 sub(self, match: re.Match) -> str:
syntax = match.group(1)
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
# if using _this_ syntax, hash it to avoid processing, but don't hash the contents incase of nested syntax
text = text.replace(syntax, _hash_text(self.name + syntax))
return text
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 the text within the bold/em markers contains '_' then hash those chars to protect them from em_re
text = (
text[len(syntax): -len(syntax)]
.replace('__', _hash_text(self.name + '__'))
.replace('_', _hash_text(self.name + '_'))
)
return syntax + text + syntax
# if no underscores are present, the text is fine and we can just leave it alone
return super().sub(match)

Expand Down
1 change: 1 addition & 0 deletions test/tm-cases/code_friendly_issue638.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>a_b this should be <strong>bold</strong> c_d this is <strong>bold</strong></p>
1 change: 1 addition & 0 deletions test/tm-cases/code_friendly_issue638.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_issue638.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a_b this should be **bold** c_d this is **bold**
Loading