aboutsummaryrefslogtreecommitdiff
path: root/ui/messages/parser/htmlparser.go
blob: cd268a51ddb0cf6f4545e45fe9b305e30f593478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package parser

import (
	"fmt"
	"io"
	"math"
	"regexp"
	"strings"

	"maunium.net/go/gomatrix"
	"maunium.net/go/gomuks/debug"
	"maunium.net/go/gomuks/lib/htmlparser"
	"maunium.net/go/gomuks/matrix/rooms"
	"maunium.net/go/gomuks/ui/messages/tstring"
	"maunium.net/go/gomuks/ui/widget"
	"maunium.net/go/tcell"
)

var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")

type MatrixHTMLProcessor struct {
	text tstring.TString

	senderID string
	sender   string
	msgtype  string

	indent    string
	listType  string
	lineIsNew bool
	openTags  *TagArray

	room *rooms.Room
}

func (parser *MatrixHTMLProcessor) newline() {
	if !parser.lineIsNew {
		parser.text = parser.text.Append("\n" + parser.indent)
		parser.lineIsNew = true
	}
}

func (parser *MatrixHTMLProcessor) Preprocess() {
	if parser.msgtype == "m.emote" {
		parser.text = tstring.NewColorTString(fmt.Sprintf("* %s ", parser.sender), widget.GetHashColor(parser.senderID))
	}
}

func (parser *MatrixHTMLProcessor) HandleText(text string) {
	style := tcell.StyleDefault
	for _, tag := range *parser.openTags {
		switch tag.Tag {
		case "b", "strong":
			style = style.Bold(true)
		case "i", "em":
			style = style.Italic(true)
		case "s", "del":
			style = style.Strikethrough(true)
		case "u", "ins":
			style = style.Underline(true)
		case "a":
			tag.Text += text
			return
		}
	}

	if !parser.openTags.Has("pre", "code") {
		text = strings.Replace(text, "\n", "", -1)
	}
	parser.text = parser.text.AppendStyle(text, style)
	parser.lineIsNew = false
}

func (parser *MatrixHTMLProcessor) HandleStartTag(tagName string, attrs map[string]string) {
	tag := &TagWithMeta{Tag: tagName}
	switch tag.Tag {
	case "h1", "h2", "h3", "h4", "h5", "h6":
		length := int(tag.Tag[1] - '0')
		parser.text = parser.text.Append(strings.Repeat("#", length) + " ")
		parser.lineIsNew = false
	case "a":
		tag.Meta, _ = attrs["href"]
	case "ol", "ul":
		parser.listType = tag.Tag
	case "li":
		indentSize := 2
		if parser.listType == "ol" {
			list := parser.openTags.Get(parser.listType)
			list.Counter++
			parser.text = parser.text.Append(fmt.Sprintf("%d. ", list.Counter))
			indentSize = int(math.Log10(float64(list.Counter))+1) + len(". ")
		} else {
			parser.text = parser.text.Append("* ")
		}
		parser.indent += strings.Repeat(" ", indentSize)
		parser.lineIsNew = false
	case "blockquote":
		parser.indent += "> "
		parser.text = parser.text.Append("> ")
		parser.lineIsNew = false
	}
	parser.openTags.PushMeta(tag)
}

func (parser *MatrixHTMLProcessor) HandleSelfClosingTag(tagName string, attrs map[string]string) {
	if tagName == "br" {
		parser.newline()
	}
}

func (parser *MatrixHTMLProcessor) HandleEndTag(tagName string) {
	tag := parser.openTags.Pop(tagName)
	if tag == nil {
		return
	}

	switch tag.Tag {
	case "li", "blockquote":
		indentSize := 2
		if tag.Tag == "li" && parser.listType == "ol" {
			list := parser.openTags.Get(parser.listType)
			indentSize = int(math.Log10(float64(list.Counter))+1) + len(". ")
		}
		if len(parser.indent) >= indentSize {
			parser.indent = parser.indent[0 : len(parser.indent)-indentSize]
		}
		// TODO this newline is sometimes not good
		parser.newline()
	case "a":
		match := matrixToURL.FindStringSubmatch(tag.Meta)
		if len(match) == 2 {
			pillTarget := match[1]
			if pillTarget[0] == '@' {
				if member := parser.room.GetMember(pillTarget); member != nil {
					parser.text = parser.text.AppendColor(member.DisplayName, widget.GetHashColor(member.UserID))
				} else {
					parser.text = parser.text.Append(pillTarget)
				}
			} else {
				parser.text = parser.text.Append(pillTarget)
			}
		} else {
			// TODO make text clickable rather than printing URL
			parser.text = parser.text.Append(fmt.Sprintf("%s (%s)", tag.Text, tag.Meta))
		}
		parser.lineIsNew = false
	case "p", "pre", "ol", "ul", "h1", "h2", "h3", "h4", "h5", "h6", "div":
		// parser.newline()
	}
}

func (parser *MatrixHTMLProcessor) ReceiveError(err error) {
	if err != io.EOF {
		debug.Print("Unexpected error parsing HTML:", err)
	}
}

func (parser *MatrixHTMLProcessor) Postprocess() {
	if len(parser.text) > 0 && parser.text[len(parser.text)-1].Char == '\n' {
		parser.text = parser.text[:len(parser.text)-1]
	}
}

// ParseHTMLMessage parses a HTML-formatted Matrix event into a UIMessage.
func ParseHTMLMessage(room *rooms.Room, evt *gomatrix.Event, senderDisplayname string) tstring.TString {
	htmlData, _ := evt.Content["formatted_body"].(string)
	htmlData = strings.Replace(htmlData, "\t", "    ", -1)
	msgtype, _ := evt.Content["msgtype"].(string)

	processor := &MatrixHTMLProcessor{
		room:      room,
		text:      tstring.NewBlankTString(),
		msgtype:   msgtype,
		senderID:  evt.Sender,
		sender:    senderDisplayname,
		indent:    "",
		listType:  "",
		lineIsNew: true,
		openTags:  &TagArray{},
	}

	parser := htmlparser.NewHTMLParserFromString(htmlData, processor)
	parser.Process()

	return processor.text
}