#!/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
@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
# Source text
elsif section == 1
- if lin =~ /^".*"$/
- @src << lin
- elsif lin =~ /^msgstr (".*")/
+ if lin =~ /^"(.*)"$/
+ @src << $1
+ elsif lin =~ /^msgstr "(.*)"/
section += 1
@dest << $1
else
# Translated text
else
- if lin =~ /^".*"$/
- @dest << lin
+ if lin =~ /^"(.*)"$/
+ @dest << $1
else
boom(section, lin, text)
end
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
-file = 'po/es/mwcc.po'
+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 "<placeholder type=\"footnote\" id=\"0\"/>"
+ if entry.src =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
+ print "found footnote\n"
+ p = entry.prev
+ 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
+ end
+ end
+ end
+ entry.output
+end
+print "Would clear %d fuzzy\n" % [c]