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

This commit is contained in:
ReK2 2020-08-17 00:57:25 +02:00
parent 2a1329d981
commit 0f77c17e9c
4 changed files with 65 additions and 14 deletions

View File

@ -50,7 +50,11 @@ type UserPreferences struct {
DisableHTML bool `yaml:"disable_html"` DisableHTML bool `yaml:"disable_html"`
DisableDownloads bool `yaml:"disable_downloads"` DisableDownloads bool `yaml:"disable_downloads"`
DisableNotifications bool `yaml:"disable_notifications"` 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. // Config contains the main config of gomuks.

View File

@ -722,7 +722,7 @@ var toggleMsg = map[string]ToggleMessage{
"downloads": SimpleToggleMessage("automatic downloads"), "downloads": SimpleToggleMessage("automatic downloads"),
"notifications": SimpleToggleMessage("desktop notifications"), "notifications": SimpleToggleMessage("desktop notifications"),
"unverified": SimpleToggleMessage("sending messages to unverified devices"), "unverified": SimpleToggleMessage("sending messages to unverified devices"),
"showurls": SimpleToggleMessage("Show text URL"), "showurls": SimpleToggleMessage("show URLs in text format"),
} }
func makeUsage() string { func makeUsage() string {
@ -766,7 +766,11 @@ func cmdToggle(cmd *Command) {
case "unverified": case "unverified":
val = &cmd.Config.SendToVerifiedOnly val = &cmd.Config.SendToVerifiedOnly
case "showurls": case "showurls":
val = &cmd.Config.Preferences.DisableShowUrls <<<<<<< HEAD
val = &cmd.Config.Preferences.DisableShowURLs
=======
val = &cmd.Config.Preferences.DisableShowurls
>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0
default: default:
cmd.Reply("Unknown toggle %s. Use /toggle without arguments for a list of togglable things.", thing) cmd.Reply("Unknown toggle %s. Use /toggle without arguments for a list of togglable things.", thing)
return return

View File

@ -17,6 +17,7 @@
package html package html
import ( import (
"fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -31,7 +32,6 @@ import (
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"maunium.net/go/gomuks/config" "maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
@ -41,9 +41,17 @@ import (
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)") var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
type htmlParser struct { 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 keepLinebreak bool
} }
@ -84,6 +92,15 @@ func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string
return "" 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 { func (parser *htmlParser) listToEntity(node *html.Node) Entity {
children := parser.nodeToEntities(node.FirstChild) children := parser.nodeToEntities(node.FirstChild)
ordered := node.Data == "ol" ordered := node.Data == "ol"
@ -174,6 +191,7 @@ func (parser *htmlParser) blockquoteToEntity(node *html.Node) Entity {
} }
func (parser *htmlParser) linkToEntity(node *html.Node) Entity { func (parser *htmlParser) linkToEntity(node *html.Node) Entity {
sameURL := false
href := parser.getAttribute(node, "href") href := parser.getAttribute(node, "href")
entity := &ContainerEntity{ entity := &ContainerEntity{
@ -187,16 +205,28 @@ func (parser *htmlParser) linkToEntity(node *html.Node) Entity {
return 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) if !parser.prefs.DisableShowURLs && !parser.hasAttribute(node, "data-mautrix-no-link") && !sameURL {
showurls := config.UserPreferences{}.DisableShowUrls
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.Children = append(
[]Entity{NewTextEntity("(" + href + ") ")}, []Entity{NewTextEntity("(" + href + ") ")},
parser.nodeToEntities(node.FirstChild)..., parser.nodeToEntities(node.FirstChild)...,
) )
>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0
} }
match := matrixToURL.FindStringSubmatch(href) match := matrixToURL.FindStringSubmatch(href)
@ -396,20 +426,33 @@ func (parser *htmlParser) Parse(htmlData string) Entity {
if bodyNode != nil { if bodyNode != nil {
return parser.singleNodeToEntity(bodyNode) return parser.singleNodeToEntity(bodyNode)
} }
return parser.singleNodeToEntity(node) return parser.singleNodeToEntity(node)
} }
const TabLength = 4 const TabLength = 4
// Parse parses a HTML-formatted Matrix event into a UIMessage. // 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 htmlData := content.FormattedBody
<<<<<<< HEAD
=======
DataMautrixNoLink := false
>>>>>>> 67c0262587404c2b2912e934092dacdba7cc2ed0
if content.Format != event.FormatHTML { if content.Format != event.FormatHTML {
htmlData = strings.Replace(html.EscapeString(content.Body), "\n", "<br/>", -1) htmlData = strings.Replace(html.EscapeString(content.Body), "\n", "<br/>", -1)
} }
htmlData = strings.Replace(htmlData, "\t", strings.Repeat(" ", TabLength), -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) root := parser.Parse(htmlData)
beRoot := root.(*ContainerEntity) beRoot := root.(*ContainerEntity)
beRoot.Block = false beRoot.Block = false

View File

@ -25,7 +25,7 @@ import (
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/gomuks/debug" "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/muksevt"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/gomuks/ui/messages/html" "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 { switch content.MsgType {
case event.MsgText, event.MsgNotice, event.MsgEmote: case event.MsgText, event.MsgNotice, event.MsgEmote:
if content.Format == event.FormatHTML { 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) content.Body = strings.Replace(content.Body, "\t", " ", -1)
return NewTextMessage(evt, displayname, content.Body) return NewTextMessage(evt, displayname, content.Body)