Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shortcodes: capture all known aliases. #427

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ impl<'a, 'o> CommonMarkFormatter<'a, 'o> {
fn format_shortcode(&mut self, ne: &NodeShortCode, entering: bool) {
if entering {
write!(self, ":").unwrap();
self.output(ne.shortcode().as_bytes(), false, Escaping::Literal);
self.output(ne.code.as_bytes(), false, Escaping::Literal);
write!(self, ":").unwrap();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ impl<'o> HtmlFormatter<'o> {
#[cfg(feature = "shortcodes")]
NodeValue::ShortCode(ref nsc) => {
if entering {
self.output.write_all(nsc.emoji().as_bytes())?;
self.output.write_all(nsc.emoji.as_bytes())?;
}
}
NodeValue::Table(..) => {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
str::from_utf8_unchecked(&self.input[self.pos + 1..self.pos + 1 + matchlen - 1])
};

let nsc = NodeShortCode::try_from(shortcode).ok()?;
let nsc = NodeShortCode::resolve(shortcode)?;
self.pos += 1 + matchlen;

Some(self.make_inline(
Expand Down
47 changes: 16 additions & 31 deletions src/parser/shortcodes.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
use std::{convert::TryFrom, str};

/// The details of an inline emoji.
/// The details of an inline "shortcode" emoji/gemoji.
///
/// ("gemoji" name context: https://github.com/github/gemoji)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeShortCode(
/// A short code that is translated into an emoji
String,
);

impl NodeShortCode {
/// Checks whether the input is a valid short code.
pub fn is_valid(value: &str) -> bool {
emojis::get_by_shortcode(value).is_some()
}
pub struct NodeShortCode {
/// The shortcode that was resolved, e.g. "rabbit".
pub code: String,

/// Get the underlying shortcode.
pub fn shortcode(&self) -> &str {
&self.0
}

/// Get the emoji for this short code.
pub fn emoji(&self) -> &'static str {
emojis::get_by_shortcode(&self.0).unwrap().as_str()
}
/// The emoji `code` resolved to, e.g. "🐰".
pub emoji: String,
}

impl TryFrom<&str> for NodeShortCode {
type Error = ();

fn try_from(value: &str) -> Result<Self, ()> {
if Self::is_valid(value) {
Ok(Self(value.into()))
} else {
Err(())
}
impl NodeShortCode {
/// Checks whether the input is a valid short code.
pub fn resolve(code: &str) -> Option<Self> {
let emoji = emojis::get_by_shortcode(code)?;
Some(NodeShortCode {
code: code.to_string(),
emoji: emoji.to_string(),
})
}
}
2 changes: 1 addition & 1 deletion src/scanners.re
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ pub fn shortcode(s: &[u8]) -> Option<usize> {
let mut marker = 0;
let len = s.len();
/*!re2c
[A-Za-z_-]+ [:] { return Some(cursor); }
[A-Za-z0-9+_-]+ [:] { return Some(cursor); }
* { return None; }
*/
}
Expand Down
52 changes: 24 additions & 28 deletions src/scanners.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ fn exercise_full_api() {
let _: String = nl.title;
}
#[cfg(feature = "shortcodes")]
nodes::NodeValue::ShortCode(ne) => {
let _: &str = ne.shortcode();
nodes::NodeValue::ShortCode(nsc) => {
let _: String = nsc.code;
let _: String = nsc.emoji;
}
nodes::NodeValue::FootnoteReference(nfr) => {
let _: String = nfr.name;
Expand Down
11 changes: 11 additions & 0 deletions src/tests/shortcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ fn emojis() {
concat!("<p>Hello, happy days! :diego:</p>\n"),
);
}

#[test]
fn emojis_specials() {
// Take a quick trip to https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json
// with `jq -r .[].aliases[] | sort | grep -E '[^a-z_-]'` to see what else there is to see.
html_opts!(
[extension.shortcodes],
":+1: :-1: :clock12::1234: :1st_place_medal: :e-mail: :non-potable_water:",
"<p>👍 👎 🕛🔢 🥇 📧 🚱</p>\n",
);
}
2 changes: 1 addition & 1 deletion src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl<'o> XmlFormatter<'o> {
#[cfg(feature = "shortcodes")]
NodeValue::ShortCode(ref nsc) => {
self.output.write_all(b" id=\"")?;
self.escape(nsc.shortcode().as_bytes())?;
self.escape(nsc.code.as_bytes())?;
self.output.write_all(b"\"")?;
}
NodeValue::Escaped => {
Expand Down