]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-migrate-gettext.rb
Start on code to clear bogus fuzzies.
[text-madewithcc.git] / fixup-migrate-gettext.rb
1 #!/usr/bin/ruby
2 # coding: utf-8
3 class PoStr
4 Sections = %w(Headers Source Translated)
5 attr_accessor :status, :src, :dest, :prev, :type, :location
6 def initialize(text)
7 section = 0
8 @status = ''
9 @src = ''
10 @dest = ''
11 @prev = ''
12
13 text.split(/\n/).each do |lin|
14 # Header
15 if section == 0
16 if lin =~ /^#(.) ([^:]*)(?::(.*))?/
17 type = $1
18 field = $2
19 value = $3
20
21 @status = 'fuzzy' if type == ',' and field == 'fuzzy'
22 @type = value if type == '.' and field == 'type'
23 @location = {:file => field, :line => value} if type == ':'
24 if type == '|'
25 if field =~ /^msgid "(.*)"/
26 @prev << $1
27 elsif field =~ /^"(.*)"$/
28 @prev << $1
29 end
30 end
31 elsif lin =~ /^msgid "(.*)"/
32 section += 1
33 @src << $1
34 else
35 boom(section, lin, text)
36 end
37
38 # Source text
39 elsif section == 1
40 if lin =~ /^"(.*)"$/
41 @src << $1
42 elsif lin =~ /^msgstr "(.*)"/
43 section += 1
44 @dest << $1
45 else
46 boom(section, lin, text)
47 end
48
49 # Translated text
50 else
51 if lin =~ /^"(.*)"$/
52 @dest << $1
53 else
54 boom(section, lin, text)
55 end
56 end
57 end
58 end
59
60 def boom(section, line, text)
61 raise RuntimeError, "Unexpected string in %s:\n %s\n\nFull text: %s" %
62 [Sections[section], line, text]
63 end
64
65 def fuzzy?
66 @status == 'fuzzy'
67 end
68
69 def output()
70 # FIXME rewrite to output proper entries
71 if '' != @type
72 print "#. type: %s\n" % [@type]
73 end
74 if fuzzy?
75 print "#, fuzzy\n"
76 if '' != @prev
77 print "#| msgid \"%s\"\n" % [@prev]
78 end
79 end
80 print "msgid \"%s\"\n" % [@src]
81 print "msgstr \"%s\"\n" % [@dest]
82 print "\n\n"
83 end
84 end
85
86 raise ArgumentError, 'Source file not specified' if ARGV.size != 1
87 file = ARGV[0]
88 strings = File.open(file,'r').read.split(/\n\n/)[1..-1].map {|str| PoStr.new(str)}
89
90 print strings
91
92 c = 0
93 strings.each do |entry|
94 if entry.fuzzy?
95 # Ignore whitespace changes between prev and src
96 if entry.prev.gsub(/ +/, ' ') == entry.src.gsub(/ +/, ' ')
97 entry.status = ''
98 print "clear fuzzy (space)\n"
99 c = c + 1
100 end
101
102 # Rewrite title strings, which lost '#' at the front
103 if entry.prev.gsub(/^#+ +/, '') == entry.src
104 entry.status = ''
105 entry.dest.gsub!(/^#+ +/, '')
106 print "cleared fuzzy (title)\n"
107 c = c + 1
108 end
109
110 # Rewrite footnotes to use "<placeholder type=\"footnote\" id=\"0\"/>"
111 if entry.src =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
112 print "found footnote\n"
113 p = entry.prev
114 p.sub!(/([a-z]\.["”]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
115 p.sub!(/([a-z]\.["”]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
116 print p, "\n"
117 print entry.src, "\n"
118 if p == entry.src
119 entry.status = ''
120 entry.dest.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
121 entry.dest.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
122 print "cleared fuzzy (footnote)\n"
123 c = c + 1
124 end
125 end
126 end
127 entry.output
128 end
129 print "Would clear %d fuzzy\n" % [c]