aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan.limberg@icloud.com>2014-11-17 22:43:23 -0500
committerWaylan Limberg <waylan.limberg@icloud.com>2014-11-17 22:43:23 -0500
commited0ff55087235db8a3f02e10d6a1ed603ef4c476 (patch)
tree88a494362cdc19ed1687c67b6b5f7c9dacbcbf51
parentf0357b28ef1723929146eabe6571d7c436c90c34 (diff)
parent69fd8b4871f3c027b78072af88bb8a62202bc7e7 (diff)
downloadmarkdown-ed0ff55087235db8a3f02e10d6a1ed603ef4c476.tar.gz
markdown-ed0ff55087235db8a3f02e10d6a1ed603ef4c476.tar.bz2
markdown-ed0ff55087235db8a3f02e10d6a1ed603ef4c476.zip
Merge pull request #367 from facelessuser/master
Fix issue #365 and #366
-rw-r--r--markdown/extensions/toc.py95
-rw-r--r--markdown/inlinepatterns.py4
-rw-r--r--tests/misc/nested-patterns.html4
-rw-r--r--tests/misc/nested-patterns.txt4
4 files changed, 56 insertions, 51 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py
index d21ea96..f7fb675 100644
--- a/markdown/extensions/toc.py
+++ b/markdown/extensions/toc.py
@@ -27,60 +27,59 @@ def order_toc_list(toc_list):
[{'level': 1}, {'level': 2}]
=>
[{'level': 1, 'children': [{'level': 2, 'children': []}]}]
-
+
A wrong list is also converted:
[{'level': 2}, {'level': 1}]
=>
[{'level': 2, 'children': []}, {'level': 1, 'children': []}]
"""
-
- def build_correct(remaining_list, prev_elements=[{'level': 1000}]):
-
- if not remaining_list:
- return [], []
-
- current = remaining_list.pop(0)
- if not 'children' in current.keys():
- current['children'] = []
-
- if not prev_elements:
- # This happens for instance with [8, 1, 1], ie. when some
- # header level is outside a scope. We treat it as a
- # top-level
- next_elements, children = build_correct(remaining_list, [current])
- current['children'].append(children)
- return [current] + next_elements, []
-
- prev_element = prev_elements.pop()
- children = []
- next_elements = []
- # Is current part of the child list or next list?
- if current['level'] > prev_element['level']:
- #print "%d is a child of %d" % (current['level'], prev_element['level'])
- prev_elements.append(prev_element)
- prev_elements.append(current)
- prev_element['children'].append(current)
- next_elements2, children2 = build_correct(remaining_list, prev_elements)
- children += children2
- next_elements += next_elements2
- else:
- #print "%d is ancestor of %d" % (current['level'], prev_element['level'])
- if not prev_elements:
- #print "No previous elements, so appending to the next set"
- next_elements.append(current)
- prev_elements = [current]
- next_elements2, children2 = build_correct(remaining_list, prev_elements)
- current['children'].extend(children2)
+
+ ordered_list = []
+ if len(toc_list):
+ # Initialize everything by processing the first entry
+ last = toc_list.pop(0)
+ last['children'] = []
+ levels = [last['level']]
+ ordered_list.append(last)
+ parents = []
+
+ # Walk the rest nesting the entries properly
+ while toc_list:
+ t = toc_list.pop(0)
+ current_level = t['level']
+ t['children'] = []
+
+ # Reduce depth if current level < last item's level
+ if current_level < levels[-1]:
+ # Pop last level since we know we are less than it
+ levels.pop()
+
+ # Pop parents and levels we are less than or equal to
+ to_pop = 0
+ for p in reversed(parents):
+ if current_level <= p['level']:
+ to_pop += 1
+ else:
+ break
+ if to_pop:
+ levels = levels[:-to_pop]
+ parents = parents[:-to_pop]
+
+ # Note current level as last
+ levels.append(current_level)
+
+ # Level is the same, so append to the current parent (if available)
+ if current_level == levels[-1]:
+ (parents[-1]['children'] if parents else ordered_list).append(t)
+
+ # Current level is > last item's level,
+ # So make last item a parent and append current as child
else:
- #print "Previous elements, comparing to those first"
- remaining_list.insert(0, current)
- next_elements2, children2 = build_correct(remaining_list, prev_elements)
- children.extend(children2)
- next_elements += next_elements2
-
- return next_elements, children
-
- ordered_list, __ = build_correct(toc_list)
+ last['children'].append(t)
+ parents.append(last)
+ levels.append(current_level)
+ last = t
+
return ordered_list
diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py
index b63bc8c..c9d82fd 100644
--- a/markdown/inlinepatterns.py
+++ b/markdown/inlinepatterns.py
@@ -101,8 +101,8 @@ BACKTICK_RE = r'(?<!\\)(`+)(.+?)(?<!`)\2(?!`)' # `e=f()` or ``e=f("`")``
ESCAPE_RE = r'\\(.)' # \<
EMPHASIS_RE = r'(\*)([^\*]+)\2' # *emphasis*
STRONG_RE = r'(\*{2}|_{2})(.+?)\2' # **strong**
-EM_STRONG_RE = r'(\*|_){3}(.+?)\2(.*?)\2{2}' # ***strongem*** or ***em*strong**
-STRONG_EM_RE = r'(\*|_){3}(.+?)\2{2}(.*?)\2' # ***strong**em*
+EM_STRONG_RE = r'(\*|_)\2{2}(.+?)\2(.*?)\2{2}' # ***strongem*** or ***em*strong**
+STRONG_EM_RE = r'(\*|_)\2{2}(.+?)\2{2}(.*?)\2' # ***strong**em*
SMART_EMPHASIS_RE = r'(?<!\w)(_)(?!_)(.+?)(?<!_)\2(?!\w)' # _smart_emphasis_
EMPHASIS_2_RE = r'(_)(.+?)\2' # _emphasis_
LINK_RE = NOIMG + BRK + \
diff --git a/tests/misc/nested-patterns.html b/tests/misc/nested-patterns.html
index 8c46a58..1c7bb43 100644
--- a/tests/misc/nested-patterns.html
+++ b/tests/misc/nested-patterns.html
@@ -5,4 +5,6 @@
<strong><a href="http://example.com"><em>link</em></a></strong>
<strong><a href="http://example.com"><em>link</em></a></strong>
<a href="http://example.com"><strong><em>link</em></strong></a></p>
-<p><strong><em>I am <strong><em>italic</em> and</strong> bold</em> I am <code>just</code> bold</strong></p> \ No newline at end of file
+<p><strong><em>I am <strong><em>italic</em> and</strong> bold</em> I am <code>just</code> bold</strong></p>
+<p>Example <strong><em>bold italic</em></strong> on the same line <strong><em>bold italic</em></strong>.</p>
+<p>Example <strong><em>bold italic</em></strong> on the same line <strong><em>bold italic</em></strong>.</p> \ No newline at end of file
diff --git a/tests/misc/nested-patterns.txt b/tests/misc/nested-patterns.txt
index e347ccf..9032cf1 100644
--- a/tests/misc/nested-patterns.txt
+++ b/tests/misc/nested-patterns.txt
@@ -7,3 +7,7 @@ __[*link*](http://example.com)__
[***link***](http://example.com)
***I am ___italic_ and__ bold* I am `just` bold**
+
+Example __*bold italic*__ on the same line __*bold italic*__.
+
+Example **_bold italic_** on the same line **_bold italic_**.