Last active
December 11, 2015 23:39
-
-
Save kierdavis/4678298 to your computer and use it in GitHub Desktop.
Converts mc chat codes to bbcode syntax
This file contains hidden or 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
import sys | |
COLOURS = { | |
"0": "000000", | |
"1": "0000aa", | |
"2": "00aa00", | |
"3": "00aaaa", | |
"4": "aa0000", | |
"5": "aa00aa", | |
"6": "aa5500", | |
"7": "aaaaaa", | |
"8": "555555", | |
"9": "5555ff", | |
"a": "55ff55", | |
"b": "55ffff", | |
"c": "ff5555", | |
"d": "ff55ff", | |
"e": "ffff55", | |
"f": "ffffff", | |
} | |
COLOR = 1 | |
BOLD = 2 | |
ITALIC = 3 | |
UNDERLINE = 4 | |
STRIKE = 5 | |
def opening(tag): | |
if tag == COLOR: | |
return "[color]" | |
elif tag == BOLD: | |
return "[b]" | |
elif tag == ITALIC: | |
return "[i]" | |
elif tag == UNDERLINE: | |
return "[u]" | |
elif tag == STRIKE: | |
return "[s]" | |
def closing(tag): | |
if tag == COLOR: | |
return "[/color]" | |
elif tag == BOLD: | |
return "[/b]" | |
elif tag == ITALIC: | |
return "[/i]" | |
elif tag == UNDERLINE: | |
return "[/u]" | |
elif tag == STRIKE: | |
return "[/s]" | |
def convert(input): | |
is_code = False | |
output = "" | |
tags = [] | |
for c in input: | |
if is_code: | |
is_code = False | |
if c in COLOURS: | |
l = [] | |
if COLOR in tags: | |
while tags[-1] != COLOR: | |
t = tags.pop(-1) | |
l.insert(0, t) | |
output += closing(t) | |
tags.pop(-1) | |
output += "[/color]" | |
tags.append(COLOR) | |
output += "[color=#%s]" % COLOURS[c] | |
for t in l: | |
tags.append(t) | |
output += opening(t) | |
elif c == "k": | |
pass | |
elif c == "l": | |
if BOLD not in tags: | |
tags.append(BOLD) | |
output += opening(BOLD) | |
elif c == "m": | |
if STRIKE not in tags: | |
tags.append(STRIKE) | |
output += opening(STRIKE) | |
elif c == "n": | |
if UNDERLINE not in tags: | |
tags.append(UNDERLINE) | |
output += opening(UNDERLINE) | |
elif c == "o": | |
if ITALIC not in tags: | |
tags.append(ITALIC) | |
output += opening(ITALIC) | |
elif c == "r": | |
for t in reversed(tags): | |
output += closing(t) | |
tags = [] | |
else: | |
output += "&" + c | |
elif c == "&": | |
is_code = True | |
else: | |
output += c | |
for t in reversed(tags): | |
output += closing(t) | |
return output | |
if __name__ == "__main__": | |
print "Enter input, end with a line '===' to convert and exit" | |
input = "" | |
while True: | |
line = raw_input("> ") | |
if line == "===": | |
break | |
input += line + "\n" | |
print convert(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment