haml-i18n-extractor 0.5.4 → 0.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,8 @@ Gem::Specification.new do |gem|
26
26
  gem.add_development_dependency 'rbench'
27
27
  gem.add_development_dependency 'm'
28
28
  gem.add_development_dependency 'pry'
29
+ gem.add_development_dependency 'pry-remote'
30
+ gem.add_development_dependency 'pry-nav'
29
31
  gem.add_development_dependency 'minitest'
30
32
  gem.add_development_dependency 'nokogiri'
31
33
  gem.add_development_dependency 'rake'
@@ -1,5 +1,7 @@
1
- Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?(:default_internal)
2
- Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?(:default_external)
1
+ if defined?(Encoding)
2
+ Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?(:default_internal)
3
+ Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?(:default_external)
4
+ end
3
5
 
4
6
  require "trollop"
5
7
 
@@ -17,7 +17,7 @@ module Haml
17
17
  class AbortFile < StandardError ; end
18
18
 
19
19
  LINE_TYPES_ALL = [:plain, :script, :silent_script, :haml_comment, :tag, :comment, :doctype, :filter, :root]
20
- LINE_TYPES_ADD_EVAL = [:plain, :tag]
20
+ LINE_TYPES_ADD_EVAL = [:plain, :tag, :script]
21
21
 
22
22
  attr_reader :haml_reader, :haml_writer
23
23
  attr_reader :info_for_yaml, :yaml_writer, :type
@@ -20,7 +20,7 @@ module Haml
20
20
 
21
21
  def interpolated_vars
22
22
  interpolations.map{|v|
23
- ":#{normalized_name(v.dup)} => #{v}"
23
+ ":#{normalized_name(v.dup)} => (#{v})"
24
24
  }.join(", ")
25
25
  end
26
26
 
@@ -33,14 +33,18 @@ module Haml
33
33
  Haml::I18n::Extractor::InterpolationHelper.new(@text_to_replace, t_name)
34
34
  end
35
35
 
36
+ def orig_interpolated?
37
+ interpolated?(@orig_line)
38
+ end
39
+
36
40
  # the new full line, including a `t()` replacement instead of the `text_to_replace` portion.
37
41
  def modified_line
38
42
  return @full_line if has_been_translated?(@full_line)
39
43
  full_line = @full_line.dup
40
44
  #puts t_method.inspect if Haml::I18n::Extractor.debug?
41
- keyname = interpolated?(full_line) ? interpolation_helper.keyname_with_vars : t_method
42
- gsub_replacement!(full_line, @text_to_replace, @orig_line, keyname)
43
- apply_ruby_evaling(full_line)
45
+ keyname = orig_interpolated? ? interpolation_helper.keyname_with_vars : t_method
46
+ gsub_replacement!(full_line, @text_to_replace, keyname)
47
+ apply_ruby_evaling!(full_line, keyname)
44
48
  full_line
45
49
  end
46
50
 
@@ -71,32 +75,45 @@ module Haml
71
75
  end
72
76
 
73
77
  # adds the = to the right place in the string ... = t()
74
- def apply_ruby_evaling(str)
78
+ def apply_ruby_evaling!(str, keyname)
75
79
  if LINE_TYPES_ADD_EVAL.include?(@line_type)
76
80
  if @line_type == :tag
77
- match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(t_method))
81
+ match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(keyname))
78
82
  str.match(/(.*?)(#{match_keyname})/)
79
83
  elem = $1
80
84
  if elem
81
85
  str.gsub!(Regexp.new(Regexp.escape(elem)), "#{elem}=") unless already_evaled?(elem)
82
86
  end
83
- elsif @line_type == :plain
87
+ elsif @line_type == :plain || (@line_type == :script && !already_evaled?(full_line))
84
88
  str.gsub!(str, "= "+str)
85
89
  end
86
90
  end
87
91
  end
88
92
 
89
- def already_evaled?(elem)
90
- # poor elem.split('').last == '='
91
- # better, haml guts:
92
- @metadata[:value] && @metadata[:value][:parse]
93
+ def already_evaled?(str)
94
+ if @line_type == :tag
95
+ if orig_interpolated?
96
+ # for tags that come in interpolated we need to explicitly
97
+ # check that they aren't evaled alreay, the metadata lies
98
+ # %tag foo #{var} bar
99
+ str.split('').last == '='
100
+ else
101
+ @metadata[:value] && @metadata[:value][:parse]
102
+ end
103
+ elsif @line_type == :script
104
+ # we need this for tags that come in like :plain but have interpolation
105
+ str.match(/^[\s\t]*=/)
106
+ end
93
107
  end
94
108
 
95
109
  def has_been_translated?(str)
96
110
  str.match T_REGEX
97
111
  end
98
112
 
99
- def gsub_replacement!(str, text_to_replace, orig_line, keyname_method )
113
+ def gsub_replacement!(str, text_to_replace, keyname_method)
114
+ # FIXME refactor this method
115
+ text_to_replace = $1 if (orig_interpolated? && text_to_replace.match(/^['"](.*)['"]$/))
116
+
100
117
  # if there are quotes surrounding the string, we want them removed as well...
101
118
  unless str.gsub!('"' + text_to_replace + '"', keyname_method )
102
119
  unless str.gsub!("'" + text_to_replace + "'", keyname_method)
@@ -6,7 +6,7 @@ module Haml
6
6
  module StringHelpers
7
7
 
8
8
  # do not pollute the key space it will make it invalid yaml
9
- NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : ; \ / . )
9
+ NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : ; \\ / . | )
10
10
  # limit the number of chars
11
11
  LIMIT_KEY_NAME = 30
12
12
 
@@ -1,7 +1,7 @@
1
1
  module Haml
2
2
  module I18n
3
3
  class Extractor
4
- VERSION = "0.5.4"
4
+ VERSION = "0.5.5"
5
5
  end
6
6
  end
7
7
  end
@@ -15,7 +15,7 @@ module Haml
15
15
  replace = "this may \#{be} the \#{dup}"
16
16
  t_name = "this_may_be_the_dup"
17
17
  helper = Haml::I18n::Extractor::InterpolationHelper.new(replace, t_name)
18
- assert helper.keyname_with_vars == "t('.this_may_be_the_dup', :be => be, :dup => dup)",
18
+ assert helper.keyname_with_vars == "t('.this_may_be_the_dup', :be => (be), :dup => (dup))",
19
19
  "returns custom t() with vars"
20
20
  end
21
21
 
@@ -26,7 +26,7 @@ module Haml
26
26
  assert helper.interpolations == ["is_this_hard?"], "can catch the interpolations"
27
27
  x = helper.keyname_with_vars
28
28
  #puts x.inspect
29
- assert x == "t('.is_this_hard_what', :is_this_hard => is_this_hard?)",
29
+ assert x == "t('.is_this_hard_what', :is_this_hard => (is_this_hard?))",
30
30
  "returns custom t() with vars"
31
31
  end
32
32
 
@@ -8,7 +8,7 @@
8
8
  %h3
9
9
  = t('.all_invoices_billable')
10
10
  %span{:style => (@billable_invoices == @active_invoices) ? "color: #090" : "color: #900"}
11
- =t('.billable_invoices_out_of_activ', :billable_invoices => @billable_invoices, :active_invoices => @active_invoices)
11
+ =t('.billable_invoices_out_of_activ', :billable_invoices => (@billable_invoices), :active_invoices => (@active_invoices))
12
12
  %h3
13
13
  = t('.24_hours_past_end_of_billing_m')
14
14
  %span{:style => (@billing_month.past_cutoff) ? "color: #090" : "color: #900"}
@@ -11,3 +11,5 @@
11
11
  %table(style="width:400px")
12
12
  %td (?)
13
13
  %td \#{@account.send(attr) || '(none)'}
14
+
15
+ %p#brand= link_to 'Some Place', '/'
@@ -6,8 +6,10 @@
6
6
 
7
7
  %span.creator{:title => "This user is an= t('.owner') of this account"} t('.owner')
8
8
  %span.title&= user.name
9
- %strong \#{@account.name}
9
+ %strong= t('.accountname', :accountname => (@account.name))
10
10
 
11
11
  %table(style="width:400px")
12
12
  %td= t('.td')
13
- %td \#{@account.send(attr) || '(none)'}
13
+ %td= t('.accountsendattr_none', :accountsendattr_none => (@account.send(attr) || '(none)'))
14
+
15
+ %p#brand= link_to t('.some_place'), '/'
@@ -3,3 +3,5 @@
3
3
 
4
4
  = "this may #{be} the #{dup}"
5
5
  = link_to "#{ad.name.first}", url_for([ad, :ok])
6
+
7
+ No quotes #{some_var} with text
@@ -1,5 +1,7 @@
1
1
  .class= t('.text_here')
2
- .other-class#id= t('.script_with_quote_with_interpo', :with_interpolationbub => with_interpolation(bub))
2
+ .other-class#id= t('.script_with_quote_with_interpo', :with_interpolationbub => (with_interpolation(bub)))
3
3
 
4
- =t('.this_may_be_the_dup', :be => be, :dup => dup)
5
- = link_to t('.adnamefirst', :adnamefirst => ad.name.first), url_for([ad, :ok])
4
+ =t('.this_may_be_the_dup', :be => (be), :dup => (dup))
5
+ = link_to t('.adnamefirst', :adnamefirst => (ad.name.first)), url_for([ad, :ok])
6
+
7
+ = t('.no_quotes_some_var_with_text', :some_var => (some_var))
data/test/support/ex5.yml CHANGED
@@ -7,3 +7,4 @@ en:
7
7
  interpolated!"'
8
8
  this_may_be_the_dup: ! ' "this may %{be} the %{dup}"'
9
9
  adnamefirst: ! '%{adnamefirst}'
10
+ no_quotes_some_var_with_text: ! '"No quotes %{some_var} with text"'
@@ -43,16 +43,6 @@ module Haml
43
43
  :t_name => "admin_dashboard", :replaced_text => "t('.admin_dashboard')", :path => "/path/to/doesntmatter.haml" }
44
44
  end
45
45
 
46
- def test_it_can_replace_the_body_of_haml_with_t_characters_example_for_link_to_and_removes_surrounding_quotes_as_well
47
- replacer = Haml::I18n::Extractor::TextReplacer.new(%{%p#brand= link_to 'Some Place', '/'}, "Some Place", :script, "/path/to/doesntmatter.haml")
48
- assert_equal replacer.replace_hash, { :modified_line => %{%p#brand= link_to t('.some_place'), '/'} ,
49
- :t_name => "some_place", :replaced_text => "Some Place", :path => "/path/to/doesntmatter.haml" }
50
-
51
- replacer = Haml::I18n::Extractor::TextReplacer.new(%{%p#brand= link_to "Some Place", "/"}, "Some Place", :script, "/path/to/doesntmatter.haml")
52
- assert_equal replacer.replace_hash, { :modified_line => %{%p#brand= link_to t('.some_place'), "/"} ,
53
- :t_name => "some_place", :replaced_text => "Some Place", :path => "/path/to/doesntmatter.haml" }
54
- end
55
-
56
46
  # keyname restrictions
57
47
  def test_it_limits_the_characters_of_the_t_namespace_it_provides_to_limit_key_name
58
48
  replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever" * 80, "this is whatever" * 80, :plain, "/path/to/doesntmatter.haml")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml-i18n-extractor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 4
10
- version: 0.5.4
9
+ - 5
10
+ version: 0.5.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shai Rosenfeld
@@ -132,7 +132,7 @@ dependencies:
132
132
  type: :development
133
133
  version_requirements: *id008
134
134
  - !ruby/object:Gem::Dependency
135
- name: minitest
135
+ name: pry-remote
136
136
  prerelease: false
137
137
  requirement: &id009 !ruby/object:Gem::Requirement
138
138
  none: false
@@ -146,7 +146,7 @@ dependencies:
146
146
  type: :development
147
147
  version_requirements: *id009
148
148
  - !ruby/object:Gem::Dependency
149
- name: nokogiri
149
+ name: pry-nav
150
150
  prerelease: false
151
151
  requirement: &id010 !ruby/object:Gem::Requirement
152
152
  none: false
@@ -160,7 +160,7 @@ dependencies:
160
160
  type: :development
161
161
  version_requirements: *id010
162
162
  - !ruby/object:Gem::Dependency
163
- name: rake
163
+ name: minitest
164
164
  prerelease: false
165
165
  requirement: &id011 !ruby/object:Gem::Requirement
166
166
  none: false
@@ -174,7 +174,7 @@ dependencies:
174
174
  type: :development
175
175
  version_requirements: *id011
176
176
  - !ruby/object:Gem::Dependency
177
- name: actionpack
177
+ name: nokogiri
178
178
  prerelease: false
179
179
  requirement: &id012 !ruby/object:Gem::Requirement
180
180
  none: false
@@ -188,7 +188,7 @@ dependencies:
188
188
  type: :development
189
189
  version_requirements: *id012
190
190
  - !ruby/object:Gem::Dependency
191
- name: rails
191
+ name: rake
192
192
  prerelease: false
193
193
  requirement: &id013 !ruby/object:Gem::Requirement
194
194
  none: false
@@ -201,6 +201,34 @@ dependencies:
201
201
  version: "0"
202
202
  type: :development
203
203
  version_requirements: *id013
204
+ - !ruby/object:Gem::Dependency
205
+ name: actionpack
206
+ prerelease: false
207
+ requirement: &id014 !ruby/object:Gem::Requirement
208
+ none: false
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ hash: 3
213
+ segments:
214
+ - 0
215
+ version: "0"
216
+ type: :development
217
+ version_requirements: *id014
218
+ - !ruby/object:Gem::Dependency
219
+ name: rails
220
+ prerelease: false
221
+ requirement: &id015 !ruby/object:Gem::Requirement
222
+ none: false
223
+ requirements:
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ hash: 3
227
+ segments:
228
+ - 0
229
+ version: "0"
230
+ type: :development
231
+ version_requirements: *id015
204
232
  description: Parse the texts out of the haml files into localization files
205
233
  email:
206
234
  - shaiguitar@gmail.com