lib/dotenv/parser.rb in dotenv-2.8.1 vs lib/dotenv/parser.rb in dotenv-3.0.0.beta

- old
+ new

@@ -1,14 +1,14 @@ require "dotenv/substitutions/variable" require "dotenv/substitutions/command" if RUBY_VERSION > "1.8.7" module Dotenv + # Error raised when encountering a syntax error while parsing a .env file. class FormatError < SyntaxError; end - # This class enables parsing of a string for key value pairs to be returned - # and stored in the Environment. It allows for variable substitutions and - # exporting of variables. + # Parses the `.env` file format into key/value pairs. + # It allows for variable substitutions, command substitutions, and exporting of variables. class Parser @substitutions = [Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command] LINE = / @@ -30,19 +30,19 @@ /x class << self attr_reader :substitutions - def call(string, is_load = false) - new(string, is_load).call + def call(...) + new(...).call end end - def initialize(string, is_load = false) + def initialize(string, overwrite: false) @string = string @hash = {} - @is_load = is_load + @overwrite = overwrite end def call # Convert line breaks to same format lines = @string.gsub(/\r\n?/, "\n") @@ -78,15 +78,19 @@ def unescape_characters(value) value.gsub(/\\([^$])/, '\1') end def expand_newlines(value) - value.gsub('\n', "\n").gsub('\r', "\r") + if (@hash["DOTENV_LINEBREAK_MODE"] || ENV["DOTENV_LINEBREAK_MODE"]) == "legacy" + value.gsub('\n', "\n").gsub('\r', "\r") + else + value.gsub('\n', "\\\\\\n").gsub('\r', "\\\\\\r") + end end def variable_not_set?(line) - !line.split[1..-1].all? { |var| @hash.member?(var) } + !line.split[1..].all? { |var| @hash.member?(var) } end def unescape_value(value, maybe_quote) if maybe_quote == '"' unescape_characters(expand_newlines(value)) @@ -98,10 +102,10 @@ end def perform_substitutions(value, maybe_quote) if maybe_quote != "'" self.class.substitutions.each do |proc| - value = proc.call(value, @hash, @is_load) + value = proc.call(value, @hash, overwrite: @overwrite) end end value end end