-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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 #1892
Closed
Closed
Add emoji support #1892
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2016 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package helpers | ||
|
||
import ( | ||
"bytes" | ||
"github.com/kyokomi/emoji" | ||
"sync" | ||
) | ||
|
||
var ( | ||
emojiInit sync.Once | ||
|
||
emojis = make(map[string][]byte) | ||
|
||
emojiDelim = []byte(":") | ||
emojiWordDelim = []byte(" ") | ||
emojiMaxSize int | ||
) | ||
|
||
// Emojify "emojifies" the input source. | ||
// Note that the input byte slice will be modified if needed. | ||
// See http://www.emoji-cheat-sheet.com/ | ||
func Emojify(source []byte) []byte { | ||
|
||
emojiInit.Do(initEmoji) | ||
|
||
start := 0 | ||
k := bytes.Index(source[start:], emojiDelim) | ||
|
||
for k != -1 { | ||
|
||
j := start + k | ||
|
||
upper := j + emojiMaxSize | ||
|
||
if upper > len(source) { | ||
upper = len(source) | ||
} | ||
|
||
endEmoji := bytes.Index(source[j+1:upper], emojiDelim) | ||
|
||
if endEmoji < 0 { | ||
break | ||
} | ||
|
||
nextWordDelim := bytes.Index(source[j:upper], emojiWordDelim) | ||
|
||
if endEmoji == 0 || (nextWordDelim != -1 && nextWordDelim < endEmoji) { | ||
start += endEmoji + 1 | ||
} else { | ||
endKey := endEmoji + j + 2 | ||
emojiKey := source[j:endKey] | ||
|
||
if emoji, ok := emojis[string(emojiKey)]; ok { | ||
source = append(source[:j], append(emoji, source[endKey:]...)...) | ||
} | ||
|
||
start += endEmoji | ||
} | ||
|
||
if start >= len(source) { | ||
break | ||
} | ||
|
||
k = bytes.Index(source[start:], emojiDelim) | ||
} | ||
|
||
return source | ||
|
||
} | ||
|
||
func initEmoji() { | ||
emojiMap := emoji.CodeMap() | ||
|
||
for k, v := range emojiMap { | ||
emojis[k] = []byte(v + emoji.ReplacePadding) | ||
|
||
if len(k) > emojiMaxSize { | ||
emojiMaxSize = len(k) | ||
} | ||
} | ||
|
||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2016 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package helpers | ||
|
||
import ( | ||
"github.com/kyokomi/emoji" | ||
"github.com/spf13/hugo/bufferpool" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestEmojiCustom(t *testing.T) { | ||
for i, this := range []struct { | ||
input string | ||
expect []byte | ||
}{ | ||
{"A :smile: a day", []byte(emoji.Sprint("A :smile: a day"))}, | ||
{"A few :smile:s a day", []byte(emoji.Sprint("A few :smile:s a day"))}, | ||
{"A :smile: and a :beer: makes the day for sure.", []byte(emoji.Sprint("A :smile: and a :beer: makes the day for sure."))}, | ||
{"A :smile: and: a :beer:", []byte(emoji.Sprint("A :smile: and: a :beer:"))}, | ||
{"A :diamond_shape_with_a_dot_inside: and then some.", []byte(emoji.Sprint("A :diamond_shape_with_a_dot_inside: and then some."))}, | ||
{":smile:", []byte(emoji.Sprint(":smile:"))}, | ||
{":smi", []byte(":smi")}, | ||
{"A :smile:", []byte(emoji.Sprint("A :smile:"))}, | ||
{":beer:!", []byte(emoji.Sprint(":beer:!"))}, | ||
{"::smile:", []byte(emoji.Sprint("::smile:"))}, | ||
{":beer::", []byte(emoji.Sprint(":beer::"))}, | ||
{" :beer: :", []byte(emoji.Sprint(" :beer: :"))}, | ||
{":beer: and :smile: and another :beer:!", []byte(emoji.Sprint(":beer: and :smile: and another :beer:!"))}, | ||
{" :beer: : ", []byte(emoji.Sprint(" :beer: : "))}, | ||
{"No smilies for you!", []byte("No smilies for you!")}, | ||
{" The motto: no smiles! ", []byte(" The motto: no smiles! ")}, | ||
{":hugo_is_the_best_static_gen:", []byte(":hugo_is_the_best_static_gen:")}, | ||
{"은행 :smile: 은행", []byte(emoji.Sprint("은행 :smile: 은행"))}, | ||
} { | ||
result := Emojify([]byte(this.input)) | ||
|
||
if !reflect.DeepEqual(result, this.expect) { | ||
t.Errorf("[%d] got '%q' but expected %q", i, result, this.expect) | ||
} | ||
|
||
} | ||
} | ||
|
||
// The Emoji benchmarks below are heavily skewed in Hugo's direction: | ||
// | ||
// Hugo have a byte slice, wants a byte slice and doesn't mind if the original is modified. | ||
|
||
func BenchmarkEmojiKyokomiFprint(b *testing.B) { | ||
|
||
f := func(in []byte) []byte { | ||
buff := bufferpool.GetBuffer() | ||
defer bufferpool.PutBuffer(buff) | ||
emoji.Fprint(buff, string(in)) | ||
|
||
bc := make([]byte, buff.Len(), buff.Len()) | ||
copy(bc, buff.Bytes()) | ||
return bc | ||
} | ||
|
||
doBenchmarkEmoji(b, f) | ||
} | ||
|
||
func BenchmarkEmojiKyokomiSprint(b *testing.B) { | ||
|
||
f := func(in []byte) []byte { | ||
return []byte(emoji.Sprint(string(in))) | ||
} | ||
|
||
doBenchmarkEmoji(b, f) | ||
} | ||
|
||
func BenchmarkHugoEmoji(b *testing.B) { | ||
doBenchmarkEmoji(b, Emojify) | ||
} | ||
|
||
func doBenchmarkEmoji(b *testing.B, f func(in []byte) []byte) { | ||
|
||
type input struct { | ||
in []byte | ||
expect []byte | ||
} | ||
|
||
data := []struct { | ||
input string | ||
expect string | ||
}{ | ||
{"A :smile: a day", emoji.Sprint("A :smile: a day")}, | ||
{"A :smile: and a :beer: day keeps the doctor away", emoji.Sprint("A :smile: and a :beer: day keeps the doctor away")}, | ||
{"A :smile: a day and 10 " + strings.Repeat(":beer: ", 10), emoji.Sprint("A :smile: a day and 10 " + strings.Repeat(":beer: ", 10))}, | ||
{"No smiles today.", "No smiles today."}, | ||
{"No smiles for you or " + strings.Repeat("you ", 1000), "No smiles for you or " + strings.Repeat("you ", 1000)}, | ||
} | ||
|
||
var in []input = make([]input, b.N*len(data)) | ||
var cnt = 0 | ||
for i := 0; i < b.N; i++ { | ||
for _, this := range data { | ||
in[cnt] = input{[]byte(this.input), []byte(this.expect)} | ||
cnt++ | ||
} | ||
} | ||
|
||
b.ResetTimer() | ||
cnt = 0 | ||
for i := 0; i < b.N; i++ { | ||
for j := range data { | ||
currIn := in[cnt] | ||
cnt++ | ||
result := f(currIn.in) | ||
if len(result) != len(currIn.expect) { | ||
b.Fatalf("[%d] emoji std, got \n%q but expected \n%q", j, result, currIn.expect) | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid using
init
and running this code whenEnableEmoji == false
?Untested idea:
init
toinitEmojify
and call at the top ofEmojify
.emojis
be nil untilinitEmojify
is called.initEmojify
isemojis != nil
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The obvious alternative to init is sync.Once. No real runtime savings ... Remember, this is also called from a template tunc.
The if nil checks are all racy, me think. But an sync.Once is simple to do and safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also: I will remove the strings.Replacer stuff. I was just curious about its performance.