From 0cedd75ff50bb04c5a842b6c03bcb812811a1413 Mon Sep 17 00:00:00 2001 From: Petter Reinholdtsen Date: Sun, 18 Feb 2018 04:56:21 +0000 Subject: [PATCH] Start on code to clear bogus fuzzies. --- fixup-migrate-gettext.rb | 83 +++++++++++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/fixup-migrate-gettext.rb b/fixup-migrate-gettext.rb index eb255da..66785d3 100755 --- a/fixup-migrate-gettext.rb +++ b/fixup-migrate-gettext.rb @@ -1,12 +1,14 @@ #!/usr/bin/ruby +# coding: utf-8 class PoStr Sections = %w(Headers Source Translated) - attr_accessor :status, :src, :dest, :type, :location + attr_accessor :status, :src, :dest, :prev, :type, :location def initialize(text) section = 0 @status = '' - @src = [] - @dest = [] + @src = '' + @dest = '' + @prev = '' text.split(/\n/).each do |lin| # Header @@ -19,7 +21,14 @@ class PoStr @status = 'fuzzy' if type == ',' and field == 'fuzzy' @type = value if type == '.' and field == 'type' @location = {:file => field, :line => value} if type == ':' - elsif lin =~ /^msgid (".*")/ + if type == '|' + if field =~ /^msgid "(.*)"/ + @prev << $1 + elsif field =~ /^"(.*)"$/ + @prev << $1 + end + end + elsif lin =~ /^msgid "(.*)"/ section += 1 @src << $1 else @@ -28,9 +37,9 @@ class PoStr # Source text elsif section == 1 - if lin =~ /^".*"$/ - @src << lin - elsif lin =~ /^msgstr (".*")/ + if lin =~ /^"(.*)"$/ + @src << $1 + elsif lin =~ /^msgstr "(.*)"/ section += 1 @dest << $1 else @@ -39,8 +48,8 @@ class PoStr # Translated text else - if lin =~ /^".*"$/ - @dest << lin + if lin =~ /^"(.*)"$/ + @dest << $1 else boom(section, lin, text) end @@ -56,9 +65,65 @@ class PoStr def fuzzy? @status == 'fuzzy' end + + def output() + # FIXME rewrite to output proper entries + if '' != @type + print "#. type: %s\n" % [@type] + end + if fuzzy? + print "#, fuzzy\n" + if '' != @prev + print "#| msgid \"%s\"\n" % [@prev] + end + end + print "msgid \"%s\"\n" % [@src] + print "msgstr \"%s\"\n" % [@dest] + print "\n\n" + end end raise ArgumentError, 'Source file not specified' if ARGV.size != 1 file = ARGV[0] strings = File.open(file,'r').read.split(/\n\n/)[1..-1].map {|str| PoStr.new(str)} + print strings + +c = 0 +strings.each do |entry| + if entry.fuzzy? + # Ignore whitespace changes between prev and src + if entry.prev.gsub(/ +/, ' ') == entry.src.gsub(/ +/, ' ') + entry.status = '' + print "clear fuzzy (space)\n" + c = c + 1 + end + + # Rewrite title strings, which lost '#' at the front + if entry.prev.gsub(/^#+ +/, '') == entry.src + entry.status = '' + entry.dest.gsub!(/^#+ +/, '') + print "cleared fuzzy (title)\n" + c = c + 1 + end + + # Rewrite footnotes to use "" + if entry.src =~ // + print "found footnote\n" + p = entry.prev + p.sub!(/([a-z]\.["”]?)(\d+)(\s)/, "\\1\\3") + p.sub!(/([a-z]\.["”]?)(\d+)$/, "\\1") + print p, "\n" + print entry.src, "\n" + if p == entry.src + entry.status = '' + entry.dest.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1\\3") + entry.dest.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1") + print "cleared fuzzy (footnote)\n" + c = c + 1 + end + end + end + entry.output +end +print "Would clear %d fuzzy\n" % [c] -- 2.47.2