Support formatting in rainbows

Fixes #119
This commit is contained in:
Tulir Asokan 2020-03-20 14:32:29 +02:00
parent 5a2c74514d
commit 87b394abec
10 changed files with 159 additions and 54 deletions

4
go.mod
View File

@ -11,6 +11,8 @@ require (
github.com/mattn/go-runewidth v0.0.8
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect
github.com/pkg/errors v0.9.1
github.com/rivo/uniseg v0.1.0
github.com/russross/blackfriday/v2 v2.0.1
github.com/sasha-s/go-deadlock v0.2.0
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/stretchr/testify v1.5.1
@ -19,7 +21,7 @@ require (
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.8
maunium.net/go/mautrix v0.1.0-beta.1
maunium.net/go/mautrix v0.1.0-beta.1.0.20200320123139-8ba1d97ed86b
maunium.net/go/mauview v0.1.0-beta.1
maunium.net/go/tcell v0.1.0
)

4
go.sum
View File

@ -71,6 +71,10 @@ gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
maunium.net/go/mautrix v0.1.0-beta.1 h1:o7EzSO3sMf7tpNvxanITcpMw3nL3SaJ/LDpBRPaZIt8=
maunium.net/go/mautrix v0.1.0-beta.1/go.mod h1:YFMU9DBeXH7cqx7sJLg0DkVxwNPbih8QbpUTYf/IjMM=
maunium.net/go/mautrix v0.1.0-beta.1.0.20200320113420-8101f052c782 h1:gLsc8FRp9mIQHXjxFpdpISUCoJO8jt4SQyaFYUCLJ5U=
maunium.net/go/mautrix v0.1.0-beta.1.0.20200320113420-8101f052c782/go.mod h1:YFMU9DBeXH7cqx7sJLg0DkVxwNPbih8QbpUTYf/IjMM=
maunium.net/go/mautrix v0.1.0-beta.1.0.20200320123139-8ba1d97ed86b h1:sQ7w1dOTvTg76wLCUyb30+jEsVIVvI4Zw4IOPdwogIE=
maunium.net/go/mautrix v0.1.0-beta.1.0.20200320123139-8ba1d97ed86b/go.mod h1:YFMU9DBeXH7cqx7sJLg0DkVxwNPbih8QbpUTYf/IjMM=
maunium.net/go/mauview v0.1.0-beta.1 h1:hRprD6NTi5Mw7i97DKmgs/TzFQeNpGPytPoswNlU/Ww=
maunium.net/go/mauview v0.1.0-beta.1/go.mod h1:og9WbzmWe9SNYNyOFlCv8qa9zMcOvG2nzRJ5vYyud9U=
maunium.net/go/tcell v0.1.0 h1:XzsEoGCvOw5nac+tlkSLzQcliLYTN4PrtA7ar2ptjSM=

View File

@ -40,7 +40,7 @@ type MatrixContainer interface {
Logout()
SendPreferencesToMatrix()
PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, message string, relation *Relation) *event.Event
PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text, html string, relation *Relation) *event.Event
SendEvent(evt *event.Event) (string, error)
Redact(roomID, eventID, reason string) error
SendTyping(roomID string, typing bool)

View File

@ -1,2 +0,0 @@
// Package bfhtml contains an extension to the Blackfriday HTML renderer to disable paragraph tags.
package bfhtml

View File

@ -1,34 +0,0 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// 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/>.
package bfhtml
import (
"io"
bf "github.com/russross/blackfriday/v2"
)
type HTMLRenderer struct {
*bf.HTMLRenderer
}
func (r *HTMLRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
if node.Type == bf.Paragraph {
return bf.GoToNext
}
return r.HTMLRenderer.RenderNode(w, node, entering)
}

View File

