From 8bbd0783541752c9b3aac9c9b3c6dda3d2e28c3d Mon Sep 17 00:00:00 2001 From: TritonHo Date: Mon, 27 Jun 2016 02:32:19 +0000 Subject: [PATCH 1/2] add emoji support --- compiler.go | 18 ++++++++++++++++-- html.go | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/compiler.go b/compiler.go index 5b34a6c..f5416f5 100644 --- a/compiler.go +++ b/compiler.go @@ -17,14 +17,26 @@ 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} +func NewCompiler(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 := `` + key + `` + array = append(array, key, img) + } + } + compiler.EmojiReplacer = strings.NewReplacer(array...) + return compiler } @@ -56,6 +68,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)) } diff --git a/html.go b/html.go index a62f4c6..644ac60 100644 --- a/html.go +++ b/html.go @@ -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. @@ -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) + `"` @@ -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") From 160120b3c144e0f28446cd48ad8da0436de80dda Mon Sep 17 00:00:00 2001 From: TritonHo Date: Mon, 27 Jun 2016 02:51:34 +0000 Subject: [PATCH 2/2] fix the code to support legacy code --- compiler.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler.go b/compiler.go index f5416f5..b7a9f7d 100644 --- a/compiler.go +++ b/compiler.go @@ -21,7 +21,10 @@ type Compiler struct { EmojiReplacer *strings.Replacer } -func NewCompiler(autoCloseTags, ignoreUnmatchedClosingTags bool, emoji map[string]string) Compiler { +func NewCompiler(autoCloseTags, ignoreUnmatchedClosingTags bool) Compiler { + 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 {