2020-06-19 22:40:06 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
2020-06-28 01:35:26 +00:00
|
|
|
"net/url"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return html.EscapeString(content.Content)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
buf.Grow(len(content.Content))
|
|
|
|
|
|
|
|
// // Sort so that all starting points are sorted incrementally.
|
|
|
|
// sort.Slice(content.Segments, func(i, j int) bool {
|
|
|
|
// i, _ = content.Segments[i].Bounds()
|
|
|
|
// j, _ = content.Segments[j].Bounds()
|
|
|
|
// return i < j
|
|
|
|
// })
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Parse all segments.
|
|
|
|
for _, segment := range content.Segments {
|
|
|
|
start, end := segment.Bounds()
|
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
if segment, ok := segment.(text.Linker); ok {
|
2020-06-28 23:01:08 +00:00
|
|
|
appended.Addf(start, `<a href="%s">`, html.EscapeString(segment.Link()))
|
|
|
|
appended.Add(end, "</a>")
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
if segment, ok := segment.(text.Imager); ok {
|
2020-06-19 22:40:06 +00:00
|
|
|
// Ends don't matter with images.
|
2020-06-28 23:01:08 +00:00
|
|
|
appended.Add(start, composeImageMarkup(segment))
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
if segment, ok := segment.(text.Colorer); ok {
|
2020-06-28 23:01:08 +00:00
|
|
|
appended.Span(start, end, fmt.Sprintf(`color="#%06X"`, segment.Color()))
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
if segment, ok := segment.(text.Attributor); ok {
|
2020-06-28 23:01:08 +00:00
|
|
|
appended.Span(start, end, markupAttr(segment.Attribute()))
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
2020-06-19 22:40:06 +00:00
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
if segment, ok := segment.(text.Codeblocker); ok {
|
2020-06-28 23:01:08 +00:00
|
|
|
// Syntax highlight the codeblock.
|
|
|
|
hl.Segments(&appended, content.Content, segment)
|
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.
|
|
|
|
if _, ok := segment.(text.Quoteblocker); ok {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
// string constant for formatting width and height in URL fragments
|
|
|
|
const f_FragmentSize = "w=%d;h=%d"
|
|
|
|
|
|
|
|
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(
|
|
|
|
`<a href="%s">%s</a>`,
|
|
|
|
html.EscapeString(u.String()), html.EscapeString(imager.ImageText()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func FragmentImageSize(URL string, maxw, maxh int) (w, h int) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
if w > 0 && h > 0 {
|
|
|
|
return imgutil.MaxSize(w, h, maxw, maxh)
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxw, maxh
|
|
|
|
}
|
|
|
|
|
2020-06-19 22:40:06 +00:00
|
|
|
func span(key, value string) string {
|
|
|
|
return "<span key=\"" + value + "\">"
|
|
|
|
}
|