Skip to content

Commit 8685e4a

Browse files
committed
[ShortCode] 3/x add html support
1 parent d6eefe0 commit 8685e4a

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pest = "2"
3232
pest_derive = "2"
3333
shell-words = { version = "1.0", optional = true }
3434
emojis = { version = "0.5.2", optional = true }
35+
unicode_reader = { version = "1.0.2", optional = true }
3536

3637
[dev-dependencies]
3738
timebomb = "0.1.2"
@@ -42,7 +43,7 @@ propfuzz = "0.0.1"
4243
[features]
4344
default = ["cli", "syntect"]
4445
cli = ["clap", "shell-words", "xdg"]
45-
shortcodes = ["emojis"]
46+
shortcodes = ["emojis", "unicode_reader"]
4647

4748
[target.'cfg(all(not(windows), not(target_arch="wasm32")))'.dependencies]
4849
xdg = { version = "^2.1", optional = true }

src/parser/shortcodes.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626
extern 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

5280
impl<'a> From<Vec<u8>> for NodeShortCode {

0 commit comments

Comments
 (0)