diff --git a/History.txt b/History.txt index e5efe0f268..ca6285acae 100644 --- a/History.txt +++ b/History.txt @@ -3,6 +3,9 @@ * Minor Enhancements * Support rb_singleton_class(). Reported by Jeremy Evans. +* Bug Fixes + * Tread non-ASCII RDoc files as text. Bug #28391 by Kouhei Sutou. + === 2.5.9 / 2010-07-06 * Bug Fixes diff --git a/lib/rdoc/parser.rb b/lib/rdoc/parser.rb index 9cfda127d9..212b35f341 100644 --- a/lib/rdoc/parser.rb +++ b/lib/rdoc/parser.rb @@ -62,12 +62,23 @@ def self.alias_extension(old_ext, new_ext) true end + def self.set_encoding(string) + if defined? Encoding then + if /coding[=:]\s*([^\s;]+)/i =~ string[%r"\A(?:#!.*\n)?.*\n"] + if enc = ::Encoding.find($1) + string.force_encoding(enc) + end + end + end + end + ## # Determines if the file is a "binary" file which basically means it has # content that an RDoc parser shouldn't try to consume. def self.binary?(file) s = File.read(file, 1024) or return false + set_encoding(s) if s[0, 2] == Marshal.dump('')[0, 2] then true diff --git a/lib/rdoc/rdoc.rb b/lib/rdoc/rdoc.rb index 9c39354c6c..e712196de3 100644 --- a/lib/rdoc/rdoc.rb +++ b/lib/rdoc/rdoc.rb @@ -405,15 +405,7 @@ def document(argv) def read_file_contents(filename) content = open filename, "rb" do |f| f.read end - - if defined? Encoding then - if /coding[=:]\s*([^\s;]+)/i =~ content[%r"\A(?:#!.*\n)?.*\n"] - if enc = ::Encoding.find($1) - content.force_encoding(enc) - end - end - end - + RDoc::Parser.set_encoding(content) content rescue Errno::EISDIR, Errno::ENOENT nil diff --git a/test/test.ja.rdoc b/test/test.ja.rdoc index 96e1db93d3..cd01cab37a 100644 --- a/test/test.ja.rdoc +++ b/test/test.ja.rdoc @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + こんにちは! 初めまして。アーロンと申します。 diff --git a/test/test_rdoc_parser.rb b/test/test_rdoc_parser.rb index ebb520032d..b97df9e542 100644 --- a/test/test_rdoc_parser.rb +++ b/test/test_rdoc_parser.rb @@ -42,6 +42,18 @@ def test_class_binary_eh_marshal File.unlink marshal end + def test_class_binary_japanese_text + file_name = File.expand_path '../test.ja.txt', __FILE__ + assert @RP.binary?(file_name) + end + + def test_class_binary_japanese_rdoc + skip "Encoding not implemented" unless defined? ::Encoding + + file_name = File.expand_path '../test.ja.rdoc', __FILE__ + assert !@RP.binary?(file_name) + end + def test_class_can_parse assert_equal @RP.can_parse(__FILE__), @RP::Ruby