Compare commits
3 Commits
a723a89e9f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b4bf3a7f3 | |||
| c283456581 | |||
| 5bde8b5535 |
@ -1,11 +1,7 @@
|
||||
|
||||
# <p align="center">Flair</p>
|
||||
|
||||
An extra simplified version of Jinja, written as a Rust lib.
|
||||
|
||||
# TODO
|
||||
* Add a function that reads from a string
|
||||
* Add a sanitize function that removes %%words%% from a string, which could be useful to avoid user-side injection
|
||||
An extra simplified template engine, written as a Rust lib.
|
||||
|
||||
## Features
|
||||
- Reads templates
|
||||
|
||||
33
src/lib.rs
33
src/lib.rs
@ -85,7 +85,7 @@ pub mod flair {
|
||||
|
||||
for line in reader {
|
||||
let mut myline = line.unwrap();
|
||||
myline = analyze_line(&mut myline, &vars);
|
||||
myline = analyze_string(&mut myline, &vars);
|
||||
_ = &mut analyzed_lines.push(myline);
|
||||
}
|
||||
return Ok(analyzed_lines.join("\n"));
|
||||
@ -94,6 +94,20 @@ pub mod flair {
|
||||
};
|
||||
}
|
||||
|
||||
///Analyzes a String, and replaces placeholders
|
||||
///with their actual value.
|
||||
pub fn analyze_string<'a>(line: &'a mut String, vars: &HashMap<String, String>) -> String {
|
||||
let re = Regex::new(r"%%[^%]*%%").unwrap();
|
||||
let mut temp_line = String::from(line.as_str());
|
||||
for occurence in re.captures_iter(line) {
|
||||
//Never goes further than zero
|
||||
//println!("Found : {:?}", &occurence[0]);
|
||||
let my_ph = analyze_word(&occurence[0]);
|
||||
temp_line = temp_line.replace(&occurence[0], &my_ph.replace_to_val(&vars));
|
||||
}
|
||||
return temp_line;
|
||||
|
||||
}
|
||||
|
||||
///Runs a bash command in a thread, returns the result
|
||||
///(Stdout, Stderr)
|
||||
@ -113,20 +127,6 @@ pub mod flair {
|
||||
}
|
||||
|
||||
|
||||
///Analyzes a line, and replaces placeholders
|
||||
///with their actual value.
|
||||
fn analyze_line<'a>(line: &'a mut String, vars: &HashMap<String, String>) -> String {
|
||||
let re = Regex::new(r"%%[^%]*%%").unwrap();
|
||||
let mut temp_line = String::from(line.as_str());
|
||||
for occurence in re.captures_iter(line) {
|
||||
//Never goes further than zero
|
||||
//println!("Found : {:?}", &occurence[0]);
|
||||
let my_ph = analyze_word(&occurence[0]);
|
||||
temp_line = temp_line.replace(&occurence[0], &my_ph.replace_to_val(&vars));
|
||||
}
|
||||
return temp_line;
|
||||
|
||||
}
|
||||
|
||||
///Used to replace individual placeholders ("words").
|
||||
fn analyze_word(word: &str) -> Placeholder {
|
||||
@ -162,7 +162,7 @@ pub mod flair {
|
||||
"This is a test line from %%name%% \n\
|
||||
%%cmd: echo Sayonara%%"
|
||||
);
|
||||
let myresult = analyze_line(&mut myline, &vars);
|
||||
let myresult = analyze_string(&mut myline, &vars);
|
||||
let expected = String::from(
|
||||
"This is a test line from RustGirl \n\
|
||||
Sayonara"
|
||||
@ -173,6 +173,5 @@ pub mod flair {
|
||||
assert_eq!(myresult, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user