2014-10-10 23:11:57 +00:00
|
|
|
Handlebars.registerHelper(
|
|
|
|
"parse", function(text) {
|
2016-02-29 16:48:57 +00:00
|
|
|
text = Handlebars.Utils.escapeExpression(text);
|
2014-10-10 23:11:57 +00:00
|
|
|
text = colors(text);
|
2016-02-29 16:48:57 +00:00
|
|
|
text = channels(text);
|
2014-10-10 23:11:57 +00:00
|
|
|
text = uri(text);
|
2016-03-22 16:04:34 +00:00
|
|
|
return text;
|
2014-10-10 23:11:57 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-09-27 22:01:28 +00:00
|
|
|
function uri(text) {
|
2016-05-12 13:07:15 +00:00
|
|
|
return window.URI.withinString(text, function(url) {
|
2014-10-11 22:57:34 +00:00
|
|
|
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("<");
|
2016-06-16 14:40:02 +00:00
|
|
|
url = "<a href='" + split[0].replace(/^www/, "http://www") + "' target='_blank' rel='noopener'>" + 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
|
|
|
}
|
|
|
|
|
2016-02-29 16:48:57 +00:00
|
|
|
/**
|
|
|
|
* Channels names are strings of length up to fifty (50) characters.
|
|
|
|
* The only restriction on a channel name is that it SHALL NOT contain
|
|
|
|
* any spaces (' '), a control G (^G or ASCII 7), a comma (',').
|
|
|
|
* Channel prefix '&' is handled as '&' because this parser is executed
|
|
|
|
* after entities in the message have been escaped. This prevents a couple of bugs.
|
|
|
|
*/
|
|
|
|
function channels(text) {
|
|
|
|
return text.replace(
|
|
|
|
/(^|\s|\x07|,)((?:#|&)[^\x07\s\,]{1,49})/g,
|
|
|
|
'$1<span class="inline-channel" role="button" tabindex="0" data-chan="$2">$2</span>'
|
|
|
|
);
|
|
|
|
}
|
2014-12-10 11:30:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MIRC compliant colour and style parser
|
|
|
|
* Unfortuanately this is a non trivial operation
|
|
|
|
* See this branch for source and tests
|
|
|
|
* https://github.com/megawac/irc-style-parser/tree/shout
|
|
|
|
*/
|
|
|
|
var styleCheck_Re = /[\x00-\x1F]/,
|
2016-05-12 13:07:15 +00:00
|
|
|
back_re = /^([0-9]{1,2})(,([0-9]{1,2}))?/,
|
|
|
|
colourKey = "\x03",
|
|
|
|
// breaks all open styles ^O (\x0F)
|
|
|
|
styleBreak = "\x0F";
|
2014-12-10 11:30:45 +00:00
|
|
|
|
|
|
|
|
2014-12-11 03:35:17 +00:00
|
|
|
function styleTemplate(settings) {
|
2016-05-12 13:07:15 +00:00
|
|
|
return "<span class='" + settings.style + "'>" + settings.text + "</span>";
|
2014-12-11 03:35:17 +00:00
|
|
|
}
|
2014-12-10 11:30:45 +00:00
|
|
|
|
|
|
|
var styles = [
|
2016-05-12 13:07:15 +00:00
|
|
|
["normal", "\x00", ""], ["underline", "\x1F"],
|
|
|
|
["bold", "\x02"], ["italic", "\x1D"]
|
2014-12-10 11:30:45 +00:00
|
|
|
].map(function(style) {
|
2016-05-12 13:07:15 +00:00
|
|
|
var escaped = encodeURI(style[1]).replace("%", "\\x");
|
|
|
|
return {
|
|
|
|
name: style[0],
|
|
|
|
style: style[2] ? style[2] : "irc-" + style[0],
|
|
|
|
key: style[1],
|
|
|
|
keyregex: new RegExp(escaped + "(.*?)(" + escaped + "|$)")
|
|
|
|
};
|
2014-12-10 11:30:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function colors(line) {
|
2016-05-12 13:07:15 +00:00
|
|
|
// http://www.mirc.com/colors.html
|
|
|
|
// http://www.aviran.org/stripremove-irc-client-control-characters/
|
|
|
|
// https://github.com/perl6/mu/blob/master/examples/rules/Grammar-IRC.pm
|
|
|
|
// regexs are cruel to parse this thing
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2016-05-12 13:07:15 +00:00
|
|
|
// already done?
|
|
|
|
if (!styleCheck_Re.test(line)) {
|
|
|
|
return line;
|
|
|
|
}
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2016-05-12 13:07:15 +00:00
|
|
|
// split up by the irc style break character ^O
|
|
|
|
if (line.indexOf(styleBreak) >= 0) {
|
|
|
|
return line.split(styleBreak).map(colors).join("");
|
|
|
|
}
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2016-05-12 13:07:15 +00:00
|
|
|
var result = line;
|
|
|
|
var parseArr = result.split(colourKey);
|
|
|
|
var text, match, colour, background = "";
|
|
|
|
for (var i = 0; i < parseArr.length; i++) {
|
|
|
|
text = parseArr[i];
|
|
|
|
match = text.match(back_re);
|
|
|
|
if (!match) {
|
|
|
|
// ^C (no colour) ending. Escape current colour and carry on
|
|
|
|
background = "";
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-24 13:57:44 +00:00
|
|
|
colour = "irc-fg" + +match[1];
|
|
|
|
// set the background colour
|
|
|
|
if (match[3]) {
|
|
|
|
background = " irc-bg" + +match[3];
|
|
|
|
}
|
2016-05-12 13:07:15 +00:00
|
|
|
// update the parsed text result
|
|
|
|
result = result.replace(colourKey + text, styleTemplate({
|
|
|
|
style: colour + background,
|
|
|
|
text: text.slice(match[0].length)
|
|
|
|
}));
|
|
|
|
}
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2016-05-12 13:07:15 +00:00
|
|
|
// Matching styles (italics/bold/underline)
|
|
|
|
// if only colours were this easy...
|
|
|
|
styles.forEach(function(style) {
|
|
|
|
if (result.indexOf(style.key) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = result.replace(style.keyregex, function(match, text) {
|
|
|
|
return styleTemplate({
|
|
|
|
"style": style.style,
|
|
|
|
"text": text
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-12-10 11:30:45 +00:00
|
|
|
|
2016-05-12 13:07:15 +00:00
|
|
|
return result;
|
2014-09-27 22:01:28 +00:00
|
|
|
}
|