aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2009-02-04 17:39:47 -0500
committerWaylan Limberg <waylan@gmail.com>2009-02-04 17:39:47 -0500
commit0f6ff94aed8c2f6529d2308f75a770f2b130bff9 (patch)
tree65e8e591b7d3e0dd40defd420afe584add539a11
parent0adb1136a124ba0218df0ef986e6f1dc6ed254ba (diff)
downloadmarkdown-0f6ff94aed8c2f6529d2308f75a770f2b130bff9.tar.gz
markdown-0f6ff94aed8c2f6529d2308f75a770f2b130bff9.tar.bz2
markdown-0f6ff94aed8c2f6529d2308f75a770f2b130bff9.zip
Completed nested lists and added a test. All tests pass.
-rw-r--r--markdown/blockprocessors.py23
-rw-r--r--tests/misc/nested-lists.html39
-rw-r--r--tests/misc/nested-lists.txt24
3 files changed, 81 insertions, 5 deletions
diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py
index bc5ddaa..79f4db9 100644
--- a/markdown/blockprocessors.py
+++ b/markdown/blockprocessors.py
@@ -135,7 +135,10 @@ class ListIndentProcessor(BlockProcessor):
if parent.tag in self.ITEM_TYPES:
# The parent is already a li. Just parse the child block.
self.parser.parseBlocks(parent, [block])
- elif len(sibling) and sibling[-1].tag in self.ITEM_TYPES :
+ elif sibling.tag in self.ITEM_TYPES:
+ # The sibling is a li. Use it as parent.
+ self.parser.parseBlocks(sibling, [block])
+ elif len(sibling) and sibling[-1].tag in self.ITEM_TYPES:
# The parent is a list (``ol`` or ``ul``) which has children.
# Assume the last child li is the parent of this block.
if sibling[-1].text:
@@ -154,18 +157,28 @@ class ListIndentProcessor(BlockProcessor):
def get_level(self, parent, block):
""" Get level of indent based on list level. """
+ # Get indent level
m = self.INDENT_RE.match(block)
if m:
indent_level = len(m.group(1))/markdown.TAB_LENGTH
else:
indent_level = 0
- level = 0
+ if self.parser.state.isstate('list'):
+ # We're in a tightlist - so we already are at correct parent.
+ level = 1
+ else:
+ # We're in a looselist - so we need to find parent.
+ level = 0
+ # Step through children of tree to find matching indent level.
while indent_level > level:
- parent = self.lastChild(parent)
- if parent:
- if parent.tag in self.LIST_TYPES:
+ child = self.lastChild(parent)
+ if child and (child.tag in self.LIST_TYPES or child.tag in self.ITEM_TYPES):
+ if child.tag in self.LIST_TYPES:
level += 1
+ parent = child
else:
+ # No more child levels. If we're short of indent_level,
+ # we have a code block. So we stop here.
break
return level, parent
diff --git a/tests/misc/nested-lists.html b/tests/misc/nested-lists.html
new file mode 100644
index 0000000..bb73784
--- /dev/null
+++ b/tests/misc/nested-lists.html
@@ -0,0 +1,39 @@
+<ul>
+<li>
+<p>item 1</p>
+<p>paragraph 2</p>
+</li>
+<li>
+<p>item 2</p>
+<ul>
+<li>item 2-1</li>
+<li>
+<p>item 2-2</p>
+<ul>
+<li>item 2-2-1</li>
+</ul>
+</li>
+<li>
+<p>item 2-3</p>
+<ul>
+<li>item 2-3-1</li>
+</ul>
+</li>
+</ul>
+</li>
+<li>
+<p>item 3</p>
+</li>
+</ul>
+<p>plain text</p>
+<ul>
+<li>item 1<ul>
+<li>item 1-1</li>
+<li>item 1-2<ul>
+<li>item 1-2-1</li>
+</ul>
+</li>
+</ul>
+</li>
+<li>item 2</li>
+</ul> \ No newline at end of file
diff --git a/tests/misc/nested-lists.txt b/tests/misc/nested-lists.txt
new file mode 100644
index 0000000..38aae15
--- /dev/null
+++ b/tests/misc/nested-lists.txt
@@ -0,0 +1,24 @@
+* item 1
+
+ paragraph 2
+
+* item 2
+
+ * item 2-1
+ * item 2-2
+
+ * item 2-2-1
+
+ * item 2-3
+
+ * item 2-3-1
+
+* item 3
+
+plain text
+
+* item 1
+ * item 1-1
+ * item 1-2
+ * item 1-2-1
+* item 2