File tree Expand file tree Collapse file tree 3 files changed +54
-2
lines changed
Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ pest = "2"
3232pest_derive = " 2"
3333shell-words = { version = " 1.0" , optional = true }
3434emojis = { version = " 0.5.2" , optional = true }
35+ unicode_reader = { version = " 1.0.2" , optional = true }
3536
3637[dev-dependencies ]
3738timebomb = " 0.1.2"
@@ -42,7 +43,7 @@ propfuzz = "0.0.1"
4243[features ]
4344default = [" cli" , " syntect" ]
4445cli = [" clap" , " shell-words" , " xdg" ]
45- shortcodes = [" emojis" ]
46+ shortcodes = [" emojis" , " unicode_reader " ]
4647
4748[target .'cfg(all(not(windows), not(target_arch="wasm32")))' .dependencies ]
4849xdg = { version = " ^2.1" , optional = true }
Original file line number Diff line number Diff line change 2424 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525 */
2626extern crate emojis;
27+ extern crate unicode_reader;
2728
28- use std:: str;
29+ use std:: { io:: Cursor , str} ;
30+
31+ //use unicode_reader::{CodePoints, Graphemes};
2932
3033/// The details of an inline emoji
3134#[ derive( Debug , Clone ) ]
@@ -40,13 +43,38 @@ impl NodeShortCode {
4043 code. emoji ( ) . is_some ( )
4144 }
4245
46+ /// Returns the referenced shortcode
4347 pub fn shortcode ( & self ) -> Option < String > {
4448 self . shortcode . clone ( )
4549 }
4650
51+ /// Returns native utf8 character of the emoji referenced from the shortcode
4752 pub fn emoji ( & self ) -> Option < & ' static str > {
4853 Some ( emojis:: get_by_shortcode ( self . shortcode ( ) ?. as_str ( ) ) ?. as_str ( ) )
4954 }
55+
56+ /// Outputs an HTML entity instead of the native emoji utf8 glyph
57+ pub fn as_entity ( & self ) -> Option < String > {
58+ match self . emoji ( ) {
59+ Some ( emoji) => {
60+ let codepoints = unicode_reader:: CodePoints :: from ( Cursor :: new ( emoji) ) ;
61+ let entities = codepoints
62+ . map ( |r| {
63+ format ! (
64+ "&#{};" ,
65+ match r. ok( ) {
66+ Some ( c) => c,
67+ None => char :: from_u32( 0 ) . unwrap( ) ,
68+ }
69+ )
70+ } )
71+ . collect :: < Vec < String > > ( )
72+ . join ( "" ) ;
73+ Some ( entities)
74+ }
75+ None => None ,
76+ }
77+ }
5078}
5179
5280impl < ' a > From < Vec < u8 > > for NodeShortCode {
You can’t perform that action at this time.
0 commit comments