colors: Add html bg color support.
This commit is contained in:
parent
7ab8cc6b3d
commit
db17c6272d
1 changed files with 42 additions and 12 deletions
|
@ -229,7 +229,6 @@ class Formatted(object):
|
||||||
return cls(parser.get_substrings())
|
return cls(parser.get_substrings())
|
||||||
|
|
||||||
def to_html(self):
|
def to_html(self):
|
||||||
# TODO BG COLOR
|
|
||||||
def add_attribute(string, name, value):
|
def add_attribute(string, name, value):
|
||||||
if name == "bold" and value:
|
if name == "bold" and value:
|
||||||
return "{bold_on}{text}{bold_off}".format(
|
return "{bold_on}{text}{bold_off}".format(
|
||||||
|
@ -257,17 +256,32 @@ class Formatted(object):
|
||||||
return "{code_on}{text}{code_off}".format(
|
return "{code_on}{text}{code_off}".format(
|
||||||
code_on="<code>", text=string, code_off="</code>"
|
code_on="<code>", text=string, code_off="</code>"
|
||||||
)
|
)
|
||||||
if name == "fgcolor" and value:
|
|
||||||
|
return string
|
||||||
|
|
||||||
|
def add_color(string, fgcolor, bgcolor):
|
||||||
|
fgcolor_string = ""
|
||||||
|
bgcolor_string = ""
|
||||||
|
|
||||||
|
if fgcolor:
|
||||||
|
fgcolor_string = " data-mx-color={}".format(
|
||||||
|
color_weechat_to_html(fgcolor)
|
||||||
|
)
|
||||||
|
|
||||||
|
if bgcolor:
|
||||||
|
bgcolor_string = " data-mx-bg-color={}".format(
|
||||||
|
color_weechat_to_html(bgcolor)
|
||||||
|
)
|
||||||
|
|
||||||
return "{color_on}{text}{color_off}".format(
|
return "{color_on}{text}{color_off}".format(
|
||||||
color_on="<font color={color}>".format(
|
color_on="<font{fg}{bg}>".format(
|
||||||
color=color_weechat_to_html(value)
|
fg=fgcolor_string,
|
||||||
|
bg=bgcolor_string
|
||||||
),
|
),
|
||||||
text=string,
|
text=string,
|
||||||
color_off="</font>",
|
color_off="</font>",
|
||||||
)
|
)
|
||||||
|
|
||||||
return string
|
|
||||||
|
|
||||||
def format_string(formatted_string):
|
def format_string(formatted_string):
|
||||||
text = formatted_string.text
|
text = formatted_string.text
|
||||||
attributes = formatted_string.attributes.copy()
|
attributes = formatted_string.attributes.copy()
|
||||||
|
@ -283,6 +297,13 @@ class Formatted(object):
|
||||||
else:
|
else:
|
||||||
text = add_attribute(text, "code", True)
|
text = add_attribute(text, "code", True)
|
||||||
attributes.pop("code")
|
attributes.pop("code")
|
||||||
|
|
||||||
|
elif attributes["fgcolor"] or attributes["bgcolor"]:
|
||||||
|
text = add_color(
|
||||||
|
text,
|
||||||
|
attributes["fgcolor"],
|
||||||
|
attributes["bgcolor"]
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
for key, value in attributes.items():
|
for key, value in attributes.items():
|
||||||
text = add_attribute(text, key, value)
|
text = add_attribute(text, key, value)
|
||||||
|
@ -311,7 +332,6 @@ class Formatted(object):
|
||||||
return "".join(plain_string)
|
return "".join(plain_string)
|
||||||
|
|
||||||
def to_weechat(self):
|
def to_weechat(self):
|
||||||
# TODO BG COLOR
|
|
||||||
def add_attribute(string, name, value, attributes):
|
def add_attribute(string, name, value, attributes):
|
||||||
if not value:
|
if not value:
|
||||||
return string
|
return string
|
||||||
|
@ -471,7 +491,6 @@ DEFAULT_ATTRIBUTES = {
|
||||||
|
|
||||||
|
|
||||||
class MatrixHtmlParser(HTMLParser):
|
class MatrixHtmlParser(HTMLParser):
|
||||||
# TODO bg color
|
|
||||||
# TODO bullets
|
# TODO bullets
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
HTMLParser.__init__(self)
|
HTMLParser.__init__(self)
|
||||||
|
@ -541,7 +560,7 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
self.text = ""
|
self.text = ""
|
||||||
elif tag == "font":
|
elif tag == "font":
|
||||||
for key, value in attrs:
|
for key, value in attrs:
|
||||||
if key == "color":
|
if key in ["data-mx-color", "color"]:
|
||||||
color = color_html_to_weechat(value)
|
color = color_html_to_weechat(value)
|
||||||
|
|
||||||
if not color:
|
if not color:
|
||||||
|
@ -551,6 +570,17 @@ class MatrixHtmlParser(HTMLParser):
|
||||||
self.add_substring(self.text, self.attributes.copy())
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
self.text = ""
|
self.text = ""
|
||||||
self.attributes["fgcolor"] = color
|
self.attributes["fgcolor"] = color
|
||||||
|
|
||||||
|
elif key in ["data-mx-bg-color"]:
|
||||||
|
color = color_html_to_weechat(value)
|
||||||
|
if not color:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if self.text:
|
||||||
|
self.add_substring(self.text, self.attributes.copy())
|
||||||
|
self.text = ""
|
||||||
|
self.attributes["bgcolor"] = color
|
||||||
|
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue