🎊 full mirc colour+style compliance

This commit is contained in:
Graeme 2014-12-10 06:30:45 -05:00
parent 1f4b3a70ff
commit 635646b0f3
2 changed files with 103 additions and 41 deletions

View file

@ -915,6 +915,18 @@ button {
.bg-14 { background: #aaa; } .bg-14 { background: #aaa; }
.bg-15 { background: #ddd; } .bg-15 { background: #ddd; }
.irc-bold {
font-weight: bold;
}
.irc-underline {
text-decoration: underline;
}
.irc-italic {
font-style:italic
}
/** /**
* TrackpadScrollEmulator * TrackpadScrollEmulator
* Version: 1.0.6 * Version: 1.0.6

View file

@ -48,46 +48,96 @@ function uri(text) {
}); });
} }
var regex = {
color: /\003([0-9]{1,2})[,]?([0-9]{1,2})?([^\003]+)/, /**
terminator: /\x0D/, * MIRC compliant colour and style parser
styles: [ * Unfortuanately this is a non trivial operation
[/\002([^\002]+)(\002)?/, ["<b>", "</b>"]], * See this branch for source and tests
[/\037([^\037]+)(\037)?/, ["<u>", "</u>"]], * https://github.com/megawac/irc-style-parser/tree/shout
] */
var styleCheck_Re = /[\x00-\x1F]/,
back_re = /^(\d{1,2})(,(\d{1,2}))?/,
colourKey = "\x03", colour_re = /\x03/g,
// breaks all open styles ^O (\x0F)
styleBreak = "\x0F";
var styleTemplate = function(settings) {
return "<span class='" + settings.style + "'>" + settings.text + "</span>";
}; };
function colors(text) {
if (!text) { var styles = [
return text; ["normal", "\x00", ""], ["underline", "\x1F"],
} ["bold", "\x02"], ["italic", "\x1D"]
if (regex.terminator.test(text)) { ].map(function(style) {
return $.map(text.split(regex.terminator), colors); var escaped = encodeURI(style[1]).replace("%", "\\x");
} return {
if (regex.color.test(text)) { name: style[0],
var match, bg; style: style[2] != null ? style[2] : "irc-" + style[0],
while (match = regex.color.exec(text)) { key: style[1],
var color = "color-" + match[1]; keyregex: new RegExp(escaped + "(.*?)(" + escaped + "|$)")
if (match[2]) { };
bg = match[2]; });
}
if (bg) { //http://www.mirc.com/colors.html
color += " bg-" + bg; var colourMap = {};
} for (var colour = 0; colour < 16; colour++) {
var text = text.replace( colourMap[colour] = {
match[0], fore: "color-" + colour,
"<span class='" + color + "'>" + match[3] + "</span>" back: "bg-" + colour
); };
} }
}
for (var i in regex.styles) { function colors(line) {
var pattern = regex.styles[i][0]; // http://www.mirc.com/colors.html
var style = regex.styles[i][1]; // http://www.aviran.org/stripremove-irc-client-control-characters/
if (pattern.test(text)) { // https://github.com/perl6/mu/blob/master/examples/rules/Grammar-IRC.pm
var match; // regexs are cruel to parse this thing
while (match = pattern.exec(text)) {
text = text.replace(match[0], style[0] + match[1] + style[1]); // already done?
} if (!styleCheck_Re.test(line)) return line;
}
} // split up by the irc style break character ^O
return text; if (line.indexOf(styleBreak) >= 0) {
return line.split(styleBreak).map(colors).join("");
}
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);
colour = match && colourMap[+match[1]];
if (!match || !colour) {
// ^C (no colour) ending. Escape current colour and carry on
background = "";
continue;
}
// set the background colour
// we don't overide the background local var to support nesting
if (colourMap[+match[3]]) {
background = " " + colourMap[+match[3]].back;
}
// update the parsed text result
result = result.replace(colourKey + text, styleTemplate({
style: colour.fore + background,
text: text.slice(match[0].length)
}));
}
// 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
});
});
});
//replace the reminent colour terminations and be done with it
return result.replace(colour_re, "");
} }