Added extra renderers
This commit is contained in:
parent
2422a90467
commit
60cc3135ca
4
go.mod
4
go.mod
|
@ -3,12 +3,14 @@ module github.com/diamondburned/cchat-gtk
|
|||
go 1.14
|
||||
|
||||
replace github.com/gotk3/gotk3 => github.com/diamondburned/gotk3 v0.0.0-20200612012846-9df87fea4f6d
|
||||
|
||||
replace github.com/diamondburned/cchat-mock => ../cchat-mock/
|
||||
|
||||
replace github.com/diamondburned/cchat-discord => ../cchat-discord/
|
||||
|
||||
require (
|
||||
github.com/Xuanwo/go-locale v0.2.0
|
||||
github.com/diamondburned/cchat v0.0.28
|
||||
github.com/diamondburned/cchat v0.0.31
|
||||
github.com/diamondburned/cchat-discord v0.0.0-00010101000000-000000000000
|
||||
github.com/diamondburned/cchat-mock v0.0.0-20200615015702-8cac8b16378d
|
||||
github.com/diamondburned/imgutil v0.0.0-20200611215339-650ac7cfaf64
|
||||
|
|
10
go.sum
10
go.sum
|
@ -16,6 +16,12 @@ github.com/diamondburned/cchat v0.0.26 h1:QBt4d65uzUPJz3jF8b2pJ09Jz8LeBRyG2ol47F
|
|||
github.com/diamondburned/cchat v0.0.26/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.28 h1:+1VnltW0rl8/NZTUP+x89jVhi3YTTR+e6iLprZ7HcwM=
|
||||
github.com/diamondburned/cchat v0.0.28/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.29 h1:fEfmv2hYO7sK4wH9GAqf400XGJg6R4DBI8xvsljn5vk=
|
||||
github.com/diamondburned/cchat v0.0.29/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.30 h1:rMACN56vBNG8L9VzY11kA0sA/mJO42JrNdUUnINZWto=
|
||||
github.com/diamondburned/cchat v0.0.30/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat v0.0.31 h1:yUgrh5xbGX0R55glyxYtVewIDL2eXLJ+okIEfVaVoFk=
|
||||
github.com/diamondburned/cchat v0.0.31/go.mod h1:+zXktogE45A0om4fT6B/z6Ii7FXNafjxsNspI0rlhbU=
|
||||
github.com/diamondburned/cchat-mock v0.0.0-20200613003444-b36f8f47debe h1:OoTLxpryxB9iQyu3bjw5N9N/3Bvu6FwklJ85X9erCAY=
|
||||
github.com/diamondburned/cchat-mock v0.0.0-20200613003444-b36f8f47debe/go.mod h1:vitBma+rd/ah+ujQsp6lPm/AfS2KtLKEh+Owxbv5BQM=
|
||||
github.com/diamondburned/cchat-mock v0.0.0-20200613233949-1e7651c8dd84 h1:NSuksZ9HiLiau93qAz4yNba6Xd7ExOFc956dumONDQ0=
|
||||
|
@ -28,10 +34,13 @@ github.com/diamondburned/imgutil v0.0.0-20200611215339-650ac7cfaf64 h1:/ykUYHuYy
|
|||
github.com/diamondburned/imgutil v0.0.0-20200611215339-650ac7cfaf64/go.mod h1:kBQKaukR/LyCfhED99/T4/XxUMDNEEzf1Fx6vreD3RQ=
|
||||
github.com/diamondburned/ningen v0.0.0-20200610212436-159f7105a2be h1:mUw8X/YzJGFSdL8y3Q/XqyzqPyIMNVSDyZGOP3JXgJA=
|
||||
github.com/diamondburned/ningen v0.0.0-20200610212436-159f7105a2be/go.mod h1:B2hq2B4va1MlnMmXuv9vXmyu9gscxJLmwrmcSB1Les8=
|
||||
github.com/diamondburned/ningen v0.0.0-20200618230530-16d4d7fbc521 h1:hp4vdK6mARWbtU7A9UAz9zr7qe0z7ppoh9RGXymWDRE=
|
||||
github.com/diamondburned/ningen v0.0.0-20200618230530-16d4d7fbc521/go.mod h1:B2hq2B4va1MlnMmXuv9vXmyu9gscxJLmwrmcSB1Les8=
|
||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
|
||||
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
|
||||
github.com/go-test/deep v1.0.6/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
|
||||
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
|
||||
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
|
||||
github.com/godbus/dbus v4.1.0+incompatible h1:WqqLRTsQic3apZUK9qC5sGNfXthmPXzUZ7nQPrNITa4=
|
||||
|
@ -75,6 +84,7 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
|
|||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/twmb/murmur3 v1.1.3 h1:D83U0XYKcHRYwYIpBKf3Pks91Z0Byda/9SJ8B6EMRcA=
|
||||
github.com/twmb/murmur3 v1.1.3/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
|
||||
github.com/yuin/goldmark v1.1.30 h1:j4d4Lw3zqZelDhBksEo3BnWg9xhXRQGJPPSL6OApZjI=
|
||||
github.com/yuin/goldmark v1.1.30/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/zalando/go-keyring v0.0.0-20200121091418-667557018717 h1:3M/uUZajYn/082wzUajekePxpUAZhMTfXvI9R+26SJ0=
|
||||
github.com/zalando/go-keyring v0.0.0-20200121091418-667557018717/go.mod h1:RaxNwUITJaHVdQ0VC7pELPZ3tOWn13nr0gZMZEhpVU0=
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"html"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/diamondburned/cchat/text"
|
||||
)
|
||||
|
@ -21,6 +22,24 @@ func newAttrAppendedMap() attrAppendMap {
|
|||
}
|
||||
}
|
||||
|
||||
func (a *attrAppendMap) span(start, end int, attr string) {
|
||||
a.add(start, `<span `+attr+`>`)
|
||||
a.add(end, "</span>")
|
||||
}
|
||||
|
||||
func (a *attrAppendMap) pair(start, end int, open, close string) {
|
||||
a.add(start, open)
|
||||
a.add(end, close)
|
||||
}
|
||||
|
||||
func (a *attrAppendMap) addf(ind int, f string, argv ...interface{}) {
|
||||
a.add(ind, fmt.Sprintf(f, argv...))
|
||||
}
|
||||
|
||||
func (a *attrAppendMap) pad(ind int) {
|
||||
a.add(ind, "\n")
|
||||
}
|
||||
|
||||
func (a *attrAppendMap) add(ind int, attr string) {
|
||||
if _, ok := a.appended[ind]; ok {
|
||||
a.appended[ind] += attr
|
||||
|
@ -43,6 +62,34 @@ func (a *attrAppendMap) finalize(strlen int) []int {
|
|||
return a.indices
|
||||
}
|
||||
|
||||
func markupAttr(attr text.Attribute) string {
|
||||
// meme fast path
|
||||
if attr == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var attrs = make([]string, 0, 1)
|
||||
if attr.Has(text.AttrBold) {
|
||||
attrs = append(attrs, `weight="bold"`)
|
||||
}
|
||||
if attr.Has(text.AttrItalics) {
|
||||
attrs = append(attrs, `style="italic"`)
|
||||
}
|
||||
if attr.Has(text.AttrUnderline) {
|
||||
attrs = append(attrs, `underline="single"`)
|
||||
}
|
||||
if attr.Has(text.AttrStrikethrough) {
|
||||
attrs = append(attrs, `strikethrough="true"`)
|
||||
}
|
||||
if attr.Has(text.AttrSpoiler) {
|
||||
attrs = append(attrs, `foreground="#808080"`) // no fancy click here
|
||||
}
|
||||
if attr.Has(text.AttrMonospace) {
|
||||
attrs = append(attrs, `font_family="monospace"`)
|
||||
}
|
||||
return strings.Join(attrs, " ")
|
||||
}
|
||||
|
||||
func RenderMarkup(content text.Rich) string {
|
||||
// Fast path.
|
||||
if len(content.Segments) == 0 {
|
||||
|
@ -67,9 +114,31 @@ func RenderMarkup(content text.Rich) string {
|
|||
start, end := segment.Bounds()
|
||||
|
||||
switch segment := segment.(type) {
|
||||
case text.Linker:
|
||||
appended.addf(start, `<a href="%s">`, html.EscapeString(segment.Link()))
|
||||
appended.add(end, "</a>")
|
||||
|
||||
case text.Imager:
|
||||
// Ends don't matter with images.
|
||||
appended.addf(start,
|
||||
`<a href="%s">%s</a>`,
|
||||
html.EscapeString(segment.Image()), html.EscapeString(segment.ImageText()),
|
||||
)
|
||||
|
||||
case text.Colorer:
|
||||
appended.add(start, fmt.Sprintf("<span color=\"#%06X\">", segment.Color()))
|
||||
appended.add(end, "</span>")
|
||||
appended.span(start, end, fmt.Sprintf(`color="#%06X"`, segment.Color()))
|
||||
|
||||
case text.Attributor:
|
||||
appended.span(start, end, markupAttr(segment.Attribute()))
|
||||
|
||||
case text.Codeblocker:
|
||||
// Treat codeblocks the same as a monospace tag.
|
||||
// TODO: add highlighting
|
||||
appended.span(start, end, `font_family="monospace"`)
|
||||
|
||||
case text.Quoteblocker:
|
||||
// TODO: pls.
|
||||
appended.span(start, end, `color="#789922"`)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,3 +155,7 @@ func RenderMarkup(content text.Rich) string {
|
|||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func span(key, value string) string {
|
||||
return "<span key=\"" + value + "\">"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue