Browse files

Adding Interpolation

- The haml writer should write the correct:
  t('.foo', :var => bar) syntax.

- The yaml file should have the variables it finds as well:
  foo => "foo %{var}"

Also other needed refactoring.

See ex5 for an example of interpolation in action.
  • Loading branch information...
1 parent 699a8c1 commit 0fb5e6920668d4f54e7d915e65d8341afa3dd512 @shaiguitar committed Sep 6, 2013
View
6 TODO
@@ -23,6 +23,12 @@
https://github.com/shaiguitar/haml-i18n-extractor/issues/2
+ Refactor out text_replacer? it's pretty simple. we can:
+ return a result class like Finder
+ and refactor the main modified_line method so it's not dumb?!
+ Change naive implementation!
+ right now it gets full_line, replace_this. just does a simple gsub.
+
Now we are able to have internal access to Haml::Parser internals, we can deal with...
ExceptionText needs to be responsible for knowing if needed to interpolate?
View
9 lib/haml-i18n-extractor.rb
@@ -3,12 +3,15 @@
require "haml-i18n-extractor/version"
require "haml-i18n-extractor/helpers"
-require "haml-i18n-extractor/extraction/text_finder"
-require "haml-i18n-extractor/extraction/exception_finder"
+require "haml-i18n-extractor/extraction/finder/text_finder"
+require "haml-i18n-extractor/extraction/finder/exception_finder"
+require "haml-i18n-extractor/extraction/replacer/text_replacer"
+require "haml-i18n-extractor/extraction/replacer/replacer_result"
+require "haml-i18n-extractor/extraction/replacer/interpolation_helper"
+
require "haml-i18n-extractor/extraction/haml_parser"
require "haml-i18n-extractor/extraction/haml_reader"
require "haml-i18n-extractor/extraction/tagging_writer"
-require "haml-i18n-extractor/extraction/text_replacer"
require "haml-i18n-extractor/extraction/haml_writer"
require "haml-i18n-extractor/extraction/yaml_writer"
require "haml-i18n-extractor/extraction/extractor"
View
15 lib/haml-i18n-extractor/extraction/extractor.rb
@@ -101,10 +101,10 @@ def process_line(orig_line, line_no)
elsif user_action.next?
raise AbortFile, "stopping to process the rest of the file"
elsif user_action.replace_line?
- append_to_info_for_yaml(line_no, replacer_result.info)
+ add_to_yaml_info(line_no, replacer_result.info)
add_to_body("#{whitespace}#{text_to_replace}")
elsif user_action.no_replace?
- append_to_info_for_yaml(line_no, Haml::I18n::Extractor::TextReplacer::ReplacerResult.new(nil,nil,nil,false,nil).info)
+ add_to_yaml_info(line_no, Haml::I18n::Extractor::ReplacerResult.new(nil,nil,nil,false,nil).info)
add_to_body("#{whitespace}#{orig_line}")
end
@@ -127,11 +127,11 @@ def replacement_result(orig_line, line_match, line_type, line_no)
if line_match && !line_match.empty?
Haml::I18n::Extractor::TextReplacer.new(orig_line, line_match, line_type, @haml_reader.path, line_metadata(line_no)).result
else
- Haml::I18n::Extractor::TextReplacer::ReplacerResult.new(orig_line, nil,line_match, false, "")
+ Haml::I18n::Extractor::ReplacerResult.new(orig_line, nil,line_match, false, "")
end
end
- def append_to_info_for_yaml(line_no, hash)
+ def add_to_yaml_info(line_no, hash)
@info_for_yaml[line_no] = hash
end
@@ -166,8 +166,11 @@ def user_action_no
def validate_haml(haml)
parser = Haml::Parser.new(haml, Haml::Options.new)
parser.parse
- rescue Haml::SyntaxError
- raise InvalidSyntax, "invalid syntax for haml #{@haml_reader.path}"
+ rescue Haml::SyntaxError => e
+ message = "invalid syntax for haml #{@haml_reader.path}\n"
+ message << "original error:\n"
+ message << e.message
+ raise InvalidSyntax, message
end
end
View
0 ...-extractor/extraction/exception_finder.rb → ...tor/extraction/finder/exception_finder.rb
File renamed without changes.
View
18 ...-i18n-extractor/extraction/text_finder.rb → ...xtractor/extraction/finder/text_finder.rb
@@ -16,13 +16,13 @@ def initialize(orig_line,line_metadata)
def process_by_regex
# [ line_type, text_found ]
- output_debug if Haml::I18n::Extractor.debug?
+ #output_debug if Haml::I18n::Extractor.debug?
result = @metadata && send("#{@metadata[:type]}", @metadata)
- result = FinderResult.new(nil,nil,nil) if result.nil?
+ result = FinderResult.new(nil,nil) if result.nil?
result
end
- class FinderResult < Struct.new(:type, :match, :is_interpolated); end
+ class FinderResult < Struct.new(:type, :match); end
private
@@ -35,7 +35,7 @@ def output_debug
def plain(line)
txt = line[:value][:text]
return nil if html_comment?(txt)
- FinderResult.new(:plain, txt, false)
+ FinderResult.new(:plain, txt)
end
def tag(line)
@@ -44,21 +44,21 @@ def tag(line)
has_script_in_tag = line[:value][:parse] # %element= foo
has_exception = link_to?(txt)
if has_script_in_tag && !has_exception
- FinderResult.new(:tag, "", false)
+ FinderResult.new(:tag, "")
else
- FinderResult.new(:tag, ExceptionFinder.new(txt).find, false)
+ FinderResult.new(:tag, ExceptionFinder.new(txt).find)
end
else
- FinderResult.new(:tag, "", false)
+ FinderResult.new(:tag, "")
end
end
def script(line)
txt = line[:value][:text]
if could_match_script?(txt)
- FinderResult.new(:script, ExceptionFinder.new(txt).find, false)
+ FinderResult.new(:script, ExceptionFinder.new(txt).find)
else
- FinderResult.new(:script, "", false)
+ FinderResult.new(:script, "")
end
end
View
51 lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb
@@ -0,0 +1,51 @@
+module Haml
+ module I18n
+ class Extractor
+ class InterpolationHelper
+
+ include Helpers::StringHelpers
+
+ # takes an original_line and text_to_replace, keyname_name and gives back the result for that...
+ def initialize(text_to_replace, t_name)
+ if Extractor.debug?
+ puts "<interpolationhelper>#{text_to_replace.inspect} #{t_name.inspect}</interpolationhelper>"
+ end
+ @t_name = t_name
+ @text_to_replace = text_to_replace
+ end
+
+ def keyname_with_vars()
+ "t('.#{@t_name}', #{interpolated_vars})"
+ end
+
+ def interpolated_vars
+ interpolations.map{|v|
+ ":#{normalized_name(v.dup)} => #{v}"
+ }.join(", ")
+ end
+
+ # matches multiple
+ def interpolations(arr = [], str = @text_to_replace)
+ # recurse scanner until no interpolations or string left
+ return arr if str.nil? || str.empty? || !interpolated?(str)
+ scanner = StringScanner.new(str)
+ scanner.scan_until(/\#{.*?}/)
+ so_far = scanner.pre_match + scanner.matched
+ arr << extract_interpolation(so_far)
+ arr = interpolations(arr, scanner.rest)
+ end
+
+ def extract_interpolation(str)
+ scanner = StringScanner.new(str)
+ scanner.scan_until /\#{/
+ rest_scanner = StringScanner.new(scanner.rest)
+ rest_scanner.scan_until(/}/)
+ interpolated = rest_scanner.pre_match
+ end
+
+
+ end
+ end
+ end
+end
+
View
24 lib/haml-i18n-extractor/extraction/replacer/replacer_result.rb
@@ -0,0 +1,24 @@
+module Haml
+ module I18n
+ class Extractor
+ class ReplacerResult
+
+ attr_accessor :modified_line, :t_name, :replaced_text, :should_be_replaced, :path
+
+ def initialize(modified_line, t_name, replaced_text, should_be_replaced, path)
+ @modified_line = modified_line
+ @t_name = t_name
+ @replaced_text = replaced_text
+ @should_be_replaced = should_be_replaced
+ @path = path
+ end
+
+ def info
+ { :modified_line => @modified_line, :t_name => @t_name, :replaced_text => @replaced_text, :path => @path}
+ end
+ end
+
+ end
+ end
+end
+
View
75 ...18n-extractor/extraction/text_replacer.rb → ...ctor/extraction/replacer/text_replacer.rb
@@ -3,13 +3,11 @@ module I18n
class Extractor
class TextReplacer
+ include Helpers::StringHelpers
+
attr_reader :full_line, :text_to_replace, :line_type
- T_REGEX = /t\('\..*'\)/
- # limit the number of chars
- LIMIT_KEY_NAME = 30
- # do not pollute the key space it will make it invalid yaml
- NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : )
+ T_REGEX = /t\('\.(.*?)'\)/
def initialize(full_line, text_to_replace,line_type, path, metadata = {})
@path = path
@@ -23,51 +21,48 @@ def initialize(full_line, text_to_replace,line_type, path, metadata = {})
end
end
- class ReplacerResult
- attr_accessor :modified_line, :keyname, :replaced_text, :should_be_replaced, :path
-
- def initialize(modified_line, keyname, replaced_text, should_be_replaced, path)
- @modified_line = modified_line
- @keyname = keyname
- @replaced_text = replaced_text
- @should_be_replaced = should_be_replaced
- @path = path
- end
-
- def info
- { :modified_line => @modified_line, :keyname => @keyname, :replaced_text => @replaced_text, :path => @path}
- end
- end
-
def result
- t_name = keyname(@text_to_replace, @orig_line)
- @result ||= ReplacerResult.new(modified_line, t_name, @text_to_replace, true, @path)
+ @result ||= Haml::I18n::Extractor::ReplacerResult.new(modified_line, t_name, @text_to_replace, true, @path)
end
def replace_hash
+ #legacy
result.info
end
+ def interpolation_helper
+ Haml::I18n::Extractor::InterpolationHelper.new(@text_to_replace, t_name)
+ 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
- return @full_line if has_been_translated?(full_line)
- remove_surrounding_quotes(full_line)
+ #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)
full_line
end
-
+
private
- def keyname(to_replace, orig_line)
+ # the_key_to_use ( for example in t('.the_key_to_use')
+ def t_name(to_replace = @text_to_replace, orig_line = @orig_line)
text_to_replace = to_replace.dup
if has_been_translated?(text_to_replace)
- text_to_replace
+ text_to_replace.match T_REGEX
+ name = $1
else
- name = normalize_name(text_to_replace)
- name = normalize_name(orig_line.dup) if name.empty?
- with_translate_method(name)
+ name = normalized_name(text_to_replace.dup)
+ name = normalized_name(orig_line.dup) if name.empty?
end
+ name
+ end
+
+ # t('.the_key_to_use')
+ def t_method
+ with_translate_method(t_name)
end
def with_translate_method(name)
@@ -78,8 +73,7 @@ def with_translate_method(name)
def apply_ruby_evaling(str)
if LINE_TYPES_ADD_EVAL.include?(@line_type)
if @line_type == :tag
- t_name = keyname(@text_to_replace, @orig_line)
- match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(t_name))
+ match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(t_method))
str.match(/(.*?)(#{match_keyname})/)
elem = $1
if elem
@@ -101,22 +95,15 @@ def has_been_translated?(str)
str.match T_REGEX
end
- def remove_surrounding_quotes(str)
+ def gsub_replacement!(str, text_to_replace, orig_line, keyname_method )
# if there are quotes surrounding the string, we want them removed as well...
- t_name = keyname(@text_to_replace, @orig_line)
- unless str.gsub!('"' + @text_to_replace + '"', t_name )
- unless str.gsub!("'" + @text_to_replace + "'", t_name)
- str.gsub!(@text_to_replace, t_name)
+ unless str.gsub!('"' + text_to_replace + '"', keyname_method )
+ unless str.gsub!("'" + text_to_replace + "'", keyname_method)
+ str.gsub!(text_to_replace, keyname_method)
end
end
end
- def normalize_name(str)
- NOT_ALLOWED_IN_KEYNAME.each{ |rm_me| str.gsub!(rm_me, "") }
- str = str.gsub(/\s+/, " ").strip
- str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
- end
-
end
end
end
View
14 lib/haml-i18n-extractor/extraction/yaml_writer.rb
@@ -10,6 +10,8 @@ class YamlWriter
attr_accessor :info_for_yaml, :yaml_file, :i18n_scope
+ include Helpers::StringHelpers
+
def initialize(i18n_scope = nil, yaml_file = nil)
@i18n_scope = i18n_scope && i18n_scope.to_sym || :en
@yaml_file = yaml_file || "./config/locales/#{@i18n_scope}.yml"
@@ -24,9 +26,9 @@ def initialize(i18n_scope = nil, yaml_file = nil)
def yaml_hash
yml = Hash.new
@info_for_yaml.map do |line_no, info|
- unless info[:keyname].nil?
- keyspace = [@i18n_scope,standardized_viewnames(info[:path]), standarized_keyname(info[:keyname]),
- info[:replaced_text]].flatten
+ unless info[:t_name].nil?
+ keyspace = [@i18n_scope,standardized_viewnames(info[:path]), info[:t_name],
+ normalize_interpolation(info[:replaced_text])].flatten
yml.deep_merge!(nested_hash({},keyspace))
end
end
@@ -78,12 +80,6 @@ def nested_hash(hash,array)
hash
end
- # comes in like "t('.some_place')", return .some_place
- def standarized_keyname(name)
- name.match(/t\('\.(.*)'\)/)
- $1
- end
-
# assuming rails format, app/views/users/index.html.haml return [users]
# app/views/admin/users/index.html.haml return [admin, users]
# app/views/admin/users/with_namespace/index.html.haml return [admin, users, with_namespace, index]
View
38 lib/haml-i18n-extractor/helpers.rb
@@ -4,9 +4,47 @@ class Extractor
module Helpers
module StringHelpers
+
+ # do not pollute the key space it will make it invalid yaml
+ NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : \ / )
+ # limit the number of chars
+ LIMIT_KEY_NAME = 30
+
+ def interpolated?(str)
+ str.match(/\#{/)
+ end
+
+ def normalized_name(str)
+ NOT_ALLOWED_IN_KEYNAME.each{ |rm_me| str.gsub!(rm_me, "") }
+ str = str.gsub(/\s+/, " ").strip
+ str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
+ end
+
+ def normalize_interpolation(str)
+ ret = change_one_interpolation(str)
+ if ret && interpolated?(ret)
+ ret = normalize_interpolation(ret)
+ end
+ ret
+ end
+
+ def change_one_interpolation(str)
+ if interpolated?(str)
+ scanner = StringScanner.new(str)
+ scanner.scan_until(/\#{(.*?)}/)
+ ret = "#{scanner.pre_match}"
+ ret << "%{#{normalized_name(scanner.matched)}}"
+ ret << "#{scanner.post_match}"
+ else
+ ret = str
+ end
+ ret
+ end
+
def html_comment?(txt)
txt.match(/<!--/) || txt.match(/-->\s*$/)
end
+
def link_to?(txt)
txt.match(/link_to/) || txt.match(/^\s*['"]/) # %element= 'foo'
end
View
8 test/integration_test.rb
@@ -24,6 +24,11 @@ def test_it_can_handle_partial_views
## EXAMPLES
+ def test_it_can_replace_a_string_body_and_have_expected_output_ex5
+ expected_output = File.read(file_path("ex5.output.haml"))
+ assert_equal Haml::I18n::Extractor.new(file_path("ex5.haml")).new_body, expected_output
+ end
+
def test_it_can_replace_a_string_body_and_have_expected_output_ex4
expected_output = File.read(file_path("ex4.output.haml"))
assert_equal Haml::I18n::Extractor.new(file_path("ex4.haml")).new_body, expected_output
@@ -39,13 +44,10 @@ def test_it_can_replace_a_string_body_and_have_expected_output_ex2
assert_equal Haml::I18n::Extractor.new(file_path("ex2.haml")).new_body, expected_output
end
-
def test_it_can_replace_a_string_body_and_have_expected_output_ex1
expected_output = File.read(file_path("ex1.output.haml"))
assert_equal Haml::I18n::Extractor.new(file_path("ex1.haml")).new_body, expected_output
end
-
-
end
end
View
43 test/interpolation_helper_test.rb
@@ -0,0 +1,43 @@
+require 'test_helper'
+
+module Haml
+ class InterpolationHelperTest < MiniTest::Unit::TestCase
+
+ def test_it_takes_text_and_returns_interpolations
+ replace = "this may \#{be} the \#{dup}"
+ t_name = "this_may_be_the_dup"
+ helper = Haml::I18n::Extractor::InterpolationHelper.new(replace, t_name)
+ assert helper.interpolations == ["be", "dup"], "can catch the interpolations"
+ end
+
+
+ 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)",
+ "returns custom t() with vars"
+ end
+
+ def test_it_takes_text_and_returns_for_str_with_no_quotes
+ replace = "\\\#{is_this_hard?} what"
+ t_name = "is_this_hard_what"
+ helper = Haml::I18n::Extractor::InterpolationHelper.new(replace, t_name)
+ 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?)",
+ "returns custom t() with vars"
+ end
+
+ # FIXME these don't work?
+ #%p \#{is_this_hard?} what
+ #%span I don't know if this \#{is_also} what
+ #%p= t('.what', :is_this_hard => is_this_hard?)
+ #%span= t('.i_dont_know_if_this_what', :is_also => is_also)
+
+ end
+end
+
+
+
View
2 test/support/ex2.output.haml
@@ -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')
+ =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"}
View
4 test/support/ex5.haml
@@ -0,0 +1,4 @@
+.class Text here
+.other-class#id= "script with quote #{with_interpolation(bub)} interpolated!"
+
+= "this may #{be} the #{dup}"
View
4 test/support/ex5.output.haml
@@ -0,0 +1,4 @@
+.class= t('.text_here')
+.other-class#id= t('.script_with_quote_with_interpo', :with_interpolationbub => with_interpolation(bub))
+
+=t('.this_may_be_the_dup', :be => be, :dup => dup)
View
8 test/support/ex5.yml
@@ -0,0 +1,8 @@
+---
+en:
+ support:
+ ex5:
+ text_here: Text here
+ script_with_quote_with_interpo: ! '"script with quote %{with_interpolationbub}
+ interpolated!"'
+ this_may_be_the_dup: ! ' "this may %{be} the %{dup}"'
View
16 test/text_replacer_test.rb
@@ -22,48 +22,48 @@ def test_but_it_raises_if_passed_a_wrong_line_type
def test_it_can_replace_the_body_of_haml_with_t_characters
replacer = Haml::I18n::Extractor::TextReplacer.new("this is whatever", "this is whatever", :plain, "/path/to/doesntmatter.haml")
assert_equal replacer.replace_hash, { :modified_line => "= t('.this_is_whatever')",
- :keyname => "t('.this_is_whatever')", :replaced_text => "this is whatever", :path => "/path/to/doesntmatter.haml" }
+ :t_name => "this_is_whatever", :replaced_text => "this is whatever", :path => "/path/to/doesntmatter.haml" }
end
def test_it_can_replace_the_body_of_haml_with_t_characters_example
replacer = Haml::I18n::Extractor::TextReplacer.new("%span Admin Dashboard", "Admin Dashboard", :tag, "/path/to/doesntmatter.haml")
assert_equal replacer.replace_hash, { :modified_line => "%span= t('.admin_dashboard')",
- :keyname => "t('.admin_dashboard')", :replaced_text => "Admin Dashboard", :path => "/path/to/doesntmatter.haml" }
+ :t_name => "admin_dashboard", :replaced_text => "Admin Dashboard", :path => "/path/to/doesntmatter.haml" }
end
def test_it_wont_replace_already_replaced_t_characters_if_they_are_not_ruby_evaled
replacer = Haml::I18n::Extractor::TextReplacer.new("%span t('.admin_dashboard')", "t('.admin_dashboard')", :tag, "/path/to/doesntmatter.haml")
assert_equal replacer.replace_hash, { :modified_line => "%span t('.admin_dashboard')",
- :keyname => "t('.admin_dashboard')", :replaced_text => "t('.admin_dashboard')", :path => "/path/to/doesntmatter.haml" }
+ :t_name => "admin_dashboard", :replaced_text => "t('.admin_dashboard')", :path => "/path/to/doesntmatter.haml" }
end
def test_it_wont_replace_already_replaced_t_characters_that_are_ruby_evaled
replacer = Haml::I18n::Extractor::TextReplacer.new("%span= t('.admin_dashboard')", "t('.admin_dashboard')", :script, "/path/to/doesntmatter.haml")
assert_equal replacer.replace_hash, { :modified_line => "%span= t('.admin_dashboard')",
- :keyname => "t('.admin_dashboard')", :replaced_text => "t('.admin_dashboard')", :path => "/path/to/doesntmatter.haml" }
+ :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'), '/'} ,
- :keyname => "t('.some_place')", :replaced_text => "Some Place", :path => "/path/to/doesntmatter.haml" }
+ :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'), "/"} ,
- :keyname => "t('.some_place')", :replaced_text => "Some Place", :path => "/path/to/doesntmatter.haml" }
+ :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")
- assert_equal replacer.replace_hash[:modified_line].size, Haml::I18n::Extractor::TextReplacer::LIMIT_KEY_NAME + 8 # = t('.')
+ assert_equal replacer.replace_hash[:modified_line].size, Haml::I18n::Extractor::Helpers::StringHelpers::LIMIT_KEY_NAME + 8 # = t('.')
end
def test_it_does_not_allow_weird_characters_in_the_keyname
weird_line = "thi:s = (is \"' `ch@racter ~ ma?dne{}ss!"
replacer = Haml::I18n::Extractor::TextReplacer.new(weird_line, weird_line, :plain, "/path/to/doesntmatter.haml")
assert_equal replacer.replace_hash, { :modified_line => "= t('.this_is_chracter_madness')",
- :keyname => "t('.this_is_chracter_madness')", :replaced_text => weird_line, :path => "/path/to/doesntmatter.haml" }
+ :t_name => "this_is_chracter_madness", :replaced_text => weird_line, :path => "/path/to/doesntmatter.haml" }
end
end
View
12 test/yaml_writer_test.rb
@@ -44,6 +44,16 @@ def ex1_yaml_hash
YAML.load File.read(file_path("ex1.yml"))
end
+ def ex5_yaml_hash
+ YAML.load File.read(file_path("ex5.yml"))
+ end
+
+ def test_it_can_deal_with_interpolated_vars
+ @ex5 = Haml::I18n::Extractor.new(file_path("ex5.haml"))
+ @ex5.run
+ assert_equal @ex5.yaml_writer.yaml_hash, ex5_yaml_hash
+ end
+
def test_defaults_for_empty_init
yaml_writer = Haml::I18n::Extractor::YamlWriter.new
assert_equal yaml_writer.yaml_file, "./config/locales/en.yml"
@@ -93,7 +103,7 @@ def test_it_relies_on_the_info_for_yaml_having_a_certain_format
setup_info_for_yaml
@ex1.yaml_writer.info_for_yaml.each do |line_no, info_for_line|
assert info_for_line.has_key?(:modified_line), "hash info has :modified_line key"
- assert info_for_line.has_key?(:keyname), "hash info has :keyname key"
+ assert info_for_line.has_key?(:t_name), "hash info has :t_name key"
assert info_for_line.has_key?(:replaced_text), "hash info has :replaced_text key"
# FIXME: since the scope right now is we're running this per file this will be the same, but keeping this right now.
assert info_for_line.has_key?(:path), "hash info has :path key"

0 comments on commit 0fb5e69

Please sign in to comment.