Skip to content
Merged
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
10 changes: 8 additions & 2 deletions lib/rdoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,20 @@ def self.remove_modeline(content)
# appear on the second or third line.
#
# Any comment style may be used to hide the markup comment.
#
# The +tomdoc+ and +markdown+ markups name comment formats rather than
# parsers, so no parser is selected for them and RDoc::Parser.for picks one
# from the file name instead.

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

return unless markup

# TODO Ruby should be returned only when the filename is correct
return RDoc::Parser::Ruby if %w[tomdoc markdown].include? markup
# tomdoc and markdown name a comment format, not a parser. Skipping them
# keeps the search below from matching RDoc::Parser::Markdown for a file
# that is not markdown.
return if %w[tomdoc markdown].include? markup

markup = Regexp.escape markup

Expand Down
34 changes: 32 additions & 2 deletions test/rdoc/parser/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,36 @@ def test_class_for_markup
end
end

def test_class_for_markup_markdown_c_file
content = <<-CONTENT
/* file.c */
/* :markup: markdown */

/* method comment */
VALUE rb_a_foo(VALUE self) {
}
CONTENT

file_name = File.join Dir.tmpdir, "file.c"
File.write file_name, content

top_level = @store.add_file file_name

parser = @RP.for top_level, content, @options, :stats

assert_kind_of @RP::C, parser
ensure
File.unlink file_name
end

def test_class_for_markup_markdown_ruby_file
with_top_level("file.rb", "# :markup: markdown\n") do |top_level, content|
parser = @RP.for top_level, content, @options, nil

assert_kind_of @RP::Ruby, parser
end
end

def test_class_use_markup
content = <<-CONTENT
# coding: utf-8 markup: rd
Expand All @@ -244,7 +274,7 @@ def test_class_use_markup_markdown

parser = @RP.use_markup content

assert_equal @RP::Ruby, parser
assert_nil parser
end

def test_class_use_markup_modeline
Expand Down Expand Up @@ -289,7 +319,7 @@ def test_class_use_markup_tomdoc

parser = @RP.use_markup content

assert_equal @RP::Ruby, parser
assert_nil parser
end

def test_class_use_markup_none
Expand Down