@ -729,9 +729,19 @@ func (c *Container) MarkRead(roomID, eventID string) {
_, _ = c.client.MakeRequest("POST", urlPath, struct{}{}, nil)
}
func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string, rel *ifc.Relation) *event.Event {
content := format.RenderMarkdown(text)
content.MsgType = msgtype
func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text, html string, rel *ifc.Relation) *event.Event {
var content mautrix.Content
if html != "" {
content = mautrix.Content{
FormattedBody: html,
Format: mautrix.FormatHTML,
Body: text,
MsgType: msgtype,
}
} else {
content = format.RenderMarkdown(text)
content.MsgType = msgtype
}
if rel != nil && rel.Type == mautrix.RelReplace {
contentCopy := content

View File

@ -153,6 +153,9 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err
func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []json.RawMessage, source EventSource) {
for _, event := range events {
if source == EventSourcePresence {
debug.Print(string(event))
}
s.processSyncEvent(room, event, source)
}
}
@ -241,7 +244,7 @@ func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage {
"m.room.power_levels",
"m.room.tombstone",
},
Limit: 50,
// Limit: 50,
},
Ephemeral: mautrix.FilterPart{
Types: []string{"m.typing", "m.receipt"},

View File

@ -22,6 +22,7 @@ import (
"io"
"math"
"os"
"regexp"
"runtime"
dbg "runtime/debug"
"runtime/pprof"
@ -29,11 +30,12 @@ import (
"strconv"
"strings"
"time"
"unicode"
"github.com/lucasb-eyer/go-colorful"
"github.com/russross/blackfriday/v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/format"
"maunium.net/go/gomuks/debug"
)
@ -79,16 +81,23 @@ var rainbow = GradientTable{
// TODO this command definitely belongs in a plugin once we have a plugin system.
func makeRainbow(cmd *Command, msgtype mautrix.MessageType) {
text := strings.Join(cmd.Args, " ")
var html strings.Builder
for i, char := range text {
if unicode.IsSpace(char) {
html.WriteRune(char)
continue
}
color := rainbow.GetInterpolatedColorFor(float64(i) / float64(len(text))).Hex()
_, _ = fmt.Fprintf(&html, "<font data-mx-color=\"%[1]s\" color=\"%[1]s\">%[2]c</font>", color, char)
}
go cmd.Room.SendMessage(msgtype, html.String())
render := NewRainbowRenderer(blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.UseXHTML,
}))
htmlBodyBytes := blackfriday.Run([]byte(text), format.Extensions, blackfriday.WithRenderer(render))
htmlBody := strings.TrimRight(string(htmlBodyBytes), "\n")
htmlBody = format.AntiParagraphRegex.ReplaceAllString(htmlBody, "$1")
text = format.HTMLToText(htmlBody)
count := strings.Count(htmlBody, render.ColorID)
i := -1
htmlBody = regexp.MustCompile(render.ColorID).ReplaceAllStringFunc(htmlBody, func(match string) string {
i++
return rainbow.GetInterpolatedColorFor(float64(i) / float64(count)).Hex()
})
go cmd.Room.SendMessageHTML(msgtype, text, htmlBody)
}
func cmdRainbow(cmd *Command) {

109
ui/rainbow.go Normal file
View File

@ -0,0 +1,109 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2020 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// 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/>.
package ui
import (
"bytes"
"fmt"
"html"
"io"
"math/rand"
"unicode"
"github.com/rivo/uniseg"
"github.com/russross/blackfriday/v2"
)
type RainbowRenderer struct {
*blackfriday.HTMLRenderer
sr *blackfriday.SPRenderer
ColorID string
}
func Rand(n int) (str string) {
b := make([]byte, n)
rand.Read(b)
str = fmt.Sprintf("%x", b)
return
}
func NewRainbowRenderer(html *blackfriday.HTMLRenderer) *RainbowRenderer {
return &RainbowRenderer{
HTMLRenderer: html,
sr: blackfriday.NewSmartypantsRenderer(html.Flags),
ColorID: Rand(16),
}
}
func (r *RainbowRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
if node.Type == blackfriday.Text {
var buf bytes.Buffer
if r.Flags&blackfriday.Smartypants != 0 {
var tmp bytes.Buffer
escapeHTML(&tmp, node.Literal)
r.sr.Process(&buf, tmp.Bytes())
} else {
if node.Parent.Type == blackfriday.Link {
escLink(&buf, node.Literal)
} else {
escapeHTML(&buf, node.Literal)
}
}
graphemes := uniseg.NewGraphemes(buf.String())
buf.Reset()
for graphemes.Next() {
runes := graphemes.Runes()
if len(runes) == 1 && unicode.IsSpace(runes[0]) {
buf.WriteRune(runes[0])
}
_, _ = fmt.Fprintf(&buf, "<font color=\"%s\">%s</font>", r.ColorID, graphemes.Str())
}
_, _ = w.Write(buf.Bytes())
return blackfriday.GoToNext
}
return r.HTMLRenderer.RenderNode(w, node, entering)
}
// This stuff is copied directly from blackfriday
var htmlEscaper = [256][]byte{
'&': []byte("&amp;"),
'<': []byte("&lt;"),
'>': []byte("&gt;"),
'"': []byte("&quot;"),
}
func escapeHTML(w io.Writer, s []byte) {
var start, end int
for end < len(s) {
escSeq := htmlEscaper[s[end]]
if escSeq != nil {
w.Write(s[start:end])
w.Write(escSeq)
start = end + 1
}
end++
}
if start < len(s) && end <= len(s) {
w.Write(s[start:end])
}
}
func escLink(w io.Writer, text []byte) {
unesc := html.UnescapeString(string(text))
escapeHTML(w, []byte(unesc))
}

View File

@ -622,6 +622,10 @@ func (view *RoomView) SendReaction(eventID string, reaction string) {
}
func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
view.SendMessageHTML(msgtype, text, "")
}
func (view *RoomView) SendMessageHTML(msgtype mautrix.MessageType, text, html string) {
defer debug.Recover()
debug.Print("Sending message", msgtype, text, "to", view.Room.ID)
if !view.config.Preferences.DisableEmojis {
@ -639,7 +643,7 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
Event: view.replying,
}
}
evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text, rel)
evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text, html, rel)
msg := view.parseEvent(evt.SomewhatDangerousCopy())
view.content.AddMessage(msg, AppendMessage)
view.ClearAllContext()