]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-migrate-gettext.rb
Improve formatting and avoid unfuzzying missing footnotes.
[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 fuzzy?
92 print "#, fuzzy\n"
93 if '' != prevstr()
94 @prev.each_with_index do |lin, idx|
95 if idx == 0
96 print "#| msgid \"%s\"\n" % lin
97 else
98 print "#| \"%s\"\n" % lin
99 end
100 end
101 end
102 end
103 @src.each_with_index do |lin, idx|
104 if idx == 0
105 print "msgid \"%s\"\n" % lin
106 else
107 print "\"%s\"\n" % lin
108 end
109 end
110 @dest.each_with_index do |lin, idx|
111 if idx == 0
112 print "msgstr \"%s\"\n" % lin
113 else
114 print "\"%s\"\n" % lin
115 end
116 end
117 print "\n"
118 end
119 end
120
121 raise ArgumentError, 'Source file not specified' if ARGV.size != 1
122 file = ARGV[0]
123 strings = File.open(file,'r').read.split(/\n\n/)[1..-1].map {|str| PoStr.new(str)}
124
125 #print strings
126
127 c = 0
128 strings.each do |entry|
129 if entry.fuzzy?
130 # Ignore whitespace changes between prev and src
131 entry.prevclean = entry.prevstr().gsub(/ +/, ' ')
132 entry.srcclean = entry.srcstr().gsub(/ +/, ' ')
133
134 if entry.prevclean == entry.srcclean
135 entry.status = ''
136 #print "clear fuzzy (space)\n"
137 c = c + 1
138 next
139 end
140
141 # Rewrite title strings, which lost '#' at the front
142 if entry.prevclean.gsub(/^#+ +/, '') == entry.srcclean
143 entry.status = ''
144 # FIXME
145 if entry.dest[0].gsub!(/^#+ +/, '') ||
146 ('' == entry.dest[0] && entry.dest[1].gsub!(/^#+ +/, ''))
147 #print "cleared fuzzy (title)\n"
148 c = c + 1
149 end
150 end
151
152 # Rewrite footnotes to use "<placeholder type=\"footnote\" id=\"0\"/>"
153 if entry.srcclean =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
154 #print "found footnote\n"
155 p = entry.prevclean
156 p.sub!(/([a-z]\.["”]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
157 p.sub!(/([a-z]\.["”]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
158 #print p, "\n"
159 #print entry.src, "\n"
160 if p == entry.srcclean
161 replaced = false
162 entry.dest.each do |part|
163 if part.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
164 replaced = true
165 end
166 end
167 entry.dest.each do |part|
168 if part.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
169 replaced = true
170 end
171 end
172 if replaced
173 #print "cleared fuzzy (footnote)\n"
174 entry.status = ''
175 c = c + 1
176 end
177 end
178 end
179 end
180 entry.output
181 end
182 print "# Would clear %d fuzzy\n" % [c]