haml-i18n-extractor 0.4.3 → 0.5.0

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.
Files changed (37) hide show
  1. data/.gitignore +1 -1
  2. data/TODO +6 -0
  3. data/lib/haml-i18n-extractor/extraction/extractor.rb +36 -26
  4. data/lib/haml-i18n-extractor/extraction/{exception_finder.rb → finder/exception_finder.rb} +0 -0
  5. data/lib/haml-i18n-extractor/extraction/{text_finder.rb → finder/text_finder.rb} +18 -12
  6. data/lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb +51 -0
  7. data/lib/haml-i18n-extractor/extraction/replacer/replacer_result.rb +24 -0
  8. data/lib/haml-i18n-extractor/extraction/{text_replacer.rb → replacer/text_replacer.rb} +38 -33
  9. data/lib/haml-i18n-extractor/extraction/{tagging_tool.rb → tagging_writer.rb} +1 -1
  10. data/lib/haml-i18n-extractor/extraction/{yaml_tool.rb → yaml_writer.rb} +9 -12
  11. data/lib/haml-i18n-extractor/flow/prompter.rb +1 -1
  12. data/lib/haml-i18n-extractor/helpers.rb +38 -0
  13. data/lib/haml-i18n-extractor/version.rb +1 -1
  14. data/lib/haml-i18n-extractor.rb +8 -5
  15. data/scripts/renew_test_syntax +18 -0
  16. data/test/cli_test.rb +2 -2
  17. data/test/exception_finder_test.rb +2 -2
  18. data/test/extractor_test.rb +23 -23
  19. data/test/haml_parser_test.rb +3 -3
  20. data/test/haml_reader_test.rb +3 -3
  21. data/test/haml_writer_test.rb +4 -2
  22. data/test/integration_test.rb +13 -11
  23. data/test/interpolation_helper_test.rb +43 -0
  24. data/test/prompter_test.rb +3 -3
  25. data/test/support/ex1.yml +15 -0
  26. data/test/support/ex2.output.haml +1 -1
  27. data/test/support/ex5.haml +4 -0
  28. data/test/support/ex5.output.haml +4 -0
  29. data/test/support/ex5.yml +8 -0
  30. data/test/{tagging_tool_test.rb → tagging_writer_test.rb} +6 -6
  31. data/test/test_helper.rb +2 -2
  32. data/test/text_finder_test.rb +22 -20
  33. data/test/text_replacer_test.rb +29 -27
  34. data/test/workflow_test.rb +5 -5
  35. data/test/yaml_writer_test.rb +120 -0
  36. metadata +27 -14
  37. data/test/yaml_tool_test.rb +0 -110
data/.gitignore CHANGED
@@ -17,7 +17,7 @@ test/version_tmp
17
17
  tmp
18
18
 
19
19
  # hacks, these should not appear where thye are.
20
- *.yml
20
+ ./*.yml
21
21
  test/support/*i18n-extractor.haml
22
22
  config
23
23
  .tags.haml-i18n-extractor
data/TODO CHANGED
@@ -23,6 +23,12 @@
23
23
 
24
24
  https://github.com/shaiguitar/haml-i18n-extractor/issues/2
25
25
 
26
+ Refactor out text_replacer? it's pretty simple. we can:
27
+ return a result class like Finder
28
+ and refactor the main modified_line method so it's not dumb?!
29
+ Change naive implementation!
30
+ right now it gets full_line, replace_this. just does a simple gsub.
31
+
26
32
  Now we are able to have internal access to Haml::Parser internals, we can deal with...
27
33
 
28
34
  ExceptionText needs to be responsible for knowing if needed to interpolate?
@@ -20,11 +20,9 @@ module Haml
20
20
  LINE_TYPES_ADD_EVAL = [:plain, :tag]
21
21
 
22
22
  attr_reader :haml_reader, :haml_writer
23
- attr_reader :locale_hash, :yaml_tool, :type
23
+ attr_reader :info_for_yaml, :yaml_writer, :type
24
24
  attr_reader :current_line
25
25
 
26
- DEFAULT_LINE_LOCALE_HASH = { :modified_line => nil,:keyname => nil,:replaced_text => nil, :path => nil }
27
-
28
26
  def initialize(haml_path, opts = {})
29
27
  @options = opts
30
28
  @type = @options[:type]
@@ -33,12 +31,12 @@ module Haml
33
31
  @haml_reader = Haml::I18n::Extractor::HamlReader.new(haml_path)
34
32
  validate_haml(@haml_reader.body)
35
33
  @haml_writer = Haml::I18n::Extractor::HamlWriter.new(haml_path, {:type => @type})
36
- @yaml_tool = Haml::I18n::Extractor::YamlTool.new(@options[:i18n_scope], @options[:yaml_file])
37
- @tagging_tool ||= Haml::I18n::Extractor::TaggingTool.new
34
+ @yaml_writer = Haml::I18n::Extractor::YamlWriter.new(@options[:i18n_scope], @options[:yaml_file])
35
+ @tagging_writer ||= Haml::I18n::Extractor::TaggingWriter.new
38
36
  # hold all the processed lines
39
37
  @body = []
40
38
  # holds a line_no => {info_about_line_replacemnts_or_not}
41
- @locale_hash = {}
39
+ @info_for_yaml = {}
42
40
 
43
41
  self.class.extractors << self
44
42
  end
@@ -46,7 +44,7 @@ module Haml
46
44
  def run
47
45
  assign_replacements
48
46
  validate_haml(@haml_writer.body)
49
- @yaml_tool.write_file
47
+ @yaml_writer.write_file
50
48
  @haml_writer.write_file
51
49
  end
52
50
 
@@ -55,7 +53,7 @@ module Haml
55
53
  end
56
54
 
57
55
  def assign_yaml
58
- @yaml_tool.locale_hash = @locale_hash
56
+ @yaml_writer.info_for_yaml = @info_for_yaml
59
57
  end
60
58
 
61
59
  def assign_replacements
@@ -78,16 +76,19 @@ module Haml
78
76
  end
79
77
 
80
78
  # this is the bulk of it:
81
- # where we end up setting body info and locale_hash.
79
+ # where we end up setting body info and info_for_yaml.
82
80
  # not _write_, just set that info in memory in correspoding locations.
83
81
  # refactor more?
84
82
  def process_line(orig_line, line_no)
85
83
  orig_line.chomp!
86
84
  orig_line, whitespace = handle_line_whitespace(orig_line)
87
- line_type, line_match = handle_line_finding(orig_line, line_no)
88
- should_be_replaced, text_to_replace, line_locale_hash = gather_replacement_info(orig_line, line_match, line_type, line_no)
85
+ finder_result = finding_result(orig_line, line_no)
86
+ replacer_result = replacement_result(orig_line, finder_result.match, finder_result.type, line_no)
87
+ should_be_replaced = replacer_result.should_be_replaced
88
+ text_to_replace = replacer_result.modified_line
89
+
90
+ user_action = should_be_replaced ? user_action_yes : user_action_no
89
91
 
90
- user_action = Haml::I18n::Extractor::UserAction.new('y') # default if no prompting: just do it.
91
92
  if should_be_replaced
92
93
  if interactive?
93
94
  user_action = @prompter.ask_user(orig_line,text_to_replace)
@@ -95,15 +96,15 @@ module Haml
95
96
  end
96
97
 
97
98
  if user_action.tag?
98
- @tagging_tool.write(line_locale_hash[:path], line_no)
99
+ @tagging_writer.write(@haml_reader.path, line_no)
99
100
  add_to_body("#{whitespace}#{orig_line}")
100
101
  elsif user_action.next?
101
102
  raise AbortFile, "stopping to process the rest of the file"
102
103
  elsif user_action.replace_line?
103
- append_to_locale_hash(line_no, line_locale_hash)
104
+ add_to_yaml_info(line_no, replacer_result.info)
104
105
  add_to_body("#{whitespace}#{text_to_replace}")
105
106
  elsif user_action.no_replace?
106
- append_to_locale_hash(line_no, DEFAULT_LINE_LOCALE_HASH)
107
+ add_to_yaml_info(line_no, Haml::I18n::Extractor::ReplacerResult.new(nil,nil,nil,false,nil).info)
107
108
  add_to_body("#{whitespace}#{orig_line}")
108
109
  end
109
110
 
@@ -122,22 +123,19 @@ module Haml
122
123
  end
123
124
  end
124
125
 
125
- def gather_replacement_info(orig_line, line_match, line_type, line_no)
126
+ def replacement_result(orig_line, line_match, line_type, line_no)
126
127
  if line_match && !line_match.empty?
127
- replacer = Haml::I18n::Extractor::TextReplacer.new(orig_line, line_match, line_type, line_metadata(line_no))
128
- hash = replacer.replace_hash.dup.merge!({:path => @haml_reader.path })
129
- [ true, hash[:modified_line], hash ]
128
+ Haml::I18n::Extractor::TextReplacer.new(orig_line, line_match, line_type, @haml_reader.path, line_metadata(line_no)).result
130
129
  else
131
- hash = DEFAULT_LINE_LOCALE_HASH
132
- [ false, orig_line, hash ]
130
+ Haml::I18n::Extractor::ReplacerResult.new(orig_line, nil,line_match, false, "")
133
131
  end
134
132
  end
135
133
 
136
- def append_to_locale_hash(line_no, hash)
137
- @locale_hash[line_no] = hash
134
+ def add_to_yaml_info(line_no, hash)
135
+ @info_for_yaml[line_no] = hash
138
136
  end
139
137
 
140
- def handle_line_finding(orig_line,lineno)
138
+ def finding_result(orig_line,lineno)
141
139
  Haml::I18n::Extractor::TextFinder.new(orig_line,line_metadata(lineno)).process_by_regex
142
140
  end
143
141
 
@@ -156,11 +154,23 @@ module Haml
156
154
  @body << ln
157
155
  end
158
156
 
157
+ def user_action_yes
158
+ Haml::I18n::Extractor::UserAction.new('y') # default if no prompting: just do it.
159
+ end
160
+
161
+ def user_action_no
162
+ Haml::I18n::Extractor::UserAction.new('n') # don't replace
163
+ end
164
+
165
+
159
166
  def validate_haml(haml)
160
167
  parser = Haml::Parser.new(haml, Haml::Options.new)
161
168
  parser.parse
162
- rescue Haml::SyntaxError
163
- raise InvalidSyntax, "invalid syntax for haml #{@haml_reader.path}"
169
+ rescue Haml::SyntaxError => e
170
+ message = "invalid syntax for haml #{@haml_reader.path}\n"
171
+ message << "original error:\n"
172
+ message << e.message
173
+ raise InvalidSyntax, message
164
174
  end
165
175
 
166
176
  end
@@ -16,20 +16,26 @@ module Haml
16
16
 
17
17
  def process_by_regex
18
18
  # [ line_type, text_found ]
19
- if Haml::I18n::Extractor.debug?
20
- puts @metadata && @metadata[:type]
21
- puts @metadata.inspect
22
- puts @orig_line
23
- end
24
- @metadata && send("#{@metadata[:type]}", @metadata)
19
+ #output_debug if Haml::I18n::Extractor.debug?
20
+ result = @metadata && send("#{@metadata[:type]}", @metadata)
21
+ result = FinderResult.new(nil,nil) if result.nil?
22
+ result
25
23
  end
26
24
 
25
+ class FinderResult < Struct.new(:type, :match); end
26
+
27
27
  private
28
28
 
29
+ def output_debug
30
+ puts @metadata && @metadata[:type]
31
+ puts @metadata.inspect
32
+ puts @orig_line
33
+ end
34
+
29
35
  def plain(line)
30
36
  txt = line[:value][:text]
31
37
  return nil if html_comment?(txt)
32
- [:plain, txt]
38
+ FinderResult.new(:plain, txt)
33
39
  end
34
40
 
35
41
  def tag(line)
@@ -38,21 +44,21 @@ module Haml
38
44
  has_script_in_tag = line[:value][:parse] # %element= foo
39
45
  has_exception = link_to?(txt)
40
46
  if has_script_in_tag && !has_exception
41
- [:tag, ""]
47
+ FinderResult.new(:tag, "")
42
48
  else
43
- [:tag, ExceptionFinder.new(txt).find]
49
+ FinderResult.new(:tag, ExceptionFinder.new(txt).find)
44
50
  end
45
51
  else
46
- [:tag, ""]
52
+ FinderResult.new(:tag, "")
47
53
  end
48
54
  end
49
55
 
50
56
  def script(line)
51
57
  txt = line[:value][:text]
52
58
  if could_match_script?(txt)
53
- [:script, ExceptionFinder.new(txt).find]
59
+ FinderResult.new(:script, ExceptionFinder.new(txt).find)
54
60
  else
55
- [:script, ""]
61
+ FinderResult.new(:script, "")
56
62
  end
57
63
  end
58
64
 
@@ -0,0 +1,51 @@
1
+ module Haml
2
+ module I18n
3
+ class Extractor
4
+ class InterpolationHelper
5
+
6
+ include Helpers::StringHelpers
7
+
8
+ # takes an original_line and text_to_replace, keyname_name and gives back the result for that...
9
+ def initialize(text_to_replace, t_name)
10
+ if Extractor.debug?
11
+ puts "<interpolationhelper>#{text_to_replace.inspect} #{t_name.inspect}</interpolationhelper>"
12
+ end
13
+ @t_name = t_name
14
+ @text_to_replace = text_to_replace
15
+ end
16
+
17
+ def keyname_with_vars()
18
+ "t('.#{@t_name}', #{interpolated_vars})"
19
+ end
20
+
21
+ def interpolated_vars
22
+ interpolations.map{|v|
23
+ ":#{normalized_name(v.dup)} => #{v}"
24
+ }.join(", ")
25
+ end
26
+
27
+ # matches multiple
28
+ def interpolations(arr = [], str = @text_to_replace)
29
+ # recurse scanner until no interpolations or string left
30
+ return arr if str.nil? || str.empty? || !interpolated?(str)
31
+ scanner = StringScanner.new(str)
32
+ scanner.scan_until(/\#{.*?}/)
33
+ so_far = scanner.pre_match + scanner.matched
34
+ arr << extract_interpolation(so_far)
35
+ arr = interpolations(arr, scanner.rest)
36
+ end
37
+
38
+ def extract_interpolation(str)
39
+ scanner = StringScanner.new(str)
40
+ scanner.scan_until /\#{/
41
+ rest_scanner = StringScanner.new(scanner.rest)
42
+ rest_scanner.scan_until(/}/)
43
+ interpolated = rest_scanner.pre_match
44
+ end
45
+
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,24 @@
1
+ module Haml
2
+ module I18n
3
+ class Extractor
4
+ class ReplacerResult
5
+
6
+ attr_accessor :modified_line, :t_name, :replaced_text, :should_be_replaced, :path
7
+
8
+ def initialize(modified_line, t_name, replaced_text, should_be_replaced, path)
9
+ @modified_line = modified_line
10
+ @t_name = t_name
11
+ @replaced_text = replaced_text
12
+ @should_be_replaced = should_be_replaced
13
+ @path = path
14
+ end
15
+
16
+ def info
17
+ { :modified_line => @modified_line, :t_name => @t_name, :replaced_text => @replaced_text, :path => @path}
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+
@@ -3,15 +3,14 @@ module Haml
3
3
  class Extractor
4
4
  class TextReplacer
5
5
 
6
+ include Helpers::StringHelpers
7
+
6
8
  attr_reader :full_line, :text_to_replace, :line_type
7
9
 
8
- T_REGEX = /t\('\..*'\)/
9
- # limit the number of chars
10
- LIMIT_KEY_NAME = 30
11
- # do not pollute the key space it will make it invalid yaml
12
- NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : )
10
+ T_REGEX = /t\('\.(.*?)'\)/
13
11
 
14
- def initialize(full_line, text_to_replace,line_type, metadata = {})
12
+ def initialize(full_line, text_to_replace,line_type, path, metadata = {})
13
+ @path = path
15
14
  @orig_line = @full_line = full_line
16
15
  @text_to_replace = text_to_replace
17
16
  @metadata = metadata
@@ -22,48 +21,61 @@ module Haml
22
21
  end
23
22
  end
24
23
 
24
+ def result
25
+ @result ||= Haml::I18n::Extractor::ReplacerResult.new(modified_line, t_name, @text_to_replace, true, @path)
26
+ end
27
+
25
28
  def replace_hash
26
- t_name = keyname(@text_to_replace, @orig_line)
27
- @replace_hash ||= { :modified_line => modified_line, :keyname => t_name, :replaced_text => @text_to_replace }
29
+ #legacy
30
+ result.info
31
+ end
32
+
33
+ def interpolation_helper
34
+ Haml::I18n::Extractor::InterpolationHelper.new(@text_to_replace, t_name)
28
35
  end
29
36
 
30
37
  # the new full line, including a `t()` replacement instead of the `text_to_replace` portion.
31
38
  def modified_line
39
+ return @full_line if has_been_translated?(@full_line)
32
40
  full_line = @full_line.dup
33
- return @full_line if has_been_translated?(full_line)
34
- remove_surrounding_quotes(full_line)
41
+ #puts t_method.inspect if Haml::I18n::Extractor.debug?
42
+ keyname = interpolated?(full_line) ? interpolation_helper.keyname_with_vars : t_method
43
+ gsub_replacement!(full_line, @text_to_replace, @orig_line, keyname)
35
44
  apply_ruby_evaling(full_line)
36
45
  full_line
37
46
  end
38
-
47
+
39
48
  private
40
49
 
41
- def keyname(to_replace, orig_line)
50
+ # the_key_to_use ( for example in t('.the_key_to_use')
51
+ def t_name(to_replace = @text_to_replace, orig_line = @orig_line)
42
52
  text_to_replace = to_replace.dup
43
53
  if has_been_translated?(text_to_replace)
44
- text_to_replace
54
+ text_to_replace.match T_REGEX
55
+ name = $1
45
56
  else
46
- name = normalize_name(text_to_replace)
47
- name = normalize_name(orig_line.dup) if name.empty?
48
- with_translate_method(name)
57
+ name = normalized_name(text_to_replace.dup)
58
+ name = normalized_name(orig_line.dup) if name.empty?
49
59
  end
60
+ name
61
+ end
62
+
63
+ # t('.the_key_to_use')
64
+ def t_method
65
+ with_translate_method(t_name)
50
66
  end
51
67
 
52
68
  def with_translate_method(name)
53
69
  "t('.#{name}')"
54
70
  end
55
71
 
56
- # adds the = to the right place in the string ... = t() stuff.
72
+ # adds the = to the right place in the string ... = t()
57
73
  def apply_ruby_evaling(str)
58
74
  if LINE_TYPES_ADD_EVAL.include?(@line_type)
59
75
  if @line_type == :tag
60
- #str.match /^([^\s\t]*)(.*)$/
61
- t_name = keyname(@text_to_replace, @orig_line)
62
- match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(t_name))
76
+ match_keyname = Regexp.new('[\s\t]*' + Regexp.escape(t_method))
63
77
  str.match(/(.*?)(#{match_keyname})/)
64
78
  elem = $1
65
- #binding.pry if str.match /color/
66
- #str.gsub!(keyname, "= #{keyname.strip}")
67
79
  if elem
68
80
  str.gsub!(Regexp.new(Regexp.escape(elem)), "#{elem}=") unless already_evaled?(elem)
69
81
  end
@@ -83,22 +95,15 @@ module Haml
83
95
  str.match T_REGEX
84
96
  end
85
97
 
86
- def remove_surrounding_quotes(str)
98
+ def gsub_replacement!(str, text_to_replace, orig_line, keyname_method )
87
99
  # if there are quotes surrounding the string, we want them removed as well...
88
- t_name = keyname(@text_to_replace, @orig_line)
89
- unless str.gsub!('"' + @text_to_replace + '"', t_name )
90
- unless str.gsub!("'" + @text_to_replace + "'", t_name)
91
- str.gsub!(@text_to_replace, t_name)
100
+ unless str.gsub!('"' + text_to_replace + '"', keyname_method )
101
+ unless str.gsub!("'" + text_to_replace + "'", keyname_method)
102
+ str.gsub!(text_to_replace, keyname_method)
92
103
  end
93
104
  end
94
105
  end
95
106
 
96
- def normalize_name(str)
97
- NOT_ALLOWED_IN_KEYNAME.each{ |rm_me| str.gsub!(rm_me, "") }
98
- str = str.gsub(/\s+/, " ").strip
99
- str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
100
- end
101
-
102
107
  end
103
108
  end
104
109
  end
@@ -1,7 +1,7 @@
1
1
  module Haml
2
2
  module I18n
3
3
  class Extractor
4
- class TaggingTool
4
+ class TaggingWriter
5
5
 
6
6
  DB = ".tags.haml-i18n-extractor"
7
7
  def initialize
@@ -6,9 +6,11 @@ require 'active_support/hash_with_indifferent_access'
6
6
  module Haml
7
7
  module I18n
8
8
  class Extractor
9
- class YamlTool
9
+ class YamlWriter
10
10
 
11
- attr_accessor :locale_hash, :yaml_file, :i18n_scope
11
+ attr_accessor :info_for_yaml, :yaml_file, :i18n_scope
12
+
13
+ include Helpers::StringHelpers
12
14
 
13
15
  def initialize(i18n_scope = nil, yaml_file = nil)
14
16
  @i18n_scope = i18n_scope && i18n_scope.to_sym || :en
@@ -19,13 +21,14 @@ module Haml
19
21
  end
20
22
  end
21
23
 
24
+ # converts the blob of info passed into it into i18n yaml like
22
25
  # {:en => {:view_name => {:key_name => :string_name } } }
23
26
  def yaml_hash
24
27
  yml = Hash.new
25
- @locale_hash.map do |line_no, info|
26
- unless info[:keyname].nil?
27
- keyspace = [@i18n_scope,standardized_viewnames(info[:path]), standarized_keyname(info[:keyname]),
28
- info[:replaced_text]].flatten
28
+ @info_for_yaml.map do |line_no, info|
29
+ unless info[:t_name].nil?
30
+ keyspace = [@i18n_scope,standardized_viewnames(info[:path]), info[:t_name],
31
+ normalize_interpolation(info[:replaced_text])].flatten
29
32
  yml.deep_merge!(nested_hash({},keyspace))
30
33
  end
31
34
  end
@@ -77,12 +80,6 @@ module Haml
77
80
  hash
78
81
  end
79
82
 
80
- # comes in like "t('.some_place')", return .some_place
81
- def standarized_keyname(name)
82
- name.match(/t\('\.(.*)'\)/)
83
- $1
84
- end
85
-
86
83
  # assuming rails format, app/views/users/index.html.haml return [users]
87
84
  # app/views/admin/users/index.html.haml return [admin, users]
88
85
  # app/views/admin/users/with_namespace/index.html.haml return [admin, users, with_namespace, index]
@@ -45,7 +45,7 @@ module Haml
45
45
  def end_message
46
46
  say("\n\n\n")
47
47
  say(highlight("Now run a git diff or such and see what changed!"))
48
- say(highlight("Check #{Haml::I18n::Extractor::TaggingTool::DB} if you have tagged any lines."))
48
+ say(highlight("Check #{Haml::I18n::Extractor::TaggingWriter::DB} if you have tagged any lines."))
49
49
  say("PS: If you have any feedback or ideas how this would be better, feel free to open an issue on github. See README for more info.")
50
50
  end
51
51
 
@@ -4,9 +4,47 @@ module Haml
4
4
  module Helpers
5
5
 
6
6
  module StringHelpers
7
+
8
+ # do not pollute the key space it will make it invalid yaml
9
+ NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : \ / )
10
+ # limit the number of chars
11
+ LIMIT_KEY_NAME = 30
12
+
13
+ def interpolated?(str)
14
+ str.match(/\#{/)
15
+ end
16
+
17
+ def normalized_name(str)
18
+ NOT_ALLOWED_IN_KEYNAME.each{ |rm_me| str.gsub!(rm_me, "") }
19
+ str = str.gsub(/\s+/, " ").strip
20
+ str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
21
+ end
22
+
23
+ def normalize_interpolation(str)
24
+ ret = change_one_interpolation(str)
25
+ if ret && interpolated?(ret)
26
+ ret = normalize_interpolation(ret)
27
+ end
28
+ ret
29
+ end
30
+
31
+ def change_one_interpolation(str)
32
+ if interpolated?(str)
33
+ scanner = StringScanner.new(str)
34
+ scanner.scan_until(/\#{(.*?)}/)
35
+ ret = "#{scanner.pre_match}"
36
+ ret << "%{#{normalized_name(scanner.matched)}}"
37
+ ret << "#{scanner.post_match}"
38
+ else
39
+ ret = str
40
+ end
41
+ ret
42
+ end
43
+
7
44
  def html_comment?(txt)
8
45
  txt.match(/<!--/) || txt.match(/-->\s*$/)
9
46
  end
47
+
10
48
  def link_to?(txt)
11
49
  txt.match(/link_to/) || txt.match(/^\s*['"]/) # %element= 'foo'
12
50
  end
@@ -1,7 +1,7 @@
1
1
  module Haml
2
2
  module I18n
3
3
  class Extractor
4
- VERSION = "0.4.3"
4
+ VERSION = "0.5.0"
5
5
  end
6
6
  end
7
7
  end
@@ -3,14 +3,17 @@ require "trollop"
3
3
  require "haml-i18n-extractor/version"
4
4
  require "haml-i18n-extractor/helpers"
5
5
 
6
- require "haml-i18n-extractor/extraction/text_finder"
7
- require "haml-i18n-extractor/extraction/exception_finder"
6
+ require "haml-i18n-extractor/extraction/finder/text_finder"
7
+ require "haml-i18n-extractor/extraction/finder/exception_finder"
8
+ require "haml-i18n-extractor/extraction/replacer/text_replacer"
9
+ require "haml-i18n-extractor/extraction/replacer/replacer_result"
10
+ require "haml-i18n-extractor/extraction/replacer/interpolation_helper"
11
+
8
12
  require "haml-i18n-extractor/extraction/haml_parser"
9
13
  require "haml-i18n-extractor/extraction/haml_reader"
10
- require "haml-i18n-extractor/extraction/tagging_tool"
11
- require "haml-i18n-extractor/extraction/text_replacer"
14
+ require "haml-i18n-extractor/extraction/tagging_writer"
12
15
  require "haml-i18n-extractor/extraction/haml_writer"
13
- require "haml-i18n-extractor/extraction/yaml_tool"
16
+ require "haml-i18n-extractor/extraction/yaml_writer"
14
17
  require "haml-i18n-extractor/extraction/extractor"
15
18
 
16
19
 
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # git co .; for f in test/*.rb; do mkdir -p /tmp/test; cp $f /tmp/$f; scripts/renew_test_syntax /tmp/$f > $f ; done; b rake
4
+ def replace(str)
5
+ str.match(/^(\s*)test/)
6
+ whitespace = $1
7
+ str = str.strip.downcase.gsub(/ do$/,"")
8
+ not_allowed = %w([ } = # > ' " % : - { ] . / \ @ , ( ) )
9
+ not_allowed.each {|c|
10
+ str = str.gsub(Regexp.new(Regexp.escape(c)),"")
11
+ }
12
+ str = str.gsub(" ","_")
13
+ str = str.gsub(/^(\s*)/,"#{whitespace}def ")
14
+ str
15
+ end
16
+
17
+ new_body = File.readlines(ARGV[0]).map{|l| l.match(/test.*do$/) ? replace(l) : l }
18
+ puts new_body
data/test/cli_test.rb CHANGED
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  module Haml
4
4
  class CLITest < MiniTest::Unit::TestCase
5
5
 
6
- test "it needs an explicit interactive or non-interactive option" do
6
+ def test_it_needs_an_explicit_interactive_or_noninteractive_option
7
7
  opts = {:non_interactive => nil, :interactive => nil}
8
8
  with_highline do
9
9
  begin
@@ -14,7 +14,7 @@ module Haml
14
14
  end
15
15
  end
16
16
 
17
- test "with a interactive option it needs a path" do
17
+ def test_with_a_interactive_option_it_needs_a_path
18
18
  opts = {:non_interactive => nil, :path => nil}
19
19
  with_highline do
20
20
  begin
@@ -22,14 +22,14 @@ module Haml
22
22
  %{link_to(pending_account_invoices_path(account),"http://random")} => ""
23
23
  }
24
24
 
25
- test "it finds text pretty simply" do
25
+ def test_it_finds_text_pretty_simply
26
26
  MATCHES.each do |k,v|
27
27
  #puts "handling #{k}"
28
28
  assert_equal find(k), v
29
29
  end
30
30
  end
31
31
 
32
- test "it actually needs to do something intellegent with intperolated values..." do
32
+ def test_it_actually_needs_to_do_something_intellegent_with_intperolated_values
33
33
  # @FIXME
34
34
  #raise "raw text matching needs to be responsible for knowing if needed to interpolate?"
35
35
  end