-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21966d2
commit 276726b
Showing
28 changed files
with
2,153 additions
and
32 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
LineLength: | ||
# Disallows lines longer that MaxLength number of symbols. | ||
MaxLength: 110 |
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,10 @@ | ||
/.deps/ | ||
/lib/ | ||
/.crystal/ | ||
/doc/ | ||
/bin | ||
|
||
# Libraries don't need dependency lock | ||
# Dependencies will be locked in application that uses them | ||
/shard.lock | ||
|
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015-2018 veelenga | ||
|
||
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. |
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,106 @@ | ||
# emoji.cr | ||
|
||
[![Crystal CI](https://github.com/veelenga/emoji.cr/actions/workflows/crystal.yml/badge.svg?branch=master)](https://github.com/veelenga/emoji.cr/actions/workflows/crystal.yml) | ||
|
||
Emoji library for Crystal. Inspired by [Emoji for Python](https://github.com/carpedm20/emoji) | ||
|
||
## Installation | ||
|
||
As a dependency in `shard.yml`: | ||
|
||
```yaml | ||
dependencies: | ||
emoji: | ||
github: veelenga/emoji.cr | ||
branch: master | ||
``` | ||
## Usage | ||
```crystal | ||
require "emoji" | ||
|
||
puts Emoji.emojize("I :heart: :beer: and :football:") | ||
``` | ||
Will print the following in console: | ||
![](assets/screen.png) | ||
Also it is possible to remove all emoji from the string: | ||
```crystal | ||
str = Emoji.emojize("Girl on :fire:") | ||
Emoji.sanitize(str) #=> "Girl on " | ||
``` | ||
|
||
Sanitizing is based on Emoji regex. There are two options available: | ||
|
||
- `:simple` emoji regex (default) | ||
- `:generated` emoji regex | ||
|
||
Simple regex uses unicode ranges to find emojis and may give some incorrect results. | ||
Generated regex is quite big, but works correctly in 100% cases. | ||
However, it is much slower than a simple regex. | ||
|
||
If you need more accuracy sanitizing emojis and don't care about performance, just use generated one: | ||
|
||
```crystal | ||
Emoji.sanitize(str, regex: :generated) | ||
``` | ||
|
||
### Regex | ||
|
||
```crystal | ||
require "emoji" | ||
string = "String which contains all kinds of emoji: | ||
- Singleton Emoji: (💎) | ||
- Textual singleton Emoji with Emoji variation: (▶️) | ||
- Emoji with skin tone modifier: (🖐🏼) | ||
- Region flag: (🇺🇦) | ||
- Sub-Region flag: (🏴) | ||
- Keycap sequence: (7️⃣) | ||
- Sequence using ZWJ (zero width joiner): (👨👩👧👦) | ||
" | ||
string.scan(Emoji::GENERATED_EMOJI_REGEX) do |m| | ||
puts "`#{m[0]}` - #{m[0].size} code points" | ||
end | ||
``` | ||
|
||
```console | ||
`💎` - 1 code points | ||
`▶️` - 2 code points | ||
`🖐🏼` - 2 code points | ||
`🇺🇦` - 2 code points | ||
`🏴` - 7 code points | ||
`7️⃣` - 3 code points | ||
`👨👩👧👦` - 7 code points | ||
``` | ||
|
||
### Binary | ||
|
||
You may also compile and use `emojize` binary that just prints to console emojized string: | ||
|
||
```console | ||
crystal build bin/emojize | ||
./emojize It will boom: :boom: | ||
``` | ||
|
||
![](assets/boom.png) | ||
|
||
## Resources | ||
|
||
- [Unicode® Technical Standard #51](http://www.unicode.org/reports/tr51/) | ||
- [Emoji Cheat Sheet](http://www.emoji-cheat-sheet.com/) | ||
|
||
## Contributing | ||
|
||
1. Fork it ( https://github.com/veelenga/emoji.cr/fork ) | ||
2. Create your feature branch (git checkout -b my-new-feature) | ||
3. Commit your changes (git commit -am 'Add some feature') | ||
4. Push to the branch (git push origin my-new-feature) | ||
5. Create a new Pull Request |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
.. |
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,10 @@ | ||
name: emoji | ||
version: 0.5.0 | ||
|
||
authors: | ||
- Vitalii Elenhaupt <[email protected]> | ||
|
||
development_dependencies: | ||
ameba: | ||
github: crystal-ameba/ameba | ||
version: ~> 1.0.0 |
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,30 @@ | ||
require "./emoji/*" | ||
|
||
module Emoji | ||
VERSION = "0.5.0" | ||
EMOJI_REGEX = /[\x{1f000}-\x{1ffff}\x{2049}-\x{3299}\x{a9}\x{2a}\x{ae}\x{fe0f}\x{203c}\x{200d}]+|[0-9#]\x{fe0f}\x{20e3}/ | ||
|
||
enum RegexType | ||
Simple | ||
Generated | ||
end | ||
|
||
@@map = Emoji::EMOJI_MAP | ||
|
||
def self.emojize(text : String) | ||
text.scan(/:[^(:\s)]+?:/).map(&.[0]) | ||
.uniq! | ||
.each do |name| | ||
code = @@map[name]? | ||
text = text.gsub(name, code) if code | ||
end | ||
text | ||
end | ||
|
||
def self.sanitize(text : String, regex : RegexType = :simple) | ||
case regex | ||
when .simple? then text.gsub(EMOJI_REGEX, "") | ||
when .generated? then text.gsub(GENERATED_EMOJI_REGEX, "") | ||
end | ||
end | ||
end |
Oops, something went wrong.