8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/comment_extractor/encoding.rb', line 8
def self.encode(content, encoding = ::Encoding.default_external)
windows_platforms = Regexp.new(%w[mingw mswin].join('|'))
content.gsub!("\r\n", "\n") if RUBY_PLATFORM =~ windows_platforms
original_encoding = content.encoding
if strip_bom(content) content.force_encoding(::Encoding::UTF_8)
content.encode!(encoding)
else
content.force_encoding(encoding)
end
unless content.valid_encoding?
content.force_encoding(original_encoding)
content.encode!(encoding)
end
unless content.valid_encoding?
raise "Unable to convert #{file_path} to #{encoding}"
end
content
rescue ArgumentError
nil
end
|