5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/annotate_yaml/annotate_yaml.rb', line 5
def annotate_locales
files = Dir.glob("config/locales/*.yml")
files.each do |file|
complete_yaml_file = File.open(file).read
complete_yaml_file_without_references = complete_yaml_file.gsub(/<<: \*.*/, '')
yaml_hash = YAML::load(complete_yaml_file_without_references)
array_of_hashes_of_values_with_keys = []
yaml_navigation(yaml_hash, [], array_of_hashes_of_values_with_keys)
string_result = ''
text = File.open(file).read
text.gsub!(/\r\n?/, "\n")
current_hash = array_of_hashes_of_values_with_keys.shift
text.each_line do |line|
begin
line_hash = YAML::load line
rescue Exception => e
string_result += line
next
end
if line_hash === false
string_result += line
else
line_key, line_value = line_hash.first
key, value = current_hash.first
if line_value == key
string_result += (line.end_with?(" \# #{value}\n") ? line : line.gsub("\n", '') + " \# #{value}\n")
current_hash = array_of_hashes_of_values_with_keys.shift
else
string_result += line
end
end
File.open(file, "w+") do |f|
f.write(string_result)
end
end
end
end
|