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

add more template functions according to fermyon/bartholomew#26 fermy… #4

Merged
merged 1 commit into from
Sep 16, 2022
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
7 changes: 5 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ fn main() {

// Example of running a template render.
let empty_context: Vec<String> = vec![];
println!("Template produced: {}", hbs.render_template(tpl, &empty_context).unwrap())
}
println!(
"Template produced: {}",
hbs.render_template(tpl, &empty_context).unwrap()
)
}
8 changes: 3 additions & 5 deletions src/date.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use handlebars::{
handlebars_helper, Handlebars,
};
use chrono::{DateTime, Utc};
use handlebars::{handlebars_helper, Handlebars};

pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(date_format: |format_string: String, date: DateTime<Utc>| {
Expand All @@ -11,8 +9,8 @@ pub fn addhelpers(x: &mut Handlebars) {
let date = Utc::now();
date.format(format_string.as_str()).to_string()
});

// Formatting dates: https://docs.rs/chrono/latest/chrono/format/strftime/index.html#specifiers
x.register_helper("date_format", Box::new(date_format));
x.register_helper("now", Box::new(now));
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use handlebars::Handlebars;

pub mod date;
pub mod gist;
pub mod list;
pub mod math;
pub mod random;
pub mod strings;
pub mod template;
pub mod tweet;

pub fn addhelpers(x: &mut Handlebars) {
Expand All @@ -12,4 +15,7 @@ pub fn addhelpers(x: &mut Handlebars) {
date::addhelpers(x);
tweet::addhelpers(x);
gist::addhelpers(x);
list::addhelpers(x);
random::addhelpers(x);
template::addhelpers(x);
}
34 changes: 34 additions & 0 deletions src/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use handlebars::{handlebars_helper, Handlebars};

pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(until_step: |start: isize, max: isize, step: usize| {
let mut arr: Vec<isize> = Vec::new();
for num in (start..max).step_by(step){
arr.push(num);
}
arr
});
handlebars_helper!(seq: | args: Vec<isize> |{
let mut arr: Vec<isize> = Vec::new();
if args.len() == 1 {
for num in 1..args[0] {
arr.push(num);
}
} else if args.len() == 2 {
for num in args[0]..args[1] {
arr.push(num);
}
} else if args.len() == 3 {
let mut num = args[0];

while if args[1] < 0 {num > args[2]} else {num < args[2]} {
arr.push(num);
num += args[1];
}
}
arr
});

x.register_helper("until_step", Box::new(until_step));
x.register_helper("seq", Box::new(seq));
}
25 changes: 25 additions & 0 deletions src/random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use handlebars::{handlebars_helper, Handlebars};
use rand::distributions::Alphanumeric;
use rand::Rng;

pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(rand_numeric: | | rand::thread_rng().gen_range(0..10));
handlebars_helper!(rand_alpha: | | {
let val: u8 = 65 + rand::thread_rng().gen_range(0..26) + (rand::thread_rng().gen_range(0..2) * 32);
let mut s = String::new();
s.insert(0, val as char);
s
});
handlebars_helper!(rand_alphanumeric: | | {
let rand_string: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(1)
.map(char::from)
.collect();
rand_string
});

x.register_helper("rand_numeric", Box::new(rand_numeric));
x.register_helper("rand_alpha", Box::new(rand_alpha));
x.register_helper("rand_alphanumeric", Box::new(rand_alphanumeric));
}
35 changes: 35 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ use handlebars::{handlebars_helper, Handlebars};
pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(upper: |s: String| s.to_uppercase());
handlebars_helper!(lower: |s: String| s.to_lowercase());
handlebars_helper!(title: |s: String| {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().chain(c).collect(),
}
});
handlebars_helper!(trunc: |l: usize, s: String| {
let mut data = s.clone();
data.truncate(l);
Expand All @@ -21,6 +28,20 @@ pub fn addhelpers(x: &mut Handlebars) {
format!("{}...", data)
}
});
handlebars_helper!(abbrevboth: |l: usize, max: usize, s: String| {
let abbr;
if l > s.len() - 2 {
abbr = &s[s.len() - 2..];
} else {
let limit = l + max;
if limit < s.len() -2 {
abbr = &s[l..limit];
} else {
abbr = &s[l..];
}
}
format!("{}...", abbr)
});
handlebars_helper!(trim: |s:String| s.trim());
handlebars_helper!(plural: |count: usize, sing: String, plur: String| if count == 1 {
sing
Expand Down Expand Up @@ -77,11 +98,21 @@ pub fn addhelpers(x: &mut Handlebars) {

result
});
handlebars_helper!(repeat: |count: usize, s: String| s.repeat(count));
handlebars_helper!(other_substr: |start: usize, end: usize, s: String| &s[start..end]);
handlebars_helper!(nospace: |s: String| s.replace(" ", ""));
handlebars_helper!(initials: |s: String| {
let initial: String = s.split(" ")
.flat_map(|s| s.chars().nth(0)).collect();
initial.to_uppercase()
});

x.register_helper("upper", Box::new(upper));
x.register_helper("lower", Box::new(lower));
x.register_helper("title", Box::new(title));
x.register_helper("trunc", Box::new(trunc));
x.register_helper("abbrev", Box::new(abbrev));
x.register_helper("abbrevboth", Box::new(abbrevboth));
x.register_helper("plural", Box::new(plural));
x.register_helper("trim", Box::new(trim));
x.register_helper("join", Box::new(join));
Expand All @@ -91,4 +122,8 @@ pub fn addhelpers(x: &mut Handlebars) {
x.register_helper("trim_suffix", Box::new(trim_suffix));
x.register_helper("trim_prefix", Box::new(trim_prefix));
x.register_helper("trim_all", Box::new(trim_all));
x.register_helper("repeat", Box::new(repeat));
x.register_helper("substr", Box::new(other_substr));
x.register_helper("nospace", Box::new(nospace));
x.register_helper("initials", Box::new(initials));
}
32 changes: 32 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use handlebars::{handlebars_helper, Handlebars};
use rand::seq::SliceRandom;

pub fn addhelpers(x: &mut Handlebars) {
handlebars_helper!(contains: |sub_str: String, content: String | content.contains(&sub_str));
karthik2804 marked this conversation as resolved.
Show resolved Hide resolved
handlebars_helper!(has_prefix: |prefix: String, content: String | content.starts_with(&prefix));
handlebars_helper!(has_suffix: |suffix: String, content: String | content.ends_with(&suffix));
handlebars_helper!(cat: |s: Vec<String>| s.join(" "));
handlebars_helper!(replace: |target: String, replacememt: String, s: String| s.replace(&target, &replacememt));
handlebars_helper!(shuffle: |s: String| {
let mut bytes = s.into_bytes();
let mut rng = rand::thread_rng();
bytes.shuffle(&mut rng);
let str = String::from_utf8(bytes).unwrap();
str
});
handlebars_helper!(indent: |count: usize, s: String| {
let mut data = s.clone();
for _ in 0..count {
data.insert(0, ' ');
}
data
});

x.register_helper("contains", Box::new(contains));
x.register_helper("has_prefix", Box::new(has_prefix));
x.register_helper("has_suffix", Box::new(has_suffix));
x.register_helper("cat", Box::new(cat));
x.register_helper("indent", Box::new(indent));
x.register_helper("replace", Box::new(replace));
x.register_helper("shuffle", Box::new(shuffle));
}