Skip to content

Commit 8358bb3

Browse files
committed
Better nested STRONG EM support.
Fixes #253. Thanks to @facelessuser for the tests. Although I removed a bunch of weird ones (even some that passed) from his PR (#342). For the most part, there is no definitive way for those to be parsed. So there is no point of testing for them. In most of those situations, authors should be mixing underscores and astericks so it is clear what is intended.
1 parent 9082ed4 commit 8358bb3

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

markdown/inlinepatterns.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def build_inlinepatterns(md_instance, **kwargs):
7575
inlinePatterns["html"] = HtmlPattern(HTML_RE, md_instance)
7676
inlinePatterns["entity"] = HtmlPattern(ENTITY_RE, md_instance)
7777
inlinePatterns["not_strong"] = SimpleTextPattern(NOT_STRONG_RE)
78-
inlinePatterns["strong_em"] = DoubleTagPattern(STRONG_EM_RE, 'strong,em')
78+
inlinePatterns["em_strong"] = DoubleTagPattern(EM_STRONG_RE, 'strong,em')
79+
inlinePatterns["strong_em"] = DoubleTagPattern(STRONG_EM_RE, 'em,strong')
7980
inlinePatterns["strong"] = SimpleTagPattern(STRONG_RE, 'strong')
8081
inlinePatterns["emphasis"] = SimpleTagPattern(EMPHASIS_RE, 'em')
8182
if md_instance.smart_emphasis:
@@ -100,7 +101,8 @@ def build_inlinepatterns(md_instance, **kwargs):
100101
ESCAPE_RE = r'\\(.)' # \<
101102
EMPHASIS_RE = r'(\*)([^\*]+)\2' # *emphasis*
102103
STRONG_RE = r'(\*{2}|_{2})(.+?)\2' # **strong**
103-
STRONG_EM_RE = r'(\*{3}|_{3})(.+?)\2' # ***strong***
104+
EM_STRONG_RE = r'(\*|_){3}(.+?)\2(.*?)\2{2}' # ***strongem*** or ***em*strong**
105+
STRONG_EM_RE = r'(\*|_){3}(.+?)\2{2}(.*?)\2' # ***strong**em*
104106
SMART_EMPHASIS_RE = r'(?<!\w)(_)(?!_)(.+?)(?<!_)\2(?!\w)' # _smart_emphasis_
105107
EMPHASIS_2_RE = r'(_)(.+?)\2' # _emphasis_
106108
LINK_RE = NOIMG + BRK + \
@@ -276,6 +278,8 @@ def handleMatch(self, m):
276278
el1 = util.etree.Element(tag1)
277279
el2 = util.etree.SubElement(el1, tag2)
278280
el2.text = m.group(3)
281+
if len(m.groups())==5:
282+
el2.tail = m.group(4)
279283
return el1
280284

281285

tests/misc/em_strong_complex.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p><em><strong>test test</strong> test test</em></p>
2+
<p><strong><em>test test</em> test test</strong></p>
3+
<p><strong><em>test</em></strong></p>
4+
<p><strong>test</strong>_</p>
5+
<p><strong><em>test</em> test</strong>_</p>
6+
<p><strong><em>test</em> test</strong></p>
7+
<p><em>test_test test_test</em></p>
8+
<p><em><strong>test test</strong> test test</em></p>
9+
<p><strong><em>test test</em> test test</strong></p>
10+
<p>*<em>test</em></p>
11+
<p><strong><em>test</em></strong></p>
12+
<p><strong>test</strong>*</p>
13+
<p><strong><em>test</em> test</strong></p>
14+
<p><em>test</em>test test<em>test</em></p>

tests/misc/em_strong_complex.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
___test test__ test test_
2+
3+
___test test_ test test__
4+
5+
___test___
6+
7+
__test___
8+
9+
___test_ test___
10+
11+
___test_ test__
12+
13+
_test_test test_test_
14+
15+
***test test** test test*
16+
17+
***test test* test test**
18+
19+
**test*
20+
21+
***test***
22+
23+
**test***
24+
25+
***test* test**
26+
27+
*test*test test*test*

0 commit comments

Comments
 (0)