Skip to content

Commit

Permalink
feat(whiskers): add css_* functions as filters
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter committed May 27, 2024
1 parent 6e70c09 commit 4057250
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
36 changes: 36 additions & 0 deletions whiskers/src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,39 @@ pub fn trunc(
)?;
Ok(tera::to_value(format!("{value:.places$}"))?)
}

pub fn css_rgb(
value: &tera::Value,
_args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let color: css_colors::RGB = (&color).into();
Ok(tera::to_value(color.to_string())?)
}

pub fn css_rgba(
value: &tera::Value,
_args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let color: css_colors::RGBA = (&color).into();
Ok(tera::to_value(color.to_string())?)
}

pub fn css_hsl(
value: &tera::Value,
_args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let color: css_colors::HSL = (&color).into();
Ok(tera::to_value(color.to_string())?)
}

pub fn css_hsla(
value: &tera::Value,
_args: &HashMap<String, tera::Value>,
) -> Result<tera::Value, tera::Error> {
let color: Color = tera::from_value(value.clone())?;
let color: css_colors::HSLA = (&color).into();
Ok(tera::to_value(color.to_string())?)
}
24 changes: 24 additions & 0 deletions whiskers/src/templating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub fn make_engine() -> tera::Tera {
tera.register_filter("urlencode_lzma", filters::urlencode_lzma);
tera.register_filter("trunc", filters::trunc);
tera.register_filter("mix", filters::mix);
tera.register_filter("css_rgb", filters::css_rgb);
tera.register_filter("css_rgba", filters::css_rgba);
tera.register_filter("css_hsl", filters::css_hsl);
tera.register_filter("css_hsla", filters::css_hsla);
tera.register_function("if", functions::if_fn);
tera.register_function("object", functions::object);
tera.register_function("css_rgb", functions::css_rgb);
Expand Down Expand Up @@ -148,6 +152,26 @@ pub fn all_filters() -> Vec<Filter> {
description: "Truncate a number to a certain number of places".to_string(),
examples: vec![filter_example!(1.123456 | trunc(places=3) => "1.123")],
},
Filter {
name: "css_rgb".to_string(),
description: "Convert a color to an RGB CSS string".to_string(),
examples: vec![filter_example!(red | css_rgb => "rgb(210, 15, 57)")],
},
Filter {
name: "css_rgba".to_string(),
description: "Convert a color to an RGBA CSS string".to_string(),
examples: vec![filter_example!(red | css_rgba => "rgba(210, 15, 57, 1.00)")],
},
Filter {
name: "css_hsl".to_string(),
description: "Convert a color to an HSL CSS string".to_string(),
examples: vec![filter_example!(red | css_hsl => "hsl(347, 87%, 44%)")],
},
Filter {
name: "css_hsla".to_string(),
description: "Convert a color to an HSLA CSS string".to_string(),
examples: vec![filter_example!(red | css_hsla => "hsla(347, 87%, 44%, 1.00)")],
},
]
}

Expand Down

0 comments on commit 4057250

Please sign in to comment.