lib/dotenv/parser.rb in dotenv-2.7.6 vs lib/dotenv/parser.rb in dotenv-2.8.0
- old
+ new
@@ -13,11 +13,11 @@
LINE = /
(?:^|\A) # beginning of line
\s* # leading whitespace
(?:export\s+)? # optional export
- ([\w\.]+) # key
+ ([\w.]+) # key
(?:\s*=\s*?|:\s+?) # separator
( # optional value begin
\s*'(?:\\'|[^'])*' # single quoted value
| # or
\s*"(?:\\"|[^"])*" # double quoted value
@@ -68,21 +68,13 @@
end
def parse_value(value)
# Remove surrounding quotes
value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')
-
- if Regexp.last_match(1) == '"'
- value = unescape_characters(expand_newlines(value))
- end
-
- if Regexp.last_match(1) != "'"
- self.class.substitutions.each do |proc|
- value = proc.call(value, @hash, @is_load)
- end
- end
- value
+ maybe_quote = Regexp.last_match(1)
+ value = unescape_value(value, maybe_quote)
+ perform_substitutions(value, maybe_quote)
end
def unescape_characters(value)
value.gsub(/\\([^$])/, '\1')
end
@@ -90,9 +82,28 @@
def expand_newlines(value)
value.gsub('\n', "\n").gsub('\r', "\r")
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))
+ elsif maybe_quote.nil?
+ unescape_characters(value)
+ else
+ value
+ end
+ end
+
+ def perform_substitutions(value, maybe_quote)
+ if maybe_quote != "'"
+ self.class.substitutions.each do |proc|
+ value = proc.call(value, @hash, @is_load)
+ end
+ end
+ value
end
end
end