Workaround for Transifex YML issues

This commit is contained in:
Gerhard Schlager 2016-07-27 00:15:44 +02:00
parent f8144f07fd
commit 3d76ad623c
No known key found for this signature in database
GPG Key ID: 7DACA3C95B36014B

View File

@ -58,7 +58,7 @@ end
# Add comments to the top of files and replace the language (first key in YAML file)
def update_file_header(filename, language)
lines = File.readlines(filename)
lines.collect! {|line| line =~ /^[a-z_]+:$/i ? "#{language}:" : line}
lines.collect! { |line| line =~ /^[a-z_]+:$/i ? "#{language}:" : line }
File.open(filename, 'w+') do |f|
f.puts(YML_FILE_COMMENTS, '') unless lines[0][0] == '#'
@ -211,6 +211,61 @@ def add_anchors_and_aliases(english_alias_data, filename)
end
end
def fix_invalid_yml_keys(filename)
lines = File.readlines(filename)
# fix YAML keys which are on the wrong line
lines.each { |line| line.gsub!(/^(.*(?:'|"))(\s*\S*:)$/, "\\1\n\\2") }
File.open(filename, 'w+') do |f|
f.puts(lines)
end
end
def fix_invalid_yml(filename)
lines = File.readlines(filename)
lines.each_with_index do |line, i|
if line =~ /^\s+#.*$/i
# remove comments
lines[i] = nil
elsif line.strip.empty?
# remove empty lines
lines[i] = nil
elsif line =~ /^\s+.*: (?:""|''|)$/i
# remove lines which contain empty string values
lines[i] = nil
elsif line =~ /^(\s)*.*: \|$/i
next_line = i + 1 < lines.size ? lines[i + 1] : nil
if next_line.nil? || line[/^\s*/].size + 2 != next_line[/^\s*/].size
# remove lines which contain the value | without a string in the next line
lines[i] = nil
end
end
end.compact!
loop do
lines.each_with_index do |line, i|
if line =~ /^\s+\w*:\s*$/i
next_line = i + 1 < lines.size ? lines[i + 1] : nil
if next_line.nil? || line[/^\s*/].size >= next_line[/^\s*/].size
# remove lines which have an empty value and are not followed
# by a key on the next level
lines[i] = nil
end
end
end
break if lines.compact!.nil?
end
File.open(filename, 'w+') do |f|
f.puts(lines)
end
end
YML_DIRS.each do |dir|
YML_FILE_PREFIXES.each do |prefix|
english_alias_data = get_english_alias_data(dir, prefix)
@ -219,7 +274,13 @@ YML_DIRS.each do |dir|
filename = yml_path(dir, prefix, language)
if filename
# TODO remove this as soon as Transifex provides valid YAML files again
fix_invalid_yml_keys(filename)
fix_invalid_yml(filename)
# TODO check if this is still needed with recent Transifex changes
add_anchors_and_aliases(english_alias_data, filename)
update_file_header(filename, language)
end
end