Skip to content

Commit 9004e90

Browse files
authored
Merge pull request #559 from aycabta/update-markdown.kpeg
Update markdown.kpeg
2 parents 60926a9 + 8ab662c commit 9004e90

File tree

2 files changed

+97
-38
lines changed

2 files changed

+97
-38
lines changed

lib/rdoc/markdown.kpeg

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@
8484
# : A little insect that is known
8585
# to enjoy picnics
8686
#
87+
# ### Strike
88+
#
89+
# Example:
90+
#
91+
# ```
92+
# This is ~~striked~~.
93+
# ```
94+
#
95+
# Produces:
96+
#
97+
# This is ~~striked~~.
98+
#
8799
# ### Github
88100
#
89101
# The #github extension enables a partial set of [Github Flavored Markdown]
@@ -193,6 +205,7 @@
193205
:github,
194206
:html,
195207
:notes,
208+
:strike,
196209
]
197210

198211
# :section: Extensions
@@ -243,6 +256,11 @@
243256

244257
extension :notes
245258

259+
##
260+
# Enables the strike extension
261+
262+
extension :strike
263+
246264
# :section:
247265

248266
##
@@ -466,6 +484,17 @@
466484
"<b>#{text}</b>"
467485
end
468486
end
487+
488+
##
489+
# Wraps `text` in strike markup for rdoc inline formatting
490+
491+
def strike text
492+
if text =~ /\A[a-z\d.\/-]+\z/i then
493+
"~#{text}~"
494+
else
495+
"<s>#{text}</s>"
496+
end
497+
end
469498
}
470499

471500
root = Doc
@@ -494,27 +523,27 @@ Para = @NonindentSpace Inlines:a @BlankLine+
494523
Plain = Inlines:a
495524
{ paragraph a }
496525

