2018-04-13 23:34:25 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2019-01-17 13:13:25 +01:00
|
|
|
// Copyright (C) 2019 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
|
|
|
|
2018-04-14 10:50:18 +02:00
|
|
|
package parser
|
2018-04-13 23:34:25 +02:00
|
|
|
|
|
|
|
import (
|
2018-04-14 10:44:07 +02:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"regexp"
|
2019-01-17 13:13:25 +01:00
|
|
|
"strconv"
|
2018-04-13 23:34:25 +02:00
|
|
|
"strings"
|
|
|
|
|
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"
|
2019-01-17 13:13:25 +01:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/tcell"
|
|
|
|
|
2018-04-14 10:44:07 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-04-13 23:34:25 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages/tstring"
|
2018-04-14 10:44:07 +02:00
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-04-13 23:34:25 +02:00
|
|
|
)
|
|
|
|
|
2018-04-14 10:44:07 +02:00
|
|
|
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
|
2018-04-13 23:34:25 +02:00
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
type htmlParser struct {
|
|
|
|
room *rooms.Room
|
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
type taggedTString struct {
|
|
|
|
tstring.TString
|
|
|
|
tag string
|
|
|
|
}
|
2018-04-18 13:20:57 +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 {
|
2018-05-31 15:59:40 +02:00
|
|
|
return style.Strikethrough(true)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 21:09:10 +01:00
|
|
|
func AdjustStyleTextColor(color tcell.Color) func(tcell.Style) tcell.Style {
|
2018-11-13 23:28:53 +01:00
|
|
|
return func(style tcell.Style) tcell.Style {
|
|
|
|
return style.Foreground(color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 21:09:10 +01:00
|
|
|
func AdjustStyleBackgroundColor(color tcell.Color) func(tcell.Style) tcell.Style {
|
|
|
|
return func(style tcell.Style) tcell.Style {
|
|
|
|
return style.Background(color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 23:28:21 +02:00
|
|
|
func (parser *htmlParser) getAttribute(node *html.Node, attribute string) string {
|
|
|
|
for _, attr := range node.Attr {
|
|
|
|
if attr.Key == attribute {
|
|
|
|
return attr.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func digits(num int) int {
|
2018-11-13 23:00:35 +01:00
|
|
|
if num <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2018-06-01 23:44:21 +02:00
|
|
|
return int(math.Floor(math.Log10(float64(num))) + 1)
|
2018-06-01 23:28:21 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
ordered := node.Data == "ol"
|
|
|
|
taggedChildren := parser.nodeToTaggedTStrings(node.FirstChild, stripLinebreak)
|
2018-06-01 23:28:21 +02:00
|
|
|
counter := 1
|
|
|
|
indentLength := 0
|
2018-05-31 15:59:40 +02:00
|
|
|
if ordered {
|
2018-06-01 23:28:21 +02:00
|
|
|
start := parser.getAttribute(node, "start")
|
|
|
|
if len(start) > 0 {
|
|
|
|
counter, _ = strconv.Atoi(start)
|
|
|
|
}
|
|
|
|
|
|
|
|
longestIndex := (counter - 1) + len(taggedChildren)
|
|
|
|
indentLength = digits(longestIndex)
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-06-01 23:28:21 +02:00
|
|
|
indent := strings.Repeat(" ", indentLength+2)
|
2018-05-31 15:59:40 +02:00
|
|
|
var children []tstring.TString
|
|
|
|
for _, child := range taggedChildren {
|
|
|
|
if child.tag != "li" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var prefix string
|
|
|
|
if ordered {
|
2018-06-01 23:28:21 +02:00
|
|
|
indexPadding := indentLength - digits(counter)
|
|
|
|
prefix = fmt.Sprintf("%d. %s", counter, strings.Repeat(" ", indexPadding))
|
2018-05-31 15:59:40 +02:00
|
|
|
} else {
|
|
|
|
prefix = "● "
|
|
|
|
}
|
|
|
|
str := child.TString.Prepend(prefix)
|
|
|
|
counter++
|
|
|
|
parts := str.Split('\n')
|
|
|
|
for i, part := range parts[1:] {
|
2018-06-01 23:28:21 +02:00
|
|
|
parts[i+1] = part.Prepend(indent)
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
|
|
|
str = tstring.Join(parts, "\n")
|
|
|
|
children = append(children, str)
|
|
|
|
}
|
|
|
|
return tstring.Join(children, "\n")
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) basicFormatToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak)
|
|
|
|
switch node.Data {
|
|
|
|
case "b", "strong":
|
|
|
|
str.AdjustStyleFull(AdjustStyleBold)
|
|
|
|
case "i", "em":
|
|
|
|
str.AdjustStyleFull(AdjustStyleItalic)
|
|
|
|
case "s", "del":
|
|
|
|
str.AdjustStyleFull(AdjustStyleStrikethrough)
|
|
|
|
case "u", "ins":
|
|
|
|
str.AdjustStyleFull(AdjustStyleUnderline)
|
2018-04-18 13:20:57 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return str
|
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-03-26 21:09:10 +01:00
|
|
|
color2, found := ColorMap[strings.ToLower(hex)]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *htmlParser) fontToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak)
|
|
|
|
fgColor, ok := parser.parseColor(node, "data-mx-color", "color")
|
|
|
|
if ok {
|
|
|
|
str.AdjustStyleFull(AdjustStyleTextColor(fgColor))
|
|
|
|
}
|
|
|
|
bgColor, ok := parser.parseColor(node, "data-mx-bg-color", "background-color")
|
|
|
|
if ok {
|
|
|
|
str.AdjustStyleFull(AdjustStyleBackgroundColor(bgColor))
|
|
|
|
}
|
2018-06-11 18:38:19 +02:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) headerToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
children := parser.nodeToTStrings(node.FirstChild, stripLinebreak)
|
|
|
|
length := int(node.Data[1] - '0')
|
|
|
|
prefix := strings.Repeat("#", length) + " "
|
|
|
|
return tstring.Join(children, "").Prepend(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *htmlParser) blockquoteToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak)
|
|
|
|
childrenArr := str.TrimSpace().Split('\n')
|
|
|
|
for index, child := range childrenArr {
|
|
|
|
childrenArr[index] = child.Prepend("> ")
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return tstring.Join(childrenArr, "\n")
|
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) linkToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak)
|
2018-06-01 23:28:21 +02:00
|
|
|
href := parser.getAttribute(node, "href")
|
2018-05-31 15:59:40 +02:00
|
|
|
if len(href) == 0 {
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
match := matrixToURL.FindStringSubmatch(href)
|
|
|
|
if len(match) == 2 {
|
|
|
|
pillTarget := match[1]
|
|
|
|
if pillTarget[0] == '@' {
|
|
|
|
if member := parser.room.GetMember(pillTarget); member != nil {
|
2018-09-05 09:55:48 +02:00
|
|
|
return tstring.NewColorTString(member.Displayname, widget.GetHashColor(pillTarget))
|
2018-05-31 15:59:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tstring.NewTString(pillTarget)
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return str.Append(fmt.Sprintf(" (%s)", href))
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) tagToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
switch node.Data {
|
|
|
|
case "blockquote":
|
|
|
|
return parser.blockquoteToTString(node, stripLinebreak)
|
|
|
|
case "ol", "ul":
|
|
|
|
return parser.listToTString(node, stripLinebreak)
|
2018-04-14 10:44:07 +02:00
|
|
|
case "h1", "h2", "h3", "h4", "h5", "h6":
|
2018-05-31 15:59:40 +02:00
|
|
|
return parser.headerToTString(node, stripLinebreak)
|
|
|
|
case "br":
|
|
|
|
return tstring.NewTString("\n")
|
|
|
|
case "b", "strong", "i", "em", "s", "del", "u", "ins":
|
|
|
|
return parser.basicFormatToTString(node, stripLinebreak)
|
2018-06-11 18:38:19 +02:00
|
|
|
case "font":
|
|
|
|
return parser.fontToTString(node, stripLinebreak)
|
2018-04-14 10:44:07 +02:00
|
|
|
case "a":
|
2018-05-31 15:59:40 +02:00
|
|
|
return parser.linkToTString(node, stripLinebreak)
|
|
|
|
case "p":
|
|
|
|
return parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak).Append("\n")
|
|
|
|
case "pre":
|
|
|
|
return parser.nodeToTString(node.FirstChild, false)
|
|
|
|
default:
|
|
|
|
return parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak)
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) singleNodeToTString(node *html.Node, stripLinebreak bool) taggedTString {
|
|
|
|
switch node.Type {
|
|
|
|
case html.TextNode:
|
|
|
|
if stripLinebreak {
|
|
|
|
node.Data = strings.Replace(node.Data, "\n", "", -1)
|
|
|
|
}
|
|
|
|
return taggedTString{tstring.NewTString(node.Data), "text"}
|
|
|
|
case html.ElementNode:
|
|
|
|
return taggedTString{parser.tagToTString(node, stripLinebreak), node.Data}
|
|
|
|
case html.DocumentNode:
|
|
|
|
return taggedTString{parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak), "html"}
|
|
|
|
default:
|
|
|
|
return taggedTString{tstring.NewBlankTString(), "unknown"}
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) nodeToTaggedTStrings(node *html.Node, stripLinebreak bool) (strs []taggedTString) {
|
|
|
|
for ; node != nil; node = node.NextSibling {
|
|
|
|
strs = append(strs, parser.singleNodeToTString(node, stripLinebreak))
|
2018-04-16 11:04:00 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var BlockTags = []string{"p", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "ul", "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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *htmlParser) nodeToTagAwareTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
strs := parser.nodeToTaggedTStrings(node, stripLinebreak)
|
|
|
|
output := tstring.NewBlankTString()
|
2018-06-01 23:28:21 +02:00
|
|
|
for _, str := range strs {
|
2018-05-31 15:59:40 +02:00
|
|
|
tstr := str.TString
|
2018-06-01 23:28:21 +02:00
|
|
|
if parser.isBlockTag(str.tag) {
|
|
|
|
tstr = tstr.Prepend("\n").Append("\n")
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
output = output.AppendTString(tstr)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return output.TrimSpace()
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) nodeToTStrings(node *html.Node, stripLinebreak bool) (strs []tstring.TString) {
|
|
|
|
for ; node != nil; node = node.NextSibling {
|
|
|
|
strs = append(strs, parser.singleNodeToTString(node, stripLinebreak).TString)
|
2018-04-14 10:44:07 +02:00
|
|
|
}
|
2018-05-31 15:59:40 +02:00
|
|
|
return
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
func (parser *htmlParser) nodeToTString(node *html.Node, stripLinebreak bool) tstring.TString {
|
|
|
|
return tstring.Join(parser.nodeToTStrings(node, stripLinebreak), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *htmlParser) Parse(htmlData string) tstring.TString {
|
|
|
|
node, _ := html.Parse(strings.NewReader(htmlData))
|
|
|
|
return parser.nodeToTagAwareTString(node, true)
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHTMLMessage parses a HTML-formatted Matrix event into a UIMessage.
|
2018-11-13 23:00:35 +01:00
|
|
|
func ParseHTMLMessage(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) tstring.TString {
|
2018-09-05 09:55:48 +02:00
|
|
|
htmlData := evt.Content.FormattedBody
|
2018-04-24 21:08:57 +02:00
|
|
|
htmlData = strings.Replace(htmlData, "\t", " ", -1)
|
2018-04-13 23:34:25 +02:00
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
parser := htmlParser{room}
|
|
|
|
str := parser.Parse(htmlData)
|
2018-04-13 23:34:25 +02:00
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
if evt.Content.MsgType == mautrix.MsgEmote {
|
2018-05-31 15:59:40 +02:00
|
|
|
str = tstring.Join([]tstring.TString{
|
|
|
|
tstring.NewTString("* "),
|
|
|
|
tstring.NewColorTString(senderDisplayname, widget.GetHashColor(evt.Sender)),
|
|
|
|
tstring.NewTString(" "),
|
|
|
|
str,
|
|
|
|
}, "")
|
|
|
|
}
|
2018-04-14 10:44:07 +02:00
|
|
|
|
2018-05-31 15:59:40 +02:00
|
|
|
return str
|
2018-04-13 23:34:25 +02:00
|
|
|
}
|