aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-06-02 00:28:21 +0300
committerTulir Asokan <tulir@maunium.net>2018-06-02 00:28:21 +0300
commit7868bd90fab77faa1e37674d543eefc1ff315fff (patch)
tree4feca995d7dd217a5151f7d5dee6a8e44835e150 /ui
parent1da02e3a13f9c6b2487378d41d757f3f2610d00a (diff)
Improve newlines in HTML parser
Diffstat (limited to 'ui')
-rw-r--r--ui/message-view.go3
-rw-r--r--ui/messages/parser/htmlparser.go51
2 files changed, 33 insertions, 21 deletions
diff --git a/ui/message-view.go b/ui/message-view.go
index 9bd1795..9f3d811 100644
--- a/ui/message-view.go
+++ b/ui/message-view.go
@@ -249,7 +249,7 @@ func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages
}
view.textBuffer = append(append(view.textBuffer[0:start], new.Buffer()...), view.textBuffer[end:]...)
- if len(new.Buffer()) != end-start+1 {
+ if len(new.Buffer()) != end-start {
metaBuffer := view.metaBuffer[0:start]
for range new.Buffer() {
metaBuffer = append(metaBuffer, new)
@@ -504,6 +504,7 @@ func (view *MessageView) Draw(screen tcell.Screen) {
if len(view.textBuffer) != len(view.metaBuffer) {
debug.Printf("Unexpected text/meta buffer length mismatch: %d != %d.", len(view.textBuffer), len(view.metaBuffer))
+ view.prevMsgCount = 0
return
}
diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go
index b88f55e..9999c94 100644
--- a/ui/messages/parser/htmlparser.go
+++ b/ui/messages/parser/htmlparser.go
@@ -28,6 +28,7 @@ import (
"maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell"
"golang.org/x/net/html"
+ "strconv"
)
var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([#@!].*)")
@@ -57,23 +58,43 @@ var AdjustStyleStrikethrough = func(style tcell.Style) tcell.Style {
return style.Strikethrough(true)
}
+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 {
+ return int(math.Floor(math.Log10(float64(num)))+1)
+}
+
func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) tstring.TString {
ordered := node.Data == "ol"
taggedChildren := parser.nodeToTaggedTStrings(node.FirstChild, stripLinebreak)
- paddingLength := 0
+ counter := 1
+ indentLength := 0
if ordered {
- paddingLength = int(math.Floor(math.Log10(float64(len(taggedChildren)))) + 1)
+ start := parser.getAttribute(node, "start")
+ if len(start) > 0 {
+ counter, _ = strconv.Atoi(start)
+ }
+
+ longestIndex := (counter - 1) + len(taggedChildren)
+ indentLength = digits(longestIndex)
}
- padding := strings.Repeat(" ", paddingLength+2)
+ indent := strings.Repeat(" ", indentLength+2)
var children []tstring.TString
- counter := 1
for _, child := range taggedChildren {
if child.tag != "li" {
continue
}
var prefix string
if ordered {
- prefix = fmt.Sprintf("%*d. ", paddingLength, counter)
+ indexPadding := indentLength - digits(counter)
+ prefix = fmt.Sprintf("%d. %s", counter, strings.Repeat(" ", indexPadding))
} else {
prefix = "● "
}
@@ -81,7 +102,7 @@ func (parser *htmlParser) listToTString(node *html.Node, stripLinebreak bool) ts
counter++
parts := str.Split('\n')
for i, part := range parts[1:] {
- parts[i+1] = part.Prepend(padding)
+ parts[i+1] = part.Prepend(indent)
}
str = tstring.Join(parts, "\n")
children = append(children, str)
@@ -122,13 +143,7 @@ func (parser *htmlParser) blockquoteToTString(node *html.Node, stripLinebreak bo
func (parser *htmlParser) linkToTString(node *html.Node, stripLinebreak bool) tstring.TString {
str := parser.nodeToTagAwareTString(node.FirstChild, stripLinebreak)
- var href string
- for _, attr := range node.Attr {
- if attr.Key == "href" {
- href = attr.Val
- break
- }
- }
+ href := parser.getAttribute(node, "href")
if len(href) == 0 {
return str
}
@@ -205,14 +220,10 @@ func (parser *htmlParser) isBlockTag(tag string) bool {
func (parser *htmlParser) nodeToTagAwareTString(node *html.Node, stripLinebreak bool) tstring.TString {
strs := parser.nodeToTaggedTStrings(node, stripLinebreak)
output := tstring.NewBlankTString()
- for i, str := range strs {
+ for _, str := range strs {
tstr := str.TString
- curIsBlock := parser.isBlockTag(str.tag)
- if i > 0 && curIsBlock {
- tstr = tstr.Prepend("\n")
- }
- if curIsBlock && len(strs) < i+1 {
- tstr = tstr.Append("\n")
+ if parser.isBlockTag(str.tag) {
+ tstr = tstr.Prepend("\n").Append("\n")
}
output = output.AppendTString(tstr)
}