497-
AtxInline = !@Newline !(@Sp? /#*/ @Sp @Newline) Inline
526+
AtxInline = !@Newline !(@Sp /#*/ @Sp @Newline) Inline
498527

499528
AtxStart = < /\#{1,6}/ >
500529
{ text.length }
501530

502-
AtxHeading = AtxStart:s @Sp? AtxInline+:a (@Sp? /#*/ @Sp)? @Newline
531+
AtxHeading = AtxStart:s @Sp AtxInline+:a (@Sp /#*/ @Sp)? @Newline
503532
{ RDoc::Markup::Heading.new(s, a.join) }
504533

505534
SetextHeading = SetextHeading1 | SetextHeading2
506535

507-
SetextBottom1 = /={3,}/ @Newline
536+
SetextBottom1 = /={1,}/ @Newline
508537

509-
SetextBottom2 = /-{3,}/ @Newline
538+
SetextBottom2 = /-{1,}/ @Newline
510539

511540
SetextHeading1 = &(@RawLine SetextBottom1)
512-
@StartList:a ( !@Endline Inline:b { a << b } )+ @Sp? @Newline
541+
@StartList:a ( !@Endline Inline:b { a << b } )+ @Sp @Newline
513542
SetextBottom1
514543
{ RDoc::Markup::Heading.new(1, a.join) }
515544

516545
SetextHeading2 = &(@RawLine SetextBottom2)
517-
@StartList:a ( !@Endline Inline:b { a << b })+ @Sp? @Newline
546+
@StartList:a ( !@Endline Inline:b { a << b })+ @Sp @Newline
518547
SetextBottom2
519548
{ RDoc::Markup::Heading.new(2, a.join) }
520549

@@ -733,6 +762,9 @@ HtmlBlockOpenScript = "<" Spnl ("script" | "SCRIPT") Spnl HtmlAttribute* ">"
733762
HtmlBlockCloseScript = "<" Spnl "/" ("script" | "SCRIPT") Spnl ">"
734763
HtmlBlockScript = HtmlBlockOpenScript (!HtmlBlockCloseScript .)* HtmlBlockCloseScript
735764

765+
HtmlBlockOpenHead = "<" Spnl ("head" | "HEAD") Spnl HtmlAttribute* ">"
766+
HtmlBlockCloseHead = "<" Spnl "/" ("head" | "HEAD") Spnl ">"
767+
HtmlBlockHead = HtmlBlockOpenHead (!HtmlBlockCloseHead .)* HtmlBlockCloseHead
736768

737769
HtmlBlockInTags = HtmlAnchor
738770
| HtmlBlockAddress
@@ -768,6 +800,7 @@ HtmlBlockInTags = HtmlAnchor
768800
| HtmlBlockThead
769801
| HtmlBlockTr
770802
| HtmlBlockScript
803+
| HtmlBlockHead
771804

772805
HtmlBlock = < ( HtmlBlockInTags | HtmlComment | HtmlBlockSelfClosing | HtmlUnclosed) >
773806
@BlankLine+
@@ -871,6 +904,7 @@ Inline = Str
871904
| @Space
872905
| Strong
873906
| Emph
907+
| Strike
874908
| Image
875909
| Link
876910
| NoteReference
@@ -896,7 +930,7 @@ Entity = ( HexEntity | DecEntity | CharEntity ):a { a }
896930
Endline = @LineBreak | @TerminalEndline | @NormalEndline
897931

898932
NormalEndline = @Sp @Newline !@BlankLine !">" !AtxStart
899-
!(Line /={3,}|-{3,}=/ @Newline)
933+
!(Line /={1,}|-{1,}/ @Newline)
900934
{ "\n" }
901935

902936
TerminalEndline = @Sp @Newline @Eof
@@ -916,46 +950,44 @@ UlLine = < /_{4,}/ > { text } |
916950

917951
Emph = EmphStar | EmphUl
918952

919-
OneStarOpen = !StarLine "*" !@Spacechar !@Newline
920-
OneStarClose = !@Spacechar !@Newline Inline:a "*"
921-
{ a }
953+
Whitespace = @Spacechar | @Newline
922954

923-
EmphStar = OneStarOpen
955+
EmphStar = "*" !@Whitespace
924956
@StartList:a
925-
( !OneStarClose Inline:l { a << l } )*
926-
OneStarClose:l { a << l }
957+
( !"*" Inline:b { a << b }
958+
| StrongStar:b { a << b }
959+
)+
960+
"*"
927961
{ emphasis a.join }
928962

929-
OneUlOpen = !UlLine "_" !@Spacechar !@Newline
930-
OneUlClose = !@Spacechar !@Newline Inline:a "_" # !Alphanumeric # TODO check
931-
{ a }
932-
933-
EmphUl = OneUlOpen
963+
EmphUl = "_" !@Whitespace
934964
@StartList:a
935-
( !OneUlClose Inline:l { a << l } )*
936-
OneUlClose:l { a << l }
965+
( !"_" Inline:b { a << b }
966+
| StrongUl:b { a << b }
967+
)+
968+
"_"
937969
{ emphasis a.join }
938970

939971
Strong = StrongStar | StrongUl
940972

941-
TwoStarOpen = !StarLine "**" !@Spacechar !@Newline
942-
TwoStarClose = !@Spacechar !@Newline Inline:a "**" { a }
943-
944-
StrongStar = TwoStarOpen
973+
StrongStar = "**" !@Whitespace
945974
@StartList:a
946-
( !TwoStarClose Inline:l { a << l } )*
947-
TwoStarClose:l { a << l }
975+
( !"**" Inline:b { a << b } )+
976+
"**"
948977
{ strong a.join }
949978

950-
TwoUlOpen = !UlLine "__" !@Spacechar !@Newline
951-
TwoUlClose = !@Spacechar !@Newline Inline:a "__" # !Alphanumeric # TODO check
952-
{ a }
979+
StrongUl = "__" !@Whitespace
980+
@StartList:a
981+
( !"__" Inline:b { a << b } )+
982+
"__"
983+
{ strong a.join }
953984

954-
StrongUl = TwoUlOpen
955-
@StartList:a
956-
( !TwoUlClose Inline:i { a << i } )*
957-
TwoUlClose:l { a << l }
958-
{ strong a.join }
985+
Strike = &{ strike? }
986+
"~~" !@Whitespace
987+
@StartList:a
988+
( !"~~" Inline:b { a << b } )+
989+
"~~"
990+
{ strike a.join }
959991

960992
# TODO alt text support
961993
Image = "!" ( ExplicitLink | ReferenceLink ):a
@@ -971,14 +1003,13 @@ ReferenceLinkDouble = Label:content < Spnl > !"[]" Label:label
9711003
ReferenceLinkSingle = Label:content < (Spnl "[]")? >
9721004
{ link_to content, content, text }
9731005

974-
ExplicitLink = Label:l Spnl "(" @Sp Source:s Spnl Title @Sp ")"
1006+
ExplicitLink = Label:l "(" @Sp Source:s Spnl Title @Sp ")"
9751007
{ "{#{l}}[#{s}]" }
9761008

9771009
Source = ( "<" < SourceContents > ">" | < SourceContents > )
9781010
{ text }
9791011

9801012
SourceContents = ( ( !"(" !")" !">" Nonspacechar )+ | "(" SourceContents ")")*
981-
| ""
9821013

9831014
Title = ( TitleSingle | TitleDouble | "" ):a
9841015
{ a }
@@ -1068,7 +1099,7 @@ Eof = !.
10681099
Nonspacechar = !@Spacechar !@Newline .
10691100
Sp = @Spacechar*
10701101
Spnl = @Sp (@Newline @Sp)?
1071-
SpecialChar = /[*_`&\[\]()<!#\\'"]/ | @ExtendedSpecialChar
1102+
SpecialChar = /[~*_`&\[\]()<!#\\'"]/ | @ExtendedSpecialChar
10721103
NormalChar = !( @SpecialChar | @Spacechar | @Newline ) .
10731104
Digit = [0-9]
10741105

@@ -1077,7 +1108,6 @@ Alphanumeric = %literals.Alphanumeric
10771108
AlphanumericAscii = %literals.AlphanumericAscii
10781109
BOM = %literals.BOM
10791110
Newline = %literals.Newline
1080-
NonAlphanumeric = %literals.NonAlphanumeric
10811111
Spacechar = %literals.Spacechar
10821112

10831113
HexEntity = /&#x/i < /[0-9a-fA-F]+/ > ";"

test/test_rdoc_markdown.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,35 @@ def test_parse_strong_emphasis_underscore
930930
assert_equal expected, doc
931931
end
932932

933+
def test_parse_strike_tilde
934+
doc = parse "it ~~works~~\n"
935+
936+
expected = @RM::Document.new(
937+
@RM::Paragraph.new("it ~works~"))
938+
939+
assert_equal expected, doc
940+
end
941+
942+
def test_parse_strike_words_tilde
943+
doc = parse "it ~~works fine~~\n"
944+
945+
expected = @RM::Document.new(
946+
@RM::Paragraph.new("it <s>works fine</s>"))
947+
948+
assert_equal expected, doc
949+
end
950+
951+
def test_parse_strike_tilde_no
952+
@parser.strike = false
953+
954+
doc = parse "it ~~works fine~~\n"
955+
956+
expected = @RM::Document.new(
957+
@RM::Paragraph.new("it ~~works fine~~"))
958+
959+
assert_equal expected, doc
960+
end
961+
933962
def test_parse_style
934963
@parser.css = true
935964

0 commit comments

Comments
 (0)