]> pere.pagekite.me Git - text-madewithcc.git/blob - fixup-migrate-gettext.rb
Correct parsing of previous string to handle colons.
[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 #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 output()
72 # FIXME rewrite to output proper entries
73 if '' != @type
74 print "#. type: %s\n" % [@type]
75 end
76 if fuzzy?
77 print "#, fuzzy\n"
78 if '' != @prev
79 print "#| msgid \"%s\"\n" % [@prev]
80 end
81 end
82 print "msgid \"%s\"\n" % [@src]
83 print "msgstr \"%s\"\n" % [@dest]
84 print "\n\n"
85 end
86 end
87
88 raise ArgumentError, 'Source file not specified' if ARGV.size != 1
89 file = ARGV[0]
90 strings = File.open(file,'r').read.split(/\n\n/)[1..-1].map {|str| PoStr.new(str)}
91
92 print strings
93
94 c = 0
95 strings.each do |entry|
96 if entry.fuzzy?
97 # Ignore whitespace changes between prev and src
98 if entry.prev.gsub(/ +/, ' ') == entry.src.gsub(/ +/, ' ')
99 entry.status = ''
100 print "clear fuzzy (space)\n"
101 c = c + 1
102 end
103
104 # Rewrite title strings, which lost '#' at the front
105 if entry.prev.gsub(/^#+ +/, '') == entry.src
106 entry.status = ''
107 entry.dest.gsub!(/^#+ +/, '')
108 print "cleared fuzzy (title)\n"
109 c = c + 1
110 end
111
112 # Rewrite footnotes to use "<placeholder type=\"footnote\" id=\"0\"/>"
113 if entry.src =~ /<placeholder type=\\"footnote\\" id=\\"0\\"\/>/
114 print "found footnote\n"
115 p = entry.prev
116 p.sub!(/([a-z]\.["”]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
117 p.sub!(/([a-z]\.["”]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
118 print p, "\n"
119 print entry.src, "\n"
120 if p == entry.src
121 entry.status = ''
122 entry.dest.sub!(/([a-z]\.["”»]?)(\d+)(\s)/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>\\3")
123 entry.dest.sub!(/([a-z]\.["”»]?)(\d+)$/, "\\1<placeholder type=\\\"footnote\\\" id=\\\"0\\\"\/>")
124 print "cleared fuzzy (footnote)\n"
125 c = c + 1
126 end
127 end
128 end
129 entry.output
130 end
131 print "Would clear %d fuzzy\n" % [c]