-
Notifications
You must be signed in to change notification settings - Fork 1
/
tajweed.js
289 lines (267 loc) · 11.2 KB
/
tajweed.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
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
278
279
280
281
282
283
284
285
286
287
288
289
class Tajweed{
constructor(){
this.meta;
this.createMetaData()
}
createMetaData(){
this.meta = [
{
'identifier': '[h',
'type': 'hamza-wasl',
'description': 'Hamzat ul Wasl',
'default_css_class': 'ham_wasl',
'html_color': '#AAAAAA'
},
{
'identifier': '[s',
'type': 'silent',
'description': 'Silent',
'default_css_class': 'slnt',
'html_color': '#AAAAAA'
},
{
'identifier': '[l',
'type': 'laam-shamsiyah',
'description': 'Lam Shamsiyyah',
'default_css_class': 'slnt',
'html_color': '#AAAAAA'
},
{
'identifier': '[n',
'type': 'madda-normal',
'description': 'Normal Prolongation: 2 Vowels',
'default_css_class': 'madda_normal',
'html_color': '#537FFF'
},
{
'identifier': '[p',
'type': 'madda-permissible',
'description': 'Permissible Prolongation: 2, 4, 6 Vowels',
'default_css_class': 'madda_permissible',
'html_color': '#4050FF'
},
{
'identifier': '[m',
'type': 'madda-necesssary',
'description': 'Necessary Prolongation: 6 Vowels',
'default_css_class': 'madda_necessary',
'html_color': '#000EBC'
},
{
'identifier': '[q',
'type': 'qalaqah',
'description': 'Qalaqah',
'default_css_class': 'qlq',
'html_color': '#DD0008'
},
{
'identifier': '[o',
'type': 'madda-obligatory',
'description': 'Obligatory Prolongation: 4-5 Vowels',
'default_css_class': 'madda_pbligatory',
'html_color': '#2144C1'
},
{
'identifier': '[c',
'type': 'ikhafa-shafawi',
'description': 'Ikhafa\' Shafawi - With Meem',
'default_css_class': 'ikhf_shfw',
'html_color': '#D500B7'
},
{
'identifier': '[f',
'type': 'ikhafa',
'description': 'Ikhafa\'',
'default_css_class': 'ikhf',
'html_color': '#9400A8'
},
{
'identifier': '[w',
'type': 'idgham-shafawi',
'description': 'Idgham Shafawi - With Meem',
'default_css_class': 'idghm_shfw',
'html_color': '#58B800'
},
{
'identifier': '[i',
'type': 'iqlab',
'description': 'Iqlab',
'default_css_class': 'iqlb',
'html_color': '#26BFFD'
},
{
'identifier': '[a',
'type': 'idgham-without-ghunnah',
'description': 'Idgham - With Ghunnah',
'default_css_class': 'idgh_ghn',
'html_color': '#169200'
},
{
'identifier': '[u',
'type': 'idgham-without-ghunnah',
'description': 'Idgham - Without Ghunnah',
'default_css_class': 'idgh_w_ghn',
'html_color': '#169200'
},
{
'identifier': '[d',
'type': 'idgham-mutajanisayn',
'description': 'Idgham - Mutajanisayn',
'default_css_class': 'idgh_mus',
'html_color': '#A1A1A1'
},
{
'identifier': '[b',
'type': 'idgham-mutaqaribayn',
'description': 'Idgham - Mutaqaribayn',
'default_css_class': 'idgh_mus',
'html_color': '#A1A1A1'
},
{
'identifier': '[g',
'type': 'ghunnah',
'description': 'Ghunnah: 2 Vowels',
'default_css_class': 'ghn',
'html_color': '#FF7E1E'
}];
}
/**
* Parses tajweed from the GlobalQuran and AlQuran APIs to return markup
* @param string $text Verse text
* @param boolean $fixWebkit Tries to fix for Chrome and Safari. This is experimental and has known problems.
* @return string Parsed text that can be used to display tajweed
*/
parse(text, fixWebkit = false)
{
if (fixWebkit) {
return this.webkitFix(this.closeParsing(this.parseTajweed(text)));
}
return this.closeParsing(this.parseTajweed(text));
}
/**
* [parseTajweed description]
* @param string $text Verse text
* @return String
*/
parseTajweed(text)
{
this.meta.forEach((meta) => {
let _re = new RegExp("(\\"+meta.identifier+")", "ig");
text = text.replace(_re, `<tajweed class="${meta.default_css_class}" data-type="${meta.type}" data-description="${meta.description}" data-tajweed="`)
})
return text;
}
/**
* This method tries to add in a fix for webkit browsers that break with <tags> inside a string.
* It does so by using the ‍ joiner, but that does not always work. It's not smart enough to know,
* for instance, when properly connect a meem to a yaa, among other things. Your best bet is to not use
* this but use Firefox or the like.
* See https://stackoverflow.com/questions/11155849/partially-colored-arabic-word-in-html
* and https://bugs.webkit.org/show_bug.cgi?id=6148.
* @param string $text Parsed tajweed verse with <tajweed> tags
* @return string
*/
webkitFix(text)
{
// Identify Tajweed tags, if there is not a space before or after, add ‍
// After
text = text.replace('/<\/tajweed>(\S)/', '‍${0}')
// Before
text = text.replace('/(\S)<tajweed class="(.*?)" data-type="(.*?)" data-description="(.*?)" data-tajweed="(.*?)">(\S)/', '${1}<tajweed class="${2}" data-type="${3}" data-description="${4}" data-tajweed="${5}">‍‍${6}')
// Let's remove all joiners where not needed for an Alif and a Waw
text = text.replace(['ٱ‍'], ['ٱ']);
return text;
}
/**
* [closeParsing description]
* @param string text
* @return string
*/
closeParsing(text)
{
let re_1 = new RegExp("(\\[)", "ig");
text = text.replace(re_1, '">');
let re_2 = new RegExp("(\\])", "ig");
text = text.replace(re_2, '</tajweed>');
return text;
}
/**
* Returns tajweed meta settings
* @return array The Tajweed metadata array
*/
getMeta()
{
return this.meta;
}
}
class Buck {
constructor() {
this.chars = "آ ا ب ت ث ج ح خ د ذ ر ز س ش ص ض ط ظ ع غ ف ق ك ل م ن ه و ي"
this.buck = "A A b t v j H x d * r z s $ S D T Z E g f q k l m n h w y"
this.trans= "ā ā b t th j h kh d dh r z s sh ṣ ḍ ṭ ẓ ʿ gh f q k l m n h w y"
this.charsArr = this.chars.split(' ');
this.buckArr = this.buck.split(' ');
this.transArr = this.trans.split(' ');
// MISSING CHARACTERS: أ إ ئ ء ة ؤ
// Let's add them!
this.charsArr.push("ى"); this.buckArr.push("Y"); this.transArr.push("ā");
this.charsArr.push("أ"); this.buckArr.push(">"); this.transArr.push("");
this.charsArr.push("إ"); this.buckArr.push("<"); this.transArr.push("");
this.charsArr.push("ئ"); this.buckArr.push("}"); this.transArr.push("");
this.charsArr.push("ء"); this.buckArr.push("X"); this.transArr.push("'");
this.charsArr.push("ؤ"); this.buckArr.push("&"); this.transArr.push(" ");
//missing characters for harakath.
this.charsArr.push("\u{0652}"); this.buckArr.push("o"); this.transArr.push("'");
this.charsArr.push("\u{064e}"); this.buckArr.push("a"); this.transArr.push("a");
this.charsArr.push("\u{0650}"); this.buckArr.push("i"); this.transArr.push("i");
this.charsArr.push("\u{064f}"); this.buckArr.push("u"); this.transArr.push("");
this.charsArr.push("\u{064b}"); this.buckArr.push("F"); this.transArr.push("an");
this.charsArr.push("\u{064d}"); this.buckArr.push("K"); this.transArr.push("in");
this.charsArr.push("\u{064c}"); this.buckArr.push("N"); this.transArr.push("un");
this.charsArr.push("\u{0626}"); this.buckArr.push("}"); this.transArr.push("");
this.charsArr.push("\u{0640}"); this.buckArr.push("_"); this.transArr.push("");
this.charsArr.push("\u{0651}"); this.buckArr.push("~"); this.transArr.push("(double)");
this.charsArr.push("\u{0653}"); this.buckArr.push("^"); this.transArr.push("");
this.charsArr.push("\u{0654}"); this.buckArr.push("#"); this.transArr.push("");
this.charsArr.push("\u{0671}"); this.buckArr.push("{"); this.transArr.push("");
this.charsArr.push("\u{0670}"); this.buckArr.push("`"); this.transArr.push("");
this.charsArr.push("\u{06e5}"); this.buckArr.push(","); this.transArr.push("");
this.charsArr.push("\u{06e6}"); this.buckArr.push("."); this.transArr.push("");
this.charsArr.push("ة"); this.buckArr.push("p"); this.transArr.push("t");
//this.charsArr.push("ة"); this.buckArr.push("P"); this.transArr.push(" ");
this.charsArr.push("\u{06df}"); this.buckArr.push("@"); this.transArr.push("");
this.charsArr.push("\u{06e2}"); this.buckArr.push("["); this.transArr.push("");
this.charsArr.push("\u{06ed}"); this.buckArr.push("]"); this.transArr.push("");
this.charsArr.push("\u{0621}"); this.buckArr.push("\""); this.transArr.push("");
this.charsArr.push("\u{06DC}"); this.buckArr.push(":"); this.transArr.push("");
this.charsArr.push("\u{06E0}"); this.buckArr.push("\""); this.transArr.push("");
this.charsArr.push(" "); this.buckArr.push(" "); this.transArr.push(" ");
this.charsArr.push(";"); this.buckArr.push(";"); this.transArr.push("");
this.charsArr.push("\n"); this.buckArr.push("\n"); this.transArr.push("");
}
toArabic(text){
let arabic = '';
let letters = text.split();
// console.log(letters)
letters.forEach((letter) => {
let buckArrKey = this.buckArr.indexOf(letter);
// console.log(buckArrKey)
if (buckArrKey != -1) {
// We've found a match
arabic += this.charsArr[buckArrKey];
// console.log(arabic)
} else {
// No match found, append the letter as it is
if (letter != NaN) {
// console.log(letter)
arabic += letter;
}
}
})
return arabic;
}
}
module.exports = {
Tajweed: Tajweed,
Buck: Buck
};