forked from iamcal/php-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.php
277 lines (203 loc) · 5.22 KB
/
parse.php
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
#
# requires php 5.2+
#
#
# open and parse html
#
$html = file_get_contents($argv[1]);
$html = str_ireplace(array('<br>', '<br/>', '<br />'), "\n", $html);
$doc = new DOMDocument();
@$doc->loadHTML($html);
#
# get an array of <tr>'s we care about
#
$all_trs = get_elts_by_tag($doc, 'tr');
$trs = array();
foreach ($all_trs as $item){
if ($item->getAttribute('class') == 'not_in_proposal'){
continue;
}
if (!preg_match('/^e-\w{3}$/', $item->getAttribute('id'))){
continue;
}
if (!(7 === count(get_elts_by_tag($item, 'td')))){
continue;
}
$trs[] = $item;
}
fprintf(STDERR, "trs count:" . count($trs)."\n");
#
# iterate over the <tr>'s, extracting the data we need
#
$items = array();
foreach ($trs as $tr){
$tds = get_elts_by_tag($tr, 'td');
$item = array(
'mapid' => parse_mapid($tds[0]),
'unicode' => parse_unicode($tds[1]),
'char_name' => parse_char_name($tds[2]),
'docomo' => parse_mobile($tds[3]),
'au' => parse_mobile($tds[4]),
'softbank' => parse_mobile($tds[5]),
'google' => parse_google($tds[6]),
);
$items[] = $item;
}
fprintf(STDERR, "codepoint count:".count($items)."\n");
#
# filter invalid codepoints
#
fprintf(STDERR, "filter only_kaomoji ; like e-554 -> [A] -> [A] -> [A] -> [A]\n");
$items = filter_only_kaomoji($items);
fprintf(STDERR, "filter invisibles\n");
$items = filter_invisibles($items);
fprintf(STDERR, "codepoint count:".count($items)."\n");
#
# export the catalog
#
echo "<"."?php \$catalog = ";
var_export($items);
echo "; ?".">";
function get_elts_by_tag($root, $tagname){
$nodelist = $root->getElementsByTagName($tagname);
$result = array();
$len = $nodelist->length;
for($i=0; $i<$len; $i++) {
$result[] = $nodelist->item($i);
}
return $result;
}
function parse_mapid($elt) {
// like <a href="#e-19E">e-19E</a>
$links = get_elts_by_tag($elt, 'a');
if(empty($links)) return null;
return $links[0]->getAttribute("href");
}
function parse_unicode($elt) {
//like U+1F469
return get_unicode_chars($elt);
}
function parse_char_name($elt) {
$lines = array_filter(array_map('trim', explode("\n", $elt->textContent)));
if(count($lines[0])>0) {
$result['title'] = $lines[0];
unset($lines[0]);
$result['desc'] = join("\n", $lines);
return $result;
}
return null;
}
function parse_mobile($elt){
if (!has_image($elt)){
return array(
'kaomoji' => trim($elt->textContent)
);
}else{
return array(
'number' => get_number_chars($elt),
'number_old' => get_old_num_chars($elt),
'unicode' => get_unicode_chars($elt),
'sjis' => get_sjis_chars($elt),
'jis' => get_jis_chars($elt),
);
}
}
function parse_google($elt){
return array(
'unicode' => get_unicode_chars($elt),
);
}
function has_image($elt) {
return count(get_elts_by_tag($elt, 'img')) > 0;
}
function get_unicode_chars($elt){ return get_some_chars($elt, '!U\+(\w{4,5})!u', 16); }
function get_sjis_chars($elt){ return get_some_chars($elt, '/\bSJIS-(\w{4})/u', 16); }
function get_jis_chars($elt){ return get_some_chars($elt, '/\bJIS-(\w{4})/u', 16); }
function get_number_chars($elt){ return get_some_chars($elt, '/\#([0-9]{1,})/'); }
function get_old_num_chars($elt){ return get_some_chars($elt, '/\#old([0-9]{1,})/'); }
function get_some_chars($elt, $rx, $base=10){
$out = array();
if (preg_match_all($rx, $elt->textContent, $m)){
foreach ($m[1] as $n){
$out[] = intval($n, $base);
}
}
return $out;
}
function filter_only_kaomoji($mapping) {
$result = array();
foreach($mapping as $map) {
if(isset($map['docomo']['kaomoji'])
&& isset($map['au']['kaomoji'])
&& isset($map['softbank']['kaomoji']))
{
continue;
}
else {
$result[] = $map;
}
}
return $result;
}
function filter_chars_group($mapping) {
$result = array();
foreach($mapping as $map) {
if( @preg_match('/\+$/', $map['docomo']['number'])
|| @preg_match('/\+$/', $map['au']['number'])
|| @preg_match('/\+$/', $map['softbank']['number'])
){
continue;
}
else {
$result[] = $map;
}
}
return $result;
}
function fix_geta_mark($mapping) {
$result = array();
foreach($mapping as $map) {
if(isset($map['docomo']['kaomoji'])
&& $map['docomo']['kaomoji'] == '〓') {
$map['docomo']['kaomoji'] = '';
}
if(isset($map['au']['kaomoji'])
&& $map['au']['kaomoji'] == '〓') {
$map['au']['kaomoji'] = '';
}
if(isset($map['softbank']['kaomoji'])
&& $map['softbank']['kaomoji'] == '〓') {
$map['softbank']['kaomoji'] = '';
}
$result[] = $map;
}
return $result;
}
function get_all_kaomoji($mapping) {
$arr = array();
foreach($mapping as $map) {
if(isset($map['docomo']['kaomoji']) ) {
$arr[ $map['docomo']['kaomoji'] ] = '1';
}
if(isset($map['au']['kaomoji']) ) {
$arr[ $map['au']['kaomoji'] ] = '1';
}
if(isset($map['softbank']['kaomoji']) ) {
$arr[ $map['softbank']['kaomoji'] ] = '1';
}
}
return array_keys($arr);
}
function filter_invisibles($items){
$out = array();
foreach ($items as $item){
if (count($item['unicode']) == 1){
if ($item['unicode'][0] == 0x2003) continue; # em space
if ($item['unicode'][0] == 0x2002) continue; # en space
if ($item['unicode'][0] == 0x2005) continue; # four-per-em space
}
$out[] = $item;
}
return $out;
}