Sha256: 7d37acfed1405865995cc5045331098e238504d907af12e69af03e3189873c41

Contents?: true

Size: 953 Bytes

Versions: 3

Compression:

Stored size: 953 Bytes

Contents

module JsDuck
  module Util

    # A helper to use instead the builtin IO class to read files in
    # correct encoding.
    #
    # By default in Ruby 1.9 the encoding is auto-detected, which can
    # have surprising results.  So in here we read in all files in UTF-8
    # (the default) or in some other encoding specified through --encoding
    # option and convert it to UTF-8 internally.
    #
    # We also allow for UTF-8 byte order mark.
    class IO
      @@encoding = "BOM|UTF-8"

      # Sets the external encoding to be used for reading files.
      # When it's different from UTF-8, the input will be converted to UTF-8.
      def self.encoding=(e)
        if e =~ /^(BOM\|)?UTF-8$/i
          @@encoding = e
        else
          @@encoding = e+":UTF-8"
        end
      end

      # Reads given filename into string
      def self.read(filename)
        File.open(filename, "r:"+@@encoding) {|f| f.read }
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsduck-5.1.0 lib/jsduck/util/io.rb
jsduck-5.0.1 lib/jsduck/util/io.rb
jsduck-5.0.0 lib/jsduck/util/io.rb