aboutsummaryrefslogtreecommitdiff
path: root/ui/messages/parser
diff options
context:
space:
mode:
Diffstat (limited to 'ui/messages/parser')
-rw-r--r--ui/messages/parser/htmlparser.go44
-rw-r--r--ui/messages/parser/parser.go24
2 files changed, 47 insertions, 21 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
}
diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go
index 94ab5b6..b36fde0 100644
--- a/ui/messages/parser/parser.go
+++ b/ui/messages/parser/parser.go
@@ -102,14 +102,24 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Eve
}
if len(evt.Content.GetReplyTo()) > 0 {
evt.Content.RemoveReplyFallback()
- replyToEvt, _ := matrix.Client().GetEvent(room.ID, evt.Content.GetReplyTo())
- replyToEvt.Content.RemoveReplyFallback()
- if len(replyToEvt.Content.FormattedBody) == 0 {
- replyToEvt.Content.FormattedBody = html.EscapeString(replyToEvt.Content.Body)
+ roomID := evt.Content.RelatesTo.InReplyTo.RoomID
+ if len(roomID) == 0 {
+ roomID = room.ID
+ }
+ replyToEvt, _ := matrix.Client().GetEvent(roomID, evt.Content.GetReplyTo())
+ if replyToEvt != nil {
+ replyToEvt.Content.RemoveReplyFallback()
+ if len(replyToEvt.Content.FormattedBody) == 0 {
+ replyToEvt.Content.FormattedBody = html.EscapeString(replyToEvt.Content.Body)
+ }
+ evt.Content.FormattedBody = fmt.Sprintf(
+ "In reply to <a href='https://matrix.to/#/%[1]s'>%[1]s</a><blockquote>%[2]s</blockquote><br/>%[3]s",
+ replyToEvt.Sender, replyToEvt.Content.FormattedBody, evt.Content.FormattedBody)
+ } else {
+ evt.Content.FormattedBody = fmt.Sprintf(
+ "In reply to unknown event https://matrix.to/#/%[1]s/%[2]s<br/>%[3]s",
+ roomID, evt.Content.GetReplyTo(), evt.Content.FormattedBody)
}
- evt.Content.FormattedBody = fmt.Sprintf(
- "In reply to <a href='https://matrix.to/#/%[1]s'>%[1]s</a><blockquote>%[2]s</blockquote><br/>%[3]s",
- replyToEvt.Sender, replyToEvt.Content.FormattedBody, evt.Content.FormattedBody)
}
ts := unixToTime(evt.Timestamp)
switch evt.Content.MsgType {