Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More fixes to interpolation

- some lines that have interpolation but aren't identified as script (see ex3/ex5)
- add surrounding quotes to interpolated vars in template
- add \\ to NOT_ALLOWED
- add development_dependency for pry-nav
  • Loading branch information...
commit 4291ced9167f3a4ab475be004017edd3f2507cca 1 parent 4e464ee
Shai Rosenfeld authored
2  haml-i18n-extractor.gemspec
View
@@ -26,6 +26,8 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rbench'
gem.add_development_dependency 'm'
gem.add_development_dependency 'pry'
+ gem.add_development_dependency 'pry-remote'
+ gem.add_development_dependency 'pry-nav'
gem.add_development_dependency 'minitest'
gem.add_development_dependency 'nokogiri'
gem.add_development_dependency 'rake'
6 lib/haml-i18n-extractor.rb
View
@@ -1,5 +1,7 @@
-Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?(:default_internal)
-Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?(:default_external)
+if defined?(Encoding)
+ Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?(:default_internal)
+ Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?(:default_external)
+end
require "trollop"
2  lib/haml-i18n-extractor/extraction/extractor.rb
View
@@ -17,7 +17,7 @@ class NotDefinedLineType < StandardError ; end
class AbortFile < StandardError ; end
LINE_TYPES_ALL = [:plain, :script, :silent_script, :haml_comment, :tag, :comment, :doctype, :filter, :root]
- LINE_TYPES_ADD_EVAL = [:plain, :tag]
+ LINE_TYPES_ADD_EVAL = [:plain, :tag, :script]
attr_reader :haml_reader, :haml_writer
attr_reader :info_for_yaml, :yaml_writer, :type
2  lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb
View
@@ -20,7 +20,7 @@ def keyname_with_vars()
def interpolated_vars
interpolations.map{|v|
- ":#{normalized_name(v.dup)} => #{v}"
+ ":#{normalized_name(v.dup)} => (#{v})"
}.join(", ")
end
39 lib/haml-i18n-extractor/extraction/replacer/text_replacer.rb
View
@@ -33,14 +33,18 @@ def interpolation_helper
Haml::I18n::Extractor::InterpolationHelper.new(@text_to_replace, t_name)
end
+ def orig_interpolated?
+ interpolated?(@orig_line)
+ end
+
# the new full line, including a `t()` replacement instead of the `text_to_replace` portion.
def modified_line
return @full_line if has_been_translated?(@full_line)
full_line = @full_line.dup
#puts t_method.inspect if Haml::I18n::Extractor.debug?
- keyname = interpolated?(full_line) ? interpolation_helper.keyname_with_vars : t_method
- gsub_replacement!(full_line, @text_to_replace, @orig_line, keyname)
- apply_ruby_evaling(full_line)
+ keyname = orig_interpolated? ? interpolation_helper.keyname_with_vars : t_method
+ gsub_replacement!(full_line, @text_to_replace, keyname)
+ apply_ruby_evaling!(full_line, keyname)
full_line
end
@@ -71,32 +75,45 @@ def with_translate_method(name)
end
# adds the = to the right place in the string ... = t()
- def apply_ruby_evaling(str)
+ def apply_ruby_evaling!(str, keyname)
if LINE_TYPES_ADD_EVAL.include?(@line_type)
if @line_type == :tag
- match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(t_method))
+ match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(keyname))
str.match(/(.*?)(#{match_keyname})/)
elem = $1
if elem
str.gsub!(Regexp.new(Regexp.escape(elem)), "#{elem}=") unless already_evaled?(elem)
end
- elsif @line_type == :plain
+ elsif @line_type == :plain || (@line_type == :script && !already_evaled?(full_line))
str.gsub!(str, "= "+str)
end
end
end
- def already_evaled?(elem)
- # poor elem.split('').last == '='
- # better, haml guts:
- @metadata[:value] && @metadata[:value][:parse]
+ def already_evaled?(str)
+ if @line_type == :tag
+ if orig_interpolated?
+ # for tags that come in interpolated we need to explicitly
+ # check that they aren't evaled alreay, the metadata lies
+ # %tag foo #{var} bar
+ str.split('').last == '='
+ else
+ @metadata[:value] && @metadata[:value][:parse]
+ end
+ elsif @line_type == :script
+ # we need this for tags that come in like :plain but have interpolation
+ str.match(/^[\s\t]*=/)
+ end
end
def has_been_translated?(str)
str.match T_REGEX
end
- def gsub_replacement!(str, text_to_replace, orig_line, keyname_method )
+ def gsub_replacement!(str, text_to_replace, keyname_method)
+ # FIXME refactor this method
+ text_to_replace = $1 if (orig_interpolated? && text_to_replace.match(/^['"](.*)['"]$/))
+
# if there are quotes surrounding the string, we want them removed as well...
unless str.gsub!('"' + text_to_replace + '"', keyname_method )
unless str.gsub!("'" + text_to_replace + "'", keyname_method)
2  lib/haml-i18n-extractor/helpers.rb
View
@@ -6,7 +6,7 @@ module Helpers
module StringHelpers
# do not pollute the key space it will make it invalid yaml
- NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : ; \ / . )
+ NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : ; \\ / . | )
# limit the number of chars
LIMIT_KEY_NAME = 30
2  lib/haml-i18n-extractor/version.rb
View
@@ -1,7 +1,7 @@
module Haml
module I18n
class Extractor
- VERSION = "0.5.3"
+ VERSION = "0.5.4"
end
end
end
4 test/interpolation_helper_test.rb
View
@@ -15,7 +15,7 @@ def test_it_takes_text_and_custom_keynames
replace = "this may \#{be} the \#{dup}"
t_name = "this_may_be_the_dup"
helper = Haml::I18n::Extractor::InterpolationHelper.new(replace, t_name)
- assert helper.keyname_with_vars == "t('.this_may_be_the_dup', :be => be, :dup => dup)",
+ assert helper.keyname_with_vars == "t('.this_may_be_the_dup', :be => (be), :dup => (dup))",
"returns custom t() with vars"
end
@@ -26,7 +26,7 @@ def test_it_takes_text_and_returns_for_str_with_no_quotes
assert helper.interpolations == ["is_this_hard?"], "can catch the interpolations"
x = helper.keyname_with_vars
#puts x.inspect
- assert x == "t('.is_this_hard_what', :is_this_hard => is_this_hard?)",
+ assert x == "t('.is_this_hard_what', :is_this_hard => (is_this_hard?))",
"returns custom t() with vars"
end
2  test/support/ex2.output.haml
View
@@ -8,7 +8,7 @@
%h3
= t('.all_invoices_billable')
%span{:style => (@billable_invoices == @active_invoices) ? "color: #090" : "color: #900"}
- =t('.billable_invoices_out_of_activ', :billable_invoices => @billable_invoices, :active_invoices => @active_invoices)
+ =t('.billable_invoices_out_of_activ', :billable_invoices => (@billable_invoices), :active_invoices => (@active_invoices))
%h3
= t('.24_hours_past_end_of_billing_m')
%span{:style => (@billing_month.past_cutoff) ? "color: #090" : "color: #900"}
2  test/support/ex3.haml
View
@@ -11,3 +11,5 @@
%table(style="width:400px")
%td (?)
%td \#{@account.send(attr) || '(none)'}
+
+%p#brand= link_to 'Some Place', '/'
6 test/support/ex3.output.haml
View
@@ -6,8 +6,10 @@
%span.creator{:title => "This user is an= t('.owner') of this account"} t('.owner')
%span.title&= user.name
- %strong \#{@account.name}
+ %strong= t('.accountname', :accountname => (@account.name))
%table(style="width:400px")
%td= t('.td')
- %td \#{@account.send(attr) || '(none)'}
+ %td= t('.accountsendattr_none', :accountsendattr_none => (@account.send(attr) || '(none)'))
+
+%p#brand= link_to t('.some_place'), '/'
2  test/support/ex5.haml
View
@@ -3,3 +3,5 @@
= "this may #{be} the #{dup}"
= link_to "#{ad.name.first}", url_for([ad, :ok])
+
+No quotes #{some_var} with text
8 test/support/ex5.output.haml
View
@@ -1,5 +1,7 @@
.class= t('.text_here')
-.other-class#id= t('.script_with_quote_with_interpo', :with_interpolationbub => with_interpolation(bub))
+.other-class#id= t('.script_with_quote_with_interpo', :with_interpolationbub => (with_interpolation(bub)))
-=t('.this_may_be_the_dup', :be => be, :dup => dup)
-= link_to t('.adnamefirst', :adnamefirst => ad.name.first), url_for([ad, :ok])
+=t('.this_may_be_the_dup', :be => (be), :dup => (dup))
+= link_to t('.adnamefirst', :adnamefirst => (ad.name.first)), url_for([ad, :ok])
+
+= t('.no_quotes_some_var_with_text', :some_var => (some_var))
1  test/support/ex5.yml
View
@@ -7,3 +7,4 @@ en:
interpolated!"'
this_may_be_the_dup: ! ' "this may %{be} the %{dup}"'
adnamefirst: ! '%{adnamefirst}'
+ no_quotes_some_var_with_text: ! '"No quotes %{some_var} with text"'
10 test/text_replacer_test.rb
View
@@ -43,16 +43,6 @@ def test_it_wont_replace_already_replaced_t_characters_that_are_ruby_evaled
:t_name => "admin_dashboard", :replaced_text => "t('.admin_dashboard')", :path => "/path/to/doesntmatter.haml" }
end
- def test_it_can_replace_the_body_of_haml_with_t_characters_example_for_link_to_and_removes_surrounding_quotes_as_well
- replacer = Haml::I18n::Extractor::TextReplacer.new(%{%p#brand= link_to 'Some Place', '/'}, "Some Place", :script, "/path/to/doesntmatter.haml")
- assert_equal replacer.replace_hash, { :modified_line => %{%p#brand= link_to t('.some_place'), '/'} ,
- :t_name => "some_place", :replaced_text => "Some Place", :path => "/path/to/doesntmatter.haml" }
-
- replacer = Haml::I18n::Extractor::TextReplacer.new(%{%p#brand= link_to "Some Place", "/"}, "Some Place", :script, "/path/to/doesntmatter.haml")
- assert_equal replacer.replace_hash, { :modified_line => %{%p#brand= link_to t('.some_place'), "/"} ,
- :t_name => "some_place", :replaced_text => "Some Place", :path => "/path/to/doesntmatter.haml" }
- end
-
# keyname restrictions
def test_it_limits_the_characters_of_the_t_namespace_it_provides_to_limit_key_name
replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever" * 80, "this is whatever" * 80, :plain, "/path/to/doesntmatter.haml")
Please sign in to comment.
Something went wrong with that request. Please try again.