Merge pull request #189 from r3k2/master
Allow for URL to be visible as text
This commit is contained in:
commit
0e1b69e4ed
@ -50,6 +50,7 @@ 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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config contains the main config of gomuks.
|
// Config contains the main config of gomuks.
|
||||||
|
@ -722,6 +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 URLs in text format"),
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeUsage() string {
|
func makeUsage() string {
|
||||||
@ -764,6 +765,8 @@ func cmdToggle(cmd *Command) {
|
|||||||
val = &cmd.Config.Preferences.DisableNotifications
|
val = &cmd.Config.Preferences.DisableNotifications
|
||||||
case "unverified":
|
case "unverified":
|
||||||
val = &cmd.Config.SendToVerifiedOnly
|
val = &cmd.Config.SendToVerifiedOnly
|
||||||
|
case "showurls":
|
||||||
|
val = &cmd.Config.Preferences.DisableShowURLs
|
||||||
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
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package html
|
package html
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -29,8 +30,10 @@ import (
|
|||||||
|
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"maunium.net/go/tcell"
|
"maunium.net/go/tcell"
|
||||||
|
|
||||||
|
"maunium.net/go/gomuks/config"
|
||||||
"maunium.net/go/gomuks/matrix/rooms"
|
"maunium.net/go/gomuks/matrix/rooms"
|
||||||
"maunium.net/go/gomuks/ui/widget"
|
"maunium.net/go/gomuks/ui/widget"
|
||||||
)
|
)
|
||||||
@ -38,6 +41,7 @@ import (
|
|||||||
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
|
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
|
||||||
|
|
||||||
type htmlParser struct {
|
type htmlParser struct {
|
||||||
|
prefs *config.UserPreferences
|
||||||
room *rooms.Room
|
room *rooms.Room
|
||||||
|
|
||||||
keepLinebreak bool
|
keepLinebreak bool
|
||||||
@ -80,6 +84,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"
|
||||||
@ -160,7 +173,7 @@ func (parser *htmlParser) headerToEntity(node *html.Node) Entity {
|
|||||||
},
|
},
|
||||||
Children: append(
|
Children: append(
|
||||||
[]Entity{NewTextEntity(strings.Repeat("#", int(node.Data[1]-'0')) + " ")},
|
[]Entity{NewTextEntity(strings.Repeat("#", int(node.Data[1]-'0')) + " ")},
|
||||||
parser.nodeToEntities(node.FirstChild)...
|
parser.nodeToEntities(node.FirstChild)...,
|
||||||
),
|
),
|
||||||
}).AdjustStyle(AdjustStyleBold)
|
}).AdjustStyle(AdjustStyleBold)
|
||||||
}
|
}
|
||||||
@ -170,16 +183,31 @@ 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")
|
||||||
|
|
||||||
entity := &ContainerEntity{
|
entity := &ContainerEntity{
|
||||||
BaseEntity: &BaseEntity{
|
BaseEntity: &BaseEntity{
|
||||||
Tag: "a",
|
Tag: "a",
|
||||||
},
|
},
|
||||||
Children: parser.nodeToEntities(node.FirstChild),
|
Children: parser.nodeToEntities(node.FirstChild),
|
||||||
}
|
}
|
||||||
href := parser.getAttribute(node, "href")
|
|
||||||
if len(href) == 0 {
|
if len(href) == 0 {
|
||||||
return entity
|
return entity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(entity.Children) == 1 {
|
||||||
|
entity, ok := entity.Children[0].(*TextEntity)
|
||||||
|
if ok && entity.Text == href {
|
||||||
|
sameURL = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !parser.prefs.DisableShowURLs && !parser.hasAttribute(node, "data-mautrix-no-link") && !sameURL {
|
||||||
|
entity.Children = append(entity.Children, NewTextEntity(fmt.Sprintf(" (%s)", href)))
|
||||||
|
}
|
||||||
|
|
||||||
match := matrixToURL.FindStringSubmatch(href)
|
match := matrixToURL.FindStringSubmatch(href)
|
||||||
if len(match) == 2 {
|
if len(match) == 2 {
|
||||||
pillTarget := match[1]
|
pillTarget := match[1]
|
||||||
@ -377,20 +405,22 @@ 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
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
parser := htmlParser{room: room}
|
parser := htmlParser{room: room, prefs: prefs}
|
||||||
root := parser.Parse(htmlData)
|
root := parser.Parse(htmlData)
|
||||||
beRoot := root.(*ContainerEntity)
|
beRoot := root.(*ContainerEntity)
|
||||||
beRoot.Block = false
|
beRoot.Block = false
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user