-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot for the PR, the changes look great. I just have a few minor comments.
3c69a5d
to
faa7111
Compare
faa7111
to
208fb76
Compare
src/template.rs
Outdated
|
||
pub fn addhelpers(x: &mut Handlebars) { | ||
handlebars_helper!(wrap: |index: usize, s: String| { | ||
wrap_helper(index, "/n".to_string(), s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be '\n' instead of '/n'?
src/template.rs
Outdated
x.register_helper("shuffle", Box::new(shuffle)); | ||
} | ||
|
||
fn wrap_helper(index: usize, ending: String, s: String) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please write some unit tests for this function. i tried running this through template and got following error:
thread 'main' panicked at 'attempt to subtract with overflow', src/template.rs:52:20
examples/template.rs
use handlebars::Handlebars;
use handlebars_sprig;
fn main() {
// Get a handlebars instance
let mut hbs = Handlebars::new();
// THE IMPORTANT PART: Add the helpers
handlebars_sprig::template::addhelpers(&mut hbs);
// From this point, you can do whatever you want to do with the
// handlebars instance. It will have all of the functions available
// to the templates.
let tpl = "{{ wrap 5 \"this is a string that needs wrapping\" }}";
// Example of running a template render.
let empty_context: Vec<String> = vec![];
println!(
"Template produced: {}",
hbs.render_template(tpl, &empty_context).unwrap()
)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, please test using unicode characters and verify how the function behaves.
src/template.rs
Outdated
|
||
while i < s.len() - 1 { | ||
wraploc += 1; | ||
if wraploc >= index { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i find the code more readable when there are not too many nested if conditions. you can probably write this as:
if wraploc > index {
i += 1
continue
}
for k in (0..i).rev()
.
.
.
@rajatjindal text wrapping seems to require a bit more logic and testing than what has been presented so far. What would your thoughts be on the deffering adding |
sounds good to me. we can add that in a later PR |
Signed-off-by: karthik Ganeshram <[email protected]>
208fb76
to
2b51a90
Compare
This is ready for a rereview |
thanks a lot for your PR. |
Would you like to PR the changes into Bartholomew or should I go ahead and make the PR? |
if you could pls do it, that would be great. |
Will do it today! |
Added more helpers as per fermyon/bartholomew#26, fermyon/bartholomew#27, fermyon/bartholomew#28 & fermyon/bartholomew#31
Signed-off-by: karthik Ganeshram [email protected]