2018-04-13 23:34:25 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-04-13 23:34:25 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-04-13 23:34:25 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-04-13 23:34:25 +02:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-04-13 23:34:25 +02:00
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
package html
|
2018-04-13 23:34:25 +02:00
|
|
|
|
|
|
|
import (
|
2020-08-17 00:57:25 +02:00
|
|
|
"fmt"
|
2018-04-14 10:44:07 +02:00
|
|
|
"regexp"
|
2019-01-17 13:13:25 +01:00
|
|
|
"strconv"
|
2018-04-13 23:34:25 +02:00
|
|
|
"strings"
|
|
|
|
|
2019-04-07 19:13:23 +02:00
|
|
|
"github.com/alecthomas/chroma"
|
|
|
|
"github.com/alecthomas/chroma/lexers"
|
|
|
|
"github.com/alecthomas/chroma/styles"
|
2018-09-05 09:55:48 +02:00
|
|
|
"github.com/lucasb-eyer/go-colorful"
|
2018-06-01 23:44:21 +02:00
|
|
|
"golang.org/x/net/html"
|
2022-04-16 18:26:24 +02:00
|
|
|
"mvdan.cc/xurls/v2"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
2022-04-15 11:53:09 +02:00
|
|
|
"go.mau.fi/tcell"
|
|
|
|
|
2020-04-16 18:27:35 +02:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2020-08-15 06:59:02 +02:00
|
|
|
|
2020-08-18 17:01:19 +02:00
|
|
|
"maunium.net/go/gomuks/config"
|
2022-04-15 21:09:15 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/muksevt"
|
2018-04-14 10:44:07 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-04-13 23:34:25 +02:00
|
|
|
)
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
type htmlParser struct {
|
2020-08-17 21:03:28 +02:00
|
|
|
prefs *config.UserPreferences
|
|
|
|
room *rooms.Room
|
2022-04-15 21:09:15 +02:00
|
|
|
evt *muksevt.Event
|
2019-04-09 00:03:10 +02:00
|
|
|
|
2022-04-15 11:58:27 +02:00
|
|
|
preserveWhitespace bool
|
2022-04-15 21:09:15 +02:00
|
|
|
linkIDCounter int
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2018-11-13 23:28:53 +01:00
|
|
|
func AdjustStyleBold(style tcell.Style) tcell.Style {
|
2018-05-31 15:59:40 +02:00
|
|
|
return style.Bold(true)
|
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2018-11-13 23:28:53 +01:00
|
|
|
func AdjustStyleItalic(style tcell.Style) tcell.Style {
|
2018-05-31 15:59:40 +02:00
|
|
|
return style.Italic(true)
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:28:53 +01:00
|
|
|
func AdjustStyleUnderline(style tcell.Style) tcell.Style {
|
2018-05-31 15:59:40 +02:00
|
|
|
return style.Underline(true)
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:28:53 +01:00
|
|
|
func AdjustStyleStrikethrough(style tcell.Style) tcell.Style {
|
2022-04-15 11:53:09 +02:00
|
|
|
return style.StrikeThrough(true)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 21:09:15 +02:00
|
|
|
func AdjustStyleTextColor(color tcell.Color) AdjustStyleFunc {
|
2018-11-13 23:28:53 +01:00
|
|
|
return func(style tcell.Style) tcell.Style {
|
|
|
|
return style.Foreground(color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 21:09:15 +02:00
|
|
|
func AdjustStyleBackgroundColor(color tcell.Color) AdjustStyleFunc {
|
2019-03-26 21:09:10 +01:00
|
|
|
return func(style tcell.Style) tcell.Style {
|
|
|
|
return style.Background(color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 21:09:15 +02:00
|
|
|
func AdjustStyleLink(url, id string) AdjustStyleFunc {
|
|
|
|
return func(style tcell.Style) tcell.Style {
|
|
|
|
return style.Hyperlink(url, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 14:14:18 +02:00
|
|
|
func (parser *htmlParser) maybeGetAttribute(node *html.Node, attribute string) (string, bool) {
|
2018-06-01 23:28:21 +02:00
|
|
|
for _, attr := range node.Attr {
|
|
|
|
if attr.Key == attribute {
|
2022-04-15 14:14:18 +02:00
|
|
|
return attr.Val, true
|
2018-06-01 23:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 14:14:18 +02:00
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string {
|
|
|
|
val, _ := parser.maybeGetAttribute(node, attribute)
|
|
|
|
return val
|
2018-06-01 23:28:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-17 00:57:25 +02:00
|
|
|
func (parser *htmlParser) hasAttribute(node *html.Node, attribute string) bool {
|
2022-04-15 14:14:18 +02:00
|
|
|
_, ok := parser.maybeGetAttribute(node, attribute)
|
|
|
|
return ok
|
2020-08-17 00:57:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) listToEntity(node *html.Node) Entity {
|
2019-04-09 00:03:10 +02:00
|
|
|
children := parser.nodeToEntities(node.FirstChild)
|
2018-05-31 15:59:40 +02:00
|
|
|
ordered := node.Data == "ol"
|
2019-04-07 21:25:53 +02:00
|
|
|
start := 1
|
2018-05-31 15:59:40 +02:00
|
|
|
if ordered {
|
2019-04-07 21:25:53 +02:00
|
|
|
if startRaw := parser.getAttribute(node, "start"); len(startRaw) > 0 {
|
|
|
|
var err error
|
|
|
|
start, err = strconv.Atoi(startRaw)
|
|
|
|
if err != nil {
|
|
|
|
start = 1
|
|
|
|
}
|
2018-06-01 23:28:21 +02:00
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2019-04-07 21:25:53 +02:00
|
|
|
listItems := children[:0]
|
|
|
|
for _, child := range children {
|
|
|
|
if child.GetTag() == "li" {
|
|
|
|
listItems = append(listItems, child)
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2019-04-09 17:42:49 +02:00
|
|
|
return NewListEntity(ordered, start, listItems)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) basicFormatToEntity(node *html.Node) Entity {
|
2019-04-10 21:49:33 +02:00
|
|
|
entity := &ContainerEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: node.Data,
|
|
|
|
},
|
2019-04-09 00:03:10 +02:00
|
|
|
Children: parser.nodeToEntities(node.FirstChild),
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
switch node.Data {
|
|
|
|
case "b", "strong":
|
2022-04-15 14:14:18 +02:00
|
|
|
entity.AdjustStyle(AdjustStyleBold, AdjustStyleReasonNormal)
|
2018-05-31 15:59:40 +02:00
|
|
|
case "i", "em":
|
2022-04-15 14:14:18 +02:00
|
|
|
entity.AdjustStyle(AdjustStyleItalic, AdjustStyleReasonNormal)
|
2019-04-10 20:06:19 +02:00
|
|
|
case "s", "del", "strike":
|
2022-04-15 14:14:18 +02:00
|
|
|
entity.AdjustStyle(AdjustStyleStrikethrough, AdjustStyleReasonNormal)
|
2018-05-31 15:59:40 +02:00
|
|
|
case "u", "ins":
|
2022-04-15 14:14:18 +02:00
|
|
|
entity.AdjustStyle(AdjustStyleUnderline, AdjustStyleReasonNormal)
|
2022-04-10 22:02:49 +02:00
|
|
|
case "code":
|
|
|
|
bgColor := tcell.ColorDarkSlateGray
|
|
|
|
fgColor := tcell.ColorWhite
|
|
|
|
entity.AdjustStyle(AdjustStyleBackgroundColor(bgColor), AdjustStyleReasonNormal)
|
|
|
|
entity.AdjustStyle(AdjustStyleTextColor(fgColor), AdjustStyleReasonNormal)
|
2022-04-15 13:03:08 +02:00
|
|
|
case "font", "span":
|
2019-04-07 02:22:51 +02:00
|
|
|
fgColor, ok := parser.parseColor(node, "data-mx-color", "color")
|
|
|
|
if ok {
|
2022-04-15 14:14:18 +02:00
|
|
|
entity.AdjustStyle(AdjustStyleTextColor(fgColor), AdjustStyleReasonNormal)
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
|
|
|
bgColor, ok := parser.parseColor(node, "data-mx-bg-color", "background-color")
|
|
|
|
if ok {
|
2022-04-15 14:14:18 +02:00
|
|
|
entity.AdjustStyle(AdjustStyleBackgroundColor(bgColor), AdjustStyleReasonNormal)
|
|
|
|
}
|
|
|
|
spoilerReason, isSpoiler := parser.maybeGetAttribute(node, "data-mx-spoiler")
|
|
|
|
if isSpoiler {
|
|
|
|
return NewSpoilerEntity(entity, spoilerReason)
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2018-04-18 13:20:57 +02:00
|
|
|
}
|
2019-04-07 02:22:51 +02:00
|
|
|
return entity
|
2018-04-18 13:20:57 +02:00
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2019-03-26 21:09:10 +01:00
|
|
|
func (parser *htmlParser) parseColor(node *html.Node, mainName, altName string) (color tcell.Color, ok bool) {
|
|
|
|
hex := parser.getAttribute(node, mainName)
|
2018-06-11 18:38:19 +02:00
|
|
|
if len(hex) == 0 {
|
2019-03-26 21:09:10 +01:00
|
|
|
hex = parser.getAttribute(node, altName)
|
2018-11-13 23:28:53 +01:00
|
|
|
if len(hex) == 0 {
|
2019-03-26 21:09:10 +01:00
|
|
|
return
|
2018-11-13 23:28:53 +01:00
|
|
|
}
|
2018-06-11 18:38:19 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 21:09:10 +01:00
|
|
|
cful, err := colorful.Hex(hex)
|
2018-06-11 18:38:19 +02:00
|
|
|
if err != nil {
|
2019-04-09 17:42:49 +02:00
|
|
|
color2, found := colorMap[strings.ToLower(hex)]
|
2019-03-26 21:09:10 +01:00
|
|
|
if !found {
|
|
|
|
return
|
2018-11-13 23:28:53 +01:00
|
|
|
}
|
2019-03-26 21:09:10 +01:00
|
|
|
cful, _ = colorful.MakeColor(color2)
|
2018-06-11 18:38:19 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 21:09:10 +01:00
|
|
|
r, g, b := cful.RGB255()
|
|
|
|
return tcell.NewRGBColor(int32(r), int32(g), int32(b)), true
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) headerToEntity(node *html.Node) Entity {
|
2019-04-10 21:49:33 +02:00
|
|
|
return (&ContainerEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: node.Data,
|
|
|
|
},
|
|
|
|
Children: append(
|
|
|
|
[]Entity{NewTextEntity(strings.Repeat("#", int(node.Data[1]-'0')) + " ")},
|
2020-08-15 06:59:02 +02:00
|
|
|
parser.nodeToEntities(node.FirstChild)...,
|
2019-04-10 21:49:33 +02:00
|
|
|
),
|
2022-04-15 14:14:18 +02:00
|
|
|
}).AdjustStyle(AdjustStyleBold, AdjustStyleReasonNormal)
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) blockquoteToEntity(node *html.Node) Entity {
|
|
|
|
return NewBlockquoteEntity(parser.nodeToEntities(node.FirstChild))
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2019-04-14 23:34:48 +02:00
|
|
|
func (parser *htmlParser) linkToEntity(node *html.Node) Entity {
|
2020-08-17 00:57:25 +02:00
|
|
|
sameURL := false
|
2020-08-15 06:59:02 +02:00
|
|
|
href := parser.getAttribute(node, "href")
|
|
|
|
|
2019-04-14 23:34:48 +02:00
|
|
|
entity := &ContainerEntity{
|
2019-04-10 21:49:33 +02:00
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: "a",
|
|
|
|
},
|
2019-04-09 00:03:10 +02:00
|
|
|
Children: parser.nodeToEntities(node.FirstChild),
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2020-08-15 06:59:02 +02:00
|
|
|
|
2022-04-15 21:09:15 +02:00
|
|
|
if len(href) == 0 {
|
2019-04-14 23:34:48 +02:00
|
|
|
return entity
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2020-08-15 06:59:02 +02:00
|
|
|
|
2020-08-17 00:57:25 +02:00
|
|
|
if len(entity.Children) == 1 {
|
|
|
|
entity, ok := entity.Children[0].(*TextEntity)
|
|
|
|
if ok && entity.Text == href {
|
|
|
|
sameURL = true
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 06:59:02 +02:00
|
|
|
|
2022-04-15 21:09:15 +02:00
|
|
|
matrixURI, _ := id.ParseMatrixURIOrMatrixToURL(href)
|
|
|
|
if matrixURI != nil && (matrixURI.Sigil1 == '@' || matrixURI.Sigil1 == '#') && matrixURI.Sigil2 == 0 {
|
|
|
|
text := NewTextEntity(matrixURI.PrimaryIdentifier())
|
|
|
|
if matrixURI.Sigil1 == '@' {
|
|
|
|
if member := parser.room.GetMember(matrixURI.UserID()); member != nil {
|
2019-04-14 23:34:48 +02:00
|
|
|
text.Text = member.Displayname
|
2022-04-15 21:09:15 +02:00
|
|
|
text.Style = text.Style.Foreground(widget.GetHashColor(matrixURI.UserID()))
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2019-04-14 23:34:48 +02:00
|
|
|
entity.Children = []Entity{text}
|
2022-04-15 21:09:15 +02:00
|
|
|
} else if matrixURI.Sigil1 == '#' {
|
2019-04-14 23:34:48 +02:00
|
|
|
entity.Children = []Entity{text}
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2022-04-16 18:59:34 +02:00
|
|
|
} else if parser.prefs.EnableInlineURLs() {
|
2022-04-15 21:09:15 +02:00
|
|
|
linkID := fmt.Sprintf("%s-%d", parser.evt.ID, parser.linkIDCounter)
|
|
|
|
parser.linkIDCounter++
|
|
|
|
entity.AdjustStyle(AdjustStyleLink(href, linkID), AdjustStyleReasonNormal)
|
|
|
|
} else if !sameURL && !parser.prefs.DisableShowURLs && !parser.hasAttribute(node, "data-mautrix-exclude-plaintext") {
|
|
|
|
entity.Children = append(entity.Children, NewTextEntity(fmt.Sprintf(" (%s)", href)))
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2019-04-14 23:34:48 +02:00
|
|
|
return entity
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) imageToEntity(node *html.Node) Entity {
|
2019-04-07 19:13:23 +02:00
|
|
|
alt := parser.getAttribute(node, "alt")
|
|
|
|
if len(alt) == 0 {
|
|
|
|
alt = parser.getAttribute(node, "title")
|
|
|
|
if len(alt) == 0 {
|
|
|
|
alt = "[inline image]"
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:49:33 +02:00
|
|
|
entity := &TextEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: "img",
|
|
|
|
},
|
2019-04-07 19:13:23 +02:00
|
|
|
Text: alt,
|
|
|
|
}
|
|
|
|
// TODO add click action and underline on hover for inline images
|
|
|
|
return entity
|
|
|
|
}
|
|
|
|
|
|
|
|
func colourToColor(colour chroma.Colour) tcell.Color {
|
|
|
|
if !colour.IsSet() {
|
|
|
|
return tcell.ColorDefault
|
|
|
|
}
|
|
|
|
return tcell.NewRGBColor(int32(colour.Red()), int32(colour.Green()), int32(colour.Blue()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func styleEntryToStyle(se chroma.StyleEntry) tcell.Style {
|
|
|
|
return tcell.StyleDefault.
|
|
|
|
Bold(se.Bold == chroma.Yes).
|
|
|
|
Italic(se.Italic == chroma.Yes).
|
|
|
|
Underline(se.Underline == chroma.Yes).
|
|
|
|
Foreground(colourToColor(se.Colour)).
|
|
|
|
Background(colourToColor(se.Background))
|
|
|
|
}
|
|
|
|
|
2020-09-04 17:09:41 +02:00
|
|
|
func tokenToTextEntity(style *chroma.Style, token *chroma.Token) *TextEntity {
|
|
|
|
return &TextEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: token.Type.String(),
|
|
|
|
Style: styleEntryToStyle(style.Get(token.Type)),
|
|
|
|
DefaultHeight: 1,
|
|
|
|
},
|
|
|
|
Text: token.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) syntaxHighlight(text, language string) Entity {
|
2019-04-08 23:59:56 +02:00
|
|
|
lexer := lexers.Get(strings.ToLower(language))
|
2019-04-07 19:13:23 +02:00
|
|
|
if lexer == nil {
|
2022-10-10 12:36:51 +02:00
|
|
|
lexer = lexers.Get("plaintext")
|
2019-04-07 19:13:23 +02:00
|
|
|
}
|
|
|
|
iter, err := lexer.Tokenise(nil, text)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-08 23:59:56 +02:00
|
|
|
// TODO allow changing theme
|
2019-04-07 19:13:23 +02:00
|
|
|
style := styles.SolarizedDark
|
2019-04-08 23:59:56 +02:00
|
|
|
|
2019-04-07 19:13:23 +02:00
|
|
|
tokens := iter.Tokens()
|
2020-09-04 17:09:41 +02:00
|
|
|
|
|
|
|
var children []Entity
|
|
|
|
for _, token := range tokens {
|
2019-04-07 19:13:23 +02:00
|
|
|
if token.Value == "\n" {
|
2020-09-04 17:09:41 +02:00
|
|
|
children = append(children, NewBreakEntity())
|
|
|
|
|
|
|
|
} else if token.Type.String() == "CommentSingle" {
|
|
|
|
children = append(children, tokenToTextEntity(style, &token))
|
|
|
|
children = append(children, NewBreakEntity())
|
|
|
|
|
|
|
|
} else if token.Type.String() == "CommentMultiline" {
|
|
|
|
lines := strings.Split(token.Value, "\n")
|
|
|
|
for i, line := range lines {
|
|
|
|
t := token.Clone()
|
|
|
|
t.Value = line
|
|
|
|
children = append(children, tokenToTextEntity(style, &t))
|
|
|
|
|
|
|
|
if i < len(lines)-1 {
|
|
|
|
children = append(children, NewBreakEntity())
|
|
|
|
}
|
2019-04-07 19:13:23 +02:00
|
|
|
}
|
2020-09-04 17:09:41 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
children = append(children, tokenToTextEntity(style, &token))
|
2019-04-07 19:13:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 17:09:41 +02:00
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
return NewCodeBlockEntity(children, styleEntryToStyle(style.Get(chroma.Background)))
|
2019-04-07 19:13:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) codeblockToEntity(node *html.Node) Entity {
|
2019-04-08 23:59:56 +02:00
|
|
|
lang := "plaintext"
|
2019-04-07 19:13:23 +02:00
|
|
|
// TODO allow disabling syntax highlighting
|
2020-10-20 12:29:50 +02:00
|
|
|
if node.FirstChild != nil && node.FirstChild.Type == html.ElementNode && node.FirstChild.Data == "code" {
|
2019-04-08 23:59:56 +02:00
|
|
|
node = node.FirstChild
|
|
|
|
attr := parser.getAttribute(node, "class")
|
2019-04-07 19:13:23 +02:00
|
|
|
for _, class := range strings.Split(attr, " ") {
|
|
|
|
if strings.HasPrefix(class, "language-") {
|
|
|
|
lang = class[len("language-"):]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2022-04-15 11:58:27 +02:00
|
|
|
parser.preserveWhitespace = true
|
2019-04-10 21:49:33 +02:00
|
|
|
text := (&ContainerEntity{
|
2019-04-09 00:03:10 +02:00
|
|
|
Children: parser.nodeToEntities(node.FirstChild),
|
2019-04-08 23:59:56 +02:00
|
|
|
}).PlainText()
|
2022-04-15 11:58:27 +02:00
|
|
|
parser.preserveWhitespace = false
|
2019-04-08 23:59:56 +02:00
|
|
|
return parser.syntaxHighlight(text, lang)
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) tagNodeToEntity(node *html.Node) Entity {
|
2018-05-31 15:59:40 +02:00
|
|
|
switch node.Data {
|
|
|
|
case "blockquote":
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.blockquoteToEntity(node)
|
2018-05-31 15:59:40 +02:00
|
|
|
case "ol", "ul":
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.listToEntity(node)
|
2018-04-14 10:44:07 +02:00
|
|
|
case "h1", "h2", "h3", "h4", "h5", "h6":
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.headerToEntity(node)
|
2018-05-31 15:59:40 +02:00
|
|
|
case "br":
|
2019-04-09 17:42:49 +02:00
|
|
|
return NewBreakEntity()
|
2022-04-10 22:02:49 +02:00
|
|
|
case "b", "strong", "i", "em", "s", "strike", "del", "u", "ins", "font", "span", "code":
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.basicFormatToEntity(node)
|
2018-04-14 10:44:07 +02:00
|
|
|
case "a":
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.linkToEntity(node)
|
2019-04-07 19:13:23 +02:00
|
|
|
case "img":
|
|
|
|
return parser.imageToEntity(node)
|
2018-05-31 15:59:40 +02:00
|
|
|
case "pre":
|
2019-04-07 02:22:51 +02:00
|
|
|
return parser.codeblockToEntity(node)
|
2019-04-10 20:06:19 +02:00
|
|
|
case "hr":
|
|
|
|
return NewHorizontalLineEntity()
|
|
|
|
case "mx-reply":
|
|
|
|
return nil
|
2018-05-31 15:59:40 +02:00
|
|
|
default:
|
2019-04-10 21:49:33 +02:00
|
|
|
return &ContainerEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: node.Data,
|
|
|
|
Block: parser.isBlockTag(node.Data),
|
|
|
|
},
|
2019-04-09 00:03:10 +02:00
|
|
|
Children: parser.nodeToEntities(node.FirstChild),
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2022-04-15 11:58:27 +02:00
|
|
|
var spaces = regexp.MustCompile("\\s+")
|
|
|
|
|
2022-04-15 22:44:59 +02:00
|
|
|
func TextToEntity(text string, eventID id.EventID, linkify bool) Entity {
|
2022-04-15 22:28:23 +02:00
|
|
|
if len(text) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-15 22:44:59 +02:00
|
|
|
if !linkify {
|
|
|
|
return NewTextEntity(text)
|
|
|
|
}
|
2022-04-16 18:26:24 +02:00
|
|
|
indices := xurls.Strict().FindAllStringIndex(text, -1)
|
2022-04-15 22:28:23 +02:00
|
|
|
if len(indices) == 0 {
|
|
|
|
return NewTextEntity(text)
|
|
|
|
}
|
|
|
|
ent := &ContainerEntity{
|
|
|
|
BaseEntity: &BaseEntity{Tag: "span"},
|
|
|
|
}
|
|
|
|
var lastEnd int
|
|
|
|
for i, item := range indices {
|
|
|
|
start, end := item[0], item[1]
|
|
|
|
if start > lastEnd {
|
|
|
|
ent.Children = append(ent.Children, NewTextEntity(text[lastEnd:start]))
|
|
|
|
}
|
|
|
|
link := text[start:end]
|
|
|
|
linkID := fmt.Sprintf("%s-%d", eventID, i)
|
|
|
|
ent.Children = append(ent.Children, NewTextEntity(link).AdjustStyle(AdjustStyleLink(link, linkID), AdjustStyleReasonNormal))
|
|
|
|
lastEnd = end
|
|
|
|
}
|
|
|
|
if lastEnd < len(text) {
|
|
|
|
ent.Children = append(ent.Children, NewTextEntity(text[lastEnd:]))
|
|
|
|
}
|
|
|
|
return ent
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) singleNodeToEntity(node *html.Node) Entity {
|
2018-05-31 15:59:40 +02:00
|
|
|
switch node.Type {
|
|
|
|
case html.TextNode:
|
2022-04-15 11:58:27 +02:00
|
|
|
if !parser.preserveWhitespace {
|
2022-04-15 19:29:23 +02:00
|
|
|
node.Data = strings.ReplaceAll(node.Data, "\n", "")
|
2022-04-15 11:58:27 +02:00
|
|
|
node.Data = spaces.ReplaceAllLiteralString(node.Data, " ")
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2022-04-16 18:59:34 +02:00
|
|
|
return TextToEntity(node.Data, parser.evt.ID, parser.prefs.EnableInlineURLs())
|
2018-05-31 15:59:40 +02:00
|
|
|
case html.ElementNode:
|
2022-04-15 13:03:08 +02:00
|
|
|
parsed := parser.tagNodeToEntity(node)
|
|
|
|
if parsed != nil && !parsed.IsBlock() && parsed.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return parsed
|
2018-05-31 15:59:40 +02:00
|
|
|
case html.DocumentNode:
|
2019-04-07 17:21:38 +02:00
|
|
|
if node.FirstChild.Data == "html" && node.FirstChild.NextSibling == nil {
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.singleNodeToEntity(node.FirstChild)
|
2019-04-07 17:21:38 +02:00
|
|
|
}
|
2019-04-10 21:49:33 +02:00
|
|
|
return &ContainerEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: "html",
|
|
|
|
Block: true,
|
|
|
|
},
|
2019-04-09 00:03:10 +02:00
|
|
|
Children: parser.nodeToEntities(node.FirstChild),
|
2019-04-07 02:22:51 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
default:
|
2019-04-07 02:22:51 +02:00
|
|
|
return nil
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) nodeToEntities(node *html.Node) (entities []Entity) {
|
2018-05-31 15:59:40 +02:00
|
|
|
for ; node != nil; node = node.NextSibling {
|
2019-04-09 00:03:10 +02:00
|
|
|
if entity := parser.singleNodeToEntity(node); entity != nil {
|
2019-04-07 02:22:51 +02:00
|
|
|
entities = append(entities, entity)
|
|
|
|
}
|
2018-04-16 11:04:00 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:25:53 +02:00
|
|
|
var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "li", "pre", "blockquote", "div", "hr", "table"}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) isBlockTag(tag string) bool {
|
|
|
|
for _, blockTag := range BlockTags {
|
|
|
|
if tag == blockTag {
|
|
|
|
return true
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
func (parser *htmlParser) Parse(htmlData string) Entity {
|
2018-05-31 15:59:40 +02:00
|
|
|
node, _ := html.Parse(strings.NewReader(htmlData))
|
2019-04-10 16:08:39 +02:00
|
|
|
bodyNode := node.FirstChild.FirstChild
|
|
|
|
for bodyNode != nil && (bodyNode.Type != html.ElementNode || bodyNode.Data != "body") {
|
|
|
|
bodyNode = bodyNode.NextSibling
|
|
|
|
}
|
|
|
|
if bodyNode != nil {
|
|
|
|
return parser.singleNodeToEntity(bodyNode)
|
|
|
|
}
|
2020-08-17 00:57:25 +02:00
|
|
|
|
2019-04-09 00:03:10 +02:00
|
|
|
return parser.singleNodeToEntity(node)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:42:49 +02:00
|
|
|
const TabLength = 4
|
|
|
|
|
|
|
|
// Parse parses a HTML-formatted Matrix event into a UIMessage.
|
2022-04-15 21:09:15 +02:00
|
|
|
func Parse(prefs *config.UserPreferences, room *rooms.Room, content *event.MessageEventContent, evt *muksevt.Event, senderDisplayname string) Entity {
|
2020-04-19 14:00:49 +02:00
|
|
|
htmlData := content.FormattedBody
|
2020-08-17 00:57:25 +02:00
|
|
|
|
2020-04-19 14:00:49 +02:00
|
|
|
if content.Format != event.FormatHTML {
|
|
|
|
htmlData = strings.Replace(html.EscapeString(content.Body), "\n", "<br/>", -1)
|
2019-04-09 17:42:49 +02:00
|
|
|
}
|
|
|
|
htmlData = strings.Replace(htmlData, "\t", strings.Repeat(" ", TabLength), -1)
|
2018-04-13 23:34:25 +02:00
|
|
|
|
2022-04-15 21:09:15 +02:00
|
|
|
parser := htmlParser{room: room, prefs: prefs, evt: evt}
|
2019-04-07 02:22:51 +02:00
|
|
|
root := parser.Parse(htmlData)
|
2022-04-19 11:01:56 +02:00
|
|
|
if root == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
beRoot, ok := root.(*ContainerEntity)
|
|
|
|
if ok {
|
|
|
|
beRoot.Block = false
|
|
|
|
if len(beRoot.Children) > 0 {
|
|
|
|
beChild, ok := beRoot.Children[0].(*ContainerEntity)
|
|
|
|
if ok && beChild.Tag == "p" {
|
|
|
|
// Hacky fix for m.emote
|
|
|
|
beChild.Block = false
|
|
|
|
}
|
2019-04-10 16:08:39 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 23:34:25 +02:00
|
|
|
|
2020-04-19 14:00:49 +02:00
|
|
|
if content.MsgType == event.MsgEmote {
|
2019-04-10 21:49:33 +02:00
|
|
|
root = &ContainerEntity{
|
|
|
|
BaseEntity: &BaseEntity{
|
|
|
|
Tag: "emote",
|
|
|
|
},
|
2019-04-09 17:42:49 +02:00
|
|
|
Children: []Entity{
|
|
|
|
NewTextEntity("* "),
|
2022-04-15 21:09:15 +02:00
|
|
|
NewTextEntity(senderDisplayname).AdjustStyle(AdjustStyleTextColor(widget.GetHashColor(evt.Sender)), AdjustStyleReasonNormal),
|
2019-04-09 17:42:49 +02:00
|
|
|
NewTextEntity(" "),
|
2019-04-07 02:22:51 +02:00
|
|
|
root,
|
|
|
|
},
|
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2019-04-07 02:22:51 +02:00
|
|
|
return root
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|