twine 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- current_section = nil
54
- doc = REXML::Document.new(f)
55
- doc.elements.each('resources/string') do |ele|
56
- key = ele.attributes["name"]
57
- value = ele.text || ''
58
- value.gsub!('\\\'', '\'')
59
- value.gsub!('\\"', '"')
60
- value.gsub!(/\n/, '')
61
- value.gsub!('&lt;', '<')
62
- value.gsub!('&amp;', '&')
63
- value = iosify_substitutions(value)
64
- set_translation_for_key(key, lang, value)
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
@@ -62,6 +62,7 @@ module Twine
62
62
  key.gsub!('\\"', '"')
63
63
  value = match[2]
64
64
  value.gsub!('\\"', '"')
65
+ value = iosify_substitutions(value)
65
66
  set_translation_for_key(key, lang, value)
66
67
  if last_comment
67
68
  set_comment_for_key(key, last_comment)
@@ -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].strip)
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].strip)
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
@@ -1,3 +1,3 @@
1
1
  module Twine
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -3,6 +3,7 @@
3
3
  <!-- Generated by Twine -->
4
4
  <!-- Language: fr -->
5
5
  <resources>
6
+ <!-- This is a comment -->
6
7
  <string name="key1">key1-french</string>
7
8
  <string name="key2">key2-french</string>
8
9
  <string name="key3">key3-french</string>
@@ -2,6 +2,7 @@
2
2
  [key1]
3
3
  en = key1-english
4
4
  tags = tag1
5
+ comment = This is a comment
5
6
  es = key1-spanish
6
7
  fr = key1-french
7
8
  [key2]
@@ -0,0 +1,5 @@
1
+ [[My Strings]]
2
+ [key with space ]
3
+ en = `string with space `
4
+ tags = tag1
5
+ comment = String ends with space
@@ -4,6 +4,7 @@
4
4
  <!-- Language: fr -->
5
5
  <resources>
6
6
  <!-- My Strings -->
7
+ <!-- This is a comment -->
7
8
  <string name="key1">key1-french</string>
8
9
  <string name="key2">key2-french</string>
9
10
  <string name="key3">key3-english</string>
@@ -6,6 +6,7 @@
6
6
 
7
7
  /********** My Strings **********/
8
8
 
9
+ /* This is a comment */
9
10
  "key1" = "key1-english";
10
11
 
11
12
  "key3" = "key3-english";
@@ -2,6 +2,7 @@
2
2
  [key1]
3
3
  en = key1-english
4
4
  tags = tag1
5
+ comment = This is a comment
5
6
  es = key1-spanish
6
7
  fr = key1-french
7
8
  [key2]
@@ -6,6 +6,7 @@
6
6
  [key1]
7
7
  en = key1-english
8
8
  tags = tag1
9
+ comment = This is a comment
9
10
  es = key1-spanish
10
11
  fr = key1-french
11
12
  [key2]
@@ -6,6 +6,6 @@
6
6
  {
7
7
 
8
8
  /* My Strings */
9
- "key1":"key1-english",
9
+ "key1":"key1-english", /* This is a comment */
10
10
  "key3":"key3-english"
11
11
  }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Apple Strings File
3
+ * Generated by Twine <%= Twine::VERSION %>
4
+ * Language: en
5
+ */
6
+
7
+ /********** My Strings **********/
8
+
9
+ /* String ends with space */
10
+ "key with space " = "string with space ";
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.0
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-02 00:00:00.000000000 Z
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: []