Skip to content
Open
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
18 changes: 14 additions & 4 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 @@ -228,14 +228,24 @@ 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 they select the Ruby parser only when the given +file_name+
# is a Ruby file (or when no +file_name+ is given).

def self.use_markup(content)
def self.use_markup(content, file_name = nil)
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 are comment formats, not parsers, so they imply
# the Ruby parser only when the file name does not select another parser
if %w[tomdoc markdown].include? markup then
return RDoc::Parser::Ruby if file_name.nil? or
can_parse_by_name(file_name) == RDoc::Parser::Ruby

return
end

markup = Regexp.escape markup

Expand Down
40 changes: 40 additions & 0 deletions test/rdoc/parser/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,28 @@ 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_use_markup
content = <<-CONTENT
# coding: utf-8 markup: rd
Expand All @@ -247,6 +269,15 @@ def test_class_use_markup_markdown
assert_equal @RP::Ruby, parser
end

def test_class_use_markup_markdown_file_name
content = <<-CONTENT
# coding: utf-8 markup: markdown
CONTENT

assert_equal @RP::Ruby, @RP.use_markup(content, 'file.rb')
assert_nil @RP.use_markup(content, 'file.c')
end

def test_class_use_markup_modeline
content = <<-CONTENT
# -*- coding: utf-8 -*-
Expand Down Expand Up @@ -292,6 +323,15 @@ def test_class_use_markup_tomdoc
assert_equal @RP::Ruby, parser
end

def test_class_use_markup_tomdoc_file_name
content = <<-CONTENT
# coding: utf-8 markup: tomdoc
CONTENT

assert_equal @RP::Ruby, @RP.use_markup(content, 'file.rb')
assert_nil @RP.use_markup(content, 'file.c')
end

def test_class_use_markup_none
parser = @RP.use_markup ''

Expand Down