2014-10-10 23:11:57 +00:00
|
|
|
Handlebars.registerHelper(
|
|
|
|
"parse", function(text) {
|
|
|
|
var wrap = wraplong(text);
|
|
|
|
text = escape(text);
|
|
|
|
text = colors(text);
|
|
|
|
text = uri(text);
|
|
|
|
if (wrap) {
|
|
|
|
return "<i class='wrap'>" + text + "</i>";
|
|
|
|
} else {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
function wraplong(text) {
|
|
|
|
var wrap = false;
|
|
|
|
var split = text.split(" ");
|
|
|
|
for (var i in split) {
|
|
|
|
if (split[i].length > 40) {
|
|
|
|
wrap = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrap;
|
|
|
|
}
|
|
|
|
|
2014-09-27 22:01:28 +00:00
|
|
|
function escape(text) {
|
|
|
|
var e = {
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
"'": "'"
|
|
|
|
};
|
|
|
|
return text.replace(/[<>']/g, function (c) {
|
|
|
|
return e[c];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function uri(text) {
|
2014-10-11 22:57:34 +00:00
|
|
|
return URI.withinString(text, function(url, start, end, source) {
|
|
|
|
if (url.indexOf("javascript:") === 0) {
|
2014-10-10 23:11:57 +00:00
|
|
|
return url;
|
2014-09-27 22:01:28 +00:00
|
|
|
}
|
2014-10-11 22:57:34 +00:00
|
|
|
var split = url.split("<");
|
|
|
|
url = "<a href='" + split[0].replace(/^www/, "//www") + "' target='_blank'>" + split[0] + "</a>";
|
2014-10-20 11:28:29 +00:00
|
|
|
if (split.length > 1) {
|
|
|
|
url += "<" + split.slice(1).join("<");
|
2014-10-11 22:57:34 +00:00
|
|
|
}
|
|
|
|
return url;
|
2014-10-10 23:11:57 +00:00
|
|
|
});
|
2014-09-27 22:01:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 17:37:22 +00:00
|
|
|
var regex = {
|
|
|
|
color: /\003([0-9]{1,2})[,]?([0-9]{1,2})?([^\003]+)/,
|
2014-10-25 19:07:15 +00:00
|
|
|
terminator: /\x0D/,
|
2014-10-11 17:37:22 +00:00
|
|
|
styles: [
|
2014-10-25 19:07:15 +00:00
|
|
|
[/\002([^\002]+)(\002)?/, ["<b>", "</b>"]],
|
|
|
|
[/\037([^\037]+)(\037)?/, ["<u>", "</u>"]],
|
|
|
|
]
|
2014-10-11 17:37:22 +00:00
|
|
|
};
|
2014-10-10 23:11:57 +00:00
|
|
|
function colors(text) {
|
|
|
|
if (!text) {
|
2014-09-27 22:01:28 +00:00
|
|
|
return text;
|
|
|
|
}
|
2014-10-25 19:07:15 +00:00
|
|
|
if (regex.terminator.test(text)) {
|
|
|
|
return $.map(text.split(regex.terminator), colors);
|
|
|
|
}
|
|
|
|
if (regex.color.test(text)) {
|
2014-10-27 21:51:18 +00:00
|
|
|
var match, bg;
|
2014-10-25 19:07:15 +00:00
|
|
|
while (match = regex.color.exec(text)) {
|
|
|
|
var color = "color-" + match[1];
|
2014-10-27 21:51:18 +00:00
|
|
|
if (match[2]) {
|
|
|
|
bg = match[2];
|
|
|
|
}
|
2014-10-25 19:07:15 +00:00
|
|
|
if (bg) {
|
|
|
|
color += " bg-" + bg;
|
|
|
|
}
|
|
|
|
var text = text.replace(
|
|
|
|
match[0],
|
|
|
|
"<span class='" + color + "'>" + match[3] + "</span>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var i in regex.styles) {
|
|
|
|
var pattern = regex.styles[i][0];
|
|
|
|
var style = regex.styles[i][1];
|
|
|
|
if (pattern.test(text)) {
|
|
|
|
var match;
|
|
|
|
while (match = pattern.exec(text)) {
|
|
|
|
text = text.replace(match[0], style[0] + match[1] + style[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
2014-09-27 22:01:28 +00:00
|
|
|
}
|