Skip to content

Commit 8d50892

Browse files
Merge pull request #640 from Crozzers/fix-code-friendly-issue638
Fix code friendly preventing other syntax from being processed (#638)
2 parents f44849c + 0308985 commit 8d50892

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## python-markdown2 2.5.5 (not yet released)
44

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

78

89
## python-markdown2 2.5.4

lib/markdown2.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,20 +2960,28 @@ class CodeFriendly(ItalicAndBoldProcessor):
29602960
'''
29612961
name = 'code-friendly'
29622962

2963+
def __init__(self, md, options):
2964+
super().__init__(md, options)
2965+
2966+
# add a prefix to it so we don't interfere with escaped/hashed chars from other stages
2967+
self.hash_table[_hash_text(self.name + '_')] = '_'
2968+
self.hash_table[_hash_text(self.name + '__')] = '__'
2969+
29632970
def sub(self, match: re.Match) -> str:
29642971
syntax = match.group(1)
29652972
text: str = match.string[match.start(): match.end()]
29662973
if '_' in syntax:
2967-
# if using _this_ syntax, hash the whole thing so that it doesn't get processed
2968-
key = _hash_text(text)
2969-
self.hash_table[key] = text
2970-
return key
2974+
# if using _this_ syntax, hash it to avoid processing, but don't hash the contents incase of nested syntax
2975+
text = text.replace(syntax, _hash_text(self.name + syntax))
2976+
return text
29712977
elif '_' in text:
2972-
# if the text within the bold/em markers contains '_' then hash those contents to protect them from em_re
2973-
text = text[len(syntax): -len(syntax)]
2974-
key = _hash_text(text)
2975-
self.hash_table[key] = text
2976-
return syntax + key + syntax
2978+
# if the text within the bold/em markers contains '_' then hash those chars to protect them from em_re
2979+
text = (
2980+
text[len(syntax): -len(syntax)]
2981+
.replace('__', _hash_text(self.name + '__'))
2982+
.replace('_', _hash_text(self.name + '_'))
2983+
)
2984+
return syntax + text + syntax
29772985
# if no underscores are present, the text is fine and we can just leave it alone
29782986
return super().sub(match)
29792987

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>a_b this should be <strong>bold</strong> c_d this is <strong>bold</strong></p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras":["code-friendly"]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a_b this should be **bold** c_d this is **bold**

0 commit comments

Comments
 (0)