Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. var ConvertCost = {
  2. colorCodes: ['W', 'U', 'B', 'R', 'G', 'C', 'X', 'Y', 'Z', 'P', 'T'],
  3. // todo: this needs support for infinity symbols, and tap symbols
  4. parse: function (string) {
  5. var idChars = ['/', '{', '}']; // characters we don't want to add to our build
  6. if (string == null || !string.trim()) { // if the string is null or empty
  7. return ''; // we are already done, return an empty string
  8. }
  9. var newFangledString = ''; // our new mana cost representation
  10. var build = ''; // for our longer identifiers we need to build up to it
  11. var iStart = '<i class="ms ms-cost ms-shadow ms-'; // the start for the <i> element with the proper classes for displaying the symbols
  12. var iEnd = '"/>'; // the end of the <i> element
  13. var lastEndTag = -1;
  14. var iSplit = ' ms-split'; // for our split symbols
  15. for (var i = 0; i < string.length; i++) { // go over each and every character of the string
  16. var currentChar = string.charAt(i);
  17. var nextChar = string.charAt(i + 1);
  18. if (currentChar === '{') { // if we are starting a new mana symbol
  19. if (i > 0 && lastEndTag < i - 1) { // if our current position is after the last time we ended a tag
  20. newFangledString += string.substring(lastEndTag + 1, i - 1) + '&nbsp;'; // add everything from the last tag to just before now to our string
  21. }
  22. }
  23. if (nextChar != null && nextChar === '}') { // this is a short easy one
  24. lastEndTag = i + 1;
  25. if (ConvertCost.colorCodes.indexOf(currentChar) > -1 || (currentChar >= '0' && currentChar <= '9')) { // if the next character is a color, or C, or X, Y, Z, or if its a digit
  26. if (build.trim()) { // if build has characters in it
  27. build += currentChar; // add the current character to the build
  28. newFangledString += (iStart + build.toLowerCase() + ((string.charAt(i - 1) === '/' && currentChar !== 'P') ? iSplit : '') + iEnd); // compile the mana symbols, and if the last character was a / and not P, it was split cost so add iSplit, otherwise don't add anything
  29. build = ''; // we are done with this symbol, reset build for the next one
  30. } else { // build is empty, meaning it is a short, easy symbol
  31. currentChar = currentChar.toLowerCase();
  32. if (currentChar === 't') {
  33. currentChar += 'ap';
  34. }
  35. newFangledString += (iStart + currentChar.toLowerCase() + iEnd); // fix up this symbol, it was a short one
  36. }
  37. }
  38. } else { // it is a long one, take a seat
  39. if (idChars.indexOf(currentChar) === -1 && (ConvertCost.colorCodes.indexOf(currentChar) > -1 || (currentChar >= '0' && currentChar <= '9'))) { // if current character isn't one of our markers (/,{,})
  40. build += currentChar; // add this character to the build
  41. }
  42. }
  43. }
  44. newFangledString += string.substring(lastEndTag + 1); // we are done, so add the rest of the string to our final product
  45. return newFangledString; // the final mana cost, each symbol is an <i> tag with the proper classes from ManaCSS
  46. }
  47. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement