From 3d76ad623cb58e0dd5cab4be51dd8ac93a6d9c41 Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Wed, 27 Jul 2016 00:15:44 +0200 Subject: [PATCH] Workaround for Transifex YML issues --- script/pull_translations.rb | 63 ++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/script/pull_translations.rb b/script/pull_translations.rb index b17a8199f18..9fc02c7b228 100644 --- a/script/pull_translations.rb +++ b/script/pull_translations.rb @@ -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