From 0f77c17e9c7d11173098ba412d62239a505b18d5 Mon Sep 17 00:00:00 2001 From: ReK2 Date: Mon, 17 Aug 2020 00:57:25 +0200 Subject: [PATCH] enable to show html as plain text if enabled and the url and text is not the same or nor contains data-mautrix-no-link --- config/config.go | 6 +++- ui/commands.go | 8 +++-- ui/messages/html/parser.go | 61 ++++++++++++++++++++++++++++++++------ ui/messages/parser.go | 4 +-- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index ce5ed6f..3f7ff1c 100644 --- a/config/config.go +++ b/config/config.go @@ -50,7 +50,11 @@ type UserPreferences struct { DisableHTML bool `yaml:"disable_html"` DisableDownloads bool `yaml:"disable_downloads"` DisableNotifications bool `yaml:"disable_notifications"` - DisableShowUrls bool `yaml:"disable_show_urls"` +<<<<<<< HEAD + DisableShowURLs bool `yaml:"disable_show_urls"` +======= + DisableShowurls bool `yaml:"disable_show_urls"` +>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 } // Config contains the main config of gomuks. diff --git a/ui/commands.go b/ui/commands.go index 6b95ec3..e666cdc 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -722,7 +722,7 @@ var toggleMsg = map[string]ToggleMessage{ "downloads": SimpleToggleMessage("automatic downloads"), "notifications": SimpleToggleMessage("desktop notifications"), "unverified": SimpleToggleMessage("sending messages to unverified devices"), - "showurls": SimpleToggleMessage("Show text URL"), + "showurls": SimpleToggleMessage("show URLs in text format"), } func makeUsage() string { @@ -766,7 +766,11 @@ func cmdToggle(cmd *Command) { case "unverified": val = &cmd.Config.SendToVerifiedOnly case "showurls": - val = &cmd.Config.Preferences.DisableShowUrls +<<<<<<< HEAD + val = &cmd.Config.Preferences.DisableShowURLs +======= + val = &cmd.Config.Preferences.DisableShowurls +>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 default: cmd.Reply("Unknown toggle %s. Use /toggle without arguments for a list of togglable things.", thing) return diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index 3c111fc..342d2d2 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -17,6 +17,7 @@ package html import ( + "fmt" "regexp" "strconv" "strings" @@ -31,7 +32,6 @@ import ( "maunium.net/go/mautrix/id" "maunium.net/go/gomuks/config" - "maunium.net/go/gomuks/debug" "maunium.net/go/tcell" "maunium.net/go/gomuks/matrix/rooms" @@ -41,9 +41,17 @@ import ( var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)") type htmlParser struct { - room *rooms.Room +<<<<<<< HEAD + prefs *config.UserPreferences + room *rooms.Room + sameURL bool +======= + prefs *config.UserPreferences + room *rooms.Room + mautrixnolink bool + sameURL bool +>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 - Config *config.Config keepLinebreak bool } @@ -84,6 +92,15 @@ func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string return "" } +func (parser *htmlParser) hasAttribute(node *html.Node, attribute string) bool { + for _, attr := range node.Attr { + if attr.Key == attribute { + return true + } + } + return false +} + func (parser *htmlParser) listToEntity(node *html.Node) Entity { children := parser.nodeToEntities(node.FirstChild) ordered := node.Data == "ol" @@ -174,6 +191,7 @@ func (parser *htmlParser) blockquoteToEntity(node *html.Node) Entity { } func (parser *htmlParser) linkToEntity(node *html.Node) Entity { + sameURL := false href := parser.getAttribute(node, "href") entity := &ContainerEntity{ @@ -187,16 +205,28 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity { return entity } - debug.Print("here value of parser.config.Preferences.ShowUrls") + if len(entity.Children) == 1 { + entity, ok := entity.Children[0].(*TextEntity) + if ok && entity.Text == href { +<<<<<<< HEAD + sameURL = true + } + } - debug.Printf("%v", config.UserPreferences{}.DisableShowUrls) - showurls := config.UserPreferences{}.DisableShowUrls + if !parser.prefs.DisableShowURLs && !parser.hasAttribute(node, "data-mautrix-no-link") && !sameURL { - if showurls { + entity.Children = append(entity.Children, NewTextEntity(fmt.Sprintf(" (%s)", href))) +======= + parser.sameURL = true + } + } + + if !parser.prefs.DisableShowurls && !parser.mautrixnolink && !parser.sameURL { entity.Children = append( []Entity{NewTextEntity("(" + href + ") ")}, parser.nodeToEntities(node.FirstChild)..., ) +>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 } match := matrixToURL.FindStringSubmatch(href) @@ -396,20 +426,33 @@ func (parser *htmlParser) Parse(htmlData string) Entity { if bodyNode != nil { return parser.singleNodeToEntity(bodyNode) } + return parser.singleNodeToEntity(node) } const TabLength = 4 // Parse parses a HTML-formatted Matrix event into a UIMessage. -func Parse(room *rooms.Room, content *event.MessageEventContent, sender id.UserID, senderDisplayname string) Entity { +func Parse(prefs *config.UserPreferences, room *rooms.Room, content *event.MessageEventContent, sender id.UserID, senderDisplayname string) Entity { htmlData := content.FormattedBody +<<<<<<< HEAD +======= + DataMautrixNoLink := false +>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 + if content.Format != event.FormatHTML { htmlData = strings.Replace(html.EscapeString(content.Body), "\n", "
", -1) } htmlData = strings.Replace(htmlData, "\t", strings.Repeat(" ", TabLength), -1) +<<<<<<< HEAD - parser := htmlParser{room: room} + parser := htmlParser{room: room, prefs: prefs} +======= + if strings.Contains(htmlData, "data-mautrix-no-link") { + DataMautrixNoLink = true + } + parser := htmlParser{room: room, prefs: prefs, mautrixnolink: DataMautrixNoLink} +>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 root := parser.Parse(htmlData) beRoot := root.(*ContainerEntity) beRoot.Block = false diff --git a/ui/messages/parser.go b/ui/messages/parser.go index 349794d..a3ee6c5 100644 --- a/ui/messages/parser.go +++ b/ui/messages/parser.go @@ -25,7 +25,7 @@ import ( "maunium.net/go/tcell" "maunium.net/go/gomuks/debug" - "maunium.net/go/gomuks/interface" + ifc "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/messages/html" @@ -204,7 +204,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *muksevt.Eve switch content.MsgType { case event.MsgText, event.MsgNotice, event.MsgEmote: if content.Format == event.FormatHTML { - return NewHTMLMessage(evt, displayname, html.Parse(room, content, evt.Sender, displayname)) + return NewHTMLMessage(evt, displayname, html.Parse(matrix.Preferences(), room, content, evt.Sender, displayname)) } content.Body = strings.Replace(content.Body, "\t", " ", -1) return NewTextMessage(evt, displayname, content.Body)