Skip to content

Instantly share code, notes, and snippets.

@vampirefrog
Created July 31, 2010 21:40
Show Gist options
  • Save vampirefrog/502639 to your computer and use it in GitHub Desktop.
Save vampirefrog/502639 to your computer and use it in GitHub Desktop.
// written by quaker66:
//! Let's convert a Cube string colorification into proper HTML spans
//! Accepts just one argument, returns the html string.
function convert_cube_string(str) {
var tmp = str; // some temp we'll return later
var found = false; // have we found some colorz??!
var cube_colors = [ // array of colors with element number corresponding to sauer type
'40FF80', // 0 = green
'60A0FF', // 1 = blue
'FFC040', // 2 = yellow
'FF4040', // 3 = red
'808080', // 4 = gray
'C040C0', // 5 = magenta
'FF8000', // 6 = orange
];
var pos = tmp.indexOf('\f'); // first occurence of \f
while (pos != -1) { // loop till there is 0 occurs.
var color = cube_colors[parseInt(tmp.substr(pos + 1, 1))]; // get a color from array
if (found) { // if we've found something before, close the span on > 6, or close+create new on 0-6
if (color) { // yay! color exists. It means we'll want to close last span.
tmp = tmp.replace(/\f[0-6]/, "</span><span style=\"color: #" + color.toString() + "\">");
} else { // There is no color. It means the num is higher than 6.
tmp = tmp.replace(/\f./, "</span>");
found = false; // pretend we've never found anything
}
} else { // if it's first occurence and its num is bigger than 6, simply ignore.
if (color) { // this means the num is 0-6. In that case, create our first span.
tmp = tmp.replace(/\f[0-6]/, "<span style=\"color: #" + color.toString() + "\">");
found = true; // yay! we've found a color! (or again?)
}
}
pos = tmp.indexOf('\f', pos + 1); // move to next position to feed while
}
// if we've found anything lately and didn't close it with \f > 6, let's do it at the end
if (found) tmp = tmp.replace(/$/, "</span>");
// we can finally return our html string.
return tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment