Skip to content

Commit c30a018

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 021d21e commit c30a018

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
@@ -437,8 +437,6 @@
437437
def parse markdown
438438
@references = {}
439439
@unlinked_references = {}
440-
@footnotes = nil
441-
@note_order = nil
442440

443441
markdown += "\n\n"
444442

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

1241-
ref = [:inline, @note_order.length]
1242-
@footnotes[ref] = paragraph a
1243-
1244-
note_for ref
1241+
note_for ref
1242+
end
12451243
}
12461244

12471245
Notes = ( Note | SkipBlock )*

lib/rdoc/markdown.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,6 @@ def paragraph parts
822822
def parse markdown
823823
@references = {}
824824
@unlinked_references = {}
825-
@footnotes = nil
826-
@note_order = nil
827825

828826
markdown += "\n\n"
829827

@@ -15478,7 +15476,7 @@ def _Note
1547815476
return _tmp
1547915477
end
1548015478

15481-
# 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 }
15479+
# InlineNote = &{ notes? } "^[" @StartList:a (!"]" Inline:l { a << l })+ "]" { if @note_order ref = [:inline, @note_order.length] @footnotes[ref] = paragraph a note_for ref end }
1548215480
def _InlineNote
1548315481

1548415482
_save = self.pos
@@ -15569,12 +15567,12 @@ def _InlineNote
1556915567
self.pos = _save
1557015568
break
1557115569
end
15572-
@result = begin; raise ParseError, 'invalid inline note' unless @note_order
15570+
@result = begin; if @note_order
15571+
ref = [:inline, @note_order.length]
15572+
@footnotes[ref] = paragraph a
1557315573

15574-
ref = [:inline, @note_order.length]
15575-
@footnotes[ref] = paragraph a
15576-
15577-
note_for ref
15574+
note_for ref
15575+
end
1557815576
; end
1557915577
_tmp = true
1558015578
unless _tmp
@@ -16845,7 +16843,7 @@ def _DefinitionListDefinition
1684516843
Rules[:_NoteReference] = rule_info("NoteReference", "&{ notes? } RawNoteReference:ref { note_for ref }")
1684616844
Rules[:_RawNoteReference] = rule_info("RawNoteReference", "\"[^\" < (!@Newline !\"]\" .)+ > \"]\" { text }")
1684716845
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 }")
16848-
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 }")
16846+
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 }")
1684916847
Rules[:_Notes] = rule_info("Notes", "(Note | SkipBlock)*")
1685016848
Rules[:_RawNoteBlock] = rule_info("RawNoteBlock", "@StartList:a (!@BlankLine !RawNoteReference OptionallyIndentedLine:l { a << l })+ < @BlankLine* > { a << text } { a }")
1685116849
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
@@ -1002,16 +1002,10 @@ def test_parse_note_inline
10021002
assert_equal expected, doc
10031003
end
10041004

1005-
def test_parse_note_inline_in_reference_label_after_reuse
1005+
def test_parse_note_inline_in_reference_label
10061006
@parser.notes = true
10071007

1008-
parse "Some text. ^[With a footnote]"
1009-
1010-
error = assert_raise RDoc::Markdown::ParseError do
1011-
parse "[foo ^[note]]: /url\n"
1012-
end
1013-
1014-
assert_equal 'invalid inline note', error.message
1008+
assert_kind_of RDoc::Markup::Document, parse("[foo ^[note]]: /url\n")
10151009
end
10161010

10171011
def test_parse_note_no_notes

0 commit comments

Comments
 (0)