i15r 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ RSpec::Matchers.define :internationalize do |non_i18n_string|
2
+ chain :to do |i18n_string|
3
+ @i18n_string = i18n_string
4
+ end
5
+
6
+ chain :to_the_same do
7
+ @i18n_string = non_i18n_string
8
+ end
9
+
10
+ match do |pattern_matcher|
11
+ @converted = pattern_matcher.run(non_i18n_string)
12
+ @converted == @i18n_string
13
+ end
14
+
15
+ failure_message_for_should do |pattern_matcher|
16
+ "#{non_i18n_string} was converted to
17
+ #{@converted} but expected
18
+ #{@i18n_string}"
19
+ end
20
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i15r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
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-12-13 00:00:00.000000000 Z
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -103,6 +103,8 @@ extra_rdoc_files: []
103
103
  files:
104
104
  - !binary |-
105
105
  LmdpdGlnbm9yZQ==
106
+ - !binary |-
107
+ LnRyYXZpcy55bWw=
106
108
  - !binary |-
107
109
  Q0hBTkdFTE9H
108
110
  - !binary |-
@@ -119,8 +121,6 @@ files:
119
121
  UkVBRE1FLm1hcmtkb3du
120
122
  - !binary |-
121
123
  UmFrZWZpbGU=
122
- - !binary |-
123
- VkVSU0lPTg==
124
124
  - !binary |-
125
125
  YmluL2kxNXI=
126
126
  - !binary |-
@@ -130,42 +130,15 @@ files:
130
130
  - !binary |-
131
131
  bGliL2kxNXIucmI=
132
132
  - !binary |-
133
- bGliL2kxNXIvYmFzZS5yYg==
134
- - !binary |-
135
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVyLnJi
136
- - !binary |-
137
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9iYXNlLnJi
138
- - !binary |-
139
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9lcmIucmI=
133
+ bGliL2kxNXIvY29uc29sZV9wcmludGVyLnJi
140
134
  - !binary |-
141
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9lcmIvcmFpbHNfaGVscGVyX21h
142
- dGNoZXIucmI=
135
+ bGliL2kxNXIvZmlsZV9yZWFkZXIucmI=
143
136
  - !binary |-
144
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9lcmIvdGFnX2F0dHJpYnV0ZV9t
145
- YXRjaGVyLnJi
137
+ bGliL2kxNXIvZmlsZV93cml0ZXIucmI=
146
138
  - !binary |-
147
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9lcmIvdGFnX2NvbnRlbnRfbWF0
148
- Y2hlci5yYg==
149
- - !binary |-
150
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9oYW1sLnJi
151
- - !binary |-
152
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9oYW1sL3JhaWxzX2hlbHBlcl9t
153
- YXRjaGVyLnJi
154
- - !binary |-
155
- bGliL2kxNXIvcGF0dGVybl9tYXRjaGVycy9oYW1sL3RhZ19jb250ZW50X21h
156
- dGNoZXIucmI=
139
+ bGliL2kxNXIvcGF0dGVybl9tYXRjaGVyLnJi
157
140
  - !binary |-
158
141
  bGliL2kxNXIvdmVyc2lvbi5yYg==
159
- - !binary |-
160
- c3BlYy9lcmIvcmFpbHNfaGVscGVyX21hdGNoZXJfc3BlYy5yYg==
161
- - !binary |-
162
- c3BlYy9lcmIvdGFnX2F0dHJpYnV0ZV9tYXRjaGVyX3NwZWMucmI=
163
- - !binary |-
164
- c3BlYy9lcmIvdGFnX2NvbnRlbnRfbWF0Y2hlcl9zcGVjLnJi
165
- - !binary |-
166
- c3BlYy9oYW1sL3JhaWxzX2hlbHBlcl9tYXRjaGVyX3NwZWMucmI=
167
- - !binary |-
168
- c3BlYy9oYW1sL3RhZ19jb250ZW50X21hdGNoZXJfc3BlYy5yYg==
169
142
  - !binary |-
170
143
  c3BlYy9pMTVyX3NwZWMucmI=
171
144
  - !binary |-
@@ -174,6 +147,8 @@ files:
174
147
  c3BlYy9zcGVjLm9wdHM=
175
148
  - !binary |-
176
149
  c3BlYy9zcGVjX2hlbHBlci5yYg==
150
+ - !binary |-
151
+ c3BlYy9zdXBwb3J0L2ludGVybmF0aW9uYWxpemVfbWF0Y2hlci5yYg==
177
152
  - !binary |-
178
153
  c3BlYy9zdXBwb3J0L25ld19mbGlnaHQuaHRtbC5lcmI=
179
154
  - !binary |-
@@ -196,7 +171,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
196
171
  version: '0'
197
172
  segments:
198
173
  - 0
