twine 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/twine/formatters/abstract.rb +85 -0
- data/lib/twine/formatters/android.rb +37 -98
- data/lib/twine/formatters/apple.rb +1 -0
- data/lib/twine/stringsfile.rb +2 -2
- data/lib/twine/version.rb +1 -1
- data/test/fixtures/fr-1.xml +1 -0
- data/test/fixtures/strings-1.txt +1 -0
- data/test/fixtures/strings-2.txt +5 -0
- data/test/fixtures/test-output-1.txt +1 -0
- data/test/fixtures/test-output-2.txt +1 -0
- data/test/fixtures/test-output-3.txt +1 -0
- data/test/fixtures/test-output-4.txt +1 -0
- data/test/fixtures/test-output-5.txt +1 -1
- data/test/fixtures/test-output-6.txt +10 -0
- data/test/twine_test.rb +8 -0
- metadata +4 -2
@@ -13,6 +13,91 @@ module Twine
|
|
13
13
|
@options = options
|
14
14
|
end
|
15
15
|
|
16
|
+
def iosify_substitutions(str)
|
17
|
+
# 1) use "@" instead of "s" for substituting strings
|
18
|
+
str.gsub!(/%([0-9\$]*)s/, '%\1@')
|
19
|
+
|
20
|
+
# 2) if substitutions are numbered, see if we can remove the numbering safely
|
21
|
+
expectedSub = 1
|
22
|
+
startFound = false
|
23
|
+
foundSub = 0
|
24
|
+
str.each_char do |c|
|
25
|
+
if startFound
|
26
|
+
if c == "%"
|
27
|
+
# this is a literal %, keep moving
|
28
|
+
startFound = false
|
29
|
+
elsif c.match(/\d/)
|
30
|
+
foundSub *= 10
|
31
|
+
foundSub += Integer(c)
|
32
|
+
elsif c == "$"
|
33
|
+
if expectedSub == foundSub
|
34
|
+
# okay to keep going
|
35
|
+
startFound = false
|
36
|
+
expectedSub += 1
|
37
|
+
else
|
38
|
+
# the numbering appears to be important (or non-existent), leave it alone
|
39
|
+
return str
|
40
|
+
end
|
41
|
+
end
|
42
|
+
elsif c == "%"
|
43
|
+
startFound = true
|
44
|
+
foundSub = 0
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# if we got this far, then the numbering (if any) is in order left-to-right and safe to remove
|
49
|
+
if expectedSub > 1
|
50
|
+
str.gsub!(/%\d+\$(.)/, '%\1')
|
51
|
+
end
|
52
|
+
|
53
|
+
return str
|
54
|
+
end
|
55
|
+
|
56
|
+
def androidify_substitutions(str)
|
57
|
+
# 1) use "s" instead of "@" for substituting strings
|
58
|
+
str.gsub!(/%([0-9\$]*)@/, '%\1s')
|
59
|
+
|
60
|
+
# 2) if there is more than one substitution in a string, make sure they are numbered
|
61
|
+
substituteCount = 0
|
62
|
+
startFound = false
|
63
|
+
str.each_char do |c|
|
64
|
+
if startFound
|
65
|
+
if c == "%"
|
66
|
+
# ignore as this is a literal %
|
67
|
+
elsif c.match(/\d/)
|
68
|
+
# leave the string alone if it already has numbered substitutions
|
69
|
+
return str
|
70
|
+
else
|
71
|
+
substituteCount += 1
|
72
|
+
end
|
73
|
+
startFound = false
|
74
|
+
elsif c == "%"
|
75
|
+
startFound = true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
if substituteCount > 1
|
80
|
+
currentSub = 1
|
81
|
+
startFound = false
|
82
|
+
newstr = ""
|
83
|
+
str.each_char do |c|
|
84
|
+
if startFound
|
85
|
+
if !(c == "%")
|
86
|
+
newstr = newstr + "#{currentSub}$"
|
87
|
+
currentSub += 1
|
88
|
+
end
|
89
|
+
startFound = false
|
90
|
+
elsif c == "%"
|
91
|
+
startFound = true
|
92
|
+
end
|
93
|
+
newstr = newstr + c
|
94
|
+
end
|
95
|
+
return newstr
|
96
|
+
else
|
97
|
+
return str
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
16
101
|
def set_translation_for_key(key, lang, value)
|
17
102
|
if @strings.strings_map.include?(key)
|
18
103
|
@strings.strings_map[key].translations[lang] = value
|
@@ -49,19 +49,44 @@ module Twine
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def read_file(path, lang)
|
52
|
+
resources_regex = /<resources>(.*)<\/resources>/m
|
53
|
+
key_regex = /<string name="(\w+)">/
|
54
|
+
comment_regex = /<!-- (.*) -->/
|
55
|
+
value_regex = /<string name="\w+">(.*)<\/string>/
|
56
|
+
key = nil
|
57
|
+
value = nil
|
58
|
+
comment = nil
|
59
|
+
|
52
60
|
File.open(path, 'r:UTF-8') do |f|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
content_match = resources_regex.match(f.read)
|
62
|
+
if content_match
|
63
|
+
for line in content_match[1].split(/\r?\n/)
|
64
|
+
key_match = key_regex.match(line)
|
65
|
+
if key_match
|
66
|
+
key = key_match[1]
|
67
|
+
value_match = value_regex.match(line)
|
68
|
+
if value_match
|
69
|
+
value = value_match[1]
|
70
|
+
value.gsub!('\\"', '"')
|
71
|
+
value = iosify_substitutions(value)
|
72
|
+
else
|
73
|
+
value = ""
|
74
|
+
end
|
75
|
+
if @options[:tags]
|
76
|
+
set_tags_for_key(key, @options[:tags])
|
77
|
+
end
|
78
|
+
set_translation_for_key(key, lang, value)
|
79
|
+
if comment and comment.length > 0
|
80
|
+
set_comment_for_key(key, comment)
|
81
|
+
end
|
82
|
+
comment = nil
|
83
|
+
end
|
84
|
+
|
85
|
+
comment_match = comment_regex.match(line)
|
86
|
+
if comment_match
|
87
|
+
comment = comment_match[1]
|
88
|
+
end
|
89
|
+
end
|
65
90
|
end
|
66
91
|
end
|
67
92
|
end
|
@@ -124,92 +149,6 @@ module Twine
|
|
124
149
|
f.puts '</resources>'
|
125
150
|
end
|
126
151
|
end
|
127
|
-
|
128
|
-
def iosify_substitutions(str)
|
129
|
-
# 1) use "@" instead of "s" for substituting strings
|
130
|
-
str.gsub!(/%([0-9\$]*)s/, '%\1@')
|
131
|
-
|
132
|
-
# 2) if substitutions are numbered, see if we can remove the numbering safely
|
133
|
-
expectedSub = 1
|
134
|
-
startFound = false
|
135
|
-
foundSub = 0
|
136
|
-
str.each_char do |c|
|
137
|
-
if startFound
|
138
|
-
if c == "%"
|
139
|
-
# this is a literal %, keep moving
|
140
|
-
startFound = false
|
141
|
-
elsif c.match(/\d/)
|
142
|
-
foundSub *= 10
|
143
|
-
foundSub += Integer(c)
|
144
|
-
elsif c == "$"
|
145
|
-
if expectedSub == foundSub
|
146
|
-
# okay to keep going
|
147
|
-
startFound = false
|
148
|
-
expectedSub += 1
|
149
|
-
else
|
150
|
-
# the numbering appears to be important (or non-existent), leave it alone
|
151
|
-
return str
|
152
|
-
end
|
153
|
-
end
|
154
|
-
elsif c == "%"
|
155
|
-
startFound = true
|
156
|
-
foundSub = 0
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
# if we got this far, then the numbering (if any) is in order left-to-right and safe to remove
|
161
|
-
if expectedSub > 1
|
162
|
-
str.gsub!(/%\d+\$(.)/, '%\1')
|
163
|
-
end
|
164
|
-
|
165
|
-
return str
|
166
|
-
end
|
167
|
-
|
168
|
-
def androidify_substitutions(str)
|
169
|
-
# 1) use "s" instead of "@" for substituting strings
|
170
|
-
str.gsub!(/%([0-9\$]*)@/, '%\1s')
|
171
|
-
|
172
|
-
# 2) if there is more than one substitution in a string, make sure they are numbered
|
173
|
-
substituteCount = 0
|
174
|
-
startFound = false
|
175
|
-
str.each_char do |c|
|
176
|
-
if startFound
|
177
|
-
if c == "%"
|
178
|
-
# ignore as this is a literal %
|
179
|
-
elsif c.match(/\d/)
|
180
|
-
# leave the string alone if it already has numbered substitutions
|
181
|
-
return str
|
182
|
-
else
|
183
|
-
substituteCount += 1
|
184
|
-
end
|
185
|
-
startFound = false
|
186
|
-
elsif c == "%"
|
187
|
-
startFound = true
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
if substituteCount > 1
|
192
|
-
currentSub = 1
|
193
|
-
startFound = false
|
194
|
-
newstr = ""
|
195
|
-
str.each_char do |c|
|
196
|
-
if startFound
|
197
|
-
if !(c == "%")
|
198
|
-
newstr = newstr + "#{currentSub}$"
|
199
|
-
currentSub += 1
|
200
|
-
end
|
201
|
-
startFound = false
|
202
|
-
elsif c == "%"
|
203
|
-
startFound = true
|
204
|
-
end
|
205
|
-
newstr = newstr + c
|
206
|
-
end
|
207
|
-
return newstr
|
208
|
-
else
|
209
|
-
return str
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
152
|
end
|
214
153
|
end
|
215
154
|
end
|
data/lib/twine/stringsfile.rb
CHANGED
@@ -88,14 +88,14 @@ module Twine
|
|
88
88
|
if line.length > 4 && line[0, 2] == '[['
|
89
89
|
match = /^\[\[(.+)\]\]$/.match(line)
|
90
90
|
if match
|
91
|
-
current_section = StringsSection.new(match[1]
|
91
|
+
current_section = StringsSection.new(match[1])
|
92
92
|
@sections << current_section
|
93
93
|
parsed = true
|
94
94
|
end
|
95
95
|
elsif line.length > 2 && line[0, 1] == '['
|
96
96
|
match = /^\[(.+)\]$/.match(line)
|
97
97
|
if match
|
98
|
-
current_row = StringsRow.new(match[1]
|
98
|
+
current_row = StringsRow.new(match[1])
|
99
99
|
@strings_map[current_row.key] = current_row
|
100
100
|
if !current_section
|
101
101
|
current_section = StringsSection.new('')
|
data/lib/twine/version.rb
CHANGED
data/test/fixtures/fr-1.xml
CHANGED
data/test/fixtures/strings-1.txt
CHANGED
data/test/twine_test.rb
CHANGED
@@ -28,6 +28,14 @@ class TwineTest < Test::Unit::TestCase
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
def test_generate_string_file_4
|
32
|
+
Dir.mktmpdir do |dir|
|
33
|
+
output_path = File.join(dir, 'en.strings')
|
34
|
+
Twine::Runner.run(%W(generate-string-file test/fixtures/strings-2.txt #{output_path} -t tag1))
|
35
|
+
assert_equal(ERB.new(File.read('test/fixtures/test-output-6.txt')).result, File.read(output_path))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
31
39
|
def test_consume_string_file_1
|
32
40
|
Dir.mktmpdir do |dir|
|
33
41
|
output_path = File.join(dir, 'strings.txt')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubyzip
|
@@ -70,11 +70,13 @@ files:
|
|
70
70
|
- test/fixtures/en-1.strings
|
71
71
|
- test/fixtures/fr-1.xml
|
72
72
|
- test/fixtures/strings-1.txt
|
73
|
+
- test/fixtures/strings-2.txt
|
73
74
|
- test/fixtures/test-output-1.txt
|
74
75
|
- test/fixtures/test-output-2.txt
|
75
76
|
- test/fixtures/test-output-3.txt
|
76
77
|
- test/fixtures/test-output-4.txt
|
77
78
|
- test/fixtures/test-output-5.txt
|
79
|
+
- test/fixtures/test-output-6.txt
|
78
80
|
- test/twine_test.rb
|
79
81
|
homepage: https://github.com/mobiata/twine
|
80
82
|
licenses: []
|