From f1bd9da184aa3071ae84af9b1bea4bfec16b50be Mon Sep 17 00:00:00 2001 From: Will Date: Tue, 21 Jun 2016 14:54:08 -0400 Subject: [PATCH] Chat formatting additions and improvements (#315) * Pagination, switch to format strings, give box a lang * working pagination --- cogs/utils/chat_formatting.py | 51 ++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/cogs/utils/chat_formatting.py b/cogs/utils/chat_formatting.py index 1f65ad242..6b6639a87 100644 --- a/cogs/utils/chat_formatting.py +++ b/cogs/utils/chat_formatting.py @@ -1,20 +1,47 @@ def bold(text): - return "**"+str(text)+"**" + return "**{}**".format(text) -def italics(text): - return "*"+str(text)+"*" -def strikethrough(text): - return "~~"+str(text)+"~~" +def box(text, lang=""): + ret = "```{}\n{}\n```".format(lang, text) + return ret -def underline(text): - return "__"+str(text)+"__" - -def box(text): - return "```"+str(text)+"```" def inline(text): - return "`"+str(text)+"`" + return "`{}`".format(text) + + +def italics(text): + return "*{}*".format(text) + + +def pagify(text, delims=[], escape=True, shorten_by=8): + """DOES NOT RESPECT MARKDOWN BOXES OR INLINE CODE""" + in_text = text + while len(in_text) > 2000: + closest_delim = max([in_text.rfind(d, 0, 2000 - shorten_by) + for d in delims]) + closest_delim = closest_delim if closest_delim != -1 else 2000 + if escape: + to_send = escape_mass_mentions(in_text[:closest_delim]) + else: + to_send = in_text[:closest_delim] + yield to_send + in_text = in_text[closest_delim:] + + if escape: + yield escape_mass_mentions(in_text) + else: + yield in_text + + +def strikethrough(text): + return "~~{}~~".format(text) + + +def underline(text): + return "__{}__".format(text) + def escape_mass_mentions(text): words = { @@ -23,4 +50,4 @@ def escape_mass_mentions(text): } for k, v in words.items(): text = text.replace(k, v) - return text \ No newline at end of file + return text