Compare commits

...

3 Commits

Author SHA1 Message Date
a723a89e9f Update readme
All checks were successful
continuous-integration/drone Build is passing
2023-01-12 10:28:36 +01:00
b7bbff8f0e Merge branch 'main' of ssh://swarm1.sq.lan:3022/Rust/flair 2023-01-12 10:25:30 +01:00
2c94e1e052 Improved analyze_file 2023-01-12 10:25:17 +01:00
3 changed files with 36 additions and 30 deletions

View File

@ -7,9 +7,13 @@ An extra simplified version of Jinja, written as a Rust lib.
* Add a function that reads from a string * 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 * 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
- 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 ? # Templates ?
@ -43,7 +47,7 @@ Clone this repo, then compile and open the doc:
cargo doc --open cargo doc --open
``` ```
## 🙇 Author ## Author
Justine Pelletreau Justine Pelletreau

View File

@ -1,13 +1,13 @@
///! A simple templating library //! A simple templating library
///! //!
///! Takes template files : any text file ending in .ft //! Takes template files : any text file ending in .ft
///! and substitutes special words. Special words can be of 2 types //! and substitutes special words. Special words can be of 2 types
///! and are written between four %: //! and are written between four %:
///! %%cmd: my_bash_command%% : this will run the command my_bash_command //! %%cmd: my_bash_command%% : this will run the command my_bash_command
///! and subtitute the output. //! and subtitute the output.
///! %%myvar%% : this will be replaced by a given String. //! %%myvar%% : this will be replaced by a given String.
///! //!
///! The only public function is analyze_file. //! The only public function is analyze_file.
pub mod flair { pub mod flair {
use std::{ use std::{
fs, fs,
@ -70,26 +70,28 @@ pub mod flair {
///vars must be : HashMap<String, String> where key is var name ///vars must be : HashMap<String, String> where key is var name
///and value is the value of our var. ///and value is the value of our var.
/// ///
///# Panics
///
///The file must be a flair template file, ending in .ft ///The file must be a flair template file, ending in .ft
///The file must exist, as well. ///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") { if !filepath.ends_with(".ft") {
panic!("Only flair template files will be accepted. \n\ return Err(String::from("Only flair template files will be accepted"));
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 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_line(&mut myline, &vars);
_ = &mut analyzed_lines.push(myline);
}
return Ok(analyzed_lines.join("\n"));
},
Err(e) => return Err(e.to_string()),
};
} }
@ -126,7 +128,7 @@ pub mod flair {
} }
///Used to replace individuals placeholders ("words"). ///Used to replace individual placeholders ("words").
fn analyze_word(word: &str) -> Placeholder { fn analyze_word(word: &str) -> Placeholder {
let mut word = String::from(word).replace("%%", ""); let mut word = String::from(word).replace("%%", "");

View File

@ -9,7 +9,7 @@ use std::{thread, time};
fn update_index(vars: &HashMap<String, String>) { fn update_index(vars: &HashMap<String, String>) {
let newvars = vars.clone(); 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"); fs::write("./static_html/index.html", analyzed).expect("Could not write index_file");
} }