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

45
README.md Normal file
View File

@ -0,0 +1,45 @@
# <p align="center">Flair</p>
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.

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 { pub mod flair {
use std::{ use std::{
fs, 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, ///Takes two values : the path of the file to analyze,
///and an HashMap of values that can be replaced when we find a ///and an HashMap of values that can be replaced when we find a
///%%var_name%% in our template file. ///%%var_name%% in our template file.
///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 exist, as well.
pub fn analyze_file(filepath: &str, vars: HashMap<String, String>) -> String { 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 file = fs::File::open(filepath).expect(&format!("File not {} found", &filepath));
let reader = BufReader::new(file).lines(); let reader = BufReader::new(file).lines();
let mut analyzed_lines: Vec<String> = Vec::new(); let mut analyzed_lines: Vec<String> = Vec::new();
@ -126,7 +145,7 @@ pub mod flair {
#[test] #[test]
fn cmd_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 expected = String::from("bonjour\n");
let (outp, _) = run_cmd(&cmd); let (outp, _) = run_cmd(&cmd);
assert_eq!(expected, outp); assert_eq!(expected, outp);

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.st", newvars); let analyzed = analyze_file("./templates/index.html.ft", newvars);
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");
} }

BIN
static_html/ferris.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -1,3 +1,23 @@
Hello Justine Squi <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example website</title>
<meta name="Description" Content="Vault">
Today is the Wed Jan 11 05:35:15 PM CET 2023 <link href="main.css" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png">
<link rel="manifest" href="/img/site.webmanifest">
<script src="vault.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<p style="text-align:center;"><img src="ferris.png" style="width:20%;"></p>
<h1>Hello Justine Squi<h1>
<h2>Today is the Wed Jan 11 05:55:39 PM CET 2023<h2>
<p>This computer was up 7 hours, 55 minutes</p>

80
static_html/main.css Normal file
View File

@ -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;
}

23
templates/index.html.ft Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example website</title>
<meta name="Description" Content="Vault">
<link href="main.css" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png">
<link rel="manifest" href="/img/site.webmanifest">
<script src="vault.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<p style="text-align:center;"><img src="ferris.png" style="width:20%;"></p>
<h1>Hello %%name%% %%surname%%<h1>
<h2>Today is the %%cmd: date%%<h2>
<p>This computer was %%cmd: uptime -p%%</p>

View File

@ -1,3 +0,0 @@
Hello %%name%% %%surname%%
Today is the %%cmd: date%%