From 2a1329d9810205afc4df3ba58a3fa13fcf8a782a Mon Sep 17 00:00:00 2001 From: ReK2 Date: Sat, 15 Aug 2020 06:59:02 +0200 Subject: [PATCH 1/4] first changes to show urls --- config/config.go | 1 + go.sum | 1 + ui/commands.go | 3 +++ ui/messages/html/parser.go | 25 ++++++++++++++++++++++--- ui/view-main.go | 2 +- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 03e186f..ce5ed6f 100644 --- a/config/config.go +++ b/config/config.go @@ -50,6 +50,7 @@ type UserPreferences struct { DisableHTML bool `yaml:"disable_html"` DisableDownloads bool `yaml:"disable_downloads"` DisableNotifications bool `yaml:"disable_notifications"` + DisableShowUrls bool `yaml:"disable_show_urls"` } // Config contains the main config of gomuks. diff --git a/go.sum b/go.sum index f3bd493..1369868 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,7 @@ maunium.net/go/mautrix v0.5.5 h1:e0Pql1FdxoNUudx2oXo1gZHMrqIh5MC72cdXEPIrYLA= maunium.net/go/mautrix v0.5.5/go.mod h1:FLbMANzwqlsX2Fgm7SDe+E4I3wSa4UxJRKqS5wGkCwA= maunium.net/go/mautrix v0.7.0-rc.1 h1:DT7bNR9q+HlFs5Oo9IqmtWPkE4WPKZdRfIWRtlqkXtM= maunium.net/go/mautrix v0.7.0-rc.1/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo= +maunium.net/go/mautrix v0.7.0 h1:9Wxs5S4Wl4S99dbBwfLZYAe/sP7VKaFikw9Ocf88kfk= maunium.net/go/mauview v0.1.1 h1:wfTXyPx3LGAGpTskh+UbBv/QItUWnEpaneHmywoYnfY= maunium.net/go/mauview v0.1.1/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o= maunium.net/go/tcell v0.2.0 h1:1Q0kN3wCOGAIGu1r3QHADsjSUOPDylKREvCv3EzJpVg= diff --git a/ui/commands.go b/ui/commands.go index 9d38396..6b95ec3 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -722,6 +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"), } func makeUsage() string { @@ -764,6 +765,8 @@ func cmdToggle(cmd *Command) { val = &cmd.Config.Preferences.DisableNotifications case "unverified": val = &cmd.Config.SendToVerifiedOnly + case "showurls": + val = &cmd.Config.Preferences.DisableShowUrls 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 17edc74..3c111fc 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -29,6 +29,9 @@ import ( "maunium.net/go/mautrix/event" "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" @@ -40,6 +43,7 @@ var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([ type htmlParser struct { room *rooms.Room + Config *config.Config keepLinebreak bool } @@ -160,7 +164,7 @@ func (parser *htmlParser) headerToEntity(node *html.Node) Entity { }, Children: append( []Entity{NewTextEntity(strings.Repeat("#", int(node.Data[1]-'0')) + " ")}, - parser.nodeToEntities(node.FirstChild)... + parser.nodeToEntities(node.FirstChild)..., ), }).AdjustStyle(AdjustStyleBold) } @@ -170,16 +174,31 @@ func (parser *htmlParser) blockquoteToEntity(node *html.Node) Entity { } func (parser *htmlParser) linkToEntity(node *html.Node) Entity { + href := parser.getAttribute(node, "href") + entity := &ContainerEntity{ BaseEntity: &BaseEntity{ Tag: "a", }, Children: parser.nodeToEntities(node.FirstChild), } - href := parser.getAttribute(node, "href") + if len(href) == 0 { return entity } + + debug.Print("here value of parser.config.Preferences.ShowUrls") + + debug.Printf("%v", config.UserPreferences{}.DisableShowUrls) + showurls := config.UserPreferences{}.DisableShowUrls + + if showurls { + entity.Children = append( + []Entity{NewTextEntity("(" + href + ") ")}, + parser.nodeToEntities(node.FirstChild)..., + ) + } + match := matrixToURL.FindStringSubmatch(href) if len(match) == 2 { pillTarget := match[1] @@ -190,7 +209,7 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity { text.Style = text.Style.Foreground(widget.GetHashColor(pillTarget)) } entity.Children = []Entity{text} - /*} else if slash := strings.IndexRune(pillTarget, '/'); slash != -1 { + /*} else if slash := strings.IndexRune(pillTarget, '/'); slash != -1 { room := pillTarget[:slash] event := pillTarget[slash+1:]*/ } else if pillTarget[0] == '#' { diff --git a/ui/view-main.go b/ui/view-main.go index fc1b9c9..3bb8a2a 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -33,7 +33,7 @@ import ( "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/debug" - "maunium.net/go/gomuks/interface" + ifc "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/lib/notification" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/widget" From 0f77c17e9c7d11173098ba412d62239a505b18d5 Mon Sep 17 00:00:00 2001 From: ReK2 Date: Mon, 17 Aug 2020 00:57:25 +0200 Subject: [PATCH 2/4] 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) From 3439b94e0ee1f063f710197ec8303854332536b8 Mon Sep 17 00:00:00 2001 From: ReK2 Date: Mon, 17 Aug 2020 21:03:28 +0200 Subject: [PATCH 3/4] fix changes requested --- config/config.go | 4 ---- ui/commands.go | 4 ---- ui/messages/html/parser.go | 35 ++--------------------------------- 3 files changed, 2 insertions(+), 41 deletions(-) diff --git a/config/config.go b/config/config.go index 3f7ff1c..7afe21f 100644 --- a/config/config.go +++ b/config/config.go @@ -50,11 +50,7 @@ type UserPreferences struct { DisableHTML bool `yaml:"disable_html"` DisableDownloads bool `yaml:"disable_downloads"` DisableNotifications bool `yaml:"disable_notifications"` -<<<<<<< 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 e666cdc..d9469fc 100644 --- a/ui/commands.go +++ b/ui/commands.go @@ -766,11 +766,7 @@ func cmdToggle(cmd *Command) { case "unverified": val = &cmd.Config.SendToVerifiedOnly case "showurls": -<<<<<<< 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 342d2d2..fd5db96 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -41,16 +41,8 @@ import ( var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)") type htmlParser struct { -<<<<<<< HEAD - prefs *config.UserPreferences - room *rooms.Room - sameURL bool -======= - prefs *config.UserPreferences - room *rooms.Room - mautrixnolink bool - sameURL bool ->>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0 + prefs *config.UserPreferences + room *rooms.Room keepLinebreak bool } @@ -208,7 +200,6 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity { if len(entity.Children) == 1 { entity, ok := entity.Children[0].(*TextEntity) if ok && entity.Text == href { -<<<<<<< HEAD sameURL = true } } @@ -216,17 +207,6 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity { if !parser.prefs.DisableShowURLs && !parser.hasAttribute(node, "data-mautrix-no-link") && !sameURL { 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) @@ -435,24 +415,13 @@ const TabLength = 4 // Parse parses a HTML-formatted Matrix event into a UIMessage. 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, 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 From 15ed2a75b55aa3142e07b9e3353cffb40c3811bd Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 18 Aug 2020 18:01:19 +0300 Subject: [PATCH 4/4] Fix minor code style details --- go.sum | 1 - ui/messages/html/parser.go | 3 +-- ui/messages/parser.go | 2 +- ui/view-main.go | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.sum b/go.sum index 1369868..f3bd493 100644 --- a/go.sum +++ b/go.sum @@ -114,7 +114,6 @@ maunium.net/go/mautrix v0.5.5 h1:e0Pql1FdxoNUudx2oXo1gZHMrqIh5MC72cdXEPIrYLA= maunium.net/go/mautrix v0.5.5/go.mod h1:FLbMANzwqlsX2Fgm7SDe+E4I3wSa4UxJRKqS5wGkCwA= maunium.net/go/mautrix v0.7.0-rc.1 h1:DT7bNR9q+HlFs5Oo9IqmtWPkE4WPKZdRfIWRtlqkXtM= maunium.net/go/mautrix v0.7.0-rc.1/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo= -maunium.net/go/mautrix v0.7.0 h1:9Wxs5S4Wl4S99dbBwfLZYAe/sP7VKaFikw9Ocf88kfk= maunium.net/go/mauview v0.1.1 h1:wfTXyPx3LGAGpTskh+UbBv/QItUWnEpaneHmywoYnfY= maunium.net/go/mauview v0.1.1/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o= maunium.net/go/tcell v0.2.0 h1:1Q0kN3wCOGAIGu1r3QHADsjSUOPDylKREvCv3EzJpVg= diff --git a/ui/messages/html/parser.go b/ui/messages/html/parser.go index fd5db96..79f7729 100644 --- a/ui/messages/html/parser.go +++ b/ui/messages/html/parser.go @@ -31,9 +31,9 @@ import ( "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" - "maunium.net/go/gomuks/config" "maunium.net/go/tcell" + "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/widget" ) @@ -205,7 +205,6 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity { } if !parser.prefs.DisableShowURLs && !parser.hasAttribute(node, "data-mautrix-no-link") && !sameURL { - entity.Children = append(entity.Children, NewTextEntity(fmt.Sprintf(" (%s)", href))) } diff --git a/ui/messages/parser.go b/ui/messages/parser.go index a3ee6c5..e674d74 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" - ifc "maunium.net/go/gomuks/interface" + "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/matrix/muksevt" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/messages/html" diff --git a/ui/view-main.go b/ui/view-main.go index 3bb8a2a..fc1b9c9 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -33,7 +33,7 @@ import ( "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/debug" - ifc "maunium.net/go/gomuks/interface" + "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/lib/notification" "maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/ui/widget"