Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add emoji support #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,29 @@ type Compiler struct {
defaultCompiler TagCompilerFunc
AutoCloseTags bool
IgnoreUnmatchedClosingTags bool

EmojiReplacer *strings.Replacer
}

func NewCompiler(autoCloseTags, ignoreUnmatchedClosingTags bool) Compiler {
compiler := Compiler{make(map[string]TagCompilerFunc), DefaultTagCompiler, autoCloseTags, ignoreUnmatchedClosingTags}
return NewCompilerWithEmoji(autoCloseTags, ignoreUnmatchedClosingTags, nil)
}
func NewCompilerWithEmoji(autoCloseTags, ignoreUnmatchedClosingTags bool, emoji map[string]string) Compiler {
compiler := Compiler{make(map[string]TagCompilerFunc), DefaultTagCompiler, autoCloseTags, ignoreUnmatchedClosingTags, nil}

for tag, compilerFunc := range DefaultTagCompilers {
compiler.SetTag(tag, compilerFunc)
}

array := []string{}
if emoji != nil {
for key, value := range emoji {
img := `<img src="` + value + `" alt="` + key + `">`
array = append(array, key, img)
}
}
compiler.EmojiReplacer = strings.NewReplacer(array...)

return compiler
}

Expand Down Expand Up @@ -56,6 +71,8 @@ func (c Compiler) CompileTree(node *BBCodeNode) *HTMLTag {
if node.ID == TEXT {
out.Value = node.Value.(string)
InsertNewlines(out)
out.SetEmojiReplacer(c.EmojiReplacer)

for _, child := range node.Children {
out.AppendChild(c.CompileTree(child))
}
Expand Down
21 changes: 17 additions & 4 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (

// HTMLTag represents a DOM node.
type HTMLTag struct {
Name string
Value string
Attrs map[string]string
Children []*HTMLTag
Name string
Value string
Attrs map[string]string
EmojiReplacer *strings.Replacer
Children []*HTMLTag
}

// NewHTMLTag creates a new HTMLTag with string contents specified by value.
Expand All @@ -32,6 +33,11 @@ func (t *HTMLTag) String() string {
if len(t.Value) > 0 {
value = html.EscapeString(t.Value)
}
if t.EmojiReplacer != nil {
//replacement for emoji
value = t.EmojiReplacer.Replace(value)
}

var attrString string
for key, value := range t.Attrs {
attrString += " " + key + `="` + strings.Replace(html.EscapeString(value), "\n", "", -1) + `"`
Expand Down Expand Up @@ -62,6 +68,13 @@ func (t *HTMLTag) AppendChild(child *HTMLTag) *HTMLTag {
return t
}

func (t *HTMLTag) SetEmojiReplacer(r *strings.Replacer) {
t.EmojiReplacer = r
for _, child := range t.Children {
child.SetEmojiReplacer(r)
}
}

func InsertNewlines(out *HTMLTag) {
if strings.ContainsRune(out.Value, '\n') {
parts := strings.Split(out.Value, "\n")
Expand Down