forked from Kottakji/minitranslate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.js
153 lines (131 loc) · 4.61 KB
/
word.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
// The word element class that replaces the normal text into a chinese word plus extra hover styling
let word = {
replaceWord: function (originalWord, array, type) {
let text = array[0][type]; // Simplified or Traditional
text = " " + text + " "; // Add the two blank lines again b/c our pattern removes the spaces
let hoverText = "";
for (let i = 0; i < array.length; i++) {
// Find out the color code
// All these spans are for creating neat color coding based on the tone
hoverText += "<span class='characterAndPinyin'>"; // This span makes sure that the character + pinyin stay on the same line
let words = array[i]["pinyin"].split(" ");
for (let c = 0; c < array[i][type].length; c++) {
hoverText += "<span class='characterColor" + words[c].match(/\d/)[0] + "'>" + array[i][type][c] + "</span>";
}
hoverText += " <span class='pinyin'>" + pinyinToUnicodePinyin(array[i]["pinyin"]) + "</span>";
hoverText += "</span>";
hoverText += "<span class='english'>\n" + measureWordToPinyin(array[i]["english"] + "</span>") + "\n";
}
return originalWord + "<span class='mt-character'>(" + text + ") <span class='hovertext'>" + hoverText + "</span></span> ";
}
};
// In the English text, sometimes they add the measure word (CL)
function measureWordToPinyin (english) {
return english.replace(/(\[.+?\])/gi, function(match, $1) {
return "[" + pinyinToUnicodePinyin($1) + "]";
});
}
// Translates pin1yin1 to Pīnyīn
function pinyinToUnicodePinyin (pinyin) {
// Pinyin looks like [pin1 pin1]
pinyin = pinyin.slice(1, -1);
return pinyin.replace(/(\w{0,3})([aieuo]|u:|r)(\w{0,3})(\d)/gi, function (match, $1, $2, $3, $4) {
// Source from http://pinyin.info/unicode/unicode_test.html
let character = $2;
switch ($2 + $4) {
case "a1":
character = "ā";
break;
case "a2":
character = "á";
break;
case "a3":
character = "ǎ";
break;
case "a4":
character = "à";
break;
case "a": // No tone
break;
case "e1":
character = "ē";
break;
case "e2":
character = "é";
break;
case "e3":
character = "ě";
break;
case "e4":
character = "è";
break;
case "e": // No tone
break;
case "i1":
character = "ī";
break;
case "i2":
character = "í";
break;
case "i3":
character = "ǐ";
break;
case "i4":
character = "ì";
break;
case "i": // No tone
break;
case "u1":
character = "ū";
break;
case "u2":
character = "ú";
break;
case "u3":
character = "ǔ";
break;
case "u4":
character = "ù";
break;
case "u": // No tone
break;
case "o1":
character = "ō";
break;
case "o2":
character = "ó";
break;
case "o3":
character = "ǒ";
break;
case "o4":
character = "ò";
break;
case "o": // No tone
break;
case "u:1": // Special u in pinyin
character = "ǖ";
break;
case "u:2":
character = "ǘ";
break;
case "u:3":
character = "ǚ";
break;
case "u:4":
character = "ǜ";
break;
case "u:": // No tone
break;
case "r5":
character = "r"; // a no tone r
break;
}
// For example, pu3 doesn't have capture group 3
let result = $1 + character;
if (typeof $3 !== "undefined") {
result += $3;
}
return result;
});
}