Sha256: 2b136e29aec2f92eabf0090919740f46be5005f1659b9cd84aa772b4fd834fb9

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

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| self.strip_utf8_bom(f.read) }
      end

      # Takes care of removing UTF-8 byte order mark in Ruby <= 1.8 which
      # doesn't have built-in encodings support.
      def self.strip_utf8_bom(string)
        if "".respond_to?(:encoding)
          string
        else
          string.sub(/\A\xEF\xBB\xBF/, "")
        end
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jsduck-5.3.4 lib/jsduck/util/io.rb
jsduck-5.3.3 lib/jsduck/util/io.rb
jsduck-5.3.2 lib/jsduck/util/io.rb
jsduck-5.3.1 lib/jsduck/util/io.rb
jsduck-5.3.0 lib/jsduck/util/io.rb
jsduck-5.2.0 lib/jsduck/util/io.rb