Skip to content

Commit

Permalink
finalized multiline-support
Browse files Browse the repository at this point in the history
it's now possible to contain newlines in a parameter if it's enclosed by "
  • Loading branch information
André Gawron committed Jan 27, 2011
1 parent b927742 commit 6b725dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
19 changes: 10 additions & 9 deletions lib/inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def initialize( filename, opts = {} )
@rgxp_section = %r/\A\s*\[([^\]]+)\]/o
@rgxp_param = %r/\A([^#{@param}]+)#{@param}(.*)\z/

@rgxp_multiline_start = %r/\A(?<param>[^#{@param}]+)#{@param}\s*"(?<value>.*)\z/
@rgxp_multiline_value = %r/\A(?<value>[^"]*)\z/
@rgxp_multiline_end = %r/\A(?<value>[^"]*)"\z/
@rgxp_multiline_start = %r/\A(?<param>[^#{@param}]+)#{@param}\s*?"+(?<value>.*)\z/
@rgxp_multiline_value = %r/\A(?<value>.*[^"])\z/
@rgxp_multiline_end = %r/\A(?<value>.*)"\z/

parse
end
Expand Down Expand Up @@ -297,14 +297,14 @@ def parse
tmp_param = $~[:param].strip
tmp_value = $~[:value] + "\n"

elsif line =~ @rgxp_multiline_value && tmp_param != "" then

tmp_value += $~[:value] + "\n"

elsif line =~ @rgxp_multiline_end && tmp_param != "" then

section[tmp_param] = tmp_value + $~[:value]
tmp_value, tmp_param = ""
tmp_value, tmp_param = "", ""

elsif line =~ @rgxp_multiline_value && tmp_param != "" then

tmp_value += $~[:value] + "\n"

# ignore blank lines and comment lines
elsif line =~ @rgxp_comment then
Expand All @@ -319,7 +319,7 @@ def parse
# otherwise we have a parameter
elsif line =~ @rgxp_param then

begin
begin
section[$1.strip] = $2.strip
rescue NoMethodError
raise Error, "parameter encountered before first section"
Expand All @@ -330,6 +330,7 @@ def parse
end
end # while
end # File.open

end

end # class IniFile
Expand Down
17 changes: 17 additions & 0 deletions test/data/multiline.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[section_one]
one = 1
two = 2

[section_two]
three = 3

; comments should be ignored
[section_three]
three = "hello
multiline"
other = stuff

[section_four]
four = "hello
multiple
multilines"
10 changes: 5 additions & 5 deletions test/test_inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'inifile'
rescue LoadError
require 'rubygems'
require '/home/melkon/rb/inifile/lib/inifile'
require 'inifile'
end

require 'fileutils'
Expand Down Expand Up @@ -335,12 +335,12 @@ def test_multiline_parsing

ini_file = IniFile.load('test/data/multiline.ini')

multiline = ini_file['section_two']
expected = {"four" => "hello\nmultiline"}
multiline = ini_file['section_three']
expected = {"three" => "hello\nmultiline", "other" => "stuff"}
assert_equal expected, multiline

multiple = ini_file['section_three']
expected = {"three" => "hello\nmultiple\nmultilines"}
multiple = ini_file['section_four']
expected = {"four" => "hello\nmultiple\nmultilines"}
assert_equal expected, multiple

end
Expand Down

0 comments on commit 6b725dd

Please sign in to comment.