Skip to content

Commit 8e4324d

Browse files
authored
Refactor markdown parsing of Image and ExplicitLink (#1323)
Create intermediate rule ExplicitLinkWithLabel that returns `{label:, link:}`, so that Image rule does not need to re-parse rdoc link format again. ```ruby # Before "[text](url)" → "{text}(url)" "![text](url)" → Image("{text}(url)") →(parse with regexp)→ "rdoc-image:url:text" # After "[text](url)" → ExplicitLink({ label: 'text', link: 'url' }) → "{text}(url)" "![text](url)" → Image({ label: 'text', link: 'url' }) → "rdoc-image:url:text" ``` #1322
1 parent 894b2f1 commit 8e4324d

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

lib/rdoc/markdown.kpeg

+7-10
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,8 @@ Strike = &{ strike? }
992992
"~~"
993993
{ strike a.join }
994994

995-
Image = "!" ExplicitLink:a
996-
{
997-
# Extract alt text and URL
998-
alt_text = a[/\{(.*?)\}/, 1] || ""
999-
url = a[/\[(.*?)\]/, 1] || ""
1000-
1001-
"rdoc-image:#{url}:#{alt_text}"
1002-
}
995+
Image = "!" ExplicitLinkWithLabel:a
996+
{ "rdoc-image:#{a[:link]}:#{a[:label]}" }
1003997

1004998
Link = ExplicitLink | ReferenceLink | AutoLink
1005999

@@ -1011,8 +1005,11 @@ ReferenceLinkDouble = Label:content < Spnl > !"[]" Label:label
10111005
ReferenceLinkSingle = Label:content < (Spnl "[]")? >
10121006
{ link_to content, content, text }
10131007

1014-
ExplicitLink = Label:l "(" @Sp Source:s Spnl Title @Sp ")"
1015-
{ "{#{l}}[#{s}]" }
1008+
ExplicitLink = ExplicitLinkWithLabel:a
1009+
{ "{#{a[:label]}}[#{a[:link]}]" }
1010+
1011+
ExplicitLinkWithLabel = Label:label "(" @Sp Source:link Spnl Title @Sp ")"
1012+
{ { label: label, link: link } }
10161013

10171014
Source = ( "<" < SourceContents > ">" | < SourceContents > )
10181015
{ text }

0 commit comments

Comments
 (0)