Merge branch 'pr-16' into olm-command

This commit is contained in:
Damir Jelić 2018-11-07 13:30:52 +01:00
commit e05af8fad3
3 changed files with 39 additions and 29 deletions

View file

@ -29,7 +29,7 @@ from typing import List
import webcolors import webcolors
from pygments import highlight 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.lexers import get_lexer_by_name, guess_lexer
from pygments.util import ClassNotFound from pygments.util import ClassNotFound
@ -62,7 +62,7 @@ class Formatted(object):
def is_formatted(self): def is_formatted(self):
# type: (Formatted) -> bool # type: (Formatted) -> bool
for string in self.substrings: for string in self.substrings:
if string.attributes != DEFAULT_ATRIBUTES: if string.attributes != DEFAULT_ATTRIBUTES:
return True return True
return False return False
@ -76,7 +76,7 @@ class Formatted(object):
""" """
text = "" # type: str text = "" # type: str
substrings = [] # type: List[FormattedString] substrings = [] # type: List[FormattedString]
attributes = DEFAULT_ATRIBUTES.copy() attributes = DEFAULT_ATTRIBUTES.copy()
i = 0 i = 0
while i < len(line): while i < len(line):
@ -165,7 +165,7 @@ class Formatted(object):
substrings.append(FormattedString(text, attributes.copy())) substrings.append(FormattedString(text, attributes.copy()))
text = "" text = ""
# Reset all the attributes # Reset all the attributes
attributes = DEFAULT_ATRIBUTES.copy() attributes = DEFAULT_ATTRIBUTES.copy()
i = i + 1 i = i + 1
# Italic # Italic
elif line[i] == "\x1D": elif line[i] == "\x1D":
@ -266,59 +266,60 @@ class Formatted(object):
def to_weechat(self): def to_weechat(self):
# TODO BG COLOR # TODO BG COLOR
def add_attribute(string, name, value): 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( return "{bold_on}{text}{bold_off}".format(
bold_on=W.color("bold"), bold_on=W.color("bold"),
text=string, text=string,
bold_off=W.color("-bold"), bold_off=W.color("-bold"),
) )
elif name == "italic":
if name == "italic" and value:
return "{italic_on}{text}{italic_off}".format( return "{italic_on}{text}{italic_off}".format(
italic_on=W.color("italic"), italic_on=W.color("italic"),
text=string, text=string,
italic_off=W.color("-italic"), italic_off=W.color("-italic"),
) )
elif name == "underline":
if name == "underline" and value:
return "{underline_on}{text}{underline_off}".format( return "{underline_on}{text}{underline_off}".format(
underline_on=W.color("underline"), underline_on=W.color("underline"),
text=string, text=string,
underline_off=W.color("-underline"), underline_off=W.color("-underline"),
) )
elif name == "strikethrough":
if name == "strikethrough" and value:
return string_strikethrough(string) return string_strikethrough(string)
elif name == "quote":
if name == "quote" and value:
return self.textwrapper.fill( return self.textwrapper.fill(
W.string_remove_color(string.replace("\n", ""), "") W.string_remove_color(string.replace("\n", ""), "")
) )
elif name == "code":
if name == "code" and value:
try: try:
lexer = get_lexer_by_name(value) lexer = get_lexer_by_name(value)
except ClassNotFound: except ClassNotFound:
lexer = guess_lexer(string) 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 # highlight adds a newline to the end of the string, remove it
# from the output # from the output
return highlight(string, lexer, WeechatFormatter())[:-1] return highlight(string, lexer,
WeechatFormatter(style=style))[:-1]
if name == "fgcolor" and value: elif name == "fgcolor":
return "{color_on}{text}{color_off}".format( return "{color_on}{text}{color_off}".format(
color_on=W.color(value), color_on=W.color(value),
text=string, text=string,
color_off=W.color("resetcolor"), color_off=W.color("resetcolor"),
) )
elif name == "bgcolor":
if name == "bgcolor" and value:
return "{color_on}{text}{color_off}".format( return "{color_on}{text}{color_off}".format(
color_on=W.color("," + value), color_on=W.color("," + value),
text=string, text=string,
color_off=W.color("resetcolor"), color_off=W.color("resetcolor"),
) )
else:
return string return string
def format_string(formatted_string): def format_string(formatted_string):
@ -370,7 +371,7 @@ class Formatted(object):
# TODO this should be a typed dict. # TODO this should be a typed dict.
DEFAULT_ATRIBUTES = { DEFAULT_ATTRIBUTES = {
"bold": False, "bold": False,
"italic": False, "italic": False,
"underline": False, "underline": False,
@ -389,7 +390,7 @@ class MatrixHtmlParser(HTMLParser):
HTMLParser.__init__(self) HTMLParser.__init__(self)
self.text = "" # type: str self.text = "" # type: str
self.substrings = [] # type: List[FormattedString] self.substrings = [] # type: List[FormattedString]
self.attributes = DEFAULT_ATRIBUTES.copy() self.attributes = DEFAULT_ATTRIBUTES.copy()
def unescape(self, text): def unescape(self, text):
"""Shim to unescape HTML in both Python 2 and 3. """Shim to unescape HTML in both Python 2 and 3.
@ -441,13 +442,13 @@ class MatrixHtmlParser(HTMLParser):
if self.text: if self.text:
self.add_substring(self.text, self.attributes.copy()) self.add_substring(self.text, self.attributes.copy())
self.text = "\n" self.text = "\n"
self.add_substring(self.text, DEFAULT_ATRIBUTES.copy()) self.add_substring(self.text, DEFAULT_ATTRIBUTES.copy())
self.text = "" self.text = ""
elif tag == "br": elif tag == "br":
if self.text: if self.text:
self.add_substring(self.text, self.attributes.copy()) self.add_substring(self.text, self.attributes.copy())
self.text = "\n" self.text = "\n"
self.add_substring(self.text, DEFAULT_ATRIBUTES.copy()) self.add_substring(self.text, DEFAULT_ATTRIBUTES.copy())
self.text = "" self.text = ""
elif tag == "font": elif tag == "font":
for key, value in attrs: for key, value in attrs:
@ -481,7 +482,7 @@ class MatrixHtmlParser(HTMLParser):
elif tag == "blockquote": elif tag == "blockquote":
self._toggle_attribute("quote") self._toggle_attribute("quote")
self.text = "\n" self.text = "\n"
self.add_substring(self.text, DEFAULT_ATRIBUTES.copy()) self.add_substring(self.text, DEFAULT_ATTRIBUTES.copy())
self.text = "" self.text = ""
elif tag == "font": elif tag == "font":
if self.text: if self.text:

View file

@ -452,6 +452,15 @@ class MatrixConfig(WeechatConfig):
"rooms (note: content is evaluated, see /help eval)"), "rooms (note: content is evaluated, see /help eval)"),
eval_cast, eval_cast,
), ),
Option(
"pygments_style",
"string",
"",
0,
0,
"native",
"Pygments style to use for highlighting source code blocks",
),
] ]
network_options = [ network_options = [

View file

@ -61,7 +61,7 @@ from .globals import SCRIPT_NAME, SERVERS, W, MAX_EVENTS
from .utf import utf8_decode from .utf import utf8_decode
from .utils import create_server_buffer, key_from_value, server_buffer_prnt 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: try:
@ -728,7 +728,7 @@ class MatrixServer(object):
room_buffer.printed_before_ack_queue.append(uuid) room_buffer.printed_before_ack_queue.append(uuid)
plain_message = formatted.to_weechat() plain_message = formatted.to_weechat()
plain_message = W.string_remove_color(plain_message, "") plain_message = W.string_remove_color(plain_message, "")
attributes = DEFAULT_ATRIBUTES.copy() attributes = DEFAULT_ATTRIBUTES.copy()
attributes["fgcolor"] = G.CONFIG.color.unconfirmed_message attributes["fgcolor"] = G.CONFIG.color.unconfirmed_message
new_formatted = Formatted([FormattedString( new_formatted = Formatted([FormattedString(
plain_message, plain_message,