From 66ba06fa2ded87dc9042e0cd51da26f99ece8717 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Sun, 3 Apr 2011 20:38:59 +0530 Subject: custom index support for ol tag A ol tag with custom start index can be defined by using a different integer as the first item numbering. Search for `3. Bird` on: http://daringfireball.net/projects/markdown/syntax --- markdown/blockprocessors.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 77fbc71..f61aecd 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -300,6 +300,11 @@ class OListProcessor(BlockProcessor): CHILD_RE = re.compile(r'^[ ]{0,3}((\d+\.)|[*+-])[ ]+(.*)') # Detect indented (nested) items of either type INDENT_RE = re.compile(r'^[ ]{4,7}((\d+\.)|[*+-])[ ]+.*') + # The integer with which the lists starts (None means start with 0) + # Eg: If list is intialized as) + # 3. Item + # The ol tag will get starts="3" attribute + STARTSWITH = None def test(self, parent, block): return bool(self.RE.match(block)) @@ -339,6 +344,10 @@ class OListProcessor(BlockProcessor): else: # This is a new list so create parent with appropriate tag. lst = util.etree.SubElement(parent, self.TAG) + # Check if a custom start integer is set + if self.STARTSWITH: + lst.attrib['start'] = self.STARTSWITH + self.parser.state.set('list') # Loop through items in block, recursively parsing each with the # appropriate parent. @@ -358,7 +367,14 @@ class OListProcessor(BlockProcessor): for line in block.split('\n'): m = self.CHILD_RE.match(line) if m: - # This is a new item. Append + # Check first item for the start index + if not items and self.TAG=='ol': + # Detect the integer value of first list item + INTEGER_RE = re.compile('(\d+)') + start_integer = INTEGER_RE.match(m.group(1)).group() + if start_integer!='1': + self.STARTSWITH = start_integer + # Append to the list items.append(m.group(3)) elif self.INDENT_RE.match(line): # This is an indented (possibly nested) item. -- cgit v1.2.3 From f1a2d3763115c41a4f0159e2f98aa76330cc17e5 Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Sun, 3 Apr 2011 20:48:42 +0530 Subject: add new list item comment --- markdown/blockprocessors.py | 1 + 1 file changed, 1 insertion(+) diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index f61aecd..13bc477 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -367,6 +367,7 @@ class OListProcessor(BlockProcessor): for line in block.split('\n'): m = self.CHILD_RE.match(line) if m: + # This is a new list item # Check first item for the start index if not items and self.TAG=='ol': # Detect the integer value of first list item -- cgit v1.2.3 From c385e5fc0a9fe7517b4afdb5b3f9c174ae0766de Mon Sep 17 00:00:00 2001 From: Rohan Jain Date: Tue, 5 Apr 2011 14:59:30 +0530 Subject: fixed startindex reset in multiple ul Now the startindex would be reset if continual unordered lists are present (tests are passed). --- markdown/blockprocessors.py | 10 ++++------ tests/misc/some-test.html | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 13bc477..64b9e09 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -300,11 +300,11 @@ class OListProcessor(BlockProcessor): CHILD_RE = re.compile(r'^[ ]{0,3}((\d+\.)|[*+-])[ ]+(.*)') # Detect indented (nested) items of either type INDENT_RE = re.compile(r'^[ ]{4,7}((\d+\.)|[*+-])[ ]+.*') - # The integer with which the lists starts (None means start with 0) + # The integer (python string) with which the lists starts (default=1) # Eg: If list is intialized as) # 3. Item # The ol tag will get starts="3" attribute - STARTSWITH = None + STARTSWITH = '1' def test(self, parent, block): return bool(self.RE.match(block)) @@ -345,7 +345,7 @@ class OListProcessor(BlockProcessor): # This is a new list so create parent with appropriate tag. lst = util.etree.SubElement(parent, self.TAG) # Check if a custom start integer is set - if self.STARTSWITH: + if self.STARTSWITH !='1': lst.attrib['start'] = self.STARTSWITH self.parser.state.set('list') @@ -372,9 +372,7 @@ class OListProcessor(BlockProcessor): if not items and self.TAG=='ol': # Detect the integer value of first list item INTEGER_RE = re.compile('(\d+)') - start_integer = INTEGER_RE.match(m.group(1)).group() - if start_integer!='1': - self.STARTSWITH = start_integer + self.STARTSWITH = INTEGER_RE.match(m.group(1)).group() # Append to the list items.append(m.group(3)) elif self.INDENT_RE.match(line): diff --git a/tests/misc/some-test.html b/tests/misc/some-test.html index b78683f..92e7262 100644 --- a/tests/misc/some-test.html +++ b/tests/misc/some-test.html @@ -51,7 +51,7 @@ ok with a bunch of items
  • Mostly fruits

    -
      +
      1. Apple
      2. Pare
      @@ -65,4 +65,4 @@ Another code example * Lists and similar stuff > Should be ignored - \ No newline at end of file + -- cgit v1.2.3