-
Notifications
You must be signed in to change notification settings - Fork 0
/
distill.rb
executable file
·191 lines (176 loc) · 4.8 KB
/
distill.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/ruby -K0
# vim: set sw=3 ai sm:
#
# Simple script to extract either untranslated strings or fuzzies from a PO file
# so that we know what's still left to translate.
#
# Copyright (C) 2004, 2020 by Ambrose Li <ambrose.li@gmail.com>
require 'getoptlong'
STATE_MSGID = 1
STATE_MSGSTR = 2
STATE_MSGID_PLURAL = 3
STATE_MSGSTR_PLURAL= 4
STATE_OTHER = 0
list_untranslated_p = nil
list_fuzzy_p = nil
verbose_p = nil
state = STATE_OTHER
$fuzzy_p = nil
$plural_p = nil
$plural_index = nil
$notes = nil
$directives = nil
$msgid = nil
$msgstr = nil
$strings = {}
$string_fuzzy_p = {}
$string_notes = {}
$string_directives = {}
def flush
if !$msgid.nil? && !$msgstr.nil?
$strings[$msgid] = $msgstr
$string_notes[$msgid] = $notes unless $notes.nil?
$string_directives[$msgid] = $directives unless $directives.nil?
$string_fuzzy_p[$msgid] = true if $fuzzy_p
$notes = $directives = $fuzzy_p = $plural_p = $msgid = $msgstr = nil
end
end
MAX_WIDTH = 64
$max_width = MAX_WIDTH
def present( s )
it = ''
a = s.split(/\\n/)
0.upto(a.length - 2) {|i| a[i] += "\\n" }
a.each {|x|
0.step(x.length, $max_width) {|y|
it += "\"" + x[y...y+$max_width] + "\"\n"
}
}
return it
end
opts = GetoptLong.new(
[ '--dont-wrap', GetoptLong::NO_ARGUMENT ],
[ '--fuzzy', GetoptLong::NO_ARGUMENT ],
[ '--untranslated', GetoptLong::NO_ARGUMENT ],
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--fuzzy'
list_fuzzy_p = true
when '--untranslated'
list_untranslated_p = true
when '--verbose'
verbose_p = true
when '--dont-wrap'
$max_width=65535
end
end
loop {
s = gets
$stderr.puts "state=#{state.inspect}, s=#{s.inspect}" if verbose_p
break if s.nil?
s.chop!
if s !~ /^"(.*)\"$/ && state == STATE_MSGSTR_PLURAL && $msgstr[$plural_index].length == 0
$stderr.puts "Warning: blank plural string msgstr[#{$plural_index}] for #{$msgid.inspect}"
end
if s =~ /^# (?:FIXME|NOTE|XXX)/
if $notes.nil?
$notes = s
else
$notes += "\n" + s
end
elsif s =~ /^#,/
flush
$directives = s
$fuzzy_p = true if s =~ /fuzzy/
elsif s =~ /^msgid\s+\"(.*)\"\s*$/
flush
$msgid = $1
state = STATE_MSGID
elsif s =~ /^msgid_plural\s+\"(.*)\"\s*$/
flush
$msgid = $1
$plural_p = true
$plural_index = nil
state = STATE_MSGID_PLURAL
elsif s =~ /^msgstr\s+\"(.*)\"\s*$/
$msgstr = $1
state = STATE_MSGSTR
elsif s =~ /^msgstr\[(\d+)\]\s+\"(.*)\"\s*$/
raise "msgstr[] found for non-plural msgid" unless $plural_p
$plural_index = $1.to_i
$msgstr = [] if $msgstr.nil? && $plural_p
$msgstr[$plural_index] = $2
state = STATE_MSGSTR_PLURAL
elsif s =~ /^msgid/
$stderr.puts "Warning: unparsable msgid line (#{s})"
elsif s =~ /^msgstr/
$stderr.puts "Warning: unparsable msgstr line (#{s})"
elsif s =~ /^msgstr/
$stderr.puts "Warning: unparsable msgstr line (#{s})"
elsif s =~ /^"(.*)\"$/
case state
when STATE_MSGID, STATE_MSGID_PLURAL
$msgid = "" if $msgid.nil?
$msgid += $1
when STATE_MSGSTR
$msgstr = "" if $msgstr.nil?
$msgstr += $1
when STATE_MSGSTR_PLURAL
$msgstr[$plural_index] = "" if $msgstr[$plural_index].nil?
$msgstr[$plural_index] += $1
else
raise "Invalid continuation"
end
elsif s =~ /^\s*#/ || s =~ /^\s*$/
flush
state = STATE_OTHER
else
$stderr.puts "Warning: unknown line (#{s})"
flush
state = STATE_OTHER
end
}
flush
list_all_p = (list_fuzzy_p.nil? && list_untranslated_p.nil? )
something_done_p = nil
n = 0
$strings.sort.each { |a|
msgid, msgstr = a
if list_all_p
puts $string_notes[msgid] unless $string_notes[msgid].nil?
end
if msgstr.kind_of?( String )
if list_all_p || (list_fuzzy_p && $string_fuzzy_p[msgid]) \
|| (list_untranslated_p && $strings[msgid].length == 0)
puts "" if something_done_p
puts $string_directives[msgid] unless $string_directives[msgid].nil?
puts "msgid #{ present(msgid) }"
puts "msgstr #{ present(msgstr) }"
something_done_p = true
n += 1
end
else
first_p = true
i = 0
msgstr.each {|s|
if list_all_p || (list_fuzzy_p && $string_fuzzy_p[msgid]) \
|| (list_untranslated_p && s.length == 0)
puts "" if something_done_p
if first_p
first_p = false
puts $string_directives[msgid] unless $string_directives[msgid].nil?
puts "msgid_plural #{ present(msgid) }"
end
puts "msgstr[#{ i }] #{ present(s) }"
something_done_p = true
n += 1
end
i += 1
}
end
}
if n > 0
puts "\n# " + n.to_s + (list_fuzzy_p ? " fuzzy": (list_untranslated_p ? " untranslated": "")) + (n == 1? " string": " strings") + " found."
end