199
- hash: 2900339974465294939
174
+ hash: -1728217150205918953
200
175
  required_rubygems_version: !ruby/object:Gem::Requirement
201
176
  none: false
202
177
  requirements:
@@ -205,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
180
  version: '0'
206
181
  segments:
207
182
  - 0
208
- hash: 2900339974465294939
183
+ hash: -1728217150205918953
209
184
  requirements: []
210
185
  rubyforge_project:
211
186
  rubygems_version: 1.8.23
@@ -213,16 +188,6 @@ signing_key:
213
188
  specification_version: 3
214
189
  summary: Eases the pain of moving to I18n view templates
215
190
  test_files:
216
- - !binary |-
217
- c3BlYy9lcmIvcmFpbHNfaGVscGVyX21hdGNoZXJfc3BlYy5yYg==
218
- - !binary |-
219
- c3BlYy9lcmIvdGFnX2F0dHJpYnV0ZV9tYXRjaGVyX3NwZWMucmI=
220
- - !binary |-
221
- c3BlYy9lcmIvdGFnX2NvbnRlbnRfbWF0Y2hlcl9zcGVjLnJi
222
- - !binary |-
223
- c3BlYy9oYW1sL3JhaWxzX2hlbHBlcl9tYXRjaGVyX3NwZWMucmI=
224
- - !binary |-
225
- c3BlYy9oYW1sL3RhZ19jb250ZW50X21hdGNoZXJfc3BlYy5yYg==
226
191
  - !binary |-
227
192
  c3BlYy9pMTVyX3NwZWMucmI=
228
193
  - !binary |-
@@ -231,6 +196,8 @@ test_files:
231
196
  c3BlYy9zcGVjLm9wdHM=
232
197
  - !binary |-
233
198
  c3BlYy9zcGVjX2hlbHBlci5yYg==
199
+ - !binary |-
200
+ c3BlYy9zdXBwb3J0L2ludGVybmF0aW9uYWxpemVfbWF0Y2hlci5yYg==
234
201
  - !binary |-
235
202
  c3BlYy9zdXBwb3J0L25ld19mbGlnaHQuaHRtbC5lcmI=
