]> pere.pagekite.me Git - text-madewithcc.git/commitdiff
Improve formatting and avoid unfuzzying missing footnotes.
authorPetter Reinholdtsen <pere@hungry.com>
Sun, 18 Feb 2018 22:08:21 +0000 (23:08 +0100)
committerPetter Reinholdtsen <pere@hungry.com>
Thu, 22 Feb 2018 18:12:09 +0000 (18:12 +0000)
fixup-migrate-gettext.rb

index 2c172fbe9a34b0f920b1f6ab58ac0886955c511e..8a2f198492c35ed1a54b6ba32521e2eb69c4a248 100755 (executable)
@@ -2,13 +2,13 @@
 # coding: utf-8
 class PoStr
   Sections = %w(Headers Source Translated)
-  attr_accessor :status, :src, :dest, :prev, :type, :location
+  attr_accessor :status, :src, :srcclean, :dest, :prev, :prevclean, :type, :location
   def initialize(text)
     section = 0
     @status = ''
-    @src = ''
-    @dest = ''
-    @prev = ''
+    @src = []
+    @dest = []
+    @prev = []
 
     text.split(/\n/).each do |lin|
       # Header
@@ -68,20 +68,53 @@ class PoStr
     @status == 'fuzzy'
   end
 
+  def srcstr()
+    return @src.join("")
+  end
+
+  def deststr()
+    return @dest.join("")
+  end
+
+  def prevstr()
+    return @prev.join("")
+  end
+
   def output()
-    # FIXME rewrite to output proper entries
+    # No use outputting empty translations
+    if '' == srcstr() && '' == deststr()
+      return
+    end
     if '' != @type
-      print "#. type: %s\n" % [@type]
+      print "#. type:%s\n" % [@type]
     end
     if fuzzy?
       print "#, fuzzy\n"
-      if '' != @prev
-        print "#| msgid \"%s\"\n" % [@prev]
+      if '' != prevstr()
+        @prev.each_with_index do |lin, idx|
+          if idx == 0
+            print "#| msgid \"%s\"\n" % lin
+          else
+            print "#| \"%s\"\n" % lin
+          end
+        end
+      end
+    end
+    @src.each_with_index do |lin, idx|
+      if idx == 0
+        print "msgid \"%s\"\n" % lin
+      else
+        print "\"%s\"\n" % lin
       end
     end
-    print "msgid \"%s\"\n" % [@src]
-    print "msgstr \"%s\"\n" % [@dest]
-    print "\n\n"
+    @dest.each_with_index do |lin, idx|
+      if idx == 0
+        print "msgstr \"%s\"\n" % lin
+      else
+        print "\"%s\"\n" % lin
+      end
+    end
+    print "\n"
   end
 end
 
@@ -89,43 +122,61 @@ 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
+#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.prevclean = entry.prevstr().gsub(/ +/, ' ')
+    entry.srcclean = entry.srcstr().gsub(/ +/, ' ')
+    
+    if entry.prevclean == entry.srcclean
       entry.status = ''
-      print "clear fuzzy (space)\n"
+      #print "clear fuzzy (space)\n"
       c = c + 1
+      next
     end
 
     # Rewrite title strings, which lost '#' at the front
-    if entry.prev.gsub(/^#+ +/, '') == entry.src
+    if entry.prevclean.gsub(/^#+ +/, '') == entry.srcclean
       entry.status = ''
-      entry.dest.gsub!(/^#+ +/, '')
-      print "cleared fuzzy (title)\n"
-      c = c + 1
+      # FIXME
+      if entry.dest[0].gsub!(/^#+ +/, '') ||
+         ('' == entry.dest[0] && entry.dest[1].gsub!(/^#+ +/, ''))
+        #print "cleared fuzzy (title)\n"
+        c = c + 1
+      end
     end
 
     # Rewrite footnotes to use "<placeholder type=\"footnote\" id=\"0\"/>"
-    if entry.src =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
-      print "found footnote\n"
-      p = entry.prev
+    if entry.srcclean =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
+      #print "found footnote\n"
+      p = entry.prevclean
       p.sub!(/([a-z]\.["”]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
       p.sub!(/([a-z]\.["”]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
-      print p, "\n"
-      print entry.src, "\n"
-      if p == entry.src
-        entry.status = ''
-        entry.dest.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
-        entry.dest.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
-        print "cleared fuzzy (footnote)\n"
-        c = c + 1
+      #print p, "\n"
+      #print entry.src, "\n"
+      if p == entry.srcclean
+        replaced = false
+        entry.dest.each do |part|
+          if part.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
+            replaced = true
+          end
+        end
+        entry.dest.each do |part|
+          if part.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
+            replaced = true
+          end
+        end
+        if replaced
+          #print "cleared fuzzy (footnote)\n"
+          entry.status = ''
+          c = c + 1
+        end
       end
     end
   end
   entry.output
 end
-print "Would clear %d fuzzy\n" % [c]
+print "Would clear %d fuzzy\n" % [c]