Class: Gherkin::Lexer::Encoding

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin/lexer/encoding.rb

Constant Summary collapse

COMMENT_OR_EMPTY_LINE_PATTERN =
/^\s*#|^\s*$/
ENCODING_PATTERN =

:nodoc:

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

Instance Method Summary collapse

Instance Method Details

#read_file(path) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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