| 1 |
// Copyright 2016 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package helpers |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"sync" |
| 19 |
|
| 20 |
"github.com/kyokomi/emoji/v2" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
emojiInit sync.Once |
| 25 |
|
| 26 |
emojis = make(map[string][]byte) |
| 27 |
|
| 28 |
emojiDelim = []byte(":") |
| 29 |
emojiWordDelim = []byte(" ") |
| 30 |
emojiMaxSize int |
| 31 |
) |
| 32 |
|
| 33 |
// Emojify "emojifies" the input source. |
| 34 |
// Note that the input byte slice will be modified if needed. |
| 35 |
// See http://www.emoji-cheat-sheet.com/ |
| 36 |
func Emojify(source []byte) []byte { |
| 37 |
emojiInit.Do(initEmoji) |
| 38 |
|
| 39 |
start := 0 |
| 40 |
k := bytes.Index(source[start:], emojiDelim) |
| 41 |
|
| 42 |
for k != -1 { |
| 43 |
|
| 44 |
j := start + k |
| 45 |
|
| 46 |
upper := min(j+emojiMaxSize, len(source)) |
| 47 |
|
| 48 |
endEmoji := bytes.Index(source[j+1:upper], emojiDelim) |
| 49 |
nextWordDelim := bytes.Index(source[j:upper], emojiWordDelim) |
| 50 |
|
| 51 |
if endEmoji < 0 { |
| 52 |
start++ |
| 53 |
} else if endEmoji == 0 || (nextWordDelim != -1 && nextWordDelim < endEmoji) { |
| 54 |
start += endEmoji + 1 |
| 55 |
} else { |
| 56 |
endKey := endEmoji + j + 2 |
| 57 |
emojiKey := source[j:endKey] |
| 58 |
|
| 59 |
if emoji, ok := emojis[string(emojiKey)]; ok { |
| 60 |
source = append(source[:j], append(emoji, source[endKey:]...)...) |
| 61 |
} |
| 62 |
|
| 63 |
start += endEmoji |
| 64 |
} |
| 65 |
|
| 66 |
if start >= len(source) { |
| 67 |
break |
| 68 |
} |
| 69 |
|
| 70 |
k = bytes.Index(source[start:], emojiDelim) |
| 71 |
} |
| 72 |
|
| 73 |
return source |
| 74 |
} |
| 75 |
|
| 76 |
func initEmoji() { |
| 77 |
emojiMap := emoji.CodeMap() |
| 78 |
|
| 79 |
for k, v := range emojiMap { |
| 80 |
emojis[k] = []byte(v) |
| 81 |
|
| 82 |
if len(k) > emojiMaxSize { |
| 83 |
emojiMaxSize = len(k) |
| 84 |
} |
| 85 |
} |
| 86 |
} |