aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go.mod1
-rw-r--r--go.sum17
-rw-r--r--ui/messages/htmlmessage.go50
-rw-r--r--ui/messages/parser/htmlparser.go95
4 files changed, 148 insertions, 15 deletions
diff --git a/go.mod b/go.mod
index aa49e6a..e201049 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@ module maunium.net/go/gomuks
go 1.12
require (
+ github.com/alecthomas/chroma v0.6.3
github.com/disintegration/imaging v1.6.0
github.com/kyokomi/emoji v2.1.0+incompatible
github.com/lithammer/fuzzysearch v1.0.2
diff --git a/go.sum b/go.sum
index b5b2657..fc46446 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,16 @@
+github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
+github.com/alecthomas/chroma v0.6.3 h1:8H1D0yddf0mvgvO4JDBKnzLd9ERmzzAijBxnZXGV/FA=
+github.com/alecthomas/chroma v0.6.3/go.mod h1:quT2EpvJNqkuPi6DmBHB+E33FXBgBBPzyH5++Dn1LPc=
+github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
+github.com/alecthomas/kong v0.1.15/go.mod h1:0m2VYms8rH0qbCqVB2gvGHk74bqLIq0HXjCs5bNbNQU=
+github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
+github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
+github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disintegration/imaging v1.6.0 h1:nVPXRUUQ36Z7MNf0O77UzgnOb1mkMMor7lmJMJXc/mA=
github.com/disintegration/imaging v1.6.0/go.mod h1:xuIt+sRxDFrHS0drzXUlCJthkJ8k7lkkUojDSR247MQ=
+github.com/dlclark/regexp2 v1.1.6 h1:CqB4MjHw0MFCDj+PHHjiESmHX+N7t0tJzKvC6M97BRg=
+github.com/dlclark/regexp2 v1.1.6/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/kyokomi/emoji v2.1.0+incompatible h1:+DYU2RgpI6OHG4oQkM5KlqD3Wd3UPEsX8jamTo1Mp6o=
@@ -8,16 +19,21 @@ github.com/lithammer/fuzzysearch v1.0.2 h1:AjCE2iwc5y+8K+h2nXVc0Pmrpjvu+JVqMgiZ0
github.com/lithammer/fuzzysearch v1.0.2/go.mod h1:bvAJyokfCQ7Vknrd4Kgc+izmMrPj5CiBAu2t6rK1Kak=
github.com/lucasb-eyer/go-colorful v1.0.1 h1:nKJRBvZWPzvkwB4sY8A3U4zgqLf2Y9c02yzPsbXu/5c=
github.com/lucasb-eyer/go-colorful v1.0.1/go.mod h1:tLy1nWSoU0DGtxQyNRrUmb6PUiB7usbds6gd97XTXwA=
+github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
+github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.0.0-20190313204849-f699dde9c340 h1:nOZbL5f2xmBAHWYrrHbHV1xatzZirN++oOQ3g83Ypgs=
github.com/rivo/uniseg v0.0.0-20190313204849-f699dde9c340/go.mod h1:SOLvOL4ybwgLJ6TYoX/rtaJ8EGOulH4XU7E9/TLrTCE=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
+github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/zyedidia/clipboard v0.0.0-20180718195219-bd31d747117d h1:Lhqt2eo+rgM8aswvM7nTtAMVm8ARPWzkE9n6eZDOccY=
github.com/zyedidia/clipboard v0.0.0-20180718195219-bd31d747117d/go.mod h1:WDk3p8GiZV9+xFWlSo8qreeoLhW6Ik692rqXk+cNeRY=
go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
@@ -32,6 +48,7 @@ golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190326090315-15845e8f865b/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go
index 51678ba..0ffb6c9 100644
--- a/ui/messages/htmlmessage.go
+++ b/ui/messages/htmlmessage.go
@@ -72,23 +72,23 @@ func (hw *HTMLMessage) Height() int {
}
func (hw *HTMLMessage) PlainText() string {
- // FIXME
- return "Plaintext unavailable"
+ return hw.Root.PlainText()
}
func (hw *HTMLMessage) NotificationContent() string {
- // FIXME
- return "Notification content unavailable"
+ return hw.Root.PlainText()
}
type HTMLEntity struct {
// Permanent variables
- Tag string
- Text string
- Style tcell.Style
- Children []*HTMLEntity
- Block bool
- Indent int
+ Tag string
+ Text string
+ Style tcell.Style
+ Children []*HTMLEntity
+ Block bool
+ Indent int
+
+ DefaultHeight int
// Non-permanent variables (calculated buffer data)
buffer []string
@@ -130,8 +130,9 @@ func (he *HTMLEntity) Draw(screen mauview.Screen) {
func (he *HTMLEntity) String() string {
var buf strings.Builder
buf.WriteString("&HTMLEntity{\n")
- _, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,\n`,
+ _, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,`,
he.Tag, he.Style, he.Block, he.Indent, he.startX, he.height)
+ buf.WriteRune('\n')
_, _ = fmt.Fprintf(&buf, ` Buffer=["%s"]`, strings.Join(he.buffer, "\", \""))
if len(he.Text) > 0 {
buf.WriteString(",\n")
@@ -150,6 +151,27 @@ func (he *HTMLEntity) String() string {
return buf.String()
}
+func (he *HTMLEntity) PlainText() string {
+ if len(he.Children) == 0 {
+ return he.Text
+ }
+ var buf strings.Builder
+ buf.WriteString(he.Text)
+ newlined := false
+ for _, child := range he.Children {
+ if child.Block && !newlined {
+ buf.WriteRune('\n')
+ }
+ newlined = false
+ buf.WriteString(child.PlainText())
+ if child.Block {
+ buf.WriteRune('\n')
+ newlined = true
+ }
+ }
+ return buf.String()
+}
+
func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int {
he.startX = startX
if he.Block {
@@ -178,6 +200,7 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int {
text := he.Text
textStartX := he.startX
for {
+ // TODO add option no wrap and character wrap options
extract := runewidth.Truncate(text, width-textStartX, "")
extract, wordWrapped := trim(extract, text, bare)
if !wordWrapped && textStartX > 0 {
@@ -210,7 +233,10 @@ func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int {
textStartX = 0
}
}
- return 0
+ if len(he.Text) == 0 && len(he.Children) == 0 {
+ he.height = he.DefaultHeight
+ }
+ return he.startX
}
func trim(extract, full string, bare bool) (string, bool) {
diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go
index 9a9c2d1..688deb3 100644
--- a/ui/messages/parser/htmlparser.go
+++ b/ui/messages/parser/htmlparser.go
@@ -23,6 +23,9 @@ import (
"strconv"
"strings"
+ "github.com/alecthomas/chroma"
+ "github.com/alecthomas/chroma/lexers"
+ "github.com/alecthomas/chroma/styles"
"github.com/lucasb-eyer/go-colorful"
"golang.org/x/net/html"
@@ -216,18 +219,102 @@ func (parser *htmlParser) linkToEntity(node *html.Node, stripLinebreak bool) *me
}
}
}
- // TODO add click action for links
+ // TODO add click action and underline on hover for links
return entity
}
-func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntity {
+func (parser *htmlParser) imageToEntity(node *html.Node) *messages.HTMLEntity {
+ alt := parser.getAttribute(node, "alt")
+ if len(alt) == 0 {
+ alt = parser.getAttribute(node, "title")
+ if len(alt) == 0 {
+ alt = "[inline image]"
+ }
+ }
+ entity := &messages.HTMLEntity{
+ Tag: "img",
+ 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))
+}
+
+func (parser *htmlParser) syntaxHighlight(text, language string) *messages.HTMLEntity {
+ lexer := lexers.Get(language)
+ if lexer == nil {
+ return nil
+ }
+ iter, err := lexer.Tokenise(nil, text)
+ if err != nil {
+ return nil
+ }
+ style := styles.SolarizedDark
+ tokens := iter.Tokens()
+ children := make([]*messages.HTMLEntity, len(tokens))
+ for i, token := range tokens {
+ if token.Value == "\n" {
+ children[i] = &messages.HTMLEntity{Block: true, Tag: "br"}
+ } else {
+ children[i] = &messages.HTMLEntity{
+ Tag: token.Type.String(),
+ Text: token.Value,
+ Style: styleEntryToStyle(style.Get(token.Type)),
+
+ DefaultHeight: 1,
+ }
+ }
+ }
return &messages.HTMLEntity{
Tag: "pre",
- Children: parser.nodeToEntities(node.FirstChild, false),
Block: true,
+ Children: children,
}
}
+func (parser *htmlParser) codeblockToEntity(node *html.Node) *messages.HTMLEntity {
+ entity := &messages.HTMLEntity{
+ Tag: "pre",
+ Block: true,
+ }
+ // TODO allow disabling syntax highlighting
+ if node.FirstChild.Type == html.ElementNode && node.FirstChild.Data == "code" {
+ text := (&messages.HTMLEntity{
+ Children: parser.nodeToEntities(node.FirstChild.FirstChild, false),
+ }).PlainText()
+ attr := parser.getAttribute(node.FirstChild, "class")
+ var lang string
+ for _, class := range strings.Split(attr, " ") {
+ if strings.HasPrefix(class, "language-") {
+ lang = class[len("language-"):]
+ break
+ }
+ }
+ if len(lang) != 0 {
+ if parsed := parser.syntaxHighlight(text, lang); parsed != nil {
+ return parsed
+ }
+ }
+ }
+ entity.Children = parser.nodeToEntities(node.FirstChild, false)
+ return entity
+}
+
func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool) *messages.HTMLEntity {
switch node.Data {
case "blockquote":
@@ -242,6 +329,8 @@ func (parser *htmlParser) tagNodeToEntity(node *html.Node, stripLinebreak bool)
return parser.basicFormatToEntity(node, stripLinebreak)
case "a":
return parser.linkToEntity(node, stripLinebreak)
+ case "img":
+ return parser.imageToEntity(node)
case "pre":
return parser.codeblockToEntity(node)
default: