support optional emoji translation

This commit is contained in:
Umputun
2019-07-13 14:04:03 -05:00
parent 9585bdff34
commit dda4f87db2
12 changed files with 4207 additions and 1 deletions
+1
View File
@@ -148,6 +148,7 @@ _this is the recommended way to run remark42_
| edit-time | EDIT_TIME | `5m` | edit window |
| read-age | READONLY_AGE | | read-only age of comments, days |
| img-proxy | IMG_PROXY | `false` | enable http->https proxy for images |
| emoji | EMOJI | `false` | enable emoji support |
| port | REMARK_PORT | `8080` | web server port |
| web-root | REMARK_WEB_ROOT | `./web` | web server root directory |
| update-limit | UPDATE_LIMIT | `0.5` | updates/sec limit |
+7 -1
View File
@@ -15,6 +15,7 @@ import (
bolt "github.com/coreos/bbolt"
log "github.com/go-pkgz/lgr"
"github.com/kyokomi/emoji"
authcache "github.com/patrickmn/go-cache"
"github.com/pkg/errors"
@@ -64,6 +65,7 @@ type ServerCommand struct {
WebRoot string `long:"web-root" env:"REMARK_WEB_ROOT" default:"./web" description:"web root directory"`
UpdateLimit float64 `long:"update-limit" env:"UPDATE_LIMIT" default:"0.5" description:"updates/sec limit"`
RestrictedWords []string `long:"restricted-words" env:"RESTRICTED_WORDS" description:"words prohibited to use in comments" env-delim:","`
EnableEmoji bool `long:"emoji" env:"EMOJI" description:"enable emoji"`
Auth struct {
TTL struct {
@@ -305,7 +307,11 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
}
imgProxy := &proxy.Image{Enabled: s.ImageProxy, RoutePath: "/api/v1/img", RemarkURL: s.RemarkURL}
commentFormatter := store.NewCommentFormatter(imgProxy)
emojiFmt := store.CommentConverterFunc(func(text string) string { return text })
if s.EnableEmoji {
emojiFmt = func(text string) string { return emoji.Sprint(text) }
}
commentFormatter := store.NewCommentFormatter(imgProxy, emojiFmt)
sslConfig, err := s.makeSSLConfig()
if err != nil {
+1
View File
@@ -24,6 +24,7 @@ require (
github.com/gorilla/feeds v1.1.1
github.com/hashicorp/go-multierror v1.0.0
github.com/jessevdk/go-flags v0.0.0-20180331124232-1c38ed7ad0cc
github.com/kyokomi/emoji v2.1.0+incompatible
github.com/microcosm-cc/bluemonday v1.0.2
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.8.1
+2
View File
@@ -87,6 +87,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kyokomi/emoji v2.1.0+incompatible h1:+DYU2RgpI6OHG4oQkM5KlqD3Wd3UPEsX8jamTo1Mp6o=
github.com/kyokomi/emoji v2.1.0+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA=
github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
github.com/nullrocks/identicon v0.0.0-20180626043057-7875f45b0022 h1:Ys0rDzh8s4UMlGaDa1UTA0sfKgvF0hQZzTYX8ktjiDc=
+2
View File
@@ -0,0 +1,2 @@
.idea
emoji.iml
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 kyokomi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+53
View File
@@ -0,0 +1,53 @@
# Emoji
Emoji is a simple golang package.
[![wercker status](https://app.wercker.com/status/7bef60de2c6d3e0e6c13d56b2393c5d8/s/master "wercker status")](https://app.wercker.com/project/byKey/7bef60de2c6d3e0e6c13d56b2393c5d8)
[![Coverage Status](https://coveralls.io/repos/kyokomi/emoji/badge.png?branch=master)](https://coveralls.io/r/kyokomi/emoji?branch=master)
[![GoDoc](https://godoc.org/github.com/kyokomi/emoji?status.svg)](https://godoc.org/github.com/kyokomi/emoji)
Get it:
```
go get github.com/kyokomi/emoji
```
Import it:
```
import (
"github.com/kyokomi/emoji"
)
```
## Usage
```go
package main
import (
"fmt"
"github.com/kyokomi/emoji"
)
func main() {
fmt.Println("Hello World Emoji!")
emoji.Println(":beer: Beer!!!")
pizzaMessage := emoji.Sprint("I like a :pizza: and :sushi:!!")
fmt.Println(pizzaMessage)
}
```
## Demo
![demo](screen/image.png)
## Reference
- [GitHub EMOJI CHEAT SHEET](http://www.emoji-cheat-sheet.com/)
## License
[MIT](https://github.com/kyokomi/emoji/blob/master/LICENSE)
+153
View File
@@ -0,0 +1,153 @@
// Package emoji terminal output.
package emoji
import (
"bytes"
"errors"
"fmt"
"io"
"regexp"
"unicode"
)
//go:generate generateEmojiCodeMap -pkg emoji
// Replace Padding character for emoji.
const (
ReplacePadding = " "
)
// CodeMap gets the underlying map of emoji.
func CodeMap() map[string]string {
return emojiCodeMap
}
// regular expression that matches :flag-[countrycode]:
var flagRegexp = regexp.MustCompile(":flag-([a-z]{2}):")
func emojize(x string) string {
str, ok := emojiCodeMap[x]
if ok {
return str + ReplacePadding
}
if match := flagRegexp.FindStringSubmatch(x); len(match) == 2 {
return regionalIndicator(match[1][0]) + regionalIndicator(match[1][1])
}
return x
}
// regionalIndicator maps a lowercase letter to a unicode regional indicator
func regionalIndicator(i byte) string {
return string('\U0001F1E6' + rune(i) - 'a')
}
func replaseEmoji(input *bytes.Buffer) string {
emoji := bytes.NewBufferString(":")
for {
i, _, err := input.ReadRune()
if err != nil {
// not replase
return emoji.String()
}
if i == ':' && emoji.Len() == 1 {
return emoji.String() + replaseEmoji(input)
}
emoji.WriteRune(i)
switch {
case unicode.IsSpace(i):
return emoji.String()
case i == ':':
return emojize(emoji.String())
}
}
}
func compile(x string) string {
if x == "" {
return ""
}
input := bytes.NewBufferString(x)
output := bytes.NewBufferString("")
for {
i, _, err := input.ReadRune()
if err != nil {
break
}
switch i {
default:
output.WriteRune(i)
case ':':
output.WriteString(replaseEmoji(input))
}
}
return output.String()
}
func compileValues(a *[]interface{}) {
for i, x := range *a {
if str, ok := x.(string); ok {
(*a)[i] = compile(str)
}
}
}
// Print is fmt.Print which supports emoji
func Print(a ...interface{}) (int, error) {
compileValues(&a)
return fmt.Print(a...)
}
// Println is fmt.Println which supports emoji
func Println(a ...interface{}) (int, error) {
compileValues(&a)
return fmt.Println(a...)
}
// Printf is fmt.Printf which supports emoji
func Printf(format string, a ...interface{}) (int, error) {
format = compile(format)
compileValues(&a)
return fmt.Printf(format, a...)
}
// Fprint is fmt.Fprint which supports emoji
func Fprint(w io.Writer, a ...interface{}) (int, error) {
compileValues(&a)
return fmt.Fprint(w, a...)
}
// Fprintln is fmt.Fprintln which supports emoji
func Fprintln(w io.Writer, a ...interface{}) (int, error) {
compileValues(&a)
return fmt.Fprintln(w, a...)
}
// Fprintf is fmt.Fprintf which supports emoji
func Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
format = compile(format)
compileValues(&a)
return fmt.Fprintf(w, format, a...)
}
// Sprint is fmt.Sprint which supports emoji
func Sprint(a ...interface{}) string {
compileValues(&a)
return fmt.Sprint(a...)
}
// Sprintf is fmt.Sprintf which supports emoji
func Sprintf(format string, a ...interface{}) string {
format = compile(format)
compileValues(&a)
return fmt.Sprintf(format, a...)
}
// Errorf is fmt.Errorf which supports emoji
func Errorf(format string, a ...interface{}) error {
compileValues(&a)
return errors.New(Sprintf(format, a...))
}
File diff suppressed because it is too large Load Diff
+25
View File
@@ -0,0 +1,25 @@
box: golang
build:
steps:
- setup-go-workspace
- script:
name: install goveralls
code: |
go get github.com/mattn/goveralls
- script:
name: go get
code: |
go get ./...
- script:
name: go build
code: |
go build ./...
- script:
name: go test
code: |
go test ./...
- script:
name: coveralls
code: |
goveralls -v -service wercker.com -repotoken $COVERALLS_TOKEN
+2
View File
@@ -68,6 +68,8 @@ github.com/hashicorp/golang-lru
github.com/hashicorp/golang-lru/simplelru
# github.com/jessevdk/go-flags v0.0.0-20180331124232-1c38ed7ad0cc
github.com/jessevdk/go-flags
# github.com/kyokomi/emoji v2.1.0+incompatible
github.com/kyokomi/emoji
# github.com/microcosm-cc/bluemonday v1.0.2
github.com/microcosm-cc/bluemonday
# github.com/nullrocks/identicon v0.0.0-20180626043057-7875f45b0022
+1
View File
@@ -47,5 +47,6 @@ services:
- NOTIFY_TYPE
- NOTIFY_TELEGRAM_TOKEN
- NOTIFY_TELEGRAM_CHAN
- EMOJI=true
volumes:
- ./var:/srv/var