2013-08-29 21:33:26 +02:00
|
|
|
var alert_words = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.words = page_params.alert_words;
|
|
|
|
|
|
|
|
// escape_user_regex taken from jquery-ui/autocomplete.js,
|
|
|
|
// licensed under MIT license.
|
|
|
|
function escape_user_regex(value) {
|
|
|
|
return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
|
|
|
|
}
|
|
|
|
|
2014-03-04 22:55:36 +01:00
|
|
|
var find_href_backwards = /href=['"][\w:\/\.]+$/;
|
|
|
|
var find_title_backwards = /title=['"][\w:\/\.]+$/;
|
|
|
|
|
2013-08-29 21:33:26 +02:00
|
|
|
exports.process_message = function (message) {
|
2013-10-11 17:30:57 +02:00
|
|
|
if (!exports.notifies(message)) {
|
2013-08-29 21:33:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(exports.words, function (word) {
|
|
|
|
var clean = escape_user_regex(word);
|
2014-02-12 23:36:09 +01:00
|
|
|
var before_punctuation = '\\s|^|>|[\\(\\".,\';\\[]';
|
|
|
|
var after_punctuation = '\\s|$|<|[\\)\\"\\?!:.,\';\\]!]';
|
2013-10-09 20:09:48 +02:00
|
|
|
|
2014-03-04 22:55:36 +01:00
|
|
|
|
|
|
|
var word_in_href = new RegExp(find_href_backwards + word, 'i');
|
|
|
|
|
2013-10-09 20:09:48 +02:00
|
|
|
var regex = new RegExp('(' + before_punctuation + ')' +
|
|
|
|
'(' + clean + ')' +
|
|
|
|
'(' + after_punctuation + ')' , 'i');
|
2014-03-04 22:55:36 +01:00
|
|
|
message.content = message.content.replace(regex, function (match, before, word, after, offset, content) {
|
|
|
|
// Don't munge URL hrefs
|
|
|
|
var pre_match = content.substring(0, offset);
|
|
|
|
if (find_href_backwards.exec(pre_match) || find_title_backwards.exec(pre_match)) {
|
|
|
|
return before + word + after;
|
|
|
|
} else {
|
|
|
|
return before + "<span class='alert-word'>" + word + "</span>" + after;
|
|
|
|
}
|
2013-10-09 20:09:48 +02:00
|
|
|
});
|
2013-08-29 21:33:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.notifies = function (message) {
|
2014-01-08 00:04:52 +01:00
|
|
|
return ((message.sender_email !== page_params.email) && message.alerted);
|
2013-08-29 21:33:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = alert_words;
|
|
|
|
}
|