aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2013-06-16 22:24:28 -0400
committerWaylan Limberg <waylan@gmail.com>2013-06-16 23:21:40 -0400
commitea4af0db29a86466e4b8b9e694299054565a29ca (patch)
treec890b6b8ee09cb073dcaee93266fb87df6c8983a
parentccc9c3ec580cb93aeed81b8320746c164a7acdb5 (diff)
downloadmarkdown-ea4af0db29a86466e4b8b9e694299054565a29ca.tar.gz
markdown-ea4af0db29a86466e4b8b9e694299054565a29ca.tar.bz2
markdown-ea4af0db29a86466e4b8b9e694299054565a29ca.zip
Attr_List Extension now support attr_lists on nested lists.
A list item with a nested list complicates were the attr_list can be. Fixes #218. Thanks for the report @jpzimmer.
-rw-r--r--markdown/extensions/attr_list.py29
-rw-r--r--tests/extensions/attr_list.html13
-rw-r--r--tests/extensions/attr_list.txt11
3 files changed, 51 insertions, 2 deletions
diff --git a/markdown/extensions/attr_list.py b/markdown/extensions/attr_list.py
index c98aa85..2bea213 100644
--- a/markdown/extensions/attr_list.py
+++ b/markdown/extensions/attr_list.py
@@ -77,13 +77,40 @@ class AttrListTreeprocessor(Treeprocessor):
def run(self, doc):
for elem in doc.getiterator():
+ #import pdb; pdb.set_trace()
if isBlockLevel(elem.tag):
# Block level: check for attrs on last line of text
RE = self.BLOCK_RE
if isheader(elem):
# header: check for attrs at end of line
RE = self.HEADER_RE
- if len(elem) and elem[-1].tail:
+ if len(elem) and elem.tag == 'li':
+ # special case list items. children may include a ul.
+ ul = None
+ # find the ul position
+ for i, child in enumerate(elem):
+ if child.tag == 'ul':
+ ul = i
+ break
+ if ul is None and elem[-1].tail:
+ # use tail of last child. no ul.
+ m = RE.search(elem[-1].tail)
+ if m:
+ self.assign_attrs(elem, m.group(1))
+ elem[-1].tail = elem[-1].tail[:m.start()]
+ if ul > 0 and elem[ul-1].tail:
+ # use tail of last child before ul
+ m = RE.search(elem[ul-1].tail)
+ if m:
+ self.assign_attrs(elem, m.group(1))
+ elem[ul-1].tail = elem[ul-1].tail[:m.start()]
+ elif elem.text:
+ # use text. ul is first child.
+ m = RE.search(elem.text)
+ if m:
+ self.assign_attrs(elem, m.group(1))
+ elem.text = elem.text[:m.start()]
+ elif len(elem) and elem[-1].tail:
# has children. Get from tail of last child
m = RE.search(elem[-1].tail)
if m:
diff --git a/tests/extensions/attr_list.html b/tests/extensions/attr_list.html
index f50cd6a..b1032f9 100644
--- a/tests/extensions/attr_list.html
+++ b/tests/extensions/attr_list.html
@@ -15,4 +15,15 @@ And a <strong class="nest">nested <a class="linky2" href="http://example.com" ti
</code></pre>
<h3 id="hash3">No colon for compatability with Headerid ext</h3>
<p id="the_end">Also a codespan: <code class="foo">{: .someclass}</code>.</p>
-<h3 _:="{:" id="hash5">Bad Syntax</h3> \ No newline at end of file
+<h3 _:="{:" id="hash5">Bad Syntax</h3>
+<ul>
+<li class="item">Item1</li>
+<li class="item">Item2<ul>
+<li class="subitem">Item2-1</li>
+</ul>
+</li>
+<li class="item"><em class="emph">Item3</em><ul>
+<li class="subitem"><em class="emph">Item3-1</em></li>
+</ul>
+</li>
+</ul> \ No newline at end of file
diff --git a/tests/extensions/attr_list.txt b/tests/extensions/attr_list.txt
index cd7f398..959c117 100644
--- a/tests/extensions/attr_list.txt
+++ b/tests/extensions/attr_list.txt
@@ -34,3 +34,14 @@ Also a codespan: `{: .someclass}`{: .foo}.
{: #the_end}
### Bad Syntax { {: #hash5 }
+
+* Item1
+ {: .item }
+* Item2
+ {: .item }
+ * Item2-1
+ {: .subitem }
+* _Item3_{: .emph }
+ {: .item }
+ * _Item3-1_{: .emph }
+ {: .subitem }