diff options
Diffstat (limited to 'ui/messages/parser')
-rw-r--r-- | ui/messages/parser/htmlparser.go | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index ddf6d36..f01d3cb 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -61,12 +61,18 @@ func AdjustStyleStrikethrough(style tcell.Style) tcell.Style { return style.Strikethrough(true) } -func AdjustStyleColor(color tcell.Color) func(tcell.Style) tcell.Style { +func AdjustStyleTextColor(color tcell.Color) func(tcell.Style) tcell.Style { return func(style tcell.Style) tcell.Style { return style.Foreground(color) } } +func AdjustStyleBackgroundColor(color tcell.Color) func(tcell.Style) tcell.Style { + return func(style tcell.Style) tcell.Style { + return style.Background(color) + } +} + func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string { for _, attr := range node.Attr { if attr.Key == attribute { @@ -137,28 +143,38 @@ func (parser *htmlParser) basicFormatToTString(node *html.Node, stripLinebreak b return str } -func (parser *htmlParser) fontToTString(node *html.Node, stripLinebreak bool) tstring.TString { - str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) - hex := parser.getAttribute(node, "data-mx-color") +func (parser *htmlParser) parseColor(node *html.Node, mainName, altName string) (color tcell.Color, ok bool) { + hex := parser.getAttribute(node, mainName) if len(hex) == 0 { - hex = parser.getAttribute(node, "color") + hex = parser.getAttribute(node, altName) if len(hex) == 0 { - return str + return } } - color, err := colorful.Hex(hex) + cful, err := colorful.Hex(hex) if err != nil { - color2, ok := ColorMap[strings.ToLower(hex)] - if !ok { - return str + color2, found := ColorMap[strings.ToLower(hex)] + if !found { + return } - color, _ = colorful.MakeColor(color2) + cful, _ = colorful.MakeColor(color2) } - r, g, b := color.RGB255() - tcellColor := tcell.NewRGBColor(int32(r), int32(g), int32(b)) - str.AdjustStyleFull(AdjustStyleColor(tcellColor)) + r, g, b := cful.RGB255() + return tcell.NewRGBColor(int32(r), int32(g), int32(b)), true +} + +func (parser *htmlParser) fontToTString(node *html.Node, stripLinebreak bool) tstring.TString { + str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak) + fgColor, ok := parser.parseColor(node, "data-mx-color", "color") + if ok { + str.AdjustStyleFull(AdjustStyleTextColor(fgColor)) + } + bgColor, ok := parser.parseColor(node, "data-mx-bg-color", "background-color") + if ok { + str.AdjustStyleFull(AdjustStyleBackgroundColor(bgColor)) + } return str } |