Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add nested list support

  • Loading branch information...
commit ce7ecafb6d7a92905166eb1ed7161256ce52c57e 1 parent 5f86a08
@drbrain drbrain authored
Showing with 109 additions and 9 deletions.
  1. +55 −9 pegdown.kpeg
  2. +54 −0 test/test_pegdown.rb
View
64 pegdown.kpeg
@@ -81,13 +81,27 @@
end
end
+ def inner_parse text # :nodoc:
+ parser = clone
+
+ parser.setup_parser text, @debug
+
+ parser.peg_parse
+
+ doc = parser.result
+
+ doc.parts
+ end
+
##
# Finds a link reference for +label+ and creates a new link to it with
# +content+ as the link text. If +label+ has not be encountered in the
# document a placeholder is created that will be filled later.
def link_to content, label = content
- raise 'enable notes extension' if content.start_with? '^' and label == content
+ raise 'enable notes extension' if
+ content.start_with? '^' and label == content
+
if ref = @references[label] then
"{#{content}}[#{ref}]"
else
@@ -98,6 +112,35 @@
end
##
+ # Creates an RDoc::Markup::ListItem from +parsed+ including parsing nested
+ # lists.
+
+ def list_item_from parsed
+ out = RDoc::Markup::ListItem.new nil
+
+ parsed.each do |part|
+ case part
+ when String then
+ case out.parts.last
+ when RDoc::Markup::Paragraph then
+ out.parts.last << part
+ else
+ out << RDoc::Markup::Paragraph.new(part)
+ end
+ when RDoc::Markup::Paragraph then
+ out << part
+ when RDoc::Markup::Raw then
+ inner = inner_parse part.text
+ out.push(*inner)
+ else
+ raise part.inspect
+ end
+ end
+
+ out
+ end
+
+ ##
# Finds a footnote reference for +label+ and creates a new link to it with
# +content+ as the link text. If +label+ has not be encountered in the
# document a placeholder is created that will be filled later.
@@ -262,26 +305,29 @@ ListLoose = StartList:a
ListItem = ( Bullet | Enumerator )
StartList:a
ListBlock:b { a << b }
- ( ListContinuationBlock { raise " a = cons($$, a); " } )*
- { RDoc::Markup::ListItem.new(nil, *a) }
+ ( ListContinuationBlock:c { a.push(*c) } )*
+ { list_item_from a }
ListItemTight =
( Bullet | Enumerator )
ListBlock:a
( !BlankLine
- ListContinuationBlock { raise " a = cons($$, a); " } )*
+ ListContinuationBlock:b { a.push(*b) } )*
!ListContinuationBlock
- { RDoc::Markup::ListItem.new(nil, a) }
+ { list_item_from a.parts }
ListBlock = !BlankLine Line:a
ListBlockLine*:c
{ RDoc::Markup::Paragraph.new(a, *c) }
-ListContinuationBlock = StartList:a
+ListContinuationBlock = StartList:a { a = [] }
( < BlankLine* >
- { a << (text.empty? ? "\001" : text) } )
- ( Indent ListBlock { raise " a = cons($$, a); " } )+
- { raise " $$ = mk_str_from_list(a, false); " }
+ { a << text unless text =~ /\A\n?\z/ } )
+ ( Indent
+ ListBlock:b {
+ a << RDoc::Markup::Raw.new(*b.parts)
+ } )+
+ { a }
Enumerator = NonindentSpace [0-9]+ "." Spacechar+
View
54 test/test_pegdown.rb
@@ -288,6 +288,60 @@ def test_parse_list_bullet_continue
assert_equal expected, doc
end
+ def test_parse_list_bullet_nest
+ doc = parse <<-MD
+* outer
+ * inner
+ MD
+
+ expected = @RM::Document.new(
+ @RM::List.new(:BULLET, *[
+ @RM::ListItem.new(nil,
+ @RM::Paragraph.new("outer\n"),
+ @RM::List.new(:BULLET, *[
+ @RM::ListItem.new(nil, @RM::Paragraph.new("inner\n"))]))]))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_list_bullet_nest_loose
+ doc = parse <<-MD
+* outer
+
+ * inner
+ MD
+
+ expected = @RM::Document.new(
+ @RM::List.new(:BULLET, *[
+ @RM::ListItem.new(nil,
+ @RM::Paragraph.new("outer\n"),
+ @RM::List.new(:BULLET, *[
+ @RM::ListItem.new(nil, @RM::Paragraph.new("inner\n"))]))]))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_list_bullet_nest_continue
+ doc = parse <<-MD
+* outer
+ * inner
+ continue
+* outer 2
+ MD
+
+ expected = @RM::Document.new(
+ @RM::List.new(:BULLET, *[
+ @RM::ListItem.new(nil,
+ @RM::Paragraph.new("outer\n"),
+ @RM::List.new(:BULLET, *[
+ @RM::ListItem.new(nil,
+ @RM::Paragraph.new("inner\n continue\n"))])),
+ @RM::ListItem.new(nil,
+ @RM::Paragraph.new("outer 2\n"))]))
+
+ assert_equal expected, doc
+ end
+
def test_parse_list_number
doc = parse <<-MD
1. one
Please sign in to comment.
Something went wrong with that request. Please try again.