Merge pull request #263 from megawac/style-parser

This commit is contained in:
Mattias Erming 2014-10-27 22:51:18 +01:00
commit a0ed791926
1 changed files with 34 additions and 30 deletions

View File

@ -50,40 +50,44 @@ function uri(text) {
var regex = { var regex = {
color: /\003([0-9]{1,2})[,]?([0-9]{1,2})?([^\003]+)/, color: /\003([0-9]{1,2})[,]?([0-9]{1,2})?([^\003]+)/,
terminator: /\x0D/,
styles: [ styles: [
[/\002([^\002]+)(\002)?/, ["<b>", "</b>"]], [/\002([^\002]+)(\002)?/, ["<b>", "</b>"]],
[/\037([^\037]+)(\037)?/, ["<u>", "</u>"]], [/\037([^\037]+)(\037)?/, ["<u>", "</u>"]],
] ]
}; };
function colors(text) { function colors(text) {
if (!text) { if (!text) {
return text; return text;
} }
if (regex.color.test(text)) { if (regex.terminator.test(text)) {
var match, bg; return $.map(text.split(regex.terminator), colors);
while (match = regex.color.exec(text)) { }
var color = "color-" + match[1]; if (regex.color.test(text)) {
if (match[2]) { var match, bg;
bg = match[2]; while (match = regex.color.exec(text)) {
} var color = "color-" + match[1];
if (bg) { if (match[2]) {
color += " bg-" + bg; bg = match[2];
} }
var text = text.replace( if (bg) {
match[0], color += " bg-" + bg;
"<span class='" + color + "'>" + match[3] + "</span>" }
); 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)) { for (var i in regex.styles) {
var match; var pattern = regex.styles[i][0];
while (match = pattern.exec(text)) { var style = regex.styles[i][1];
text = text.replace(match[0], style[0] + match[1] + style[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; }
}
}
return text;
} }