Documentation
¶
Overview ¶
Package micromessage is a MiniMessage (https://docs.advntr.dev/minimessage) parser and renderer for Gate/Minekube's component model (go.minekube.com/common/minecraft/component).
Example ¶
This mirrors how you'd use the library inside a Gate plugin:
msg, err := micromessage.Deserialize("<gradient:aqua:blue>Welcome</gradient> <red>%s</red>!")
if err != nil {
return err
}
return player.SendMessage(msg) // player is a go.minekube.com/gate proxy.Player
player.SendMessage takes a component.Component (msg component.Component, opts ...command.MessageOption), and *component.Text (what Deserialize returns) implements that interface directly, so no adapting is needed.
package main
import (
"fmt"
"github.com/Hoppou-Hangout/micromessage"
)
func main() {
msg, err := micromessage.Deserialize(`<gradient:aqua:blue>Welcome</gradient> <red><bold>friend</bold></red>!`)
if err != nil {
panic(err)
}
fmt.Println(len(msg.Children()) > 0)
}
Output: true
Index ¶
- Variables
- func Deserialize(input string, opts ...Option) (result *c.Text, err error)
- func MustDeserialize(input string, opts ...Option) *c.Text
- type ArgumentError
- type ArgumentQueue
- type Kind
- type ModifyingTag
- type Node
- type Option
- type ParserDirective
- type Preprocessor
- type Preset
- type StyleApplicable
- func ClickStyle(ev c.ClickEvent) StyleApplicable
- func ColorStyle(col mccolor.Color) StyleApplicable
- func DecorationStyle(dec c.Decoration, on bool) StyleApplicable
- func FontStyle(k key.Key) StyleApplicable
- func HoverStyle(ev c.HoverEvent) StyleApplicable
- func InsertionStyle(v string) StyleApplicable
- func ShadowStyle(sc *c.ShadowColor) StyleApplicable
- type Tag
- type TagResolver
- type TagResolverBuilder
- type TagResolverFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultPreset contains every standard tag with no restrictions -- // the same as never applying a preset at all, matching Adventure's // MiniMessage.Preset.DEFAULT. DefaultPreset = Preset{WithTags(StandardTags.All())} // NonInteractablePreset disables click events, hover events, and text // insertion (the "<click>"/"<hover>"/"<insert>" tags are not part of its // tag set at all, so they render as literal text), and additionally // strips any interactable style a custom TagResolver might still // introduce, matching Adventure's MiniMessage.Preset.NON_INTERACTABLE. NonInteractablePreset = Preset{ WithTags(NewTagResolverBuilder(). Resolver(StandardTags.Color()). Resolver(StandardTags.Decorations()). Resolver(StandardTags.Font()). Resolver(StandardTags.Shadow()). Resolver(StandardTags.Gradient()). Resolver(StandardTags.Rainbow()). Resolver(StandardTags.Transition()). Resolver(StandardTags.Translatable()). Resolver(StandardTags.Newline()). Resolver(StandardTags.Reset()). Build()), withPostProcess(stripInteractable), } // FormattedTextPreset only allows text components and their formatting // (color, shadow, font, decoration) -- no click/hover/insertion tags, // and its post-processor drops any non-text component (e.g. a // Translation a custom TagResolver produced) from the result, matching // Adventure's MiniMessage.Preset.FORMATTED_TEXT. FormattedTextPreset = Preset{ WithTags(NewTagResolverBuilder(). Resolver(StandardTags.Color()). Resolver(StandardTags.Decorations()). Resolver(StandardTags.Font()). Resolver(StandardTags.Shadow()). Resolver(StandardTags.Gradient()). Resolver(StandardTags.Rainbow()). Resolver(StandardTags.Transition()). Resolver(StandardTags.Newline()). Resolver(StandardTags.Reset()). Build()), withPostProcess(formattedTextOnly), } )
var StandardTags standardTags
StandardTags exposes the built-in tag vocabulary as individually selectable TagResolvers, matching Adventure's StandardTags class. Combine them with TagResolverBuilder (or pass several to WithTags) to enable only a subset -- e.g. WithTags(StandardTags.Color()) allows "<red>" and "<color:...>" but leaves "<bold>" (and everything else) as literal text. StandardTags.All() is the default set used when WithTags is never called.
Functions ¶
func Deserialize ¶
Deserialize parses a MiniMessage string into a *component.Text tree compatible with go.minekube.com/common and, by extension, Gate.
By default every standard tag (StandardTags.All()) is recognized and unresolved/invalid tags are rendered as literal text -- matching Adventure's MiniMessage.miniMessage(). Pass Options to customize this: WithTags to restrict/replace the tag vocabulary, WithTagResolver to add placeholders or other dynamic tags, WithPreprocessor to rewrite the raw input before parsing, WithStrict to error on unclosed tags, and WithDebug to receive diagnostics.
Types ¶
type ArgumentError ¶ added in v0.6.0
type ArgumentError struct{ Message string }
ArgumentError is the panic value PopOr/PeekOr raise on a missing required argument; Deserialize recovers it into a plain error.
func (*ArgumentError) Error ¶ added in v0.6.0
func (e *ArgumentError) Error() string
type ArgumentQueue ¶ added in v0.6.0
type ArgumentQueue struct {
// contains filtered or unexported fields
}
ArgumentQueue gives a TagResolver access to a tag's raw arguments, matching Adventure's ArgumentQueue/Tag.Argument.
func (*ArgumentQueue) HasNext ¶ added in v0.6.0
func (q *ArgumentQueue) HasNext() bool
HasNext reports whether another argument remains.
func (*ArgumentQueue) PeekOr ¶ added in v0.6.0
func (q *ArgumentQueue) PeekOr(errorMessage string) string
PeekOr is like PopOr but does not advance the queue.
func (*ArgumentQueue) Pop ¶ added in v0.6.0
func (q *ArgumentQueue) Pop() (value string, ok bool)
Pop returns the next argument and advances the queue, or ok=false if there isn't one.
func (*ArgumentQueue) PopOr ¶ added in v0.6.0
func (q *ArgumentQueue) PopOr(errorMessage string) string
PopOr returns the next argument and advances the queue, or panics with a *ArgumentError carrying errorMessage if there isn't one -- matching Adventure's Tag.Argument#popOr, which interrupts tag resolution on missing required arguments. Deserialize recovers this into a normal error.
func (*ArgumentQueue) Remaining ¶ added in v0.6.0
func (q *ArgumentQueue) Remaining() int
Remaining returns how many arguments are left.
func (*ArgumentQueue) Rest ¶ added in v0.6.0
func (q *ArgumentQueue) Rest() []string
Rest returns every remaining argument and drains the queue.
type ModifyingTag ¶ added in v0.6.0
type ModifyingTag interface {
Visit(node *Node)
PostVisit()
Apply(current c.Component, depth int) c.Component
}
ModifyingTag is the interface custom Modifying tags implement, matching Adventure's Modifying tag kind (used internally for <gradient> and <rainbow>). Visit is called once per node in the wrapped content, in depth-first order, before any component is produced; PostVisit is called once after the full traversal; then Apply is called once per produced child component, in order, to transform it.
If a Modifying tag carries state across Visit calls, its TagResolver must return a fresh instance per tag occurrence -- state is not reset between uses.
type Node ¶ added in v0.3.1
type Node struct {
Kind Kind
Text string // set when Kind == KindText
Name string // tag name, set when Kind == KindElement
Args []string
Tag Tag // the Tag this element resolved to, already looked up at parse time
Children []*Node
}
Node is either a run of text (which may be the exact source text of a tag that didn't resolve to anything) or a resolved tag.
type Option ¶ added in v0.6.0
type Option func(*options)
Option configures a Deserialize call.
func WithDebug ¶ added in v0.6.0
WithDebug registers a callback that receives diagnostic messages about why a tag failed to resolve, matching Adventure's Builder#debug(Consumer<String>).
func WithPreprocessor ¶ added in v0.6.0
func WithPreprocessor(p Preprocessor) Option
WithPreprocessor registers a Preprocessor run over the raw input string before parsing. Multiple preprocessors may be passed (or this option repeated); they run in the order added, each seeing the previous one's output.
func WithStrict ¶ added in v0.6.0
WithStrict enables strict mode: an unclosed tag is a parse error instead of being auto-closed at EOF, matching Adventure's Builder#strict(true). Tags that simply don't resolve to anything are still rendered as literal text either way.
func WithTagResolver ¶ added in v0.6.0
func WithTagResolver(r TagResolver) Option
WithTagResolver registers an additional TagResolver, tried after the active tag set (the standard set, or whatever WithTags supplied). This is the common way to add placeholders/dynamic tags without disabling any built-ins, matching Adventure's builder.editTags(b -> b.resolver(...)).
func WithTags ¶ added in v0.6.0
func WithTags(resolver TagResolver) Option
WithTags replaces the standard built-in tag set with resolver, matching Adventure's MiniMessage.builder().tags(resolver). Built-in tags not included in resolver (directly or via StandardTags.All()) stop being recognized and are rendered as literal text. If never called, the default is StandardTags.All().
type ParserDirective ¶ added in v0.6.0
type ParserDirective int
ParserDirective is an instruction to the parser rather than something that produces or modifies a component, matching Adventure's ParserDirective.
const ( // Reset indicates that this tag should close all currently open tags, // exactly like <reset>. Registering a resolver for a custom tag name via // DirectiveTag(Reset) gives it identical behavior to <reset> under a // different name. Reset ParserDirective = iota )
type Preprocessor ¶ added in v0.6.0
Preprocessor transforms the raw input string before it is lexed and parsed, e.g. to translate legacy '&'-formatted color codes into MiniMessage tags. Preprocessors run in the order passed to Deserialize, each seeing the previous one's output.
type Preset ¶ added in v0.6.0
type Preset []Option
Preset is a pre-built bundle of Options, matching Adventure's MiniMessage.Preset. Turn one into an Option with Apply:
msg, err := micromessage.Deserialize(input, micromessage.NonInteractablePreset.Apply())
type StyleApplicable ¶ added in v0.6.0
type StyleApplicable interface {
// contains filtered or unexported methods
}
StyleApplicable is one change StylingTag applies to a c.Style, matching Adventure's StyleBuilderApplicable. Build one with ColorStyle, DecorationStyle, ClickStyle, HoverStyle, FontStyle, InsertionStyle, or ShadowStyle.
func ClickStyle ¶ added in v0.6.0
func ClickStyle(ev c.ClickEvent) StyleApplicable
func ColorStyle ¶ added in v0.6.0
func ColorStyle(col mccolor.Color) StyleApplicable
func DecorationStyle ¶ added in v0.6.0
func DecorationStyle(dec c.Decoration, on bool) StyleApplicable
func FontStyle ¶ added in v0.6.0
func FontStyle(k key.Key) StyleApplicable
func HoverStyle ¶ added in v0.6.0
func HoverStyle(ev c.HoverEvent) StyleApplicable
func InsertionStyle ¶ added in v0.6.0
func InsertionStyle(v string) StyleApplicable
func ShadowStyle ¶ added in v0.6.0
func ShadowStyle(sc *c.ShadowColor) StyleApplicable
type Tag ¶ added in v0.6.0
type Tag struct {
// contains filtered or unexported fields
}
Tag is what a TagResolver produces for a tag it recognizes. Build one with Text, Parsed, ComponentTag, StylingTag, or ModifyingTag -- matching Adventure's three Tag kinds (PreProcess, Inserting, Modifying) plus ParserDirective for tags like RESET.
func ComponentTag ¶ added in v0.6.0
ComponentTag returns a Tag that inserts comp verbatim, with whatever style comp already carries -- the ambient style at the tag's position is not applied to it. Self-closing by default, matching Adventure's Placeholder.component.
func DirectiveTag ¶ added in v0.6.0
func DirectiveTag(d ParserDirective) Tag
DirectiveTag returns a Tag that behaves as a parser directive, e.g. an alternate spelling of <reset>. See ParserDirective.
func ModifyingTagValue ¶ added in v0.6.0
func ModifyingTagValue(m ModifyingTag) Tag
ModifyingTagValue returns a Tag backed by a custom ModifyingTag implementation, matching Adventure's Modifying tags (used internally for <gradient> and <rainbow>).
func Parsed ¶ added in v0.6.0
Parsed returns a Tag whose value is parsed as its own MiniMessage snippet and spliced in at the tag's position, inheriting the ambient style. The same TagResolvers apply recursively inside it (capped at maxTagDepth to catch self-reference). This is a pragmatic stand-in for Adventure's PreProcess tags: rather than splicing the raw string back into the input and re-lexing (which needs a resolver-aware, single-pass parser), it is parsed and rendered as an independent sub-document at render time. For all but pathological cases (tag boundaries split across the substitution) the result is the same. Self-closing by default -- it never wraps content of its own, so trailing input after it stays ordinary sibling text.
func StylingTag ¶ added in v0.6.0
func StylingTag(styles ...StyleApplicable) Tag
StylingTag returns an Inserting Tag that applies a set of style changes to its wrapped content, matching Adventure's Tag.styling(StyleBuilderApplicable...). Not self-closing: it wraps whatever follows until a matching close tag (or EOF), same as the built-in <color>/<bold>/<click>/... tags.
func Text ¶
Text returns a Tag whose value is inserted as literal text, taking on the ambient style at the tag's position but never itself re-parsed as MiniMessage. It is self-closing by default, matching Adventure's Placeholder.unparsed.
func (Tag) SelfClosing ¶ added in v0.6.0
SelfClosing marks an Inserting Tag (Text/ComponentTag/StylingTag) as never taking a close tag or wrapping content, even if the input writes it as <tag>...</tag> or leaves it unclosed. Text and ComponentTag are self-closing by default; use this to opt StylingTag/custom tags in too.
type TagResolver ¶ added in v0.6.0
type TagResolver interface {
ResolveTag(name string, args *ArgumentQueue) (tag Tag, ok bool)
}
TagResolver supplies a Tag for a tag occurrence (name + arguments) the parser encounters. Built-in tags (color names, "bold", "gradient", ...) are themselves ordinary TagResolvers -- see StandardTags -- so the active tag vocabulary is entirely determined by which resolvers are in play; see WithTags and WithTagResolver.
Tag names passed to ResolveTag are already lower-cased (a leading "!"/"?"/"#" sigil, if any, is preserved), matching Adventure's case-insensitive tag name resolution.
func Placeholder ¶
func Placeholder(name string, tag Tag) TagResolver
Placeholder returns a TagResolver that matches a single tag name case-insensitively and always resolves to tag, ignoring any arguments -- the common case (a named value substituted wherever <name> appears).
func Resolver ¶ added in v0.6.0
func Resolver(name string, d ParserDirective) TagResolver
Resolver returns a TagResolver that matches a single tag name case-insensitively and resolves to a ParserDirective, e.g. a "<clear>" tag that behaves exactly like "<reset>".
type TagResolverBuilder ¶ added in v0.6.0
type TagResolverBuilder struct {
// contains filtered or unexported fields
}
TagResolverBuilder composes multiple TagResolvers into one, tried in the order added, matching Adventure's TagResolver.builder().
func NewTagResolverBuilder ¶ added in v0.6.0
func NewTagResolverBuilder() *TagResolverBuilder
NewTagResolverBuilder returns an empty TagResolverBuilder, matching Adventure's TagResolver.builder().
func (*TagResolverBuilder) Build ¶ added in v0.6.0
func (b *TagResolverBuilder) Build() TagResolver
Build returns a single TagResolver that tries the most-recently-added resolver first, matching Adventure's TagResolverBuilder (the last resolver added for a given name wins).
func (*TagResolverBuilder) Resolver ¶ added in v0.6.0
func (b *TagResolverBuilder) Resolver(r TagResolver) *TagResolverBuilder
Resolver appends r to the set of resolvers this builder combines.
type TagResolverFunc ¶ added in v0.6.0
type TagResolverFunc func(name string, args *ArgumentQueue) (Tag, bool)
TagResolverFunc adapts a plain function to a TagResolver.
func (TagResolverFunc) ResolveTag ¶ added in v0.6.0
func (f TagResolverFunc) ResolveTag(name string, args *ArgumentQueue) (Tag, bool)