Skip to content

Commit

Permalink
feat: output metadata (closes #7 and #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
wait-what committed Jul 16, 2023
1 parent ebdf946 commit a5b22bb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
47 changes: 47 additions & 0 deletions docs/output-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Output metadata
The output metadata consists of a JSON file with the structure shown below. It is loosely based on [Google emoji metadata](https://github.com/googlefonts/emoji-metadata).

```json
[
{
"group": "group_name",
"emojis": [
// Emoji objects
]
}
]
```

## Emoji object
### Required fields
- `src` is the path to the emoji image
- `description` is a string

### Optional fields
- `shortcodes` can be an array of strings, or an empty array
- `codepoint` and `root_codepoint` can be an array of numbers, or an empty array
- `category` can be an array of strings, or an empty array

```json
{
"codepoint": [
9996,
8205,
128994
],
"root_codepoint": [
9996
],
"src": "expressions/skintones/human/victory_hand_g",
"shortcodes": [
":victory_hand_g:",
":v_g:"
],
"category": [
"expressions",
"skintones",
"human"
],
"description": "something"
},
```
4 changes: 3 additions & 1 deletion sample-input/emoji/hands/hands.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ description = "something"
tags = [ "extra" ]
shortcodes = [ "victory_hand%shortcode", "v%shortcode" ]
codepoint = [ "U+270C", "$zwj", "%codepoint" ]
root_codepoint = [ "U+270C" ]
colormaps = [ "$skintones.extra" ]

[[emoji]]
Expand All @@ -55,6 +56,7 @@ name = "Victory hand (human)"
category = [ "expressions", "skintones", "human" ]
description = "something"
tags = [ "unicode" ]
codepoint = [ "U+270C", "$vs16" ]
codepoint = [ "U+270C" ]
root_codepoint = [ "U+270C" ]
shortcodes = [ "victory_hand", "v" ]
colormaps = [ "%skintones.default" ]
29 changes: 26 additions & 3 deletions src/load/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct Emoji {
pub description: String,
pub tags: Vec<String>,
pub codepoint: Option<Vec<String>>,
pub root_codepoint: Option<Vec<String>>,
pub shortcodes: Vec<String>,
pub colormaps: Vec<String>,
}
Expand Down Expand Up @@ -380,7 +381,28 @@ impl Pack {
.map(|c| match c.as_str() {
Some(c) => c.to_string(),
None => panic!(
"Emoji codepoint is not a string in {:?}",
"Emoji 'codepoint' component is not a string in {:?}",
manifest_path
),
})
.collect(),
),
None => None,
};

let root_codepoint: Option<Vec<String>> = match emoji.get("root_codepoint") {
Some(codepoint) => Some(
codepoint
.as_array()
.expect(&format!(
"Emoji 'root_codepoint' is not an array in {:?}",
manifest_path
))
.iter()
.map(|c| match c.as_str() {
Some(c) => c.to_string(),
None => panic!(
"Emoji 'root_codepoint' component is not a string in {:?}",
manifest_path
),
})
Expand All @@ -400,7 +422,7 @@ impl Pack {
.map(|s| match s.as_str() {
Some(s) => s.to_string(),
None => {
panic!("Emoji contains invalid shortcode (must be a string) in {:?}", manifest_path)
panic!("Emoji contains invalid 'shortcode' (must be a string) in {:?}", manifest_path)
}
})
.collect(),
Expand All @@ -418,7 +440,7 @@ impl Pack {
.map(|c| match c.as_str() {
Some(c) => c.to_string(),
None => {
panic!("Emoji colormap is not a string in {:?}", manifest_path)
panic!("Emoji 'colormap' is not a string in {:?}", manifest_path)
}
})
.collect(),
Expand All @@ -433,6 +455,7 @@ impl Pack {
category,
tags,
codepoint,
root_codepoint,
shortcodes,
colormaps,
});
Expand Down
23 changes: 19 additions & 4 deletions src/process/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct Group {

#[derive(Serialize)]
struct Emoji {
base: Option<Vec<usize>>,
codepoint: Option<Vec<u64>>,
root_codepoint: Option<Vec<u64>>,
src: Option<String>,
shortcodes: Vec<String>,
category: Vec<String>,
Expand All @@ -27,13 +28,26 @@ pub fn generate_metadata(emojis: &Vec<EmojiEncoded>) -> String {
groups.insert(group.clone(), Vec::new());
}

let base: Option<Vec<usize>> = match emoji.emoji.codepoint.as_ref() {
let codepoint: Option<Vec<u64>> = match emoji.emoji.codepoint.as_ref() {
Some(codepoint) => Some(
codepoint
.iter()
.map(|codepoint| {
let codepoint = codepoint.replace("U+", "");
usize::from_str_radix(&codepoint, 16).unwrap()
u64::from_str_radix(&codepoint, 16).unwrap()
})
.collect(),
),
None => None,
};

let root_codepoint: Option<Vec<u64>> = match emoji.emoji.root_codepoint.as_ref() {
Some(codepoint) => Some(
codepoint
.iter()
.map(|codepoint| {
let codepoint = codepoint.replace("U+", "");
u64::from_str_radix(&codepoint, 16).unwrap()
})
.collect(),
),
Expand All @@ -48,7 +62,8 @@ pub fn generate_metadata(emojis: &Vec<EmojiEncoded>) -> String {
.collect();

let emoji = Emoji {
base,
codepoint,
root_codepoint: root_codepoint,
src: emoji.filename.clone(),
category: emoji.emoji.category.clone(),
shortcodes,
Expand Down

0 comments on commit a5b22bb

Please sign in to comment.