forked from unicode-org/icu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractCollationData.pl
executable file
·251 lines (233 loc) · 5.43 KB
/
extractCollationData.pl
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/perl
#
# Copyright (C) 2017 and later: Unicode, Inc. and others.
# License & terms of use: http://www.unicode.org/copyright.html
#
use strict;
use Unicode::UCD 'charinfo';
use Unicode::Normalize;
use utf8;
use open ':utf8';
my $printout = 0;
my $braces = 0;
my $colls = 0;
my $aliased = 0;
my $newName = "";
my $filename;
my $suffix;
my $locale;
NEW_FILE:
foreach my $arg (@ARGV) {
if($newName =~ /^$/) {
$locale = $arg;
$locale =~ s#^.*/##g;
$locale =~ s/\.txt//;
} else {
$newName = "";
}
my $command = "/home/weiv/build/current/bin/uconv -x hex-any/Java -f utf8 -t utf8 $arg";
print $command."\n";
my @bundle = `$command`;
foreach $_ (@bundle) {
#while(<>) {
#print $ARGV if eof;
if(/^\/\//) {
next;
}
if(/collations/) {
print "found Collations\n";
$colls = 1;
if(/alias/) {
print "collations are aliased\n";
$aliased = 1;
}
}
if($aliased) {
print "processing aliased data: $_\n";
if(/\{/) {
print "Braces opened\n";
$braces = 1;
}
if($braces && /\"(.*)\"/) {
$newName = $1;
print "Aliasing to $newName\n";
}
if($braces && /\}/) {
$braces = 0;
print "Braces closed\n";
$aliased = 0;
print "Switching from $filename to $newName\n";
$arg =~ s/$locale\.txt$/$newName\.txt/;
print "$arg\n";
redo NEW_FILE;
}
}
if(/standard|phonebook|traditional|pinyin|stroke|direct/ && $colls) {
print "found $& collation\n";
$suffix = "_".uc($&);
if(/standard/) {
$suffix = "";
}
}
if(/Sequence/ && $colls) {
#binmode ARGV, ":utf8";
$printout = 1;
#$filename = $ARGV;
$filename = $locale;
if($suffix) {
$filename .= "_".$suffix;
}
$filename .= "_collation.html";
print "filename is $filename\n";
#open(OUT, ">:utf8", "$filename");
open(OUT, ">$filename");
printHeading($arg);
#next;
}
my $line = $_;
if($line =~ /\{/ && $printout) {
$braces++;
}
if($printout) {
print OUT processLine($line);
print OUT "\n";
}
if( $line =~ /\}/ && $printout) {
$braces--;
if($braces == 0) {
$printout = 0;
printFooting();
close(OUT);
}
}
}
}
sub processLine {
my $line = shift;
$_ = $line;
my $i = 0;
my $j = 0;
my $result;
# remove comments
s#//.*$##g;
# remove "Sequence" if present
s/Sequence\s*//;
# remove leading brace if present
s/^\s*{//;
# remove trailing brace if present
s/}\s*$//;
# remove trailing quote
s/"\s*$//;
#remove lead quote
s/^\s*"//;
#separate options
s/(\[.*\])/\n\1/g;
#separate resets
s/\s*\&\s*/\n\& /g;
#separate strengths and insert spaces
s/\s*(<{1,4})\s*/\n\1 /g;
#separate equals and insert spaces
s/\s*=\s*/\n= /g;
# break into individual reset/strength/setting lines
my @lines = split(/\n/);
my $line;
my $name;
my $spanEnd = "";
my $result = "";
my $names = "";
my $codes = "";
my $lrm = "";
foreach $line (@lines) {
# skip empty lines
if($line =~ /^$/) {
next;
}
$spanEnd = "";
$name = "";
$lrm = "";
$line = NFC($line);
# for resets and strengths we will get name for elements
if($line =~ /<{1,4} |= |& \[.*\]|& /) {
$name = "<span title=\"";
$names = "";
$codes = "";
my $start = $&;
my $rest = $';
for ($j = 0; $j < length($rest); $j++) {
my $char = substr($rest, $j, 1);
my $charVal = ord($char);
# some of elements are part of the syntax, so they are
# entered without translation to the name
if($charVal == 0x002F || $charVal == 0x007C) {
$name .= $codes.$names." $char ";
$codes = "";
$names = "";
} elsif($charVal == 0x0027) { #quote requires more processing
#$name .= "'";
} else {
my $charinfo = charinfo($charVal);
$codes .= $charinfo->{'code'}." ";
$names .= "{".$charinfo->{'name'}."} ";
if($charinfo->{'bidi'} eq "R" || $charinfo->{'bidi'} eq "AL") {
$lrm = "‎";
}
#$name .= $charinfo->{'code'}." {".$charinfo->{'name'}."} ";
}
}
$name .= $codes.$names."\" >";
$spanEnd = "</span>";
}
#print $name."\n";
if($line =~ /^<<<</) {
$line = " $line";
} elsif($line =~ /^<<</) {
$line = " $line";
} elsif($line =~ /^<</) {
$line = " $line";
} elsif($line =~ /^</) {
$line = " $line";
} elsif($line =~ /^=/) {
$line = " $line";
}
# insert spaces around vertical bars (fix prefixes)
# insert spaces around slashes (fix expansions)
$line =~ s#/# / #g;
# replace &
$line =~ s/\&/&/g;
# replace spaces
$line =~ s/ / /g;
# replace <
$line =~ s/</</g;
# replace >
$line =~ s/>/>/g;
#$lines[$i] = $name.$lrm.$line."</span><br>";
#$i++;
$result .= $name.$lrm.$line.$spanEnd."<br>\n";
}
#$_ = join("\n", @lines);
return $result;
}
sub printHeading {
my $filename = shift;
$filename =~ s/\.txt//;
print OUT <<"EndOfHeading";
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
# Collation data resource bundle generated for locale: $filename<br>
# For platform icu reference platform UCA<br><br>
$filename {<br>
CollationElements {<br>
Sequence {<br>
EndOfHeading
}
sub printFooting {
print OUT <<"EndOfFooting";
}<br>
}<br>
}<br>
</pre>
</html>
EndOfFooting
}