From ea4af0db29a86466e4b8b9e694299054565a29ca Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sun, 16 Jun 2013 22:24:28 -0400 Subject: 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. --- markdown/extensions/attr_list.py | 29 ++++++++++++++++++++++++++++- tests/extensions/attr_list.html | 13 ++++++++++++- tests/extensions/attr_list.txt | 11 +++++++++++ 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 nested

No colon for compatability with Headerid ext

Also a codespan: {: .someclass}.

-

Bad Syntax

\ No newline at end of file +

Bad Syntax

+
    +
  • Item1
  • +
  • Item2
      +
    • Item2-1
    • +
    +
  • +
  • Item3
      +
    • Item3-1
    • +
    +
  • +
\ 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 } -- cgit v1.2.3