From 85ac70859d5c995058d4509179bc484b6cd9904c Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 16 Dec 2009 07:21:33 +0000 Subject: [PATCH] Implement issue 33: "cuddled-lists" extra. Still have doc and version updates to come. --- lib/markdown2.py | 40 ++++++++++++++++++------ test/tm-cases/cuddled_para_and_list.html | 25 +++++++++++++++ test/tm-cases/cuddled_para_and_list.tags | 2 +- test/tm-cases/cuddled_para_and_list.text | 20 ++++++++++++ 4 files changed, 76 insertions(+), 11 deletions(-) diff --git a/lib/markdown2.py b/lib/markdown2.py index 46778060..1ad6736d 100755 --- a/lib/markdown2.py +++ b/lib/markdown2.py @@ -1129,12 +1129,12 @@ def _do_lists(self, text): return text _list_item_re = re.compile(r''' - (\n)? # leading line = \1 - (^[ \t]*) # leading whitespace = \2 - (%s) [ \t]+ # list marker = \3 - ((?:.+?) # list item text = \4 - (\n{1,2})) # eols = \5 - (?= \n* (\Z | \2 (%s) [ \t]+)) + (\n)? # leading line = \1 + (^[ \t]*) # leading whitespace = \2 + (?P%s) [ \t]+ # list marker = \3 + ((?:.+?) # list item text = \4 + (\n{1,2})) # eols = \5 + (?= \n* (\Z | \2 (?P%s) [ \t]+)) ''' % (_marker_any, _marker_any), re.M | re.X | re.S) @@ -1380,15 +1380,35 @@ def _form_paragraphs(self, text): text = text.strip('\n') # Wrap

tags. - grafs = re.split(r"\n{2,}", text) - for i, graf in enumerate(grafs): + grafs = [] + for i, graf in enumerate(re.split(r"\n{2,}", text)): if graf in self.html_blocks: # Unhashify HTML blocks - grafs[i] = self.html_blocks[graf] + grafs.append(self.html_blocks[graf]) else: + cuddled_list = None + if "cuddled-lists" in self.extras: + # Need to put back trailing '\n' for `_list_item_re` + # match at the end of the paragraph. + li = self._list_item_re.search(graf + '\n') + # Two of the same list marker in this paragraph: a likely + # candidate for a list cuddled to preceding paragraph + # text (issue 33). Note the `[-1]` is a quick way to + # consider numeric bullets (e.g. "1." and "2.") to be + # equal. + if (li and li.group("next_marker") + and li.group("marker")[-1] == li.group("next_marker")[-1]): + start = li.start() + cuddled_list = self._do_lists(graf[start:]).rstrip("\n") + assert cuddled_list.startswith("

    ") or cuddled_list.startswith("
      ") + graf = graf[:start] + # Wrap

      tags. graf = self._run_span_gamut(graf) - grafs[i] = "

      " + graf.lstrip(" \t") + "

      " + grafs.append("

      " + graf.lstrip(" \t") + "

      ") + + if cuddled_list: + grafs.append(cuddled_list) return "\n\n".join(grafs) diff --git a/test/tm-cases/cuddled_para_and_list.html b/test/tm-cases/cuddled_para_and_list.html index da6947fa..2402667c 100644 --- a/test/tm-cases/cuddled_para_and_list.html +++ b/test/tm-cases/cuddled_para_and_list.html @@ -5,3 +5,28 @@
    1. bullet2
    2. bullet3
+ +

I did these too:

+ +
    +
  • bullet1
  • +
  • bullet2
  • +
  • bullet3 this is +a longer one.
  • +
+ +

And these in order:

+ +
    +
  1. bullet1
  2. +
  3. bullet2
  4. +
  5. bullet3
  6. +
+ +

Here is an asterisk, +* What do you think +about it?

+ +

I suggest using version +8. Oops, now this line is treated +as a sub-list.

diff --git a/test/tm-cases/cuddled_para_and_list.tags b/test/tm-cases/cuddled_para_and_list.tags index e6433144..d66f4f5f 100644 --- a/test/tm-cases/cuddled_para_and_list.tags +++ b/test/tm-cases/cuddled_para_and_list.tags @@ -1 +1 @@ -smedberg issue33 knownfailure +smedberg issue33 diff --git a/test/tm-cases/cuddled_para_and_list.text b/test/tm-cases/cuddled_para_and_list.text index 0dffc389..198d2b7b 100644 --- a/test/tm-cases/cuddled_para_and_list.text +++ b/test/tm-cases/cuddled_para_and_list.text @@ -2,3 +2,23 @@ I did these things: * bullet1 * bullet2 * bullet3 + +I did these too: +* bullet1 +* bullet2 +* bullet3 this is +a longer one. + +And these in order: +1. bullet1 +2. bullet2 +3. bullet3 + +Here is an asterisk, +* What do you think +about it? + +I suggest using version +8. Oops, now this line is treated +as a sub-list. +