2020-07-14 07:24:55 +00:00
|
|
|
package markup
|
2020-06-19 22:40:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
2020-06-28 01:35:26 +00:00
|
|
|
"net/url"
|
2020-07-14 07:24:55 +00:00
|
|
|
"sort"
|
2020-06-19 22:40:06 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-06-28 23:01:08 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/attrmap"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/hl"
|
2020-06-19 22:40:06 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-28 01:35:26 +00:00
|
|
|
"github.com/diamondburned/imgutil"
|
2020-06-19 22:40:06 +00:00
|
|
|
)
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// Hyphenate controls whether or not texts should have hyphens on wrap.
|
|
|
|
var Hyphenate = false
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func hyphenate(text string) string {
|
|
|
|
return fmt.Sprintf(`<span insert_hyphens="%t">%s</span>`, Hyphenate, text)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderOutput is the output of a render.
|
|
|
|
type RenderOutput struct {
|
|
|
|
Markup string
|
2020-08-13 22:50:51 +00:00
|
|
|
Input string // useless to keep parts, as Go will keep all alive anyway
|
2020-10-15 06:32:11 +00:00
|
|
|
Mentions []MentionSegment
|
|
|
|
}
|
|
|
|
|
|
|
|
// MentionSegment is a type that satisfies both Segment and Mentioner.
|
|
|
|
type MentionSegment struct {
|
|
|
|
text.Segment
|
|
|
|
text.Mentioner
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// f_Mention is used to print and parse mention URIs.
|
2020-08-13 22:50:51 +00:00
|
|
|
const f_Mention = "cchat://mention/%d" // %d == Mentions[i]
|
2020-07-14 07:24:55 +00:00
|
|
|
|
|
|
|
// IsMention returns the mention if the URI is correct, or nil if none.
|
2020-10-15 06:32:11 +00:00
|
|
|
func (r RenderOutput) IsMention(uri string) text.Segment {
|
2020-07-14 07:24:55 +00:00
|
|
|
var i int
|
|
|
|
|
|
|
|
if _, err := fmt.Sscanf(uri, f_Mention, &i); err != nil {
|
|
|
|
return nil
|
2020-06-19 22:40:06 +00:00
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
|
|
|
|
if i >= len(r.Mentions) {
|
|
|
|
return nil
|
2020-06-19 22:40:06 +00:00
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
|
|
|
|
return r.Mentions[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func Render(content text.Rich) string {
|
|
|
|
return RenderCmplx(content).Markup
|
2020-06-19 22:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// RenderCmplx renders content into a complete output.
|
|
|
|
func RenderCmplx(content text.Rich) RenderOutput {
|
2020-07-18 07:16:47 +00:00
|
|
|
return RenderCmplxWithConfig(content, RenderConfig{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type RenderConfig struct {
|
|
|
|
// NoMentionLinks prevents the renderer from wrapping mentions with a
|
|
|
|
// hyperlink. This prevents invalid colors.
|
|
|
|
NoMentionLinks bool
|
|
|
|
}
|
|
|
|
|
2020-08-17 00:13:47 +00:00
|
|
|
// NoMentionLinks is the config to render author names. It disables author
|
|
|
|
// mention links, as there's no way to make normal names not appear blue.
|
|
|
|
var NoMentionLinks = RenderConfig{
|
|
|
|
NoMentionLinks: true,
|
|
|
|
}
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
func RenderCmplxWithConfig(content text.Rich, cfg RenderConfig) RenderOutput {
|
2020-06-19 22:40:06 +00:00
|
|
|
// Fast path.
|
|
|
|
if len(content.Segments) == 0 {
|
2020-07-14 07:24:55 +00:00
|
|
|
return RenderOutput{
|
|
|
|
Markup: hyphenate(html.EscapeString(content.Content)),
|
2020-08-13 22:50:51 +00:00
|
|
|
Input: content.Content,
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
buf.Grow(len(content.Content))
|
|
|
|
|
2020-07-17 00:21:14 +00:00
|
|
|
// Sort so that all ending points are sorted decrementally. We probably
|
|
|
|
// don't need SliceStable here, as we're sorting again.
|
|
|
|
sort.Slice(content.Segments, func(i, j int) bool {
|
|
|
|
_, i = content.Segments[i].Bounds()
|
|
|
|
_, j = content.Segments[j].Bounds()
|
|
|
|
return i > j
|
|
|
|
})
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// Sort so that all starting points are sorted incrementally.
|
|
|
|
sort.SliceStable(content.Segments, func(i, j int) bool {
|
|
|
|
i, _ = content.Segments[i].Bounds()
|
|
|
|
j, _ = content.Segments[j].Bounds()
|
|
|
|
return i < j
|
|
|
|
})
|
2020-06-19 22:40:06 +00:00
|
|
|
|
|
|
|
// map to append strings to indices
|
2020-06-28 23:01:08 +00:00
|
|
|
var appended = attrmap.NewAppendedMap()
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// map to store mentions
|
2020-10-15 06:32:11 +00:00
|
|
|
var mentions []MentionSegment
|
2020-07-14 07:24:55 +00:00
|
|
|
|
2020-06-19 22:40:06 +00:00
|
|
|
// Parse all segments.
|
|
|
|
for _, segment := range content.Segments {
|
|
|
|
start, end := segment.Bounds()
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if linker := segment.AsLinker(); linker != nil {
|
|
|
|
appended.Anchor(start, end, linker.Link())
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// Only inline images if start == end per specification.
|
|
|
|
if start == end {
|
|
|
|
if imager := segment.AsImager(); imager != nil {
|
|
|
|
appended.Open(start, composeImageMarkup(imager))
|
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if avatarer := segment.AsAvatarer(); avatarer != nil {
|
|
|
|
// Ends don't matter with images.
|
|
|
|
appended.Open(start, composeAvatarMarkup(avatarer))
|
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if colorer := segment.AsColorer(); colorer != nil {
|
|
|
|
appended.Span(start, end, colorAttrs(colorer.Color(), false)...)
|
2020-07-17 00:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// Mentioner needs to be before colorer, as we'd want the below color
|
|
|
|
// segment to also highlight the full mention as well as make the
|
|
|
|
// padding part of the hyperlink.
|
2020-10-15 06:32:11 +00:00
|
|
|
if mentioner := segment.AsMentioner(); mentioner != nil {
|
2020-07-14 07:24:55 +00:00
|
|
|
// Render the mention into "cchat://mention:0" or such. Other
|
|
|
|
// components will take care of showing the information.
|
2020-08-13 22:50:51 +00:00
|
|
|
if !cfg.NoMentionLinks {
|
2020-07-18 07:16:47 +00:00
|
|
|
appended.AnchorNU(start, end, fmt.Sprintf(f_Mention, len(mentions)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the mention segment into the list regardless of hyperlinks.
|
2020-10-15 06:32:11 +00:00
|
|
|
mentions = append(mentions, MentionSegment{
|
|
|
|
Segment: segment,
|
|
|
|
Mentioner: mentioner,
|
|
|
|
})
|
|
|
|
|
|
|
|
if colorer := segment.AsColorer(); colorer != nil {
|
|
|
|
// Only pad the name and add a dimmed background if the bounds
|
|
|
|
// do not cover the whole segment.
|
|
|
|
var cover = (start == 0) && (end == len(content.Content))
|
|
|
|
appended.Span(start, end, colorAttrs(colorer.Color(), !cover)...)
|
|
|
|
if !cover {
|
|
|
|
appended.Pad(start, end)
|
|
|
|
}
|
2020-07-09 04:14:56 +00:00
|
|
|
}
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if attributor := segment.AsAttributor(); attributor != nil {
|
|
|
|
appended.Span(start, end, markupAttr(attributor.Attribute()))
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if codeblocker := segment.AsCodeblocker(); codeblocker != nil {
|
|
|
|
start, end := segment.Bounds()
|
2020-06-28 23:01:08 +00:00
|
|
|
// Syntax highlight the codeblock.
|
2020-10-15 06:32:11 +00:00
|
|
|
hl.Segments(&appended, content.Content, start, end, codeblocker.CodeblockLanguage())
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
// TODO: make this not shit. Maybe make it somehow not rely on green
|
|
|
|
// arrows. Or maybe.
|
2020-10-15 06:32:11 +00:00
|
|
|
if segment.AsQuoteblocker() != nil {
|
2020-06-28 23:01:08 +00:00
|
|
|
appended.Span(start, end, `color="#789922"`)
|
2020-06-19 22:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastIndex = 0
|
|
|
|
|
2020-06-28 23:01:08 +00:00
|
|
|
for _, index := range appended.Finalize(len(content.Content)) {
|
2020-06-19 22:40:06 +00:00
|
|
|
// Write the content.
|
|
|
|
buf.WriteString(html.EscapeString(content.Content[lastIndex:index]))
|
|
|
|
// Write the tags.
|
2020-06-28 23:01:08 +00:00
|
|
|
buf.WriteString(appended.Get(index))
|
2020-06-19 22:40:06 +00:00
|
|
|
// Set the last index.
|
|
|
|
lastIndex = index
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
return RenderOutput{
|
|
|
|
Markup: hyphenate(buf.String()),
|
2020-08-13 22:50:51 +00:00
|
|
|
Input: content.Content,
|
2020-07-14 07:24:55 +00:00
|
|
|
Mentions: mentions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// splitRGBA splits the given rgba integer into rgb and a.
|
|
|
|
func splitRGBA(rgba uint32) (rgb, a uint32) {
|
|
|
|
rgb = rgba >> 8 // extract the RGB bits
|
|
|
|
a = rgba & 0xFF // extract the A bits
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// colorAttrs renders the given color into a list of attributes.
|
|
|
|
func colorAttrs(c uint32, bg bool) []string {
|
|
|
|
// Split the RGBA color value to calculate.
|
|
|
|
rgb, a := splitRGBA(c)
|
|
|
|
|
|
|
|
// Render the hex representation beforehand.
|
|
|
|
hex := fmt.Sprintf("#%06X", rgb)
|
|
|
|
|
|
|
|
attrs := make([]string, 1, 4)
|
|
|
|
attrs[0] = fmt.Sprintf(`color="%s"`, hex)
|
2020-07-14 07:24:55 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// If we have an alpha that isn't solid (100%), then write it.
|
|
|
|
if a < 0xFF {
|
|
|
|
// Calculate alpha percentage.
|
|
|
|
perc := a * 100 / 255
|
|
|
|
attrs = append(attrs, fmt.Sprintf(`fgalpha="%d%%"`, perc))
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// Draw a faded background if we explicitly requested for one.
|
2020-07-14 07:24:55 +00:00
|
|
|
if bg {
|
2020-10-15 06:32:11 +00:00
|
|
|
// Calculate how faded the background should be for visual purposes.
|
|
|
|
perc := a * 10 / 255 // always 10% or less.
|
|
|
|
attrs = append(attrs, fmt.Sprintf(`bgalpha="%d%%"`, perc))
|
|
|
|
attrs = append(attrs, fmt.Sprintf(`bgcolor="%s"`, hex))
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
2020-06-19 22:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
const (
|
|
|
|
// string constant for formatting width and height in URL fragments
|
|
|
|
f_FragmentSize = "w=%d;h=%d"
|
|
|
|
f_AnchorNoUnderline = `<a href="%s"><span underline="none">%s</span></a>`
|
|
|
|
)
|
2020-06-28 01:35:26 +00:00
|
|
|
|
|
|
|
func composeImageMarkup(imager text.Imager) string {
|
|
|
|
u, err := url.Parse(imager.Image())
|
|
|
|
if err != nil {
|
|
|
|
// If the URL is invalid, then just write a normal text.
|
|
|
|
return html.EscapeString(imager.ImageText())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the URL fragment with our own.
|
|
|
|
if w, h := imager.ImageSize(); w > 0 && h > 0 {
|
|
|
|
u.Fragment = fmt.Sprintf(f_FragmentSize, w, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2020-07-18 07:16:47 +00:00
|
|
|
f_AnchorNoUnderline,
|
2020-06-28 01:35:26 +00:00
|
|
|
html.EscapeString(u.String()), html.EscapeString(imager.ImageText()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func composeAvatarMarkup(avatarer text.Avatarer) string {
|
|
|
|
u, err := url.Parse(avatarer.Avatar())
|
|
|
|
if err != nil {
|
|
|
|
// If the URL is invalid, then just write a normal text.
|
|
|
|
return html.EscapeString(avatarer.AvatarText())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the URL fragment with our own.
|
|
|
|
if size := avatarer.AvatarSize(); size > 0 {
|
|
|
|
u.Fragment = fmt.Sprintf(f_FragmentSize, size, size) + ";round"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2020-07-18 07:16:47 +00:00
|
|
|
f_AnchorNoUnderline,
|
2020-07-14 07:24:55 +00:00
|
|
|
html.EscapeString(u.String()), html.EscapeString(avatarer.AvatarText()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
// FragmentImageSize tries to parse the width and height encoded in the URL
|
|
|
|
// fragment, which is inserted by the markup renderer. A pair of zero values are
|
|
|
|
// returned if there is none. The returned width and height will be the minimum
|
|
|
|
// of the given maxes and the encoded sizes.
|
2020-07-14 07:24:55 +00:00
|
|
|
func FragmentImageSize(URL string, maxw, maxh int) (w, h int, round bool) {
|
2020-06-28 01:35:26 +00:00
|
|
|
u, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the error, as we can check for the integers.
|
|
|
|
fmt.Sscanf(u.Fragment, f_FragmentSize, &w, &h)
|
2020-07-14 07:24:55 +00:00
|
|
|
round = strings.HasSuffix(u.Fragment, ";round")
|
2020-06-28 01:35:26 +00:00
|
|
|
|
|
|
|
if w > 0 && h > 0 {
|
2020-07-14 07:24:55 +00:00
|
|
|
w, h = imgutil.MaxSize(w, h, maxw, maxh)
|
|
|
|
return
|
2020-06-28 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
return maxw, maxh, round
|
2020-06-28 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 22:40:06 +00:00
|
|
|
func span(key, value string) string {
|
|
|
|
return "<span key=\"" + value + "\">"
|
|
|
|
}
|
2020-07-14 07:24:55 +00:00
|
|
|
|
|
|
|
func markupAttr(attr text.Attribute) string {
|
|
|
|
// meme fast path
|
|
|
|
if attr == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var attrs = make([]string, 0, 1)
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeBold) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `weight="bold"`)
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeItalics) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `style="italic"`)
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeUnderline) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `underline="single"`)
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeStrikethrough) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `strikethrough="true"`)
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeSpoiler) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `alpha="35%"`) // no fancy click here
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeMonospace) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `font_family="monospace"`)
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
if attr.Has(text.AttributeDimmed) {
|
2020-07-14 07:24:55 +00:00
|
|
|
attrs = append(attrs, `alpha="35%"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(attrs, " ")
|
|
|
|
}
|