Simplify Formatted.to_weechat logic.

This commit is contained in:
Denis Kasak 2018-11-06 18:25:56 +01:00
parent aae5732018
commit 1a796322f3

View file

@ -266,36 +266,33 @@ 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:
@ -304,22 +301,20 @@ class Formatted(object):
# 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())[:-1]
elif name == "fgcolor":
if name == "fgcolor" 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"),
) )
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):
text = formatted_string.text text = formatted_string.text