Compare commits

..

3 Commits

Author SHA1 Message Date
7b4bf3a7f3 Update 'README.md' 2023-01-13 13:39:04 +01:00
c283456581 Update 'README.md' 2023-01-13 13:38:31 +01:00
5bde8b5535 Made analyze_string public
All checks were successful
continuous-integration/drone Build is passing
2023-01-13 13:26:16 +01:00
2 changed files with 17 additions and 22 deletions

View File

@ -1,11 +1,7 @@
# <p align="center">Flair</p> # <p align="center">Flair</p>
An extra simplified version of Jinja, written as a Rust lib. An extra simplified template engine, 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
## Features ## Features
- Reads templates - Reads templates

View File

@ -85,7 +85,7 @@ pub mod flair {
for line in reader { for line in reader {
let mut myline = line.unwrap(); let mut myline = line.unwrap();
myline = analyze_line(&mut myline, &vars); myline = analyze_string(&mut myline, &vars);
_ = &mut analyzed_lines.push(myline); _ = &mut analyzed_lines.push(myline);
} }
return Ok(analyzed_lines.join("\n")); 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 ///Runs a bash command in a thread, returns the result
///(Stdout, Stderr) ///(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"). ///Used to replace individual placeholders ("words").
fn analyze_word(word: &str) -> Placeholder { fn analyze_word(word: &str) -> Placeholder {
@ -162,7 +162,7 @@ pub mod flair {
"This is a test line from %%name%% \n\ "This is a test line from %%name%% \n\
%%cmd: echo Sayonara%%" %%cmd: echo Sayonara%%"
); );
let myresult = analyze_line(&mut myline, &vars); let myresult = analyze_string(&mut myline, &vars);
let expected = String::from( let expected = String::from(
"This is a test line from RustGirl \n\ "This is a test line from RustGirl \n\
Sayonara" Sayonara"
@ -173,6 +173,5 @@ pub mod flair {
assert_eq!(myresult, expected); assert_eq!(myresult, expected);
} }
} }
} }