From 1a796322f34548a1565908fb313b8817c4587caf Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Tue, 6 Nov 2018 18:25:56 +0100 Subject: [PATCH 1/4] Simplify Formatted.to_weechat logic. --- matrix/colors.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/matrix/colors.py b/matrix/colors.py index 89b578d..88df27e 100644 --- a/matrix/colors.py +++ b/matrix/colors.py @@ -266,36 +266,33 @@ class Formatted(object): def to_weechat(self): # TODO BG COLOR def add_attribute(string, name, value): - if name == "bold" and value: + if not value: + return string + elif name == "bold": return "{bold_on}{text}{bold_off}".format( bold_on=W.color("bold"), text=string, bold_off=W.color("-bold"), ) - - if name == "italic" and value: + elif name == "italic": return "{italic_on}{text}{italic_off}".format( italic_on=W.color("italic"), text=string, italic_off=W.color("-italic"), ) - - if name == "underline" and value: + elif name == "underline": return "{underline_on}{text}{underline_off}".format( underline_on=W.color("underline"), text=string, underline_off=W.color("-underline"), ) - - if name == "strikethrough" and value: + elif name == "strikethrough": return string_strikethrough(string) - - if name == "quote" and value: + elif name == "quote": return self.textwrapper.fill( W.string_remove_color(string.replace("\n", ""), "") ) - - if name == "code" and value: + elif name == "code": try: lexer = get_lexer_by_name(value) except ClassNotFound: @@ -304,22 +301,20 @@ class Formatted(object): # highlight adds a newline to the end of the string, remove it # from the output return highlight(string, lexer, WeechatFormatter())[:-1] - - if name == "fgcolor" and value: + elif name == "fgcolor": return "{color_on}{text}{color_off}".format( color_on=W.color(value), text=string, color_off=W.color("resetcolor"), ) - - if name == "bgcolor" and value: + elif name == "bgcolor": return "{color_on}{text}{color_off}".format( color_on=W.color("," + value), text=string, color_off=W.color("resetcolor"), ) - - return string + else: + return string def format_string(formatted_string): text = formatted_string.text From 560c8d0c54c7058cfa912ee2652e8cacf3f6b4fd Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Wed, 7 Nov 2018 12:05:46 +0100 Subject: [PATCH 2/4] Make pygments style configurable. --- matrix/colors.py | 10 ++++++++-- matrix/config.py | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/matrix/colors.py b/matrix/colors.py index 88df27e..41c2600 100644 --- a/matrix/colors.py +++ b/matrix/colors.py @@ -29,7 +29,7 @@ from typing import List import webcolors from pygments import highlight -from pygments.formatter import Formatter +from pygments.formatter import Formatter, get_style_by_name from pygments.lexers import get_lexer_by_name, guess_lexer from pygments.util import ClassNotFound @@ -298,9 +298,15 @@ class Formatted(object): except ClassNotFound: lexer = guess_lexer(string) + try: + style = get_style_by_name(G.CONFIG.look.pygments_style) + except ClassNotFound: + style = "native" + # highlight adds a newline to the end of the string, remove it # from the output - return highlight(string, lexer, WeechatFormatter())[:-1] + return highlight(string, lexer, + WeechatFormatter(style=style))[:-1] elif name == "fgcolor": return "{color_on}{text}{color_off}".format( color_on=W.color(value), diff --git a/matrix/config.py b/matrix/config.py index 4d69c76..b7ac695 100644 --- a/matrix/config.py +++ b/matrix/config.py @@ -357,6 +357,16 @@ class MatrixConfig(WeechatConfig): "rooms (note: content is evaluated, see /help eval)"), eval_cast, ), + Option( + "pygments_style", + "string", + "", + 0, + 0, + "native", + "Pygments style to use for highlighting source code blocks", + eval_cast, + ), ] network_options = [ From a74ac888a39cccc8e5e2db7e99304e12cc5ae81b Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Wed, 7 Nov 2018 12:57:32 +0100 Subject: [PATCH 3/4] Fix typo: DEFAULT_ATRIBUTES -> DEFAULT_ATTRIBUTES --- matrix/colors.py | 16 ++++++++-------- matrix/server.py | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/matrix/colors.py b/matrix/colors.py index 41c2600..261a5bd 100644 --- a/matrix/colors.py +++ b/matrix/colors.py @@ -62,7 +62,7 @@ class Formatted(object): def is_formatted(self): # type: (Formatted) -> bool for string in self.substrings: - if string.attributes != DEFAULT_ATRIBUTES: + if string.attributes != DEFAULT_ATTRIBUTES: return True return False @@ -76,7 +76,7 @@ class Formatted(object): """ text = "" # type: str substrings = [] # type: List[FormattedString] - attributes = DEFAULT_ATRIBUTES.copy() + attributes = DEFAULT_ATTRIBUTES.copy() i = 0 while i < len(line): @@ -165,7 +165,7 @@ class Formatted(object): substrings.append(FormattedString(text, attributes.copy())) text = "" # Reset all the attributes - attributes = DEFAULT_ATRIBUTES.copy() + attributes = DEFAULT_ATTRIBUTES.copy() i = i + 1 # Italic elif line[i] == "\x1D": @@ -371,7 +371,7 @@ class Formatted(object): # TODO this should be a typed dict. -DEFAULT_ATRIBUTES = { +DEFAULT_ATTRIBUTES = { "bold": False, "italic": False, "underline": False, @@ -390,7 +390,7 @@ class MatrixHtmlParser(HTMLParser): HTMLParser.__init__(self) self.text = "" # type: str self.substrings = [] # type: List[FormattedString] - self.attributes = DEFAULT_ATRIBUTES.copy() + self.attributes = DEFAULT_ATTRIBUTES.copy() def unescape(self, text): """Shim to unescape HTML in both Python 2 and 3. @@ -442,13 +442,13 @@ class MatrixHtmlParser(HTMLParser): if self.text: self.add_substring(self.text, self.attributes.copy()) self.text = "\n" - self.add_substring(self.text, DEFAULT_ATRIBUTES.copy()) + self.add_substring(self.text, DEFAULT_ATTRIBUTES.copy()) self.text = "" elif tag == "br": if self.text: self.add_substring(self.text, self.attributes.copy()) self.text = "\n" - self.add_substring(self.text, DEFAULT_ATRIBUTES.copy()) + self.add_substring(self.text, DEFAULT_ATTRIBUTES.copy()) self.text = "" elif tag == "font": for key, value in attrs: @@ -482,7 +482,7 @@ class MatrixHtmlParser(HTMLParser): elif tag == "blockquote": self._toggle_attribute("quote") self.text = "\n" - self.add_substring(self.text, DEFAULT_ATRIBUTES.copy()) + self.add_substring(self.text, DEFAULT_ATTRIBUTES.copy()) self.text = "" elif tag == "font": if self.text: diff --git a/matrix/server.py b/matrix/server.py index 7fadc92..78de798 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -61,7 +61,7 @@ from .globals import SCRIPT_NAME, SERVERS, W, MAX_EVENTS from .utf import utf8_decode from .utils import create_server_buffer, key_from_value, server_buffer_prnt -from .colors import Formatted, FormattedString, DEFAULT_ATRIBUTES +from .colors import Formatted, FormattedString, DEFAULT_ATTRIBUTES try: @@ -728,7 +728,7 @@ class MatrixServer(object): room_buffer.printed_before_ack_queue.append(uuid) plain_message = formatted.to_weechat() plain_message = W.string_remove_color(plain_message, "") - attributes = DEFAULT_ATRIBUTES.copy() + attributes = DEFAULT_ATTRIBUTES.copy() attributes["fgcolor"] = G.CONFIG.color.unconfirmed_message new_formatted = Formatted([FormattedString( plain_message, From 9b6ca7886614d78c5a0b9504c32904811678f9dc Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Wed, 7 Nov 2018 13:24:22 +0100 Subject: [PATCH 4/4] Don't use eval_cast for pygments_style option. --- matrix/config.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matrix/config.py b/matrix/config.py index b7ac695..76afb30 100644 --- a/matrix/config.py +++ b/matrix/config.py @@ -365,7 +365,6 @@ class MatrixConfig(WeechatConfig): 0, "native", "Pygments style to use for highlighting source code blocks", - eval_cast, ), ]