From 798e77939147db186677cd74bb440cbe5cd37379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 30 Aug 2018 13:37:13 +0200 Subject: [PATCH] colors: Handle quoted code segments. --- matrix/_weechat.py | 4 ++++ matrix/colors.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/matrix/_weechat.py b/matrix/_weechat.py index a4ab139..54cbe37 100644 --- a/matrix/_weechat.py +++ b/matrix/_weechat.py @@ -168,3 +168,7 @@ def nicklist_remove_nick(*_, **__): def nicklist_search_nick(*args, **kwargs): return buffer_new(args, kwargs) + + +def string_remove_color(message, _): + return message diff --git a/matrix/colors.py b/matrix/colors.py index 7405e0b..6e0f878 100644 --- a/matrix/colors.py +++ b/matrix/colors.py @@ -334,12 +334,36 @@ class Formatted(object): ) attributes.pop("strikethrough") + def indent(text, prefix): + return prefix + text.replace("\n", "\n{}".format(prefix)) + for key, value in attributes.items(): + # Don't use textwrap to quote the code + if attributes["code"] and key == "quote" and value: + continue + text = add_attribute(text, key, value) - return text + + # If we're quoted code add quotation marks now. + if attributes["quote"] and key == "code" and value: + text = indent(text, "{}>{} ".format( + W.color(G.CONFIG.color.quote), + W.color("reset"))) + + # If we're code don't remove multiple newlines blindly + if attributes["code"]: + return text + return re.sub(r"\n+", "\n", text) weechat_strings = map(format_string, self.substrings) - return re.sub(r"\n+", "\n", "".join(weechat_strings)).strip() + + # Remove duplicate \n elements from the list + strings = [] + for string in weechat_strings: + if len(strings) == 0 or string != "\n" or string != strings[-1]: + strings.append(string) + + return "".join(strings).strip() # TODO this should be a typed dict.