Added docs

This commit is contained in:
Justine
2023-01-11 18:10:06 +01:00
parent 346d3c5b16
commit c7f05b95ad
8 changed files with 192 additions and 8 deletions

View File

@ -1,3 +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.
pub mod flair {
use std::{
fs,
@ -53,13 +63,22 @@ pub mod flair {
}
///Takes a file and submits it for analysis.
///Takes a .ft file and submits it for analysis.
///Takes two values : the path of the file to analyze,
///and an HashMap of values that can be replaced when we find a
///%%var_name%% in our template file.
///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 {
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();
@ -126,7 +145,7 @@ pub mod flair {
#[test]
fn cmd_test() {
let cmd = String::from("/usr/bin/echo bonjour");
let cmd = String::from("echo bonjour");
let expected = String::from("bonjour\n");
let (outp, _) = run_cmd(&cmd);
assert_eq!(expected, outp);

View File

@ -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.st", newvars);
let analyzed = analyze_file("./templates/index.html.ft", newvars);
fs::write("./static_html/index.html", analyzed).expect("Could not write index_file");
}