]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-migrate-gettext.rb
Updated web version.
[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, :srcclean, :dest, :prev, :prevclean, :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 #print lin, "\n"
26 if lin =~ /^\#\| msgid "(.*)"$/
27 #print "first '%s'\n" % $1
28 @prev << $1
29 elsif lin =~ /^\#\| "(.*)"$/
30 @prev << $1
31 end
32 end
33 elsif lin =~ /^msgid "(.*)"/
34 section += 1
35 @src << $1
36 else
37 boom(section, lin, text)
38 end
39
40 # Source text
41 elsif section == 1
42 if lin =~ /^"(.*)"$/
43 @src << $1
44 elsif lin =~ /^msgstr "(.*)"/
45 section += 1
46 @dest << $1
47 else
48 boom(section, lin, text)
49 end
50
51 # Translated text
52 else
53 if lin =~ /^"(.*)"$/
54 @dest << $1
55 else
56 boom(section, lin, text)
57 end
58 end
59 end
60 end
61
62 def boom(section, line, text)
63 raise RuntimeError, "Unexpected string in %s:\n %s\n\nFull text: %s" %
64 [Sections[section], line, text]
65 end
66
67 def fuzzy?
68 @status == 'fuzzy'
69 end
70
71 def srcstr()
72 return @src.join("")
73 end
74
75 def deststr()
76 return @dest.join("")
77 end
78
79 def prevstr()
80 return @prev.join("")
81 end
82
83 def output()
84 # No use outputting empty translations
85 if '' == srcstr() && '' == deststr()
86 return
87 end
88 if '' != @type
89 print "#. type:%s\n" % [@type]
90 end
91 if @location
92 print "#: %s:%s\n" % [@location[:file], @location[:line]]
93 end
94 if fuzzy?
95 print "#, fuzzy\n"
96 if '' != prevstr()
97 @prev.each_with_index do |lin, idx|
98 if idx == 0
99 print "#| msgid \"%s\"\n" % lin
100 else
101 print "#| \"%s\"\n" % lin
102 end
103 end
104 end
105 end
106 @src.each_with_index do |lin, idx|
107 if idx == 0
108 print "msgid \"%s\"\n" % lin
109 else
110 print "\"%s\"\n" % lin
111 end
112 end
113 @dest.each_with_index do |lin, idx|
114 if idx == 0
115 print "msgstr \"%s\"\n" % lin
116 else
117 print "\"%s\"\n" % lin
118 end
119 end
120 print "\n"
121 end
122 end
123
124 raise ArgumentError, 'Source file not specified' if ARGV.size != 1
125 file = ARGV[0]
126 strings = File.open(file,'r').read.split(/\n\n/)[1..-1].map {|str| PoStr.new(str)}
127
128 #print strings
129
130 c = 0
131 strings.each do |entry|
132 # Ignore whitespace changes between prev and src
133 entry.prevclean = entry.prevstr().gsub(/ +/, ' ')
134 entry.srcclean = entry.srcstr().gsub(/ +/, ' ')
135
136 if entry.fuzzy?
137 if entry.prevclean == entry.srcclean
138 entry.status = ''
139 #print "# clear fuzzy (space)\n"
140 c = c + 1
141 entry.output
142 next
143 end
144
145 # Rewrite title strings, which lost '#' at the front
146 if entry.prevclean.gsub(/^#+ +/, '') == entry.srcclean
147 # FIXME Not safe to clear fuzzy flag, might have bogus translation
148 entry.status = ''
149 if entry.dest[0].gsub!(/^#+ +/, '') ||
150 ('' == entry.dest[0] && entry.dest[1].gsub!(/^#+ +/, ''))
151 #print "cleared fuzzy (title)\n"
152 c = c + 1
153 end
154 end
155
156 # Rewrite footnotes to use "<placeholder type=\"footnote\" id=\"0\"/>"
157 if entry.srcclean =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
158 #print "found footnote\n"
159 p = entry.prevclean
160 p.sub!(/([a-z]\.["”]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
161 p.sub!(/([a-z]\.["”]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
162 #print p, "\n"
163 #print entry.src, "\n"
164 if p == entry.srcclean
165 replaced = false
166 entry.dest.each do |part|
167 if part.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
168 replaced = true
169 end
170 end
171 entry.dest.each do |part|
172 if part.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
173 replaced = true
174 end
175 end
176 if replaced
177 #print "cleared fuzzy (footnote)\n"
178 entry.status = ''
179 c = c + 1
180 end
181 end
182 end
183 end
184
185 # Copy image references and single URLs unchanged, even for non-fuzzies
186 if entry.prevclean =~ /!\[\]\(Pictures\// ||
187 entry.srcclean =~ /^<ulink url=\\\".+\\\"\/>$/
188 entry.dest = entry.src.clone()
189 entry.status = ''
190 c = c + 1
191 end
192
193 entry.output
194 end
195 print "# Cleared %d fuzzy strings\n" % [c]