Skip to content

Commit

Permalink
get a basic cli working
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Apr 25, 2024
1 parent 21966d2 commit 276726b
Show file tree
Hide file tree
Showing 28 changed files with 2,153 additions and 32 deletions.
3 changes: 3 additions & 0 deletions lib/.shards.info
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ shards:
ameba:
git: https://github.com/crystal-ameba/ameba.git
version: 1.6.1
emoji:
git: https://github.com/veelenga/emoji.cr.git
version: 0.5.0
3 changes: 3 additions & 0 deletions lib/emoji/.ameba.yml
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
10 changes: 10 additions & 0 deletions lib/emoji/.gitignore
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

21 changes: 21 additions & 0 deletions lib/emoji/LICENSE
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.
106 changes: 106 additions & 0 deletions lib/emoji/README.md
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
Binary file added lib/emoji/assets/boom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lib/emoji/assets/screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lib/emoji/lib
10 changes: 10 additions & 0 deletions lib/emoji/shard.yml
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
30 changes: 30 additions & 0 deletions lib/emoji/src/emoji.cr
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
Loading

0 comments on commit 276726b

Please sign in to comment.