convert emojis

This commit is contained in:
Maciej Jur 2024-04-17 23:31:20 +02:00
parent b41c815aed
commit 279677eb5c
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
3 changed files with 41 additions and 0 deletions

10
Cargo.lock generated
View file

@ -242,6 +242,15 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a"
[[package]]
name = "emojis"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ee61eb945bff65ee7d19d157d39c67c33290ff0742907413fd5eefd29edc979"
dependencies = [
"phf",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@ -861,6 +870,7 @@ version = "0.1.0"
dependencies = [
"aho-corasick",
"chrono",
"emojis",
"glob",
"grass",
"gray_matter",

View file

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
aho-corasick = "1.1.3"
chrono = "0.4.35"
emojis = "0.6.1"
glob = "0.3.1"
grass = { version = "0.13.2", default-features = false, features = ["random"] }
gray_matter = { version = "0.2.6", default-features = false, features = ["yaml"] }

View file

@ -35,6 +35,7 @@ static KATEX_B: Lazy<katex::Opts> = Lazy::new(||
pub fn parse(text: &str) -> String {
let stream = Parser::new_ext(text, *OPTS)
.map(make_math)
.map(make_emoji)
.collect::<Vec<_>>();
let stream = make_code(stream)
@ -99,3 +100,32 @@ fn make_ruby(event: Event) -> Vec<Event> {
_ => vec![event],
}
}
fn make_emoji(event: Event) -> Event {
match event {
Event::Text(ref text) => {
let mut buf = None;
let mut top = 0;
let mut old = 0;
for (idx, _) in text.match_indices(':') {
let key = &text[old..idx];
if let Some(emoji) = emojis::get_by_shortcode(key) {
let buf = buf.get_or_insert_with(|| String::with_capacity(text.len()));
buf.push_str(&text[top..old-1]);
buf.push_str(emoji.as_str());
top = idx;
}
old = idx + 1;
}
match buf {
None => event,
Some(buf) => Event::Text(buf.into())
}
},
_ => event,
}
}