Permalink
Browse files

Parse headings

  • Loading branch information...
1 parent fae312c commit b93f582cc7b81e8f651681c8445c7f997b6bf57e @drbrain drbrain committed Oct 2, 2011
Showing with 42 additions and 8 deletions.
  1. +9 −8 pegdown.kpeg
  2. +33 −0 test/test_pegdown.rb
View
@@ -41,11 +41,10 @@ Plain = Inlines:a
AtxInline = !Newline !(Sp? "#"* Sp Newline) Inline
AtxStart = < ( "######" | "#####" | "####" | "###" | "##" | "#" ) >
- { raise " $$ = mk_element(H1 + (strlen(yytext) - 1)); " }
+ { text.length }
-AtxHeading = AtxStart:s Sp? StartList:a ( AtxInline { raise " a = cons($$, a); " } )+ (Sp? "#"* Sp)? Newline
- { raise "$$ = mk_list(s->key, a);
- free(s);" }
+AtxHeading = AtxStart:s Sp? AtxInline+:a (Sp? "#"* Sp)? Newline
+ { RDoc::Markup::Heading.new(s, *a) }
SetextHeading = SetextHeading1 | SetextHeading2
@@ -54,12 +53,14 @@ SetextBottom1 = "===" "="* Newline
SetextBottom2 = "---" "-"* Newline
SetextHeading1 = &(RawLine SetextBottom1)
- StartList:a ( !Endline Inline { raise " a = cons($$, a); " } )+ Sp? Newline
- SetextBottom1 { raise " $$ = mk_list(H1, a); " }
+ StartList:a ( !Endline Inline:a )+ Sp? Newline
+ SetextBottom1
+ { RDoc::Markup::Heading.new(1, *a) }
SetextHeading2 = &(RawLine SetextBottom2)
- StartList:a ( !Endline Inline { raise " a = cons($$, a); " } )+ Sp? Newline
- SetextBottom2 { raise " $$ = mk_list(H2, a); " }
+ StartList:a ( !Endline Inline:a )+ Sp? Newline
+ SetextBottom2
+ { RDoc::Markup::Heading.new(2, *a) }
Heading = SetextHeading | AtxHeading
View
@@ -18,6 +18,39 @@ def mu_pp obj
s.chomp
end
+ def test_parse_heading_atx
+ doc = parse "# heading\n"
+
+ expected = @RM::Document.new(
+ @RM::Heading.new(1, "heading"))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_heading_setext_dash
+ doc = parse <<-MD
+heading
+---
+ MD
+
+ expected = @RM::Document.new(
+ @RM::Heading.new(2, "heading"))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_heading_setext_equals
+ doc = parse <<-MD
+heading
+===
+ MD
+
+ expected = @RM::Document.new(
+ @RM::Heading.new(1, "heading"))
+
+ assert_equal expected, doc
+ end
+
def test_parse_list_bullet
doc = parse <<-MD
* one

0 comments on commit b93f582

Please sign in to comment.