lib/dotenv/parser.rb in dotenv-2.3.0 vs lib/dotenv/parser.rb in dotenv-2.4.0
- old
+ new
@@ -10,63 +10,63 @@
class Parser
@substitutions =
[Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command]
LINE = /
+ \A
\s*
(?:export\s+)? # optional export
([\w\.]+) # key
(?:\s*=\s*|:\s+?) # separator
( # optional value begin
- '(?:\\'|[^'])*' # single quoted value
+ '(?:\'|[^'])*' # single quoted value
| # or
- "(?:\\"|[^"])*" # double quoted value
+ "(?:\"|[^"])*" # double quoted value
| # or
- [^#\r\n]+ # unquoted value
+ [^#\n]+ # unquoted value
)? # value end
\s*
(?:\#.*)? # optional comment
+ \z
/x
class << self
attr_reader :substitutions
- def call(string, is_load)
+ def call(string, is_load = false)
new(string, is_load).call
end
end
- def initialize(string, is_load)
+ def initialize(string, is_load = false)
@string = string
@hash = {}
@is_load = is_load
end
def call
- # Process matches
- @string.scan(LINE).each do |key, value|
- @hash[key] = parse_value(value || "")
- end
- # Process non-matches
- @string.gsub(LINE, "").split(/[\n\r]+/).each do |line|
+ @string.split(/[\n\r]+/).each do |line|
parse_line(line)
end
@hash
end
private
def parse_line(line)
- if line.split.first == "export"
+ if (match = line.match(LINE))
+ key, value = match.captures
+ @hash[key] = parse_value(value || "")
+ elsif line.split.first == "export"
if variable_not_set?(line)
raise FormatError, "Line #{line.inspect} has an unset variable"
end
end
end
def parse_value(value)
# Remove surrounding quotes
- value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')
+ value = value.strip.sub(/\A(['"])(.*)\1\z/, '\2')
if Regexp.last_match(1) == '"'
value = unescape_characters(expand_newlines(value))
end