gitea-tea/vendor/github.com/yuin/goldmark-emoji/definition/definition.go
Norwin 222d0501df Detect markdown line width, resolve relative URLs (#332)
~~this is semi-blocked by https://github.com/charmbracelet/glamour/pull/96, but behaviour isn't really worse than the previous behaviour (most links work, some are still broken)~~

#### testcase for link resolver
```
tea pr 332
tea checkout 332 && make install && tea pr 332
```

- [rel](./332)
- [abs](/gitea/tea/pulls/332)
- [full](https://gitea.com/gitea/tea/pulls/332)

Co-authored-by: Norwin Roosen <git@nroo.de>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/332
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
2021-03-12 20:28:46 +08:00

107 lines
2.2 KiB
Go

package definition
// Emoji is a data structure that holds a single emoji.
type Emoji struct {
// Name is a name of this emoji.
Name string
// ShortNames is a shorter representation of this emoji.
ShortNames []string
// Unicode is an unicode representation of this emoji.
Unicode []rune
}
// NewEmoji returns a new Emoji.
func NewEmoji(name string, unicode []rune, shortNames ...string) Emoji {
if len(shortNames) == 0 {
panic("Emoji must have at leat 1 short name.")
}
if unicode == nil || len(unicode) == 0 {
unicode = []rune{0xFFFD}
}
return Emoji{
Name: name,
ShortNames: shortNames,
Unicode: unicode,
}
}
// IsUnicode returns true if this emoji is defined in unicode, otherwise false.
func (em *Emoji) IsUnicode() bool {
return !(len(em.Unicode) == 1 && em.Unicode[0] == 0xFFFD)
}
// Emojis is a collection of emojis.
type Emojis interface {
// Get returns (*Emoji, true) if found mapping associated with given short name, otherwise (nil, false).
Get(shortName string) (*Emoji, bool)
// Add adds new emojis to this collection.
Add(Emojis)
// Clone clones this collection.
Clone() Emojis
}
type emojis struct {
list []Emoji
m map[string]*Emoji
children []Emojis
}
// NewEmojis returns a new Emojis.
func NewEmojis(es ...Emoji) Emojis {
m := &emojis{
list: es,
m: map[string]*Emoji{},
children: []Emojis{},
}
for i, _ := range es {
emoji := &m.list[i]
for _, s := range emoji.ShortNames {
m.m[s] = emoji
}
}
return m
}
func (m *emojis) Add(emojis Emojis) {
m.children = append(m.children, emojis)
}
func (m *emojis) Clone() Emojis {
es := &emojis{
list: m.list,
m: m.m,
children: make([]Emojis, len(m.children)),
}
copy(es.children, m.children)
return es
}
func (m *emojis) Get(shortName string) (*Emoji, bool) {
v, ok := m.m[shortName]
if ok {
return v, ok
}
for _, es := range m.children {
v, ok := es.Get(shortName)
if ok {
return v, ok
}
}
return nil, false
}
// EmojisOption sets options for Emojis.
type EmojisOption func(Emojis)
// WithEmojis is an EmojisOption that adds emojis to the Emojis.
func WithEmojis(emojis ...Emoji) EmojisOption {
return func(m Emojis) {
m.Add(NewEmojis(emojis...))
}
}