Sha256: 4faf1250597f111a9839f03b80a4af195ad78193f69b350270dcc92b3d74ba9f

Contents?: true

Size: 1.06 KB

Versions: 33

Compression:

Stored size: 1.06 KB

Contents

module Gherkin
  module Lexer
    class Encoding
      native_impl('gherkin')

      COMMENT_OR_EMPTY_LINE_PATTERN = /^\s*#|^\s*$/
      ENCODING_PATTERN = /^\s*#\s*encoding\s*:\s*([0-9a-zA-Z\-]+)/i #:nodoc:
      DEFAULT_ENCODING = 'UTF-8'

      def read_file(path)
        source = File.new(path).read
        enc = encoding(source)
        if(enc != DEFAULT_ENCODING)
          # Read it again with different encoding
          source = File.new(path, "r:#{enc}:#{DEFAULT_ENCODING}").read
          if source.respond_to?(:encode)
            source = source.encode(DEFAULT_ENCODING)
          else
            require 'iconv'
            source = Iconv.new(DEFAULT_ENCODING, enc).iconv(source)
          end
        end
        source
      end

    private

      def encoding(source)
        encoding = DEFAULT_ENCODING
        source.each_line do |line|
          break unless COMMENT_OR_EMPTY_LINE_PATTERN =~ line
          if ENCODING_PATTERN =~ line
            encoding = $1
            break
          end
        end
        encoding.upcase
      end
    end
  end
end

Version data entries

33 entries across 31 versions & 3 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/gherkin-2.12.2/lib/gherkin/lexer/encoding.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/gherkin-2.12.2/lib/gherkin/lexer/encoding.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/gherkin-2.12.2/lib/gherkin/lexer/encoding.rb
gherkin-2.12.2 lib/gherkin/lexer/encoding.rb
gherkin-2.12.2-x86-mswin32 lib/gherkin/lexer/encoding.rb
gherkin-2.12.2-x86-mingw32 lib/gherkin/lexer/encoding.rb
gherkin-2.12.2-java lib/gherkin/lexer/encoding.rb
gherkin-2.12.1 lib/gherkin/lexer/encoding.rb
gherkin-2.12.1-x86-mswin32 lib/gherkin/lexer/encoding.rb
gherkin-2.12.1-x86-mingw32 lib/gherkin/lexer/encoding.rb
gherkin-2.12.1-java lib/gherkin/lexer/encoding.rb
librarian-puppet-0.9.9 vendor/gems/ruby/1.9.1/gems/gherkin-2.11.6/lib/gherkin/lexer/encoding.rb
gherkin-2.12.0 lib/gherkin/lexer/encoding.rb
gherkin-2.12.0-x86-mingw32 lib/gherkin/lexer/encoding.rb
gherkin-2.12.0-x86-mswin32 lib/gherkin/lexer/encoding.rb
gherkin-2.12.0-java lib/gherkin/lexer/encoding.rb
gherkin-2.11.8 lib/gherkin/lexer/encoding.rb
gherkin-2.11.8-x86-mswin32 lib/gherkin/lexer/encoding.rb
gherkin-2.11.8-x86-mingw32 lib/gherkin/lexer/encoding.rb
gherkin-2.11.8-java lib/gherkin/lexer/encoding.rb