Skip to content

Commit

Permalink
update parser, docs and release
Browse files Browse the repository at this point in the history
  • Loading branch information
Zheoni committed Jan 13, 2024
1 parent 76f77d3 commit b03aa0b
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 237 deletions.
236 changes: 112 additions & 124 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cooklang-chef"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
authors = ["Zheoni <[email protected]>"]
description = "CLI to manage cooklang recipes"
Expand All @@ -23,10 +23,10 @@ serde_json = "1"
strum = { version = "0.25", features = ["derive"] }
toml = "0.8"
cooklang = { workspace = true }
cooklang-fs = { version = "0.9", path = "./cooklang-fs" }
cooklang-to-human = { version = "0.9", path = "./cooklang-to-human" }
cooklang-to-cooklang = { version = "0.9", path = "./cooklang-to-cooklang" }
cooklang-to-md = { version = "0.9", path = "./cooklang-to-md" }
cooklang-fs = { version = "0.12", path = "./cooklang-fs" }
cooklang-to-human = { version = "0.12", path = "./cooklang-to-human" }
cooklang-to-cooklang = { version = "0.12", path = "./cooklang-to-cooklang" }
cooklang-to-md = { version = "0.12", path = "./cooklang-to-md" }
textwrap = { workspace = true, features = ["terminal_size"] }
tracing = "0.1"
tracing-subscriber = "0.3"
Expand All @@ -42,6 +42,7 @@ directories = "5.0.1"
inquire = "0.6.2"
shell-words = "1.1"
enum-map = "2.7.3"
emojis = "0.6"
# Serve
tokio = { version = "1", features = ["full"], optional = true }
axum = { version = "0.7", features = ["ws", "macros", "http2"], optional = true }
Expand Down Expand Up @@ -83,7 +84,7 @@ members = ["cooklang-fs", "cooklang-to-cooklang", "cooklang-to-human", "cooklang
repository = "https://github.com/Zheoni/cooklang-chef"

[workspace.dependencies]
cooklang = "0.11"
cooklang = "0.12"
owo-colors = "=3.5"
once_cell = "1.17"
textwrap = "0.16"
Expand Down
2 changes: 1 addition & 1 deletion cooklang-fs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cooklang-fs"
version = "0.9.0"
version = "0.12.0"
edition = "2021"
authors = ["Zheoni <[email protected]>"]
description = "Utilities for cooklang recipes in a file system"
Expand Down
2 changes: 1 addition & 1 deletion cooklang-to-cooklang/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cooklang-to-cooklang"
version = "0.9.0"
version = "0.12.0"
edition = "2021"
authors = ["Zheoni <[email protected]>"]
description = "Format cooklang back to cooklang"
Expand Down
2 changes: 1 addition & 1 deletion cooklang-to-human/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cooklang-to-human"
version = "0.9.0"
version = "0.12.0"
edition = "2021"
authors = ["Zheoni <[email protected]>"]
description = "Format cooklang in a human friendly way"
Expand Down
25 changes: 12 additions & 13 deletions cooklang-to-human/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,19 @@ fn header(w: &mut impl io::Write, recipe: &ScaledRecipe, name: &str) -> Result {
" {}{} ",
recipe
.metadata
.emoji
.as_ref()
.emoji()
.map(|s| format!("{s} "))
.unwrap_or_default(),
name
);
writeln!(w, "{}", title_text.style(styles().title))?;
if !recipe.metadata.tags.is_empty() {
let mut tags = String::new();
for tag in &recipe.metadata.tags {
if let Some(tags) = recipe.metadata.tags() {
let mut tags_str = String::new();
for tag in tags {
let color = tag_color(tag);
write!(&mut tags, "{} ", format!("#{tag}").color(color)).unwrap();
write!(&mut tags_str, "{} ", format!("#{tag}").color(color)).unwrap();
}
print_wrapped(w, &tags)?;
print_wrapped(w, &tags_str)?;
}
writeln!(w)
}
Expand All @@ -84,7 +83,7 @@ fn tag_color(tag: &str) -> owo_colors::AnsiColors {
}

fn metadata(w: &mut impl io::Write, recipe: &ScaledRecipe) -> Result {
if let Some(desc) = &recipe.metadata.description {
if let Some(desc) = recipe.metadata.description() {
print_wrapped_with_options(w, desc, |o| {
o.initial_indent("\u{2502} ").subsequent_indent("\u{2502}")
})?;
Expand All @@ -93,21 +92,21 @@ fn metadata(w: &mut impl io::Write, recipe: &ScaledRecipe) -> Result {

let mut meta_fmt =
|name: &str, value: &str| writeln!(w, "{}: {}", name.style(styles().meta_key), value);
if let Some(author) = &recipe.metadata.author {
if let Some(author) = recipe.metadata.author() {
let text = author
.name()
.or(author.url().map(|u| u.as_str()))
.unwrap_or("-");
meta_fmt("author", text)?;
}
if let Some(source) = &recipe.metadata.source {
if let Some(source) = recipe.metadata.source() {
let text = source
.name()
.or(source.url().map(|u| u.as_str()))
.unwrap_or("-");
meta_fmt("source", text)?;
}
if let Some(time) = &recipe.metadata.time {
if let Some(time) = recipe.metadata.time() {
let time_fmt = |t: u32| {
format!(
"{}",
Expand All @@ -130,7 +129,7 @@ fn metadata(w: &mut impl io::Write, recipe: &ScaledRecipe) -> Result {
}
}
}
if let Some(servings) = &recipe.metadata.servings {
if let Some(servings) = recipe.metadata.servings() {
let index = recipe
.scaled_data()
.and_then(|d| d.target.index())
Expand Down Expand Up @@ -162,7 +161,7 @@ fn metadata(w: &mut impl io::Write, recipe: &ScaledRecipe) -> Result {
meta_fmt("servings", &text)?;
}
for (key, value) in recipe.metadata.map_filtered() {
meta_fmt(&key, &value)?;
meta_fmt(key, value)?;
}
if !recipe.metadata.map.is_empty() {
writeln!(w)?;
Expand Down
2 changes: 1 addition & 1 deletion cooklang-to-md/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cooklang-to-md"
version = "0.9.0"
version = "0.12.0"
edition = "2021"
authors = ["Zheoni <[email protected]>"]
description = "Format cooklang to markdown"
Expand Down
64 changes: 39 additions & 25 deletions cooklang-to-md/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Options {
///
/// It will appear just after the tags (if its enabled and
/// there are any tags; if not, after the title).
#[serde(deserialize_with = "enum_or_bool")]
#[serde(deserialize_with = "des_or_bool")]
pub description: DescriptionStyle,
/// Make every step a regular paragraph
///
Expand All @@ -65,7 +65,8 @@ pub struct Options {
/// Add the name of the recipe to the front-matter
///
/// A key `name` in the metadata has preference over this.
pub front_matter_name: bool,
#[serde(deserialize_with = "des_or_bool")]
pub front_matter_name: FrontMatterName,
}

impl Default for Options {
Expand All @@ -75,7 +76,7 @@ impl Default for Options {
description: DescriptionStyle::Blockquote,
escape_step_numbers: false,
italic_amounts: true,
front_matter_name: true,
front_matter_name: FrontMatterName::default(),
}
}
}
Expand All @@ -102,7 +103,26 @@ impl From<bool> for DescriptionStyle {
}
}

fn enum_or_bool<'de, D, T>(deserializer: D) -> Result<T, D::Error>
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(transparent)]
pub struct FrontMatterName(pub Option<String>);

impl Default for FrontMatterName {
fn default() -> Self {
Self(Some("name".to_string()))
}
}

impl From<bool> for FrontMatterName {
fn from(value: bool) -> Self {
match value {
true => Self::default(),
false => Self(None),
}
}
}

fn des_or_bool<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: serde::Deserializer<'de>,
T: serde::Deserialize<'de> + From<bool>,
Expand All @@ -111,12 +131,12 @@ where
#[serde(untagged)]
enum Wrapper<T> {
Bool(bool),
Enum(T),
Thing(T),
}

let v = match Wrapper::deserialize(deserializer)? {
Wrapper::Bool(v) => T::from(v),
Wrapper::Enum(val) => val,
Wrapper::Thing(val) => val,
};
Ok(v)
}
Expand Down Expand Up @@ -153,17 +173,19 @@ pub fn print_md_with_options(

writeln!(writer, "# {}\n", name)?;

if opts.tags && !recipe.metadata.tags.is_empty() {
for (i, tag) in recipe.metadata.tags.iter().enumerate() {
write!(writer, "#{tag}")?;
if i < recipe.metadata.tags.len() - 1 {
write!(writer, " ")?;
if opts.tags {
if let Some(tags) = recipe.metadata.tags() {
for (i, tag) in tags.iter().enumerate() {
write!(writer, "#{tag}")?;
if i < tags.len() - 1 {
write!(writer, " ")?;
}
}
writeln!(writer, "\n")?;
}
writeln!(writer, "\n")?;
}

if let Some(desc) = &recipe.metadata.description {
if let Some(desc) = recipe.metadata.description() {
match opts.description {
DescriptionStyle::Hidden => {}
DescriptionStyle::Blockquote => {
Expand Down Expand Up @@ -199,9 +221,9 @@ fn frontmatter(

let mut map = IndexMap::new();

if opts.front_matter_name {
if let Some(name_key) = &opts.front_matter_name.0 {
// add name, will be overrided if other given
map.insert("name", name.into());
map.insert(name_key.as_str(), name.into());
}

// add all the raw metadata entries
Expand All @@ -212,27 +234,19 @@ fn frontmatter(
// overwrite special values if any and correct
macro_rules! override_special_key {
($meta:ident, $thing:ident) => {
if let Some(val) = &$meta.$thing {
if let Some(val) = &$meta.$thing() {
map.insert(
stringify!($thing),
serde_yaml::to_value(val.clone()).unwrap(),
);
}
};
($meta:ident, $thing:ident : not_empty) => {
if !$meta.$thing.is_empty() {
map.insert(
stringify!($thing),
serde_yaml::to_value($meta.$thing.clone()).unwrap(),
);
}
};
}
override_special_key!(metadata, author);
override_special_key!(metadata, source);
override_special_key!(metadata, time);
override_special_key!(metadata, servings);
override_special_key!(metadata, tags : not_empty);
override_special_key!(metadata, tags);

const FRONTMATTER_FENCE: &str = "---";
writeln!(w, "{}", FRONTMATTER_FENCE)?;
Expand Down
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
- [The CLI](./cli.md)
- [Using references](./using_references.md)
- [Extensions](https://github.com/cooklang/cooklang-rs/blob/main/extensions.md)
- [Units file](./units_file.md)
- [Units file](./units_file.md)
- [Special metadata keys](./special_metadata.md)
Loading

0 comments on commit b03aa0b

Please sign in to comment.