Sha256: 0fc2fe06d06386ed0b8ae1ae587a1cf1d4c413ba9294ee1c9640860d2921db94

Contents?: true

Size: 1.39 KB

Versions: 16

Compression:

Stored size: 1.39 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"

      # Configures the encoding from command line options.
      def self.configure(opts)
        encoding = opts.encoding if opts.encoding
      end

      # 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

16 entries across 16 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/util/io.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/util/io.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/util/io.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/util/io.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/util/io.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/util/io.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.10 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.9 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.8 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.7 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.5 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.4 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.3 lib/jsduck/util/io.rb
jsduck-troopjs-0.0.1 lib/jsduck/util/io.rb
jsduck-6.0.0beta lib/jsduck/util/io.rb