Compare commits
6 Commits
8d510abf07
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b4bf3a7f3 | |||
| c283456581 | |||
| 5bde8b5535 | |||
| a723a89e9f | |||
| b7bbff8f0e | |||
| 2c94e1e052 |
16
README.md
16
README.md
@ -1,15 +1,15 @@
|
||||
|
||||
# <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
|
||||
- Dynamically generate content based on rust variables or bash commands
|
||||
- Dynamically generate content based on variables or bash commands
|
||||
|
||||
# Why ?
|
||||
I'm learning Rust and try to create simple, usable tools for myself.
|
||||
This is by no means meant to end up in production anywhere.
|
||||
|
||||
# Templates ?
|
||||
|
||||
@ -43,7 +43,7 @@ Clone this repo, then compile and open the doc:
|
||||
cargo doc --open
|
||||
```
|
||||
|
||||
## 🙇 Author
|
||||
## Author
|
||||
Justine Pelletreau
|
||||
|
||||
|
||||
|
||||
85
src/lib.rs
85
src/lib.rs
@ -1,13 +1,13 @@
|
||||
///! A simple templating library
|
||||
///!
|
||||
///! Takes template files : any text file ending in .ft
|
||||
///! and substitutes special words. Special words can be of 2 types
|
||||
///! and are written between four %:
|
||||
///! %%cmd: my_bash_command%% : this will run the command my_bash_command
|
||||
///! and subtitute the output.
|
||||
///! %%myvar%% : this will be replaced by a given String.
|
||||
///!
|
||||
///! The only public function is analyze_file.
|
||||
//! A simple templating library
|
||||
//!
|
||||
//! Takes template files : any text file ending in .ft
|
||||
//! and substitutes special words. Special words can be of 2 types
|
||||
//! and are written between four %:
|
||||
//! %%cmd: my_bash_command%% : this will run the command my_bash_command
|
||||
//! and subtitute the output.
|
||||
//! %%myvar%% : this will be replaced by a given String.
|
||||
//!
|
||||
//! The only public function is analyze_file.
|
||||
pub mod flair {
|
||||
use std::{
|
||||
fs,
|
||||
@ -70,28 +70,44 @@ pub mod flair {
|
||||
///vars must be : HashMap<String, String> where key is var name
|
||||
///and value is the value of our var.
|
||||
///
|
||||
///# Panics
|
||||
///
|
||||
///The file must be a flair template file, ending in .ft
|
||||
///The file must exist, as well.
|
||||
pub fn analyze_file(filepath: &str, vars: HashMap<String, String>) -> String {
|
||||
pub fn analyze_file(filepath: &str, vars: HashMap<String, String>) -> Result<String, String> {
|
||||
if !filepath.ends_with(".ft") {
|
||||
panic!("Only flair template files will be accepted. \n\
|
||||
Please rename your file to be a .ft file.");
|
||||
}
|
||||
let file = fs::File::open(filepath).expect(&format!("File not {} found", &filepath));
|
||||
let reader = BufReader::new(file).lines();
|
||||
let mut analyzed_lines: Vec<String> = Vec::new();
|
||||
|
||||
for line in reader {
|
||||
let mut myline = line.unwrap();
|
||||
myline = analyze_line(&mut myline, &vars);
|
||||
_ = &mut analyzed_lines.push(myline);
|
||||
return Err(String::from("Only flair template files will be accepted"));
|
||||
}
|
||||
|
||||
return analyzed_lines.join("\n");
|
||||
let file = fs::File::open(filepath);
|
||||
match file {
|
||||
Ok(f) => {
|
||||
let reader = BufReader::new(f).lines();
|
||||
let mut analyzed_lines: Vec<String> = Vec::new();
|
||||
|
||||
for line in reader {
|
||||
let mut myline = line.unwrap();
|
||||
myline = analyze_string(&mut myline, &vars);
|
||||
_ = &mut analyzed_lines.push(myline);
|
||||
}
|
||||
return Ok(analyzed_lines.join("\n"));
|
||||
},
|
||||
Err(e) => return Err(e.to_string()),
|
||||
};
|
||||
}
|
||||
|
||||
///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)
|
||||
@ -111,22 +127,8 @@ 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 individuals placeholders ("words").
|
||||
///Used to replace individual placeholders ("words").
|
||||
fn analyze_word(word: &str) -> Placeholder {
|
||||
let mut word = String::from(word).replace("%%", "");
|
||||
|
||||
@ -160,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"
|
||||
@ -171,6 +173,5 @@ pub mod flair {
|
||||
assert_eq!(myresult, expected);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ use std::{thread, time};
|
||||
|
||||
fn update_index(vars: &HashMap<String, String>) {
|
||||
let newvars = vars.clone();
|
||||
let analyzed = analyze_file("./templates/index.html.ft", newvars);
|
||||
let analyzed = analyze_file("./templates/index.html.ft", newvars).unwrap();
|
||||
fs::write("./static_html/index.html", analyzed).expect("Could not write index_file");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user