Skip to content

Commit 884d25b

Browse files
author
Waylan Limberg
committed
Partial fix for #253.
This actually fixes the immediate problem. The method on the inlinePattern returns the correct subtree. However, for some reason, the parent element of that subtree is later stripped of all content (perhaps by the inlineplaceholder stuff). So, for example, given this input: ***foo** bar* The inlinepattern returns: <em><strong>foo</strong> bar</em> However, later it is reduced to: <em></em> Interestingly, this still works: ***foo*** Correctly becomes: <strong><em>foo</em></strong> I'm guessing that the tail on the `em` tag in the first instance is tripping things up some how.
1 parent 62e5485 commit 884d25b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

markdown/inlinepatterns.py

Lines changed: 16 additions & 6 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["strong_em"] = DoubleTagPattern(A_STRONG_EM_RE, 'strong,em')
79+
inlinePatterns["strong_em_"] = DoubleTagPattern(U_STRONG_EM_RE, 'strong,em')
7980
inlinePatterns["strong"] = SimpleTagPattern(STRONG_RE, 'strong')
8081
inlinePatterns["emphasis"] = SimpleTagPattern(EMPHASIS_RE, 'em')
8182
if md_instance.smart_emphasis:
@@ -99,8 +100,9 @@ def build_inlinepatterns(md_instance, **kwargs):
99100
BACKTICK_RE = r'(?<!\\)(`+)(.+?)(?<!`)\2(?!`)' # `e=f()` or ``e=f("`")``
100101
ESCAPE_RE = r'\\(.)' # \<
101102
EMPHASIS_RE = r'(\*)([^\*]+)\2' # *emphasis*
102-
STRONG_RE = r'(\*{2}|_{2})(.+?)\2' # **strong**
103-
STRONG_EM_RE = r'(\*{3}|_{3})(.+?)\2' # ***strong***
103+
STRONG_RE = r'(\*{2}|_{2})(.+?)\2' # **strong**
104+
A_STRONG_EM_RE = r'\*{3}(.+?)\*{2}([^*]*)\*' # ***strongem***
105+
U_STRONG_EM_RE = r'_{3}(.+?)_{2}([^_]*)_' # ___strongem___
104106
SMART_EMPHASIS_RE = r'(?<!\w)(_)(?!_)(.+?)(?<!_)\2(?!\w)' # _smart_emphasis_
105107
EMPHASIS_2_RE = r'(_)(.+?)\2' # _emphasis_
106108
LINK_RE = NOIMG + BRK + \
@@ -276,9 +278,17 @@ class DoubleTagPattern(SimpleTagPattern):
276278
"""
277279
def handleMatch(self, m):
278280
tag1, tag2 = self.tag.split(",")
279-
el1 = util.etree.Element(tag1)
280-
el2 = util.etree.SubElement(el1, tag2)
281-
el2.text = m.group(3)
281+
if m.group(3):
282+
# This is a match for `***foo** bar*`. Group 3 contains ` bar`.
283+
el1 = util.etree.Element(tag2)
284+
el2 = util.etree.SubElement(el1, tag1)
285+
el2.text = m.group(2)
286+
el2.tail = m.group(3)
287+
else:
288+
# This is a match for `***foo***`. We don't switch tag order here.
289+
el1 = util.etree.Element(tag1)
290+
el2 = util.etree.SubElement(el1, tag2)
291+
el2.text = m.group(2)
282292
return el1
283293

284294

0 commit comments

Comments
 (0)