236
203
  - !binary |-
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.4.3
@@ -1,123 +0,0 @@
1
- require "optparse"
2
- require "ostruct"
3
-
4
- class AppFolderNotFound < Exception; end
5
-
6
- module I15R
7
- class Base
8
- attr_reader :options
9
-
10
- def self.get_i18n_message_string(text, prefix)
11
- #TODO: downcase does not work properly for accented chars, like 'Ú'
12
- #TODO: [:punct:] would be nice but it includes _ which we don't want to remove
13
- key = text.strip.downcase.gsub(/[\s\/]+/, '_').gsub(/[!?.,:"';()]/, '')
14
- indent = ""
15
- (0..prefix.split(".").size).each { |i| indent = " " + indent }
16
- "#{prefix}.#{key}"
17
- end
18
-
19
- def initialize
20
- @options = OpenStruct.new
21
- @options.prefix = nil
22
- @options
23
- end
24
-
25
- def parse_options(args)
26
- opts = OptionParser.new do |opts|
27
- opts.banner = "Usage: ruby i15r.rb [options] <path_to_internationalize>"
28
- opts.on("--prefix PREFIX",
29
- "apply PREFIX to generated I18n messages instead of deriving it from the path") do |prefix|
30
- @options.prefix = prefix
31
- end
32
- end
33
-
34
- if args.length.zero?
35
- puts opts.banner
36
- exit
37
- end
38
-
39
- opts.on_tail("-p", "--pretend", "Do not write the files, just show what would be replaced") do
40
- @options.dry_run = true
41
- end
42
-
43
- opts.on_tail("-h", "--help", "Show this message") do
44
- puts opts
45
- exit
46
- end
47
-
48
- opts.on_tail("--version", "Show version") do
49
- puts "0.0.1"
50
- exit
51
- end
52
-
53
- opts.parse!(args)
54
- # @options
55
- end
56
-
57
- def prefix
58
- @options.prefix
59
- end
60
-
61
- def dry_run?
62
- !!@options.dry_run
63
- end
64
-
65
- def file_path_to_message_prefix(file)
66
- segments = File.expand_path(file).split('/').select { |segment| !segment.empty? }
67
- subdir = %w(views helpers controllers models).find do |app_subdir|
68
- segments.index(app_subdir)
69
- end
70
- if subdir.nil?
71
- raise AppFolderNotFound, "No app. subfolders were found to determine prefix. Path is #{File.expand_path(file)}"
72
- end
73
- first_segment_index = segments.index(subdir) + 1
74
- file_name_without_extensions = segments.last.split('.')[0..0]
75
- path_segments = segments.slice(first_segment_index...-1)
76
- (path_segments + file_name_without_extensions).join('.')
77
- end
78
-
79
- def get_content_from(file)
80
- File.read(File.expand_path(file))
81
- end
82
-
83
- def write_content_to(path, content)
84
- open(File.expand_path(path), "w") { |f| f.write(content) }
85
- end
86
-
87
- def show_diff(plain_row, i9l_row)
88
- $stdout.puts "- #{plain_row}"
89
- $stdout.puts "+ #{i9l_row}"
90
- $stdout.puts
91
- end
92
-
93
- def internationalize_file(path)
94
- text = get_content_from(path)
95
- prefix = self.prefix || file_path_to_message_prefix(path)
96
- template_type = path[/(?:.*)\.(.*)$/, 1]
97
- i18ned_text = sub_plain_strings(text, prefix, template_type.to_sym)
98
- write_content_to(path, i18ned_text) unless dry_run?
99
- end
100
-
101
- def display_indented_header(prefix)
102
- puts "en:"
103
- prefix_parts = prefix.split(".").each_with_index do |p, i|
104
- p = "#{p}:"
105
- #TODO: perhaps " "*i is simpler
106
- (0..i).each { |i| p = " " + p }
107
- puts "#{p}"
108
- end
109
- end
110
-
111
- def sub_plain_strings(text, prefix, file_type)
112
- i15d = I15R::PatternMatchers::Base.run(text, prefix, file_type) do |plain_row, i9l_row|
113
- show_diff(plain_row, i9l_row)
114
- end
115
- i15d + "\n"
116
- end
117
-
118
- def internationalize!(path)
119
- files = File.directory?(path) ? Dir.glob("#{path}/**/*.{erb,haml}") : [path]
120
- files.each { |file| internationalize_file(file) }
121
- end
122
- end
123
- end
@@ -1,40 +0,0 @@
1
- module I15R
2
- module PatternMatchers
3
- class Base
4
- # def self.inherited(descendant)
5
- # descendant.methods(false).grep /^match/ do |matcher_method|
6
- # descendant.method(matcher_method).call
7
- # end
8
- # end
9
-
10
- def self.matches(file_type, &block)
11
- @@matchers ||= {}
12
- @@matchers[file_type] ||= []
13
- @@matchers[file_type].push(block)
14
- end
15
-
16
- def self.run(text, prefix, file_type)
17
- lines = text.split("\n")
18
- i18ned_lines = lines.map do |line|
19
- @@matchers[file_type].inject(line) do |i18ned_line, matcher|
20
- m = matcher.call(i18ned_line, prefix)
21
- unless m.nil?
22
- row_before_match, row_after_match = m
23
- yield row_before_match, row_after_match if block_given?
24
- row_after_match
25
- else
26
- i18ned_line
27
- end
28
- end
29
- end
30
- i18ned_lines.join("\n")
31
- end
32
-
33
- #TODO: use method_added to add to matchers so that
34
- # matchers do not have to be explicitly registered.
35
- def self.register_matcher(matcher_method)
36
- self.method(matcher_method).call
37
- end
38
- end
39
- end
40
- end
@@ -1,10 +0,0 @@
1
- module I15R
2
- module PatternMatchers
3
- module Erb
4
- end
5
- end
6
- end
7
-
8
- require "i15r/pattern_matchers/erb/rails_helper_matcher"
9
- require "i15r/pattern_matchers/erb/tag_content_matcher"
10
- require "i15r/pattern_matchers/erb/tag_attribute_matcher"
@@ -1,75 +0,0 @@
1
- require 'i15r/base'
2
-
3
- module I15R
4
- module PatternMatchers
5
- module Erb
6
- class RailsHelperMatcher < Base
7
-
8
- def self.run(text, prefix)
9
- super(text, prefix, :erb)
10
- end
11
-
12
- def self.match_link_to_title
13
- patt = /^(.*)<%=\s*link_to\s+['"](.*?)['"]\s*,(.*)%>(.*)$/
14
- matches(:erb) do |text, prefix|
15
- if m = patt.match(text)
16
- i18n_string = I15R::Base.get_i18n_message_string(m[2], prefix)
17
- i18ned_row = %(#{m[1]}<%= link_to I18n.t("#{i18n_string}"),#{m[3]}%>#{m[4]})
18
- [m[0], i18ned_row]
19
- end
20
- end
21
- end
22
- register_matcher :match_link_to_title
23
-
24
- def self.match_label_helper_text
25
- patt = /^(.*)<%=(.*)\.label(.*),\s*['"](.*?)['"]\s*%>(.*)$/
26
- matches(:erb) do |text, prefix|
27
- if m = patt.match(text)
28
- i18n_string = I15R::Base.get_i18n_message_string(m[4], prefix)
29
- i18ned_row = %(#{m[1]}<%=#{m[2]}.label#{m[3]}, I18n.t("#{i18n_string}") %>#{m[5]})
30
- [m[0], i18ned_row]
31
- end
32
- end
33
- end
34
- register_matcher :match_label_helper_text
35
-
36
- def self.match_label_tag_helper_text
37
- patt = /^(.*)<%=(.*)label_tag (.*),\s*['"](.*?)['"]\s*%>(.*)$/
38
- matches(:erb) do |text, prefix|
39
- if m = patt.match(text)
40
- i18n_string = I15R::Base.get_i18n_message_string(m[4], prefix)
41
- i18ned_row = %(#{m[1]}<%=#{m[2]}label_tag #{m[3]}, I18n.t("#{i18n_string}") %>#{m[5]})
42
- [m[0], i18ned_row]
43
- end
44
- end
45
- end
46
- register_matcher :match_label_tag_helper_text
47
-
48
- def self.match_submit_helper_text
49
- patt = /^(.*)<%=(.*)\.submit\s*['"](.*?)['"]\s*%>(.*)$/
50
- matches(:erb) do |text, prefix|
51
- if m = patt.match(text)
52
- i18n_string = I15R::Base.get_i18n_message_string(m[3], prefix)
53
- i18ned_row = %(#{m[1]}<%=#{m[2]}.submit I18n.t("#{i18n_string}") %>#{m[4]})
54
- [m[0], i18ned_row]
55
- end
56
- end
57
- end
58
- register_matcher :match_submit_helper_text
59
-
60
- def self.match_submit_tag_helper_text
61
- patt = /^(.*)<%=\s*submit_tag\s*['"](.*?)['"]\s*%>(.*)$/
62
- matches(:erb) do |text, prefix|
63
- if m = patt.match(text)
64
- i18n_string = I15R::Base.get_i18n_message_string(m[2], prefix)
65
- i18ned_row = %(#{m[1]}<%= submit_tag I18n.t("#{i18n_string}") %>#{m[3]})
66
- [m[0], i18ned_row]
67
- end
68
- end
69
- end
70
- register_matcher :match_submit_tag_helper_text
71
-
72
- end
73
- end
74
- end
75
- end
@@ -1,26 +0,0 @@
1
- require 'i15r/base'
2
-
3
- module I15R
4
- module PatternMatchers
5
- module Erb
6
- class TagAttributeMatcher < Base
7
-
8
- def self.run(text, prefix)
9
- super(text, prefix, :erb)
10
- end
11
-
12
- def self.match_title_attribute
13
- patt = /^(.*)(<a\s+.*title=)['"](.*?)['"](.*)/
14
- matches(:erb) do |text, prefix|
15
- if m = patt.match(text)
16
- i18n_string = I15R::Base.get_i18n_message_string(m[3], prefix)
17
- i18ned_row = %(#{m[1]}#{m[2]}"<%= I18n.t("#{i18n_string}") %>"#{m[4]})
18
- [m[0], i18ned_row]
19
- end
20
- end
21
- end
22
- register_matcher :match_title_attribute
23
- end
24
- end
25
- end
26
- end
@@ -1,40 +0,0 @@
1
- require 'i15r/base'
2
-
3
- module I15R
4
- module PatternMatchers
5
- module Erb
6
- class TagContentMatcher < Base
7
-
8
- def self.run(text, prefix)
9
- super(text, prefix, :erb)
10
- end
11
-
12
- def self.match_tag_content_on_one_line
13
- patt = /^(.*)>(\s*)([[:alnum:][:upper:]][\s[:alnum:][:upper:][:punct:]]+)\s*<\/(.*)$/i
14
- matches(:erb) do |text, prefix|
15
- if m = patt.match(text)
16
- i18n_string = I15R::Base.get_i18n_message_string(m[3], prefix)
17
- ending_punctuation = m[3][/([?.!:\s]*)$/, 1]
18
- i15d_row = %(#{m[1]}>#{m[2]}<%= I18n.t("#{i18n_string}") %>#{ending_punctuation.to_s}</#{m[4]})
19
- [m[0], i15d_row]
20
- end
21
- end
22
- end
23
- register_matcher :match_tag_content_on_one_line
24
-
25
- def self.match_tag_content_multiline
26
- patt = /^(\s*)([[:alnum:][:upper:]][[:alnum:][:upper:][:space:],']*)(.*)$/i
27
- matches(:erb) do |text, prefix|
28
- if m = patt.match(text)
29
- i18n_string = I15R::Base.get_i18n_message_string(m[2], prefix)
30
- i15d_row = %(#{m[1]}<%= I18n.t("#{i18n_string}") %>#{m[3]})
31
- [m[0], i15d_row]
32
- end
33
- end
34
- end
35
- register_matcher :match_tag_content_multiline
36
-
37
- end
38
- end
39
- end
40
- end