Merge pull request #1820 from kxz/dtext-lists

Fixes for bulleted list rendering (#1803)
This commit is contained in:
Albert Yi
2013-07-01 18:13:52 -07:00
2 changed files with 22 additions and 7 deletions

View File

@@ -86,15 +86,22 @@ class DText
def self.parse_list(str, options = {})
html = ""
current_item = ""
layout = []
nest = 0
str.split(/\n/).each do |line|
if line =~ /^\s*(\*+) (.+)/
if nest > 0
html += "<li>#{current_item}</li>"
elsif not current_item.strip.empty?
html += "<p>#{current_item}</p>"
end
nest = $1.size
content = parse_inline($2)
current_item = parse_inline($2)
else
content = parse_inline(line)
current_item += parse_inline(line)
end
if nest > layout.size
@@ -108,10 +115,10 @@ class DText
html += "</#{elist}>"
end
end
html += "<li>#{content}</li>"
end
html += "<li>#{current_item}</li>"
while layout.any?
elist = layout.pop
html += "</#{elist}>"

View File

@@ -125,6 +125,14 @@ class DTextTest < ActiveSupport::TestCase
assert_equal('<ul><li><a href="/posts/1">post #1</a></li></ul>', p("* post #1").gsub(/\n/, ""))
end
def test_lists_not_preceded_by_newline
assert_equal('<p>ab</p><ul><li>c</li><li>d</li></ul>', p("a\nb\n* c\n* d").gsub(/\n/, ""))
end
def test_lists_with_multiline_items
assert_equal('<p>a</p><ul><li>bc</li><li>de</li></ul>', p("a\n* b\nc\n* d\ne").gsub(/\n/, ""))
end
def test_inline_tags
assert_equal('<p><a href="/posts?tags=tag">tag</a></p>', p("{{tag}}"))
end