haml-i18n-extractor 0.3.5 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +1 -1
- data/TODO +28 -0
- data/lib/haml-i18n-extractor/exception_finder.rb +37 -0
- data/lib/haml-i18n-extractor/extractor.rb +18 -12
- data/lib/haml-i18n-extractor/haml_parser.rb +46 -0
- data/lib/haml-i18n-extractor/haml_reader.rb +10 -0
- data/lib/haml-i18n-extractor/text_finder.rb +52 -67
- data/lib/haml-i18n-extractor/text_replacer.rb +27 -13
- data/lib/haml-i18n-extractor/version.rb +1 -1
- data/lib/haml-i18n-extractor.rb +9 -1
- data/test/exception_finder_test.rb +46 -0
- data/test/extractor_test.rb +21 -2
- data/test/haml_parser_test.rb +31 -0
- data/test/haml_reader_test.rb +7 -2
- data/test/support/ex1.haml +4 -0
- data/test/support/ex1.output.haml +5 -1
- data/test/support/ex2.haml +31 -0
- data/test/support/ex2.output.haml +31 -0
- data/test/support/ex3.haml +13 -0
- data/test/support/ex3.output.haml +13 -0
- data/test/support/with_filter.haml +22 -0
- data/test/text_finder_test.rb +64 -64
- data/test/text_replacer_test.rb +11 -11
- data/test/yaml_tool_test.rb +1 -15
- metadata +197 -200
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MDg2NjFhOWVjNTZkOTc4ZWFiMGNkNDc4YmFmNTg3YjhkYmJmZDEwOQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NmZiYWU3YjVkZWNlN2U3MzFjNjIwNjJkZGI1ODAxMWRhZDQ2MGI2OA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MmE3ZThkNzJmN2MxYjJlMGM0ZDM4M2NkZTY4NDkzMzk0YjdhYjY2YzM0NDUz
|
10
|
+
NGVmMTZmMWVkNTIzYjU1MDQ0Mzc4NzUwMzJhYjA5NTIwMTczZGU3ZjU1MDc0
|
11
|
+
OWYzZjI0OWUwNTBmOWQ0YTQ3ZDlhMzg4YjM1NGJmMWU3MGUxZTg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzdhNzRkYThiNWMyZjAzMzFhNjExOTliZjg5ZjE1ZGZlMmI3NTM0MGJhNDg2
|
14
|
+
OTViOWRiYTZiYTEwYzQ2ODczYzdlYWI5ZWYxYzE2NGZjNmZhMTU2Y2MyYzMy
|
15
|
+
N2NmYzNlZDBjODJiZTg2YmQ1YzEwODM3MDljNzNhZGY1ZmQ3Mjg=
|
data/.gitignore
CHANGED
data/TODO
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
- Refactor tests: split out what is integration vs unit, too much coupling.
|
7
7
|
- Prompter.new should just be a singleton.
|
8
8
|
- rm hax_shit, move to setup/teardown, shutdown/startup
|
9
|
+
- make directories now that are more abstractions? finder/ -> finder,exceptionfinder,etc...replacer/ -> parser too, & stuffz?
|
9
10
|
- rm the :overwrite, :dump stuff to UserAction?
|
10
11
|
|
11
12
|
* Issues
|
@@ -17,3 +18,30 @@
|
|
17
18
|
|
18
19
|
- handled email suffixes (foo.en.html.haml etc)
|
19
20
|
- add herbgobbler (ERB) and this to another more generalized repo for translating templates?
|
21
|
+
|
22
|
+
* Refactor
|
23
|
+
|
24
|
+
https://github.com/shaiguitar/haml-i18n-extractor/issues/2
|
25
|
+
|
26
|
+
Now we are able to have internal access to Haml::Parser internals, we can deal with...
|
27
|
+
|
28
|
+
Html Comments?!
|
29
|
+
Filters?!
|
30
|
+
use with_filter.haml...
|
31
|
+
|
32
|
+
ExceptionText needs to be responsible for knowing if needed to interpolate?
|
33
|
+
|
34
|
+
(BUG: duplicate texts in one line...)
|
35
|
+
Basically, need to make a list of texts that the finder needs to match w/out a full haml element, etc, just raw text
|
36
|
+
IF no match, keep behavior.
|
37
|
+
IF match no_interpolation?, check if duplicate, find correct index.
|
38
|
+
IF match with_interpolation?, find interpolation index, check if whole thing duplicate, find correct index.
|
39
|
+
|
40
|
+
it then passes back to orchestrator info as well:
|
41
|
+
text_it_should_translate
|
42
|
+
indexes of what to replace, where etc?
|
43
|
+
line_type
|
44
|
+
is_interpolated?
|
45
|
+
?
|
46
|
+
|
47
|
+
?
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Haml
|
2
|
+
module I18n
|
3
|
+
class Extractor
|
4
|
+
class ExceptionFinder
|
5
|
+
|
6
|
+
def initialize(text)
|
7
|
+
@text = text
|
8
|
+
end
|
9
|
+
|
10
|
+
LINK_TO_REGEX_DOUBLE_Q = /link_to\s*\(?\s*["](.*?)["]\s*,\s*(.*)\)?/
|
11
|
+
LINK_TO_REGEX_SINGLE_Q = /link_to\s*\(?\s*['](.*?)[']\s*,\s*(.*)\)?/
|
12
|
+
LINK_TO_BLOCK_FORM_SINGLE_Q = /link_to\s*\(?['](.*?)[']\)?.*\sdo\s*$/
|
13
|
+
LINK_TO_BLOCK_FORM_DOUBLE_Q = /link_to\s*\(?["](.*?)["]\)?.*\sdo\s*$/
|
14
|
+
LINK_TO_NO_QUOTES = /link_to\s*\(?([^'"]*?)\)?.*/
|
15
|
+
|
16
|
+
# this class simply returns text except for anything that matches these regexes.
|
17
|
+
# returns first match.
|
18
|
+
EXCEPTION_MATCHES = [ LINK_TO_BLOCK_FORM_DOUBLE_Q, LINK_TO_BLOCK_FORM_SINGLE_Q,
|
19
|
+
LINK_TO_REGEX_DOUBLE_Q, LINK_TO_REGEX_SINGLE_Q , LINK_TO_NO_QUOTES ]
|
20
|
+
|
21
|
+
def find
|
22
|
+
ret = @text
|
23
|
+
EXCEPTION_MATCHES.each do |regex|
|
24
|
+
if @text.match(regex)
|
25
|
+
ret = $1
|
26
|
+
break # return whatever it finds on first try, order of above regexes matters
|
27
|
+
end
|
28
|
+
end
|
29
|
+
ret
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
@@ -11,11 +11,12 @@ module Haml
|
|
11
11
|
class NotDefinedLineType < StandardError ; end
|
12
12
|
class AbortFile < StandardError ; end
|
13
13
|
|
14
|
-
LINE_TYPES_ALL = [:
|
15
|
-
LINE_TYPES_ADD_EVAL = [:
|
14
|
+
LINE_TYPES_ALL = [:plain, :script, :silent_script, :haml_comment, :tag, :comment, :doctype, :filter, :root]
|
15
|
+
LINE_TYPES_ADD_EVAL = [:plain, :tag]
|
16
16
|
|
17
17
|
attr_reader :haml_reader, :haml_writer
|
18
18
|
attr_reader :locale_hash, :yaml_tool, :type
|
19
|
+
attr_reader :current_line
|
19
20
|
|
20
21
|
DEFAULT_LINE_LOCALE_HASH = { :modified_line => nil,:keyname => nil,:replaced_text => nil, :path => nil }
|
21
22
|
|
@@ -57,15 +58,16 @@ module Haml
|
|
57
58
|
|
58
59
|
def new_body
|
59
60
|
begin
|
60
|
-
@
|
61
|
-
|
62
|
-
process_line(orig_line,
|
61
|
+
@current_line = 1
|
62
|
+
@haml_reader.lines.each do |orig_line|
|
63
|
+
process_line(orig_line, @current_line)
|
64
|
+
@current_line += 1
|
63
65
|
end
|
64
66
|
rescue AbortFile
|
65
67
|
@prompter.moving_to_next_file
|
66
|
-
add_rest_of_file_to_body(@
|
68
|
+
add_rest_of_file_to_body(@current_line)
|
67
69
|
end
|
68
|
-
@body.join("\n")
|
70
|
+
@body.join("\n") + "\n"
|
69
71
|
end
|
70
72
|
|
71
73
|
# this is the bulk of it:
|
@@ -75,7 +77,7 @@ module Haml
|
|
75
77
|
def process_line(orig_line, line_no)
|
76
78
|
orig_line.chomp!
|
77
79
|
orig_line, whitespace = handle_line_whitespace(orig_line)
|
78
|
-
line_type, line_match = handle_line_finding(orig_line)
|
80
|
+
line_type, line_match = handle_line_finding(orig_line, line_no)
|
79
81
|
should_be_replaced, text_to_replace, line_locale_hash = gather_replacement_info(orig_line, line_match, line_type, line_no)
|
80
82
|
|
81
83
|
user_action = Haml::I18n::Extractor::UserAction.new('y') # default if no prompting: just do it.
|
@@ -108,14 +110,14 @@ module Haml
|
|
108
110
|
private
|
109
111
|
|
110
112
|
def add_rest_of_file_to_body(line_no)
|
111
|
-
@haml_reader.lines[line_no..@haml_reader.lines.size-1].map do |orig_ln|
|
113
|
+
@haml_reader.lines[line_no-1..@haml_reader.lines.size-1].map do |orig_ln|
|
112
114
|
add_to_body(orig_ln.chomp)
|
113
115
|
end
|
114
116
|
end
|
115
117
|
|
116
118
|
def gather_replacement_info(orig_line, line_match, line_type, line_no)
|
117
119
|
if line_match && !line_match.empty?
|
118
|
-
replacer = Haml::I18n::Extractor::TextReplacer.new(orig_line, line_match, line_type)
|
120
|
+
replacer = Haml::I18n::Extractor::TextReplacer.new(orig_line, line_match, line_type, line_metadata(line_no))
|
119
121
|
hash = replacer.replace_hash.dup.merge!({:path => @haml_reader.path })
|
120
122
|
[ true, hash[:modified_line], hash ]
|
121
123
|
else
|
@@ -128,8 +130,12 @@ module Haml
|
|
128
130
|
@locale_hash[line_no] = hash
|
129
131
|
end
|
130
132
|
|
131
|
-
def handle_line_finding(orig_line)
|
132
|
-
Haml::I18n::Extractor::TextFinder.new(orig_line).process_by_regex
|
133
|
+
def handle_line_finding(orig_line,lineno)
|
134
|
+
Haml::I18n::Extractor::TextFinder.new(orig_line,line_metadata(lineno)).process_by_regex
|
135
|
+
end
|
136
|
+
|
137
|
+
def line_metadata(lineno)
|
138
|
+
@haml_reader.metadata[lineno]
|
133
139
|
end
|
134
140
|
|
135
141
|
def handle_line_whitespace(orig_line)
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'haml/parser'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
module I18n
|
5
|
+
class Extractor
|
6
|
+
class HamlParser < Haml::Parser
|
7
|
+
|
8
|
+
def initialize(haml)
|
9
|
+
super(haml, Haml::Options.new)
|
10
|
+
end
|
11
|
+
|
12
|
+
def flattened_values
|
13
|
+
# make the haml we passed in a parse tree!
|
14
|
+
@ht_parse_tree = self.parse
|
15
|
+
ret = flatten_tree_attrs(@ht_parse_tree,[])
|
16
|
+
ret[1..-2] # we only want the actual lines, not the root node and last line which don't really exist
|
17
|
+
#ret
|
18
|
+
end
|
19
|
+
alias_method :metadata, :flattened_values
|
20
|
+
|
21
|
+
# recurse the tree and return an array of the lines
|
22
|
+
# don't care about the tree structure, we don't care about context, just line-by-line
|
23
|
+
# but we want the haml::parser metadata for all the lines, iterated over
|
24
|
+
def flatten_tree_attrs(node,array)
|
25
|
+
array << node_attributes(node)
|
26
|
+
node.children.each do |child|
|
27
|
+
flatten_tree_attrs(child,array)
|
28
|
+
end
|
29
|
+
array
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def node_attributes(node)
|
35
|
+
attrs = {}
|
36
|
+
keep = node.members.reject{|m| m == :parent || m == :children }
|
37
|
+
keep.each{|attr|
|
38
|
+
attrs[attr] = node[attr]
|
39
|
+
}
|
40
|
+
attrs
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -14,6 +14,16 @@ module Haml
|
|
14
14
|
file.rewind
|
15
15
|
end
|
16
16
|
|
17
|
+
# cache me?
|
18
|
+
def metadata
|
19
|
+
parser = Haml::I18n::Extractor::HamlParser.new(@body)
|
20
|
+
line_metadatas = parser.flattened_values
|
21
|
+
metadata = {}
|
22
|
+
# blank lines are missing! so api [] access per line in file, not by array index.
|
23
|
+
line_metadatas.each{|line_data| metadata.merge!(line_data[:line] => line_data) }
|
24
|
+
metadata
|
25
|
+
end
|
26
|
+
|
17
27
|
end
|
18
28
|
end
|
19
29
|
end
|
@@ -5,90 +5,75 @@ module Haml
|
|
5
5
|
class Extractor
|
6
6
|
class TextFinder
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
## hint: Use http://rubular.com
|
13
|
-
SIMPLE_STRING_REGEX = /["'](.*)["']/
|
14
|
-
# = "yes"
|
15
|
-
# = 'yes'
|
16
|
-
LINK_TO_REGEX = /link_to\s*\(?\s*['"](.*)['"]\s*,\s*(.*)\)?/
|
17
|
-
# = link_to "yes", "http://bla.com"
|
18
|
-
# = link_to('yes' , "http://bla.com")
|
19
|
-
# = link_to( "yes's w space", "http://bla.com")
|
20
|
-
ELEMENT_REGEX = /%([\w.#-]+)({.+?})?(=)?(.*)/
|
21
|
-
# %foo.bar yes
|
22
|
-
# %foo.bar= no
|
23
|
-
# %foo{:a => 'b'} yes
|
24
|
-
# rubular.com against %foo#bar#cheez{:cheeze => 'hell'}= "what #{var}"
|
25
|
-
|
26
|
-
def initialize(haml)
|
27
|
-
@haml = haml
|
28
|
-
@parser = Haml::Parser.new(haml, Haml::Options.new)
|
8
|
+
def initialize(orig_line,line_metadata)
|
9
|
+
@orig_line = orig_line
|
10
|
+
@metadata = line_metadata
|
29
11
|
end
|
30
|
-
|
31
|
-
def line
|
32
|
-
if @haml == ""
|
33
|
-
return Haml::Parser::Line.new("", "", "", 0, @parser, false)
|
34
|
-
end
|
35
12
|
|
36
|
-
match = @haml.rstrip.scan(/(([ \t]+)?(.*?))(?:\Z|\r\n|\r|\n)/m)
|
37
|
-
match.pop
|
38
|
-
haml_line ||= match.each_with_index.map do |(full, whitespace, text), index|
|
39
|
-
Haml::Parser::Line.new(whitespace, text.rstrip, full, index, @parser, false)
|
40
|
-
end.first
|
41
|
-
haml_line
|
42
|
-
end
|
43
|
-
|
44
13
|
def find
|
45
14
|
text_match = process_by_regex.last
|
46
15
|
end
|
47
16
|
|
48
17
|
# returns [ line_type, text_found ]
|
49
18
|
def process_by_regex
|
50
|
-
|
51
|
-
|
52
|
-
[:
|
53
|
-
|
54
|
-
parse_silent_script
|
55
|
-
when Haml::Parser::ELEMENT
|
56
|
-
parse_element
|
57
|
-
when Haml::Parser::SCRIPT
|
58
|
-
parse_loud_script
|
59
|
-
else
|
60
|
-
[:text, line.full.strip]
|
19
|
+
return [:plain, ""] if @metadata.nil? # a linebreak in a haml file, empty.
|
20
|
+
if ENV['DEBUG']
|
21
|
+
puts @metadata[:type]
|
22
|
+
puts @orig_line
|
61
23
|
end
|
24
|
+
@metadata && send("#{@metadata[:type]}", @metadata)
|
62
25
|
end
|
63
|
-
|
26
|
+
|
64
27
|
private
|
65
28
|
|
66
|
-
def
|
67
|
-
line
|
68
|
-
[:
|
29
|
+
def plain(line)
|
30
|
+
txt = line[:value][:text]
|
31
|
+
[:plain, txt]
|
69
32
|
end
|
70
|
-
|
71
|
-
def
|
72
|
-
line
|
73
|
-
|
74
|
-
|
75
|
-
|
33
|
+
|
34
|
+
def tag(line)
|
35
|
+
txt = line[:value][:value]
|
36
|
+
if txt
|
37
|
+
has_script_in_tag = line[:value][:parse] # %element= foo
|
38
|
+
has_exception = txt.match(/link_to/) || txt.match(/^\s*['"]/) # %element= 'foo'
|
39
|
+
if has_script_in_tag && !has_exception
|
40
|
+
[:tag, ""]
|
41
|
+
else
|
42
|
+
[:tag, ExceptionFinder.new(txt).find]
|
43
|
+
end
|
44
|
+
else
|
45
|
+
[:tag, ""]
|
46
|
+
end
|
76
47
|
end
|
77
|
-
|
78
|
-
def
|
79
|
-
line
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
if is_loud_script
|
85
|
-
self.class.new("= #{text}").process_by_regex # treat like a loud script.
|
48
|
+
|
49
|
+
def script(line)
|
50
|
+
txt = line[:value][:text]
|
51
|
+
scanner = StringScanner.new(txt)
|
52
|
+
scanner.scan(/\s+/)
|
53
|
+
if scanner.scan(/['"]/) || scanner.scan(/link_to/)
|
54
|
+
[:script, ExceptionFinder.new(txt).find]
|
86
55
|
else
|
87
|
-
[:
|
56
|
+
[:script, ""]
|
88
57
|
end
|
89
58
|
end
|
90
|
-
|
59
|
+
|
60
|
+
def filter(line)
|
61
|
+
#$stderr.puts('=' * 80)
|
62
|
+
#$stderr.puts(line.inspect)
|
63
|
+
#$stderr.puts("have not handled filters!")
|
64
|
+
#$stderr.puts("please remind me to fix this")
|
65
|
+
#$stderr.puts('=' * 80)
|
66
|
+
end
|
67
|
+
|
68
|
+
# move to method missing and LINE_TYPES_IGNORE?
|
69
|
+
# LINE_TYPES_IGNORE = [:silent_script, :haml_comment, :comment, :doctype, :root]
|
70
|
+
def silent_script(line); end
|
71
|
+
def haml_comment(line); end
|
72
|
+
def comment(line); end
|
73
|
+
def doctype(line); end
|
74
|
+
def root(line); end
|
75
|
+
|
91
76
|
end
|
92
77
|
end
|
93
78
|
end
|
94
|
-
end
|
79
|
+
end
|
@@ -9,11 +9,12 @@ module Haml
|
|
9
9
|
# limit the number of chars
|
10
10
|
LIMIT_KEY_NAME = 30
|
11
11
|
# do not pollute the key space it will make it invalid yaml
|
12
|
-
NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } )
|
13
|
-
|
14
|
-
def initialize(full_line, text_to_replace,line_type)
|
15
|
-
@full_line = full_line
|
12
|
+
NOT_ALLOWED_IN_KEYNAME = %w( ~ ` ! @ # $ % ^ & * - ( ) , ? { } = ' " : )
|
13
|
+
|
14
|
+
def initialize(full_line, text_to_replace,line_type, metadata = {})
|
15
|
+
@orig_line = @full_line = full_line
|
16
16
|
@text_to_replace = text_to_replace
|
17
|
+
@metadata = metadata
|
17
18
|
if LINE_TYPES_ALL.include?(line_type)
|
18
19
|
@line_type = line_type
|
19
20
|
else
|
@@ -24,7 +25,7 @@ module Haml
|
|
24
25
|
def replace_hash
|
25
26
|
@replace_hash ||= { :modified_line => modified_line, :keyname => keyname, :replaced_text => @text_to_replace }
|
26
27
|
end
|
27
|
-
|
28
|
+
|
28
29
|
# the new full line, including a `t()` replacement instead of the `text_to_replace` portion.
|
29
30
|
def modified_line
|
30
31
|
full_line = @full_line.dup
|
@@ -33,26 +34,39 @@ module Haml
|
|
33
34
|
apply_ruby_evaling(full_line)
|
34
35
|
full_line
|
35
36
|
end
|
36
|
-
|
37
|
+
|
37
38
|
private
|
38
|
-
|
39
|
+
|
39
40
|
def keyname
|
40
41
|
text_to_replace = @text_to_replace.dup
|
41
|
-
has_been_translated?(text_to_replace)
|
42
|
+
if has_been_translated?(text_to_replace)
|
43
|
+
text_to_replace
|
44
|
+
else
|
45
|
+
name = to_keyname(text_to_replace)
|
46
|
+
name = to_keyname(@orig_line.dup) if name.empty?
|
47
|
+
"t('.#{name}')"
|
48
|
+
end
|
42
49
|
end
|
43
50
|
|
51
|
+
# adds the = to the right place in the string ... = t() stuff.
|
44
52
|
def apply_ruby_evaling(str)
|
45
53
|
if LINE_TYPES_ADD_EVAL.include?(@line_type)
|
46
|
-
if @line_type == :
|
54
|
+
if @line_type == :tag
|
47
55
|
str.match /^([^\s\t]*)(.*)$/
|
48
56
|
elem, keyname = $1, $2
|
49
|
-
str.gsub!($2, "= #{$2.strip}")
|
50
|
-
elsif @line_type == :
|
57
|
+
str.gsub!($2, "= #{$2.strip}") unless already_evaled?(elem)
|
58
|
+
elsif @line_type == :plain
|
51
59
|
str.gsub!(str, "= "+str)
|
52
60
|
end
|
53
61
|
end
|
54
62
|
end
|
55
63
|
|
64
|
+
def already_evaled?(elem)
|
65
|
+
# poor elem.split('').last == '='
|
66
|
+
# better, haml guts:
|
67
|
+
@metadata[:value] && @metadata[:value][:parse]
|
68
|
+
end
|
69
|
+
|
56
70
|
def has_been_translated?(str)
|
57
71
|
str.match T_REGEX
|
58
72
|
end
|
@@ -65,13 +79,13 @@ module Haml
|
|
65
79
|
end
|
66
80
|
end
|
67
81
|
end
|
68
|
-
|
82
|
+
|
69
83
|
def to_keyname(str)
|
70
84
|
NOT_ALLOWED_IN_KEYNAME.each{ |rm_me| str.gsub!(rm_me, "") }
|
71
85
|
str = str.gsub(/\s+/, " ").strip
|
72
86
|
str.downcase.tr(' ', '_')[0..LIMIT_KEY_NAME-1]
|
73
87
|
end
|
74
|
-
|
88
|
+
|
75
89
|
end
|
76
90
|
end
|
77
91
|
end
|
data/lib/haml-i18n-extractor.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
require "haml-i18n-extractor/version"
|
2
2
|
require "haml-i18n-extractor/helpers"
|
3
|
+
|
3
4
|
require "haml-i18n-extractor/text_finder"
|
4
|
-
require "haml-i18n-extractor/
|
5
|
+
require "haml-i18n-extractor/exception_finder"
|
6
|
+
|
7
|
+
|
8
|
+
require "haml-i18n-extractor/haml_parser"
|
5
9
|
require "haml-i18n-extractor/haml_reader"
|
10
|
+
|
11
|
+
require "haml-i18n-extractor/text_replacer"
|
6
12
|
require "haml-i18n-extractor/haml_writer"
|
7
13
|
require "haml-i18n-extractor/yaml_tool"
|
14
|
+
|
8
15
|
require "haml-i18n-extractor/extractor"
|
16
|
+
|
9
17
|
require "haml-i18n-extractor/prompter"
|
10
18
|
require "haml-i18n-extractor/user_action"
|
11
19
|
require "haml-i18n-extractor/tagging_tool"
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
class ExceptionFinderTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
MATCHES = {
|
7
|
+
%{TEXT} => "TEXT",
|
8
|
+
%{"TEXT"} => "\"TEXT\"",
|
9
|
+
%{'TEXT'} => "'TEXT'",
|
10
|
+
%{(TEX'T)} => "(TEX'T)",
|
11
|
+
%{"TEX'T"} => "\"TEX'T\"",
|
12
|
+
%{ TEXT} => " TEXT",
|
13
|
+
|
14
|
+
%{TEXT \#{with_code}} => "TEXT \#{with_code}",
|
15
|
+
|
16
|
+
%{link_to 'TEXT', "http://bla"} => "TEXT",
|
17
|
+
%{link_to('TEXT', "http://bla")} => "TEXT",
|
18
|
+
%{link_to "TEXT", "http://bla")} => "TEXT",
|
19
|
+
%{link_to("TEXT"), role: 'button', data: {toggle: 'dropdown'} do} => "TEXT",
|
20
|
+
%{link_to "TEXT", role: 'button', data: {toggle: 'dropdown'} do} => "TEXT",
|
21
|
+
%{link_to pending_account_invoices_path(account) do} => "",
|
22
|
+
%{link_to(pending_account_invoices_path(account),"http://random")} => ""
|
23
|
+
}
|
24
|
+
|
25
|
+
test "it finds text pretty simply" do
|
26
|
+
MATCHES.each do |k,v|
|
27
|
+
#puts "handling #{k}"
|
28
|
+
assert_equal find(k), v
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
test "it actually needs to do something intellegent with intperolated values..." do
|
33
|
+
# @FIXME
|
34
|
+
#raise "raw text matching needs to be responsible for knowing if needed to interpolate?"
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def find(txt)
|
40
|
+
Haml::I18n::Extractor::ExceptionFinder.new(txt).find
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
|
data/test/extractor_test.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
module Haml
|
4
|
+
# really should just be part of integration_test.rb , testing shit from the orchestration class
|
4
5
|
class ExtractorTest < MiniTest::Unit::TestCase
|
5
6
|
|
6
7
|
def setup
|
@@ -11,6 +12,11 @@ module Haml
|
|
11
12
|
@ex1.run
|
12
13
|
end
|
13
14
|
|
15
|
+
test "it should be able to process filters with the haml_parser now..." do
|
16
|
+
#@FIXME
|
17
|
+
#raise 'implment me...check the finder#filters method and make sure you process the whole file at once so the parser gets it...'
|
18
|
+
end
|
19
|
+
|
14
20
|
test "with a type of overwrite or dump affecting haml writer" do
|
15
21
|
h = Haml::I18n::Extractor.new(file_path("ex1.haml"), :type => :overwrite)
|
16
22
|
assert_equal h.haml_writer.overwrite?, true
|
@@ -78,7 +84,7 @@ module Haml
|
|
78
84
|
with_highline(user_input) do
|
79
85
|
h.run
|
80
86
|
end
|
81
|
-
|
87
|
+
assert (File.readlines(Haml::I18n::Extractor::TaggingTool::DB).size != 0), "tag lines get added to file"
|
82
88
|
end
|
83
89
|
|
84
90
|
|
@@ -91,11 +97,24 @@ module Haml
|
|
91
97
|
end
|
92
98
|
end
|
93
99
|
|
94
|
-
|
100
|
+
# really integration tests...movez.
|
101
|
+
test "it can replace a string body and have expected output ex3" do
|
102
|
+
expected_output = File.read(file_path("ex3.output.haml"))
|
103
|
+
assert_equal Haml::I18n::Extractor.new(file_path("ex3.haml")).new_body, expected_output
|
104
|
+
end
|
105
|
+
|
106
|
+
test "it can replace a string body and have expected output ex2" do
|
107
|
+
expected_output = File.read(file_path("ex2.output.haml"))
|
108
|
+
assert_equal Haml::I18n::Extractor.new(file_path("ex2.haml")).new_body, expected_output
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
test "it can replace a string body and have expected output ex1" do
|
95
113
|
expected_output = File.read(file_path("ex1.output.haml"))
|
96
114
|
assert_equal @ex1.new_body, expected_output
|
97
115
|
end
|
98
116
|
|
117
|
+
|
99
118
|
test "it writes the haml to an out file if valid haml output" do
|
100
119
|
FileUtils.rm_rf(@ex1.haml_writer.path)
|
101
120
|
assert_equal File.exists?(@ex1.haml_writer.path), false
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Haml
|
4
|
+
class HamlParserTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@body = "- if true\n bar"
|
8
|
+
end
|
9
|
+
|
10
|
+
test "it can collect metadata about lines" do
|
11
|
+
tree = Haml::I18n::Extractor::HamlParser.new(@body)
|
12
|
+
line_metadatas = tree.flattened_values
|
13
|
+
assert_equal line_metadatas.size, 2
|
14
|
+
end
|
15
|
+
|
16
|
+
test "it can collect metadata about lines" do
|
17
|
+
parser = Haml::I18n::Extractor::HamlParser.new(@body)
|
18
|
+
line_metadatas = parser.flattened_values
|
19
|
+
assert_equal line_metadatas.size, 2
|
20
|
+
end
|
21
|
+
|
22
|
+
# easy api to use index <-> lineno
|
23
|
+
test "it is sorted by line numbers" do
|
24
|
+
parser = Haml::I18n::Extractor::HamlParser.new(@body)
|
25
|
+
line_metadatas = parser.flattened_values
|
26
|
+
assert_equal line_metadatas, line_metadatas.sort_by{|m| m[:line]}
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/test/haml_reader_test.rb
CHANGED
@@ -5,10 +5,11 @@ module Haml
|
|
5
5
|
class HamlReaderTest < MiniTest::Unit::TestCase
|
6
6
|
|
7
7
|
TEMP_FILE_PATH = "/tmp/foo_haml_extractor_test"
|
8
|
+
LINE_COUNT = 10
|
8
9
|
|
9
10
|
def setup
|
10
11
|
FileUtils.rm_rf(TEMP_FILE_PATH)
|
11
|
-
|
12
|
+
LINE_COUNT.times do |index|
|
12
13
|
File.open(TEMP_FILE_PATH, "a+") do |f|
|
13
14
|
f.puts "line #{index}"
|
14
15
|
end
|
@@ -22,7 +23,11 @@ module Haml
|
|
22
23
|
end
|
23
24
|
|
24
25
|
test "has an array of lines in that file" do
|
25
|
-
assert_equal @reader.lines.size,
|
26
|
+
assert_equal @reader.lines.size, LINE_COUNT
|
27
|
+
end
|
28
|
+
|
29
|
+
test "has metadata about each line" do
|
30
|
+
assert_equal @reader.metadata.size, LINE_COUNT
|
26
31
|
end
|
27
32
|
|
28
33
|
end
|
data/test/support/ex1.haml
CHANGED
@@ -24,5 +24,9 @@
|
|
24
24
|
%li
|
25
25
|
%a{:href => url_for([:admin, :accounts])}
|
26
26
|
%span Accounts
|
27
|
+
= link_to pending_account_invoices_path(account) do
|
28
|
+
this_linkez
|
27
29
|
- if true
|
28
30
|
What is@ supposed to be, is supposed to be! ~
|
31
|
+
%td=link_to("Details", admin_usage_import_url(usage_import.usage_report))
|
32
|
+
"raNdom"
|