class Gherkin::Lexer::Encoding

Constants

COMMENT_OR_EMPTY_LINE_PATTERN
DEFAULT_ENCODING

Public Instance Methods

read_file(path) click to toggle source
# File lib/gherkin/lexer/encoding.rb, line 10
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 Instance Methods

encoding(source) click to toggle source
# File lib/gherkin/lexer/encoding.rb, line 28
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