This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
73 lines (64 loc) · 2.07 KB
/
index.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
// Load all of our data
var subscripts = require('./data/subscripts');
var superscripts = require('./data/superscripts');
var symbols = require('./data/symbols');
var textbb = require('./data/textbb');
var textbf = require('./data/textbf');
var textcal = require('./data/textcal');
var textfrak = require('./data/textfrak');
var textit = require('./data/textit');
var textmono = require('./data/textmono');
// Attempts to find a replacement for a value in one
// of our objects. If none exists, it returns the
// original object
var attemptReplace = function(obj, key) {
return obj[key] !== undefined ? obj[key] : key;
};
// Loops through our string, converting all of the symbols
// that it finds. It also handles grouped symbols, as in:
// \frak{ABC}
function applyModifier(text, modifier, obj) {
text = text.replace(modifier, '^');
var newText = '', mode = 'normal', i, unit;
for (i = 0; i < text.length; i++) {
unit = text[i];
if (mode === 'normal' && unit === '^') {
mode = 'modified';
continue;
} else if (mode === 'modified' && unit === '{') {
mode = 'long';
continue;
} else if (mode === 'modified') {
newText += attemptReplace(obj, unit);
mode = 'normal';
continue;
} else if (mode === 'long' && unit === '}') {
mode = 'normal';
continue;
}
if (mode === 'normal') {
newText += unit;
} else {
newText += attemptReplace(obj, unit);
}
}
return newText;
}
// Replace str with unicode representations
module.exports = function(str) {
// Replace all of our symbols
Object.keys(symbols).forEach(function(key) {
var val = symbols[key];
str = str.replace(key, val);
});
// Apply all of the modifiers
str = applyModifier(str, '^', superscripts);
str = applyModifier(str, '_', subscripts);
str = applyModifier(str, '\\bb', textbb);
str = applyModifier(str, '\\bf', textbf);
str = applyModifier(str, '\\it', textit);
str = applyModifier(str, '\\cal', textcal);
str = applyModifier(str, '\\frak', textfrak);
str = applyModifier(str, '\\mono', textmono);
return str;
};