Skip to content

Commit

Permalink
Implement issue 33: "cuddled-lists" extra.
Browse files Browse the repository at this point in the history
Still have doc and version updates to come.
  • Loading branch information
trentm committed Dec 16, 2009
1 parent 050690c commit 85ac708
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
40 changes: 30 additions & 10 deletions lib/markdown2.py
Expand Up @@ -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<marker>%s) [ \t]+ # list marker = \3
((?:.+?) # list item text = \4
(\n{1,2})) # eols = \5
(?= \n* (\Z | \2 (?P<next_marker>%s) [ \t]+))
''' % (_marker_any, _marker_any),
re.M | re.X | re.S)

Expand Down Expand Up @@ -1380,15 +1380,35 @@ def _form_paragraphs(self, text):
text = text.strip('\n')

# Wrap <p> 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("<ul>") or cuddled_list.startswith("<ol>")
graf = graf[:start]

# Wrap <p> tags.
graf = self._run_span_gamut(graf)
grafs[i] = "<p>" + graf.lstrip(" \t") + "</p>"
grafs.append("<p>" + graf.lstrip(" \t") + "</p>")

if cuddled_list:
grafs.append(cuddled_list)

return "\n\n".join(grafs)

Expand Down
25 changes: 25 additions & 0 deletions test/tm-cases/cuddled_para_and_list.html
Expand Up @@ -5,3 +5,28 @@
<li>bullet2</li>
<li>bullet3</li>
</ul>

<p>I did these too:</p>

<ul>
<li>bullet1</li>
<li>bullet2</li>
<li>bullet3 this is
a longer one.</li>
</ul>

<p>And these in order:</p>

<ol>
<li>bullet1</li>
<li>bullet2</li>
<li>bullet3</li>
</ol>

<p>Here is an asterisk,
* What do you think
about it?</p>

<p>I suggest using version
8. Oops, now this line is treated
as a sub-list.</p>
2 changes: 1 addition & 1 deletion test/tm-cases/cuddled_para_and_list.tags
@@ -1 +1 @@
smedberg issue33 knownfailure
smedberg issue33
20 changes: 20 additions & 0 deletions test/tm-cases/cuddled_para_and_list.text
Expand Up @@ -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.

0 comments on commit 85ac708

Please sign in to comment.