Skip to content

Commit

Permalink
Bug #28391
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Aug 11, 2010
1 parent 8d6dd97 commit 5177623
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/rdoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/test.ja.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

こんにちは!

初めまして。アーロンと申します。
Expand Down
12 changes: 12 additions & 0 deletions test/test_rdoc_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 5177623

Please sign in to comment.