Merge pull request #4348 from gschlager/transifex

Workaround for Transifex YML issues
This commit is contained in:
Neil Lalonde 2016-07-27 12:34:42 -04:00 committed by GitHub
commit 0fa8fb1774
1 changed files with 62 additions and 1 deletions

View File

@ -58,7 +58,7 @@ end
# Add comments to the top of files and replace the language (first key in YAML file) # Add comments to the top of files and replace the language (first key in YAML file)
def update_file_header(filename, language) def update_file_header(filename, language)
lines = File.readlines(filename) 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| File.open(filename, 'w+') do |f|
f.puts(YML_FILE_COMMENTS, '') unless lines[0][0] == '#' f.puts(YML_FILE_COMMENTS, '') unless lines[0][0] == '#'
@ -211,6 +211,61 @@ def add_anchors_and_aliases(english_alias_data, filename)
end end
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_DIRS.each do |dir|
YML_FILE_PREFIXES.each do |prefix| YML_FILE_PREFIXES.each do |prefix|
english_alias_data = get_english_alias_data(dir, 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) filename = yml_path(dir, prefix, language)
if filename 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) add_anchors_and_aliases(english_alias_data, filename)
update_file_header(filename, language) update_file_header(filename, language)
end end
end end