Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/rdoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def self.for(top_level, content, options, stats)
file_name = top_level.absolute_name
return if binary? file_name

parser = use_markup content
parser = use_markup content, file_name

unless parser then
parse_name = file_name
Expand Down Expand Up @@ -229,21 +229,22 @@ def self.remove_modeline(content)
#
# Any comment style may be used to hide the markup comment.

def self.use_markup(content)
def self.use_markup(content, file_path)
markup = content.lines.first(3).grep(/markup:\s+(\w+)/) { $1 }.first

return unless markup
if markup
return RDoc::Parser::Ruby if markup == "tomdoc"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be a tomdoc parser. So I kept this for backwards compatibility.


# TODO Ruby should be returned only when the filename is correct
return RDoc::Parser::Ruby if %w[tomdoc markdown].include? markup

markup = Regexp.escape markup

_, selected = RDoc::Parser.parsers.find do |_, parser|
/^#{markup}$/i =~ parser.name.sub(/.*:/, '')
markup = Regexp.escape markup
_, selected = RDoc::Parser.parsers.find do |_, parser|
/^#{markup}$/i =~ parser.name.sub(/.*:/, '')
end
selected
else
selected = RDoc::Parser.can_parse_by_name(file_path)
return if selected == RDoc::Parser::Simple
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has an empty regexp and would match on any file. This breaks shebang detection, so I explicitly exclude it here.

selected
end

selected
end

##
Expand Down
32 changes: 23 additions & 9 deletions test/rdoc/parser/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_class_use_markup
# coding: utf-8 markup: rd
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_equal @RP::RD, parser
end
Expand All @@ -242,9 +242,9 @@ def test_class_use_markup_markdown
# coding: utf-8 markup: markdown
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_equal @RP::Ruby, parser
assert_equal @RP::Markdown, parser
end

def test_class_use_markup_modeline
Expand All @@ -253,7 +253,7 @@ def test_class_use_markup_modeline
# markup: rd
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_equal @RP::RD, parser
end
Expand All @@ -266,7 +266,7 @@ def test_class_use_markup_modeline_shebang
*/
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_equal @RP::RD, parser
end
Expand All @@ -277,7 +277,7 @@ def test_class_use_markup_shebang
# coding: utf-8 markup: rd
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_equal @RP::RD, parser
end
Expand All @@ -287,13 +287,13 @@ def test_class_use_markup_tomdoc
# coding: utf-8 markup: tomdoc
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_equal @RP::Ruby, parser
end

def test_class_use_markup_none
parser = @RP.use_markup ''
parser = @RP.use_markup '', ''

assert_nil parser
end
Expand All @@ -303,11 +303,25 @@ def test_class_use_markup_unknown
# :markup: RDoc
CONTENT

parser = @RP.use_markup content
parser = @RP.use_markup content, ""

assert_nil parser
end

def test_class_use_markup_filepath
parser = @RP.use_markup "", "foo.rb"

assert_equal @RP::Ruby, parser
end

def test_class_use_markup_content_and_filepath
parser = @RP.use_markup <<~C, "foo.c"
/* :markup: markdown */
C

assert_equal @RP::Markdown, parser
end

def test_initialize
with_top_level("file.rb", "") do |top_level, content|
@RP.new top_level, content, @options, nil
Expand Down
Loading