2020-05-19 06:07:22 +00:00
|
|
|
package text
|
|
|
|
|
|
|
|
// Rich is a normal text wrapped with optional format segments.
|
|
|
|
type Rich struct {
|
|
|
|
Content string
|
|
|
|
// Segments are optional rich-text segment markers.
|
|
|
|
Segments []Segment
|
|
|
|
}
|
|
|
|
|
2020-06-20 07:10:23 +00:00
|
|
|
// Empty returns whether or not the rich text is considered empty.
|
2020-05-19 06:07:22 +00:00
|
|
|
func (r Rich) Empty() bool {
|
|
|
|
return r.Content == ""
|
|
|
|
}
|
|
|
|
|
2020-06-20 07:10:23 +00:00
|
|
|
// String returns the content. This is used mainly for printing.
|
|
|
|
func (r Rich) String() string {
|
|
|
|
return r.Content
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:07:22 +00:00
|
|
|
// Segment is the minimum requirement for a format segment. Frontends will use
|
|
|
|
// this to determine when the format starts and ends. They will also assert this
|
|
|
|
// interface to any other formatting interface, including Linker, Colorer and
|
|
|
|
// Attributor.
|
2020-07-07 20:26:39 +00:00
|
|
|
//
|
|
|
|
// Note that a segment may implement multiple interfaces. For example, a
|
|
|
|
// Mentioner may also implement Colorer.
|
2020-05-19 06:07:22 +00:00
|
|
|
type Segment interface {
|
|
|
|
Bounds() (start, end int)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linker is a hyperlink format that a segment could implement. This implies
|
|
|
|
// that the segment should be replaced with a hyperlink, similarly to the anchor
|
|
|
|
// tag with href being the URL and the inner text being the text string.
|
|
|
|
type Linker interface {
|
2020-06-10 03:51:54 +00:00
|
|
|
Segment
|
2020-06-18 00:02:45 +00:00
|
|
|
Link() (url string)
|
2020-05-19 06:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Imager implies the segment should be replaced with a (possibly inlined)
|
2020-06-18 00:34:38 +00:00
|
|
|
// image. Only the starting bound matters, as images cannot substitute texts.
|
2020-05-19 06:07:22 +00:00
|
|
|
type Imager interface {
|
2020-06-10 03:51:54 +00:00
|
|
|
Segment
|
2020-06-18 03:45:30 +00:00
|
|
|
// Image returns the URL for the image.
|
2020-05-19 06:07:22 +00:00
|
|
|
Image() (url string)
|
2020-06-18 03:45:30 +00:00
|
|
|
// ImageSize returns the requested dimension for the image. This function
|
|
|
|
// could return (0, 0), which the frontend should use the image's
|
|
|
|
// dimensions.
|
|
|
|
ImageSize() (w, h int)
|
|
|
|
// ImageText returns the underlying text of the image. Frontends could use
|
|
|
|
// this for hovering or displaying the text instead of the image.
|
|
|
|
ImageText() string
|
2020-05-19 06:07:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 08:15:50 +00:00
|
|
|
// Avatarer implies the segment should be replaced with a rounded-corners
|
|
|
|
// image. This works similarly to Imager.
|
|
|
|
type Avatarer interface {
|
|
|
|
Segment
|
|
|
|
// Avatar returns the URL for the image.
|
|
|
|
Avatar() (url string)
|
|
|
|
// AvatarSize returns the requested dimension for the image. This function
|
|
|
|
// could return (0, 0), which the frontend should use the avatar's
|
|
|
|
// dimensions.
|
|
|
|
AvatarSize() (size int)
|
|
|
|
// AvatarText returns the underlying text of the image. Frontends could use
|
|
|
|
// this for hovering or displaying the text instead of the image.
|
|
|
|
AvatarText() string
|
|
|
|
}
|
|
|
|
|
2020-07-07 20:26:39 +00:00
|
|
|
// Mentioner implies that the segment can be clickable, and when clicked it
|
|
|
|
// should open up a dialog containing information from MentionInfo().
|
|
|
|
type Mentioner interface {
|
|
|
|
Segment
|
|
|
|
// MentionInfo returns the popup information of the mentioned segment. This
|
|
|
|
// is typically user information or something similar to that context.
|
|
|
|
MentionInfo() Rich
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:07:22 +00:00
|
|
|
// Colorer is a text color format that a segment could implement. This is to be
|
|
|
|
// applied directly onto the text.
|
|
|
|
type Colorer interface {
|
2020-06-10 03:51:54 +00:00
|
|
|
Segment
|
2020-06-04 04:15:52 +00:00
|
|
|
Color() uint32
|
2020-05-19 06:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attributor is a rich text markup format that a segment could implement. This
|
|
|
|
// is to be applied directly onto the text.
|
|
|
|
type Attributor interface {
|
2020-06-10 03:51:54 +00:00
|
|
|
Segment
|
2020-05-19 06:07:22 +00:00
|
|
|
Attribute() Attribute
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attribute is the type for basic rich text markup attributes.
|
|
|
|
type Attribute uint16
|
|
|
|
|
2020-06-17 23:10:53 +00:00
|
|
|
// HasAttr returns whether or not "attr" has "this" attribute.
|
|
|
|
func (attr Attribute) Has(this Attribute) bool {
|
|
|
|
return (attr & this) == this
|
|
|
|
}
|
|
|
|
|
2020-05-19 06:07:22 +00:00
|
|
|
const (
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrBold represents bold text.
|
2020-05-19 06:07:22 +00:00
|
|
|
AttrBold Attribute = 1 << iota
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrItalics represents italicized text.
|
2020-05-19 06:07:22 +00:00
|
|
|
AttrItalics
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrUnderline represents underlined text.
|
2020-05-19 06:07:22 +00:00
|
|
|
AttrUnderline
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrStrikethrough represents strikethrough text.
|
2020-05-19 06:07:22 +00:00
|
|
|
AttrStrikethrough
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrSpoiler represents spoiler text, which usually looks blacked out
|
|
|
|
// until hovered or clicked on.
|
2020-05-19 06:07:22 +00:00
|
|
|
AttrSpoiler
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrMonospace represents monospaced text, typically for inline code.
|
2020-05-19 06:07:22 +00:00
|
|
|
AttrMonospace
|
2020-06-10 03:51:54 +00:00
|
|
|
// AttrDimmed represents dimmed text, typically slightly less visible than
|
|
|
|
// other text.
|
|
|
|
AttrDimmed
|
2020-05-19 06:07:22 +00:00
|
|
|
)
|
2020-06-10 03:51:54 +00:00
|
|
|
|
|
|
|
// Codeblocker is a codeblock that supports optional syntax highlighting using
|
|
|
|
// the language given. Note that as this is a block, it will appear separately
|
|
|
|
// from the rest of the paragraph.
|
|
|
|
//
|
|
|
|
// This interface is equivalent to Markdown's codeblock syntax.
|
|
|
|
type Codeblocker interface {
|
|
|
|
Segment
|
|
|
|
CodeblockLanguage() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quoteblocker represents a quoteblock that behaves similarly to the blockquote
|
|
|
|
// HTML tag. The quoteblock may be represented typically by an actaul quoteblock
|
|
|
|
// or with green arrows prepended to each line.
|
|
|
|
type Quoteblocker interface {
|
|
|
|
Segment
|
|
|
|
// Quote does nothing; it's only here to distinguish the interface.
|
|
|
|
Quote()
|
|
|
|
}
|