diff options
Diffstat (limited to 'lib/rdoc/encoding.rb')
-rw-r--r-- | lib/rdoc/encoding.rb | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/lib/rdoc/encoding.rb b/lib/rdoc/encoding.rb index ab752ee665..0b1ec6728e 100644 --- a/lib/rdoc/encoding.rb +++ b/lib/rdoc/encoding.rb @@ -1,7 +1,5 @@ # coding: US-ASCII -require 'rdoc' - ## # This class is a wrapper around File IO and Encoding that helps RDoc load # files and convert them to the correct encoding. @@ -27,26 +25,40 @@ module RDoc::Encoding RDoc::Encoding.set_encoding content if Object.const_defined? :Encoding then - encoding ||= Encoding.default_external - orig_encoding = content.encoding - - if utf8 then - content.force_encoding Encoding::UTF_8 - content.encode! encoding - else - # assume the content is in our output encoding - content.force_encoding encoding - end - - unless content.valid_encoding? then - # revert and try to transcode - content.force_encoding orig_encoding - content.encode! encoding - end - - unless content.valid_encoding? then - warn "unable to convert #{filename} to #{encoding}, skipping" - content = nil + begin + encoding ||= Encoding.default_external + orig_encoding = content.encoding + + if utf8 then + content.force_encoding Encoding::UTF_8 + content.encode! encoding + else + # assume the content is in our output encoding + content.force_encoding encoding + end + + unless content.valid_encoding? then + # revert and try to transcode + content.force_encoding orig_encoding + content.encode! encoding + end + + unless content.valid_encoding? then + warn "unable to convert #{filename} to #{encoding}, skipping" + content = nil + end + rescue Encoding::InvalidByteSequenceError, + Encoding::UndefinedConversionError => e + if force_transcode then + content.force_encoding orig_encoding + content.encode!(encoding, + :invalid => :replace, :undef => :replace, + :replace => '?') + return content + else + warn "unable to convert #{e.message} for #{filename}, skipping" + return nil + end end end @@ -55,15 +67,6 @@ module RDoc::Encoding raise unless e.message =~ /unknown encoding name - (.*)/ warn "unknown encoding name \"#{$1}\" for #{filename}, skipping" nil - rescue Encoding::UndefinedConversionError => e - if force_transcode then - content.force_encoding orig_encoding - content.encode! encoding, :undef => :replace, :replace => '?' - content - else - warn "unable to convert #{e.message} for #{filename}, skipping" - nil - end rescue Errno::EISDIR, Errno::ENOENT nil end |