Skip to content

Commit 6549e8b

Browse files
committed
Ignore inline notes before note ordering
Inline notes can be parsed while Markdown references are being collected, before footnote ordering has been initialized. Treat that early pass as a no-op so malformed reference labels do not crash the parser.
1 parent a23d1c1 commit 6549e8b

3 files changed

Lines changed: 14 additions & 24 deletions

File tree

lib/rdoc/markdown.kpeg

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,6 @@
439439
def parse markdown
440440
@references = {}
441441
@unlinked_references = {}
442-
@footnotes = nil
443-
@note_order = nil
444442

445443
markdown += "\n\n"
446444

@@ -1238,12 +1236,12 @@ InlineNote = &{ notes? }
12381236
@StartList:a
12391237
( !"]" Inline:l { a << l } )+
12401238
"]"
1241-
{ raise ParseError, 'invalid inline note' unless @note_order
1239+
{ if @note_order
1240+
ref = [:inline, @note_order.length]
1241+
@footnotes[ref] = paragraph a
12421242

1243-
ref = [:inline, @note_order.length]
1244-
@footnotes[ref] = paragraph a
1245-
1246-
note_for ref
1243+
note_for ref
1244+
end
12471245
}
12481246

12491247
Notes = ( Note | SkipBlock )*

lib/rdoc/markdown.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -824,8 +824,6 @@ def paragraph parts
824824
def parse markdown
825825
@references = {}
826826
@unlinked_references = {}
827-
@footnotes = nil
828-
@note_order = nil
829827

830828
markdown += "\n\n"
831829

@@ -15480,7 +15478,7 @@ def _Note
1548015478
return _tmp
1548115479
end
1548215480

15483-
# InlineNote = &{ notes? } "^[" @StartList:a (!"]" Inline:l { a << l })+ "]" { raise ParseError, 'invalid inline note' unless @note_order ref = [:inline, @note_order.length] @footnotes[ref] = paragraph a note_for ref }
15481+
# InlineNote = &{ notes? } "^[" @StartList:a (!"]" Inline:l { a << l })+ "]" { if @note_order ref = [:inline, @note_order.length] @footnotes[ref] = paragraph a note_for ref end }
1548415482
def _InlineNote
1548515483

1548615484
_save = self.pos
@@ -15571,12 +15569,12 @@ def _InlineNote
1557115569
self.pos = _save
1557215570
break
1557315571
end
15574-
@result = begin; raise ParseError, 'invalid inline note' unless @note_order
15572+
@result = begin; if @note_order
15573+
ref = [:inline, @note_order.length]
15574+
@footnotes[ref] = paragraph a
1557515575

15576-
ref = [:inline, @note_order.length]
15577-
@footnotes[ref] = paragraph a
15578-
15579-
note_for ref
15576+
note_for ref
15577+
end
1558015578
; end
1558115579
_tmp = true
1558215580
unless _tmp
@@ -16847,7 +16845,7 @@ def _DefinitionListDefinition
1684716845
Rules[:_NoteReference] = rule_info("NoteReference", "&{ notes? } RawNoteReference:ref { note_for ref }")
1684816846
Rules[:_RawNoteReference] = rule_info("RawNoteReference", "\"[^\" < (!@Newline !\"]\" .)+ > \"]\" { text }")
1684916847
Rules[:_Note] = rule_info("Note", "&{ notes? } @NonindentSpace RawNoteReference:ref \":\" @Sp @StartList:a RawNoteBlock:i { a.concat i } (&Indent RawNoteBlock:i { a.concat i })* { @footnotes[ref] = paragraph a nil }")
16850-
Rules[:_InlineNote] = rule_info("InlineNote", "&{ notes? } \"^[\" @StartList:a (!\"]\" Inline:l { a << l })+ \"]\" { raise ParseError, 'invalid inline note' unless @note_order ref = [:inline, @note_order.length] @footnotes[ref] = paragraph a note_for ref }")
16848+
Rules[:_InlineNote] = rule_info("InlineNote", "&{ notes? } \"^[\" @StartList:a (!\"]\" Inline:l { a << l })+ \"]\" { if @note_order ref = [:inline, @note_order.length] @footnotes[ref] = paragraph a note_for ref end }")
1685116849
Rules[:_Notes] = rule_info("Notes", "(Note | SkipBlock)*")
1685216850
Rules[:_RawNoteBlock] = rule_info("RawNoteBlock", "@StartList:a (!@BlankLine !RawNoteReference OptionallyIndentedLine:l { a << l })+ < @BlankLine* > { a << text } { a }")
1685316851
Rules[:_CodeFence] = rule_info("CodeFence", "&{ github? } Ticks3 (@Sp StrChunk:format)? @Sp @Newline? < ((!\"`\" Nonspacechar)+ | !Ticks3 /`+/ | Spacechar | @Newline)+ > Ticks3 @Sp @Newline* { verbatim = RDoc::Markup::Verbatim.new text verbatim.format = format.intern if format.instance_of?(String) verbatim }")

test/rdoc/rdoc_markdown_test.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,16 +1025,10 @@ def test_parse_note_reference_in_reference_label
10251025
assert_equal expected, doc
10261026
end
10271027

1028-
def test_parse_note_inline_in_reference_label_after_reuse
1028+
def test_parse_note_inline_in_reference_label
10291029
@parser.notes = true
10301030

1031-
parse "Some text. ^[With a footnote]"
1032-
1033-
error = assert_raise RDoc::Markdown::ParseError do
1034-
parse "[foo ^[note]]: /url\n"
1035-
end
1036-
1037-
assert_equal 'invalid inline note', error.message
1031+
assert_kind_of RDoc::Markup::Document, parse("[foo ^[note]]: /url\n")
10381032
end
10391033

10401034
def test_parse_note_no_notes

0 commit comments

Comments
 (0)