-
-
Save bakugo/1545c39231d6ed10226b4600d7270681 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
def convert_comment(a): | |
# basic html escape | |
a = a.replace("&", "&") | |
a = a.replace("\"", """) | |
a = a.replace("\'", "'") | |
a = a.replace("<", "<") | |
a = a.replace(">", ">") | |
# newlines | |
a = a.replace("\r\n", "\n") | |
# fix [/spoiler] that should be </span> | |
if "<span" in a: | |
a = re.sub("(<span(.+?))\[\/spoiler\]", "\\1</span>", a) | |
# spoilers | |
# needs to be first because they will end other stuff like quotes | |
for idx in range(10): | |
if not "[spoiler]" in a: break | |
a = re.sub("\[spoiler\](.*?)\[\/spoiler\]", "<s>\\1</s>", a, flags=re.S) | |
# remaining tags | |
if "[" in a: | |
a = re.sub("\[banned\](.*?)\[\/banned\]", "<strong style=\"color: red;\">\\1</strong>", a, flags=re.S) | |
a = re.sub("\[code\](.*?)\[\/code\]", "<pre class=\"prettyprint\">\\1</pre>", a, flags=re.S) | |
a = re.sub("\[math\](.*?)\[\/math\]", "<span class=\"math\">\\1</span>", a, flags=re.S) | |
a = re.sub("\[eqn\](.*?)\[\/eqn\]", "<div class=\"math\">\\1</div>", a, flags=re.S) | |
a = re.sub("\[sjis\](.*?)\[\/sjis\]", "<span class=\"sjis\">\\1</span>", a, flags=re.S) | |
a = re.sub("\[shiftjis\](.*?)\[\/shiftjis\]", "<span class=\"sjis\">\\1</span>", a, flags=re.S) | |
if ">" in a: | |
# quotelinks | |
a = re.sub("(>>(\d+))", "<a>\\1</a>", a) | |
a = re.sub("(>>>\/(\w+)\/(\d+)?)", "<a>\\1</a>", a) | |
# greentext | |
a = re.sub("(^|\n)( )?(>(?:.*?))(?=$|\n|<\/?s>)", "\\1\\2<span class=\"quote\">\\3</span>", a, flags=re.S) | |
if ":lit]" in a: | |
a = re.sub("\[(\/?[a-z]+?):lit\]", "[\\1]", a) | |
a = a.replace("\n", "<br>") | |
return a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment