diff --git a/README.md b/README.md new file mode 100644 index 0000000..5813197 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ + +#

Flair

+ +An extra simplified version of Flask, written as a Rust lib. +## 🧐 Features +- Reads templates +- Dynamically generate content based on rust variables or bash commands + +# Templates ? + +Templates are defined as .ft files. + +They may contain any text. Special words are defined between percentage signs: +``` +Hello %%name%% +``` + +## Try +An example website is included. + +To see it, clone this repo, then: +```bash +cargo run +``` + +Next, go to http://localhost:8080 + +## Use +Add this to your Cargo.toml +```bash +flair = { git = "https://gitea.squi.fr/Rust/flair.git" } +``` + +## Doc +Clone this repo, then compile and open the doc: +```bash +cargo doc --open +``` + +## 🙇 Author +Justine Pelletreau + + +## ➤ License +Distributed as is. diff --git a/src/lib.rs b/src/lib.rs index e6ad330..d32e68d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 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 { + 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 = 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); diff --git a/src/main.rs b/src/main.rs index b115340..23681ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::{thread, time}; fn update_index(vars: &HashMap) { 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"); } diff --git a/static_html/ferris.png b/static_html/ferris.png new file mode 100644 index 0000000..866412d Binary files /dev/null and b/static_html/ferris.png differ diff --git a/static_html/index.html b/static_html/index.html index 210a4fc..bf40aa4 100644 --- a/static_html/index.html +++ b/static_html/index.html @@ -1,3 +1,23 @@ -Hello Justine Squi + + + + + Example website + -Today is the Wed Jan 11 05:35:15 PM CET 2023 \ No newline at end of file + + + + + + + + + + +

+

Hello Justine Squi

+ +

Today is the Wed Jan 11 05:55:39 PM CET 2023

+ +

This computer was up 7 hours, 55 minutes

\ No newline at end of file diff --git a/static_html/main.css b/static_html/main.css new file mode 100644 index 0000000..f7a6b7e --- /dev/null +++ b/static_html/main.css @@ -0,0 +1,80 @@ +miniature { + width: 200px; + height: 200px; + border-radius: 4px; + padding: 5px; + margin-top: 10px; + border: 1px solid #ddd; + object-fit: cover; +} + + +/* (X) DOES NOT MATTER */ +body, html { + background-color: #111111; + padding: 0; + margin: 0; + font-family: Verdana, "Bitstream Vera Sans", sans-serif; + color: #FFFFFF; + line-height: 1em; +} + +a { + text-decoration: none; + color: #FFFFFF; +} + +a:hover { + color: #F012BE; +} + +.container { + height: 100%; + width: 70%; + display: flex; + column-gap: 2rem; + row-gap: 8rem; + flex-direction: row; + flex-wrap: wrap; + align-content: flex-end; + justify-content: center; + text-align: center; + margin: auto; +} + +.item { + height: 200px; + font-size: 15px; + width: 150px; + line-height: 1em; + padding: 3em; + align-items: center; + display: flex; + flex-direction: column; +} +/* +@media screen and (max-width: 920px) { + .item { + width: 30%; + } +} + +@media screen and (max-width: 640px) { + .item { + width: 50%; + } +} +*/ +h1 { + text-align: center; +} + +h2 { + + color: #61cObf; + font-size: 1.5em; + line-height: 1px; + margin: 0px; + text-align: center; +} + diff --git a/templates/index.html.ft b/templates/index.html.ft new file mode 100644 index 0000000..3915742 --- /dev/null +++ b/templates/index.html.ft @@ -0,0 +1,23 @@ + + + + + Example website + + + + + + + + + + + + +

+

Hello %%name%% %%surname%%

+ +

Today is the %%cmd: date%%

+ +

This computer was %%cmd: uptime -p%%

diff --git a/templates/index.html.st b/templates/index.html.st deleted file mode 100644 index a1fedf2..0000000 --- a/templates/index.html.st +++ /dev/null @@ -1,3 +0,0 @@ -Hello %%name%% %%surname%% - -Today is the %%cmd: date%%