-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorize_hanzi.js
176 lines (168 loc) · 5.03 KB
/
colorize_hanzi.js
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
// The following code changes the text color
// of hanzi according to pinyin tones.
// You can change the colors in the CSS
function colorHanzi(html, hanzi, pinyin) {
function getTones(pinyin) {
function getToneNumber(diac) {
var allDiacs = ['āēīōūǖ','áéíóúǘ','ǎěǐǒǔǚ','àèìòùǜ','aeiouür'];
for (var i = 0; i < allDiacs.length; i++) {
if (allDiacs[i].includes(diac)) {
return i+1;
}
}
return 0;
}
var a = '([aāáǎà])';
var e = '([eēéěè])';
var ae = '([aāáǎàeēéěè])';
var i = '([iīíǐì])';
var o = '([oōóǒò])';
var u = '([uūúǔùüǖǘǚǜ])';
var eu = '([eēéěèuūúǔù])';
var regex=
'(?:'
+'(?:'
+'mi'+u
+'|[pm]'+o+'u'
+'|[bpm](?:'
+o
+'|'+e+'(?:i|ng??)?'
+'|'+a+'(?:ng?|i|o)?'
+'|i(?:'+e+'|'+a+'[no])'
+'|'+i+'(?:ng?)?'
+'|'+u
+')'
+')'
+'|(?:f(?:'+o+'u?|'+ae+'(?:ng?|i)?|'+u+'))'
+'|(?:'
+'d(?:'+e+'(?:i|ng?)|i(?:'+a+'[on]?|'+u+'))'
+'|[dt](?:'
+a+'(?:i|ng?|o)?'
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(?:i|ng??)?|'+o+'|'+e+'(?:i|ng?)?|'+u+'))'
+'|y(?:'+a+'(?:o|ng??)?|'+e+'|'+i+'(?:ng?)?|'+o+'(?:u|ng)?|u(?:'+e+'|'+a+'n)|'+u+'n??)'
+')'
+'([12345])?';
pinyin = pinyin.normalize();
pinyin = pinyin.split('/')[0];
pinyin = pinyin.trim();
const re = new RegExp(regex, 'g');
const matches = pinyin.matchAll(re);
var tones = Array.from(matches).map(function (match) {
var m = match;
m.shift();
var diac = m.filter(function(v) { return v !== undefined; });
if (diac.length > 0) {
if (diac.length > 1 && '12345'.includes(diac[1])) {
return parseInt(diac[1]); // tone as pinyin number
} else {
return getToneNumber(diac[0]); // tone as pinyin diacritic
}
} else {
return 0;
}
});
return tones;
}
pinyin = pinyin.toLowerCase();
var tones = getTones(pinyin);
hanzi = hanzi.normalize();
// \p{Han} according to ftp://ftp.unicode.org/Public/UNIDATA/Scripts.txt (only codepoints lower than U+FFFF)
const Han = '\u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u3005\u3007\u3021-\u3029\u3038-\u303A\u303B\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFA6D\uFA70-\uFAD9';
const hanziRe = '[/0-9'+Han+']';
const re = new RegExp(hanziRe, 'gu');
var memory = {};
var n = 0;
return html.replace(re, function (match) {
if (match == '/') {
n = 0;
return match;
}
var tone = tones[n++];
if (tone === undefined && match in memory) {
tone = memory[match];
}
if (tone !== undefined) {
memory[match] = tone;
var tag = '<span class="tone tone-' + tone + '">' + match + '</span>';
return tag;
} else {
return match;
}
});
}
function colorFlashCard() {
var pinyin = document.getElementById("ddzw-pinyin");
if (!pinyin)
return;
var hanziElem = document.getElementById("ddzw-hanzi");
if (!hanziElem)
return;
hanziElem.innerHTML = colorHanzi(hanziElem.innerHTML, hanziElem.textContent, pinyin.innerText);
}
try {
module.exports = {
colorHanzi: colorHanzi
}
} catch (e) {
}
if (typeof require === 'undefined' || typeof require.main === 'undefined') {
colorFlashCard();
}