Skip to content

Instantly share code, notes, and snippets.

@bakugo
Last active July 23, 2022 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakugo/1545c39231d6ed10226b4600d7270681 to your computer and use it in GitHub Desktop.
Save bakugo/1545c39231d6ed10226b4600d7270681 to your computer and use it in GitHub Desktop.
def convert_comment(a):
# basic html escape
a = a.replace("&", "&")
a = a.replace("\"", """)
a = a.replace("\'", "'")
a = a.replace("<", "&lt;")
a = a.replace(">", "&gt;")
# newlines
a = a.replace("\r\n", "\n")
# fix [/spoiler] that should be </span>
if "&lt;span" in a:
a = re.sub("(&lt;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 "&gt;" in a:
# quotelinks
a = re.sub("(&gt;&gt;(\d+))", "<a>\\1</a>", a)
a = re.sub("(&gt;&gt;&gt;\/(\w+)\/(\d+)?)", "<a>\\1</a>", a)
# greentext
a = re.sub("(^|\n)( )?(&gt;(?:.*?))(?=$|\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