Module: CommentExtractor::Encoding
- Defined in:
- lib/comment_extractor/encoding.rb
Class Method Summary collapse
- .encode(content, encoding = ::Encoding.default_external) ⇒ Object
- .read_file(file_path, encoding = ::Encoding.default_external) ⇒ Object
Class Method Details
.encode(content, encoding = ::Encoding.default_external) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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) # When the content contains bom, it is UTF-8 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 end |
.read_file(file_path, encoding = ::Encoding.default_external) ⇒ Object
3 4 5 6 |
# File 'lib/comment_extractor/encoding.rb', line 3 def self.read_file(file_path, encoding = ::Encoding.default_external) content = File.open(file_path, 'rb') { |f| f.read } self.encode(content) end |