From 782ba0657a0bddc6ccb31b1792f3fbf4500a0087 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 14 Apr 2018 11:44:07 +0300 Subject: Make HTML rendering more advanced Also add Python-like HTML parser thing in lib/htmlparser --- ui/messages/htmlparser.go | 218 ++++++++++++++++++++++++++++------------------ 1 file changed, 134 insertions(+), 84 deletions(-) (limited to 'ui/messages/htmlparser.go') diff --git a/ui/messages/htmlparser.go b/ui/messages/htmlparser.go index 0475e7a..aa6211e 100644 --- a/ui/messages/htmlparser.go +++ b/ui/messages/htmlparser.go @@ -17,120 +17,170 @@ package messages import ( + "fmt" + "io" + "math" + "regexp" "strings" - "golang.org/x/net/html" "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" ) -// TagArray is a reversed queue for remembering what HTML tags are open. -type TagArray []string +var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)") -// Pushb converts the given byte array into a string and calls Push(). -func (ta *TagArray) Pushb(tag []byte) { - ta.Push(string(tag)) +type MatrixHTMLProcessor struct { + text tstring.TString + + indent string + listType string + lineIsNew bool + openTags *TagArray + + room *rooms.Room } -// Popb converts the given byte array into a string and calls Pop(). -func (ta *TagArray) Popb(tag []byte) { - ta.Pop(string(tag)) +func (parser *MatrixHTMLProcessor) newline() { + if !parser.lineIsNew { + parser.text = parser.text.Append("\n" + parser.indent) + parser.lineIsNew = true + } } -// Hasb converts the given byte array into a string and calls Has(). -func (ta *TagArray) Hasb(tag []byte) { - ta.Has(string(tag)) +func (parser *MatrixHTMLProcessor) Preprocess() {} + +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 } -// HasAfterb converts the given byte array into a string and calls HasAfter(). -func (ta *TagArray) HasAfterb(tag []byte, after int) { - ta.HasAfter(string(tag), after) +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) } -// Push adds the given tag to the array. -func (ta *TagArray) Push(tag string) { - *ta = append(*ta, "") - copy((*ta)[1:], *ta) - (*ta)[0] = tag +func (parser *MatrixHTMLProcessor) HandleSelfClosingTag(tagName string, attrs map[string]string) { + if tagName == "br" { + parser.newline() + } } -// Pop removes the given tag from the array. -func (ta *TagArray) Pop(tag string) { - if (*ta)[0] == tag { - // This is the default case and is lighter than append(), so we handle it separately. - *ta = (*ta)[1:] - } else if index := ta.Has(tag); index != -1 { - *ta = append((*ta)[:index], (*ta)[index+1:]...) +func (parser *MatrixHTMLProcessor) HandleEndTag(tagName string) { + tag := parser.openTags.Pop(tagName) + + 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.DisplayName)) + } 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() } } -// Has returns the first index where the given tag is, or -1 if it's not in the list. -func (ta *TagArray) Has(tag string) int { - return ta.HasAfter(tag, -1) +func (parser *MatrixHTMLProcessor) ReceiveError(err error) { + if err != io.EOF { + debug.Print("Unexpected error parsing HTML:", err) + } } -// HasAfter returns the first index after the given index where the given tag is, -// or -1 if the given tag is not on the list after the given index. -func (ta *TagArray) HasAfter(tag string, after int) int { - for i := after + 1; i < len(*ta); i++ { - if (*ta)[i] == tag { - return i - } +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] } - return -1 } // ParseHTMLMessage parses a HTML-formatted Matrix event into a UIMessage. -func ParseHTMLMessage(evt *gomatrix.Event) tstring.TString { - //textData, _ := evt.Content["body"].(string) +func ParseHTMLMessage(room *rooms.Room, evt *gomatrix.Event) tstring.TString { htmlData, _ := evt.Content["formatted_body"].(string) - z := html.NewTokenizer(strings.NewReader(htmlData)) - text := tstring.NewTString("") - - openTags := &TagArray{} - -Loop: - for { - tt := z.Next() - switch tt { - case html.ErrorToken: - break Loop - case html.TextToken: - style := tcell.StyleDefault - for _, tag := range *openTags { - switch 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) - } - } - text = text.AppendStyle(string(z.Text()), style) - case html.SelfClosingTagToken, html.StartTagToken: - tagb, _ := z.TagName() - tag := string(tagb) - switch tag { - case "br": - debug.Print("BR found") - debug.Print(text.String()) - text = text.Append("\n") - default: - if tt == html.StartTagToken { - openTags.Push(tag) - } - } - case html.EndTagToken: - tagb, _ := z.TagName() - openTags.Popb(tagb) - } + processor := &MatrixHTMLProcessor{ + room: room, + text: tstring.NewBlankTString(), + indent: "", + listType: "", + lineIsNew: true, + openTags: &TagArray{}, } - return text + parser := htmlparser.NewHTMLParserFromString(htmlData, processor) + parser.Process() + + return processor.text } -- cgit v1.2.3