forked from MegaGM/nodebb-plugin-mega-colors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
51 lines (42 loc) · 1.47 KB
/
library.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
(function (module) {
'use strict';
var Plugin = {},
codeRegex = /(?:<pre>(?:.|\n)*?<\/pre>|<code>(?:.|\n)*?<\/code>)/gi,
colorRegex = /%\((#(?:[A-Fa-f0-9]{3}(?:[A-Fa-f0-9]{3})?)|(?:rgb\(\d{1,3},\d{1,3},\d{1,3}\))|(?:[a-zа-я]){3,})\)\[(.+?)\]/gi;
Plugin.parse = function (data, callback) {
if (data && 'string' === typeof data) {
data = parser(data);
} else if (data.postData && data.postData.content && data.postData.content.match(colorRegex)) {
data.postData.content = parser(data.postData.content);
} else if (data.userData && data.userData.signature && data.userData.signature.match(colorRegex)) {
data.userData.signature = parser(data.userData.signature);
}
callback(null, data);
};
function parser (data) {
var codeTags = [];
function chooseColor ($1) {
$1 = $1.toLowerCase();
if ('лидер' === $1) return '#cf0000';
if ('генерал' === $1) return '#f7632b';
if ('офицер' === $1) return '#cf6800';
if ('рекрутер' === $1) return '#008000';
if ('рыцарь' === $1) return '#0090ff';
if ('соратник' === $1) return '#3ba8a8';
return $1;
}
data = data.replace(codeRegex, function (match) {
codeTags.push(match);
return '___CODE___';
});
data = data.replace(colorRegex, function (match, $1, $2) {
$1 = chooseColor($1);
return '<span style="color: ' + $1 + ';">' + $2 + '</span>';
});
data = data.replace(/___CODE___/gi, function (match) {
return codeTags.shift();
});
return data;
}
module.exports = Plugin;
})(module);