Browse files

Implement emphasis and strong emphasis

  • Loading branch information...
1 parent 1b2572d commit 2632f7127e43cc0cf4afaaaf703b92a0d2dac1bf @drbrain drbrain committed Oct 30, 2011
Showing with 53 additions and 16 deletions.
  1. +17 −16 pegdown.kpeg
  2. +36 −0 test/test_pegdown.rb
View
33 pegdown.kpeg
@@ -562,42 +562,43 @@ UlLine = < "____" "_"* > { text } |
Emph = EmphStar | EmphUl
OneStarOpen = !StarLine "*" !Spacechar !Newline
-OneStarClose = !Spacechar !Newline a:inline !StrongStar "*" { raise " $$ = a; " }
+OneStarClose = !Spacechar !Newline Inline:a !StrongStar "*" { "_#{a}_" }
EmphStar = OneStarOpen
StartList:a
- ( !OneStarClose Inline { raise " a = cons($$, a); " } )*
- OneStarClose { raise " a = cons($$, a); " }
- { raise " $$ = mk_list(EMPH, a); " }
+ ( !OneStarClose Inline:l { a << l } )*
+ OneStarClose:l { a << l }
+ { a }
OneUlOpen = !UlLine "_" !Spacechar !Newline
-OneUlClose = !Spacechar !Newline a:inline !StrongUl "_" !Alphanumeric { raise " $$ = a; " }
+OneUlClose = !Spacechar !Newline Inline:a !StrongUl "_" !Alphanumeric
+ { "_#{a}_" }
EmphUl = OneUlOpen
StartList:a
- ( !OneUlClose Inline { raise " a = cons($$, a); " } )*
- OneUlClose { raise " a = cons($$, a); " }
- { raise " $$ = mk_list(EMPH, a); " }
+ ( !OneUlClose Inline:l { a << l } )*
+ OneUlClose:l { a << l }
+ { a }
Strong = StrongStar | StrongUl
TwoStarOpen = !StarLine "**" !Spacechar !Newline
-TwoStarClose = !Spacechar !Newline a:inline "**" { raise " $$ = a; " }
+TwoStarClose = !Spacechar !Newline Inline:a "**" { "*#{a}*" }
StrongStar = TwoStarOpen
StartList:a
- ( !TwoStarClose Inline { raise " a = cons($$, a); " } )*
- TwoStarClose { raise " a = cons($$, a); " }
- { raise " $$ = mk_list(STRONG, a); " }
+ ( !TwoStarClose Inline:l { a << l } )*
+ TwoStarClose:l { a << l }
+ { a }
TwoUlOpen = !UlLine "__" !Spacechar !Newline
-TwoUlClose = !Spacechar !Newline a:inline "__" !Alphanumeric { raise " $$ = a; " }
+TwoUlClose = !Spacechar !Newline Inline:a "__" !Alphanumeric { "*#{a}*" }
StrongUl = TwoUlOpen
StartList:a
- ( !TwoUlClose Inline { raise " a = cons($$, a); " } )*
- TwoUlClose { raise " a = cons($$, a); " }
- { raise " $$ = mk_list(STRONG, a); " }
+ ( !TwoUlClose Inline:a { a << l } )*
+ TwoUlClose:l { a << l }
+ { a }
Image = "!" ( ExplicitLink | ReferenceLink )
{ raise 'if ($$->key == LINK) {
View
36 test/test_pegdown.rb
@@ -98,6 +98,24 @@ def test_parse_entity_named
assert_equal expected, doc
end
+ def test_parse_emphasis_star
+ doc = parse "it *works*\n"
+
+ expected = @RM::Document.new(
+ @RM::Paragraph.new("it _works_"))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_emphasis_underscore
+ doc = parse "it _works_\n"
+
+ expected = @RM::Document.new(
+ @RM::Paragraph.new("it _works_"))
+
+ assert_equal expected, doc
+ end
+
def test_parse_escape
doc = parse "Backtick: \\`"
@@ -367,6 +385,24 @@ def test_parse_rule_star
assert_equal expected, doc
end
+ def test_parse_strong_star
+ doc = parse "it **works**\n"
+
+ expected = @RM::Document.new(
+ @RM::Paragraph.new("it *works*"))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_strong_underscore
+ doc = parse "it __works__\n"
+
+ expected = @RM::Document.new(
+ @RM::Paragraph.new("it *works*"))
+
+ assert_equal expected, doc
+ end
+
# def test_parse_style
# doc = parse <<-MD
#<style></style>

0 comments on commit 2632f71

Please sign in to comment.