dot-properties 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/dot_properties.rb +14 -6
- data/lib/dot_properties/version.rb +1 -1
- data/spec/dot_properties_spec.rb +13 -3
- data/spec/fixtures/sample.properties +4 -0
- metadata +1 -1
data/README.md
CHANGED
data/lib/dot_properties.rb
CHANGED
@@ -111,7 +111,7 @@ class DotProperties
|
|
111
111
|
if item[:value].nil? or item[:value].empty?
|
112
112
|
escape(item[:key])
|
113
113
|
else
|
114
|
-
"#{escape(item[:key])}#{item[:delimiter]}#{item[:value]}"
|
114
|
+
"#{escape(item[:key])}#{item[:delimiter]}#{encode(item[:value])}"
|
115
115
|
end
|
116
116
|
else
|
117
117
|
item[:value]
|
@@ -125,7 +125,7 @@ class DotProperties
|
|
125
125
|
{ type: :blank, value: item }
|
126
126
|
else
|
127
127
|
key, delimiter, value = item.strip.split /(\s*(?<!\\)[\s:=]\s*)/, 2
|
128
|
-
{ type: :value, key: unescape(key), delimiter: delimiter, value: value.to_s.gsub(/\\\n\s*/,'') }
|
128
|
+
{ type: :value, key: unescape(decode(key)), delimiter: delimiter, value: unescape(decode(value.to_s.gsub(/\\\n\s*/,''))) }
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
@@ -133,12 +133,20 @@ class DotProperties
|
|
133
133
|
@content.find { |item| item[:type] == :value and item[:key] == key }
|
134
134
|
end
|
135
135
|
|
136
|
-
def
|
137
|
-
|
136
|
+
def encode(v)
|
137
|
+
v.gsub(/[\r\n]/) { |m| "\\u#{'%4.4X' % m.codepoints.to_a}" }.gsub(/\\/,'\\'*3)
|
138
138
|
end
|
139
139
|
|
140
|
-
def
|
141
|
-
|
140
|
+
def decode(v)
|
141
|
+
v.gsub(/\\u([0-9A-Fa-f]{4})/) { |m| $1.hex.chr('UTF-8') }
|
142
|
+
end
|
143
|
+
|
144
|
+
def escape(v)
|
145
|
+
v.gsub(/[\s:=\\]/) { |m| "\\#{m}" }
|
146
|
+
end
|
147
|
+
|
148
|
+
def unescape(v)
|
149
|
+
v.gsub(/(?<!\\)\\/,'')
|
142
150
|
end
|
143
151
|
|
144
152
|
end
|
data/spec/dot_properties_spec.rb
CHANGED
@@ -10,9 +10,9 @@ describe DotProperties do
|
|
10
10
|
let(:propfile) { File.expand_path('../fixtures/sample.properties', __FILE__) }
|
11
11
|
subject { DotProperties.load(propfile) }
|
12
12
|
|
13
|
-
let(:properties) {
|
14
|
-
let(:comments) {
|
15
|
-
let(:blanks) {
|
13
|
+
let(:properties) { 21 }
|
14
|
+
let(:comments) { 16 }
|
15
|
+
let(:blanks) { 11 }
|
16
16
|
|
17
17
|
it { should be_an_instance_of(DotProperties) }
|
18
18
|
|
@@ -110,6 +110,16 @@ describe DotProperties do
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
+
describe "content escapes" do
|
114
|
+
it "should parse Java-escaped unicode" do
|
115
|
+
expect(subject['unicode']).to eq("Command\t\u2318\nOption\t\u2325")
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should unescape escaped entities" do
|
119
|
+
expect(subject['key with:several=escapes']).to eq('value with#several\escapes')
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
113
123
|
describe "additional methods" do
|
114
124
|
it "#<<" do
|
115
125
|
subject << ""
|
@@ -43,6 +43,10 @@ bar.empty=
|
|
43
43
|
baz.empty:
|
44
44
|
quux.empty =
|
45
45
|
|
46
|
+
# Escapes
|
47
|
+
unicode=Command\u0009\u2318\u000AOption\u0009\u2325
|
48
|
+
key\ with\:several\=es\c\apes:value\ with\#several\\escapes
|
49
|
+
|
46
50
|
# References
|
47
51
|
present = This value contains two resolvable references, ${foo.normal} and ${bar.extraspace}
|
48
52
|
missing = This value contains one resolvable reference, ${foo.normal}, and one unresolvable reference, ${quux.missing}
|