haml-i18n-extractor 0.4.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/TODO +6 -0
- data/lib/haml-i18n-extractor/extraction/extractor.rb +36 -26
- data/lib/haml-i18n-extractor/extraction/{exception_finder.rb → finder/exception_finder.rb} +0 -0
- data/lib/haml-i18n-extractor/extraction/{text_finder.rb → finder/text_finder.rb} +18 -12
- data/lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb +51 -0
- data/lib/haml-i18n-extractor/extraction/replacer/replacer_result.rb +24 -0
- data/lib/haml-i18n-extractor/extraction/{text_replacer.rb → replacer/text_replacer.rb} +38 -33
- data/lib/haml-i18n-extractor/extraction/{tagging_tool.rb → tagging_writer.rb} +1 -1
- data/lib/haml-i18n-extractor/extraction/{yaml_tool.rb → yaml_writer.rb} +9 -12
- data/lib/haml-i18n-extractor/flow/prompter.rb +1 -1
- data/lib/haml-i18n-extractor/helpers.rb +38 -0
- data/lib/haml-i18n-extractor/version.rb +1 -1
- data/lib/haml-i18n-extractor.rb +8 -5
- data/scripts/renew_test_syntax +18 -0
- data/test/cli_test.rb +2 -2
- data/test/exception_finder_test.rb +2 -2
- data/test/extractor_test.rb +23 -23
- data/test/haml_parser_test.rb +3 -3
- data/test/haml_reader_test.rb +3 -3
- data/test/haml_writer_test.rb +4 -2
- data/test/integration_test.rb +13 -11
- data/test/interpolation_helper_test.rb +43 -0
- data/test/prompter_test.rb +3 -3
- data/test/support/ex1.yml +15 -0
- data/test/support/ex2.output.haml +1 -1
- data/test/support/ex5.haml +4 -0
- data/test/support/ex5.output.haml +4 -0
- data/test/support/ex5.yml +8 -0
- data/test/{tagging_tool_test.rb → tagging_writer_test.rb} +6 -6
- data/test/test_helper.rb +2 -2
- data/test/text_finder_test.rb +22 -20
- data/test/text_replacer_test.rb +29 -27
- data/test/workflow_test.rb +5 -5
- data/test/yaml_writer_test.rb +120 -0
- metadata +27 -14
- data/test/yaml_tool_test.rb +0 -110
data/.gitignore
CHANGED
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 :
|
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
|
-
@
|
37
|
-
@
|
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
|
-
@
|
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
|
-
@
|
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
|
-
@
|
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
|
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
|
-
|
88
|
-
|
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
|
-
@
|
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
|
-
|
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
|
-
|
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
|
126
|
+
def replacement_result(orig_line, line_match, line_type, line_no)
|
126
127
|
if line_match && !line_match.empty?
|
127
|
-
|
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
|
-
|
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
|
137
|
-
@
|
134
|
+
def add_to_yaml_info(line_no, hash)
|
135
|
+
@info_for_yaml[line_no] = hash
|
138
136
|
end
|
139
137
|
|
140
|
-
def
|
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
|
-
|
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
|
File without changes
|
@@ -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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
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
|
-
|
47
|
+
FinderResult.new(:tag, "")
|
42
48
|
else
|
43
|
-
|
49
|
+
FinderResult.new(:tag, ExceptionFinder.new(txt).find)
|
44
50
|
end
|
45
51
|
else
|
46
|
-
|
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
|
-
|
59
|
+
FinderResult.new(:script, ExceptionFinder.new(txt).find)
|
54
60
|
else
|
55
|
-
|
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
|
-
|
27
|
-
|
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
|
-
|
34
|
-
|
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
|
-
|
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 =
|
47
|
-
name =
|
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()
|
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
|
-
|
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
|
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
|
-
|
89
|
-
|
90
|
-
|
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
|
@@ -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
|
9
|
+
class YamlWriter
|
10
10
|
|
11
|
-
attr_accessor :
|
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
|
-
@
|
26
|
-
unless info[:
|
27
|
-
keyspace = [@i18n_scope,standardized_viewnames(info[:path]),
|
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::
|
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
|
data/lib/haml-i18n-extractor.rb
CHANGED
@@ -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/
|
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/
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|