Last active
December 25, 2015 21:29
-
-
Save JLChnToZ/7043235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MCFormatToHTML = (src) -> | |
src = src.replace /&/g, "&" | |
src = src.replace /</g, "<" | |
src = src.replace />/g, ">" | |
src = src.replace /"/g, """ | |
src = src.replace /\s/, " " | |
src = src.replace /\n/, "<br />" | |
colors = ["000", "00A", "0A0", "0AA", "A00", "A0A", "FA0", "AAA", "555", "55F", "5F5", "5FF", "F55", "F5F", "FF5", "FFF"] | |
shadowColors = ["000000", "00002A", "002A00", "002A2A", "2A0000", "2A002A", "2A2A00", "2A2A2A", "151515", "15153F", "153F15", "153F3F", "3F1515", "3F153F", "3F3F15", "3F3F3F"] | |
color = "000" | |
isBold = isUnderline = isStrike = isItalic = isCode = isStyleChanged = no | |
styleText = "text-shadow:2px 2px #000;color:#000" | |
ret = "<span style=\"#{styleText}\">" | |
i = 0 | |
while i < src.length | |
if src[i] is "§" | |
isCode = yes | |
else if isCode | |
switch src[i].toLowerCase() | |
when "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" | |
color = parseInt src[i], 16 | |
when "l" | |
isBold = yes | |
when "m" | |
isStrike = yes | |
when "n" | |
isUnderline = yes | |
when "o" | |
isItalic = yes | |
isBold = isUnderline = isStrike = isItalic = no unless ("0123456789abcdefr".indexOf src[i].toLowerCase()) is -1 | |
styleText = "color:##{colors[color]};text-shadow:2px 2px ##{shadowColors[color]};" | |
styleText += "font-weight:bold;" if isBold | |
styleText += "font-weight:italic;" if isItalic | |
styleText += "text-decoration:" if isUnderline or isStrike | |
styleText += "underline" if isUnderline | |
styleText += " line-through" if isStrike | |
isCode = no | |
isStyleChanged = yes | |
else | |
ret += "</span><span style=\"#{styleText}\">" if isStyleChanged | |
ret += src[i] | |
isStyleChanged = no | |
i++ | |
"#{ret}</span>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MCFormatToHTML(src) { | |
src = src.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'). | |
replace(/"/g, '"').replace(/\s/, " "); | |
var colors = ['000', '00A', '0A0', '0AA', 'A00', | |
'A0A', 'FA0', 'AAA', '555', '55F', | |
'5F5', '5FF', 'F55', 'F5F', 'FF5', 'FFF'], | |
shadowColors = ['00000', '002A', '002A00', '002A2A', '2A0000', | |
'2A002A', '2A2A00', '2A2A2A', '151515', '15153F', | |
'153F15', '153F3F', '3F1515', '3F153F', '3F3F15', '3F3F3F'], | |
color = '000', | |
isBold = false, isUnderline = false, isStrike = false, | |
isItalic = false, isCode = false, isStyleChanged = false, | |
ret = '<span style="text-shadow:2px 2px #000;color:#000">'; | |
for (var i = 0; i < src.length; i++) { | |
if (src[i] == '§') { | |
isCode = true; | |
} else if (isCode) { | |
switch (src[i].toLowerCase()) { | |
case '0': case '1': case '2': case '3': | |
case '4': case '5': case '6': case '7': | |
case '8': case '9': case 'a': case 'b': | |
case 'c': case 'd': case 'e': case 'f': | |
color = parseInt(src[i], 16); | |
break; | |
case 'l': | |
isBold = true; | |
break; | |
case 'm': | |
isStrike = true; | |
break; | |
case 'n': | |
isUnderline = true; | |
break; | |
case 'o': | |
isItalic = true; | |
break; | |
} | |
if('0123456789abcdefr'.indexOf(src[i].toLowerCase()) != -1) | |
isBold = isUnderline = isStrike = isItalic = false; | |
isCode = false; | |
isStyleChanged = true; | |
} else { | |
ret += (isStyleChanged ? | |
'</span><span style="color:#' + colors[color] + ';text-shadow:2px 2px #' + | |
shadowColors[color] + ';' + | |
(isBold ? 'font-weight:bold;' : '') + | |
'text-decoration:' + (isUnderline ? 'underline' : '') + | |
(!isUnderline && !isStrike ? 'none' : (isUnderline && isStrike ? ' ' : '')) + | |
(isStrike ? 'line-through' : '') + ';' + | |
(isItalic ? 'font-weight:italic' : '') + '">' : '') + src[i]; | |
isStyleChanged = false; | |
} | |
} | |
ret += '</span>'; | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment