117 lines
3.1 KiB
Rust
117 lines
3.1 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::io::Write;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::Mutex;
|
|
use tera::{Context, Tera};
|
|
|
|
#[derive(Serialize)]
|
|
struct NavLink {
|
|
href: String,
|
|
text: String,
|
|
active: AtomicBool,
|
|
}
|
|
|
|
pub fn render_pages() {
|
|
// Use globbing
|
|
let tera = match Tera::new("templates/**/*.html") {
|
|
Ok(t) => t,
|
|
Err(e) => {
|
|
println!("Parsing error(s): {}", e);
|
|
::std::process::exit(1);
|
|
}
|
|
};
|
|
|
|
// Setup basic elements
|
|
let nav_home = NavLink {
|
|
href: "/".to_string(),
|
|
text: "Home".to_string(),
|
|
active: AtomicBool::new(true),
|
|
};
|
|
let nav_about = NavLink {
|
|
href: "/about/".to_string(),
|
|
text: "About".to_string(),
|
|
active: AtomicBool::new(false),
|
|
};
|
|
let nav_products = NavLink {
|
|
href: "/products/".to_string(),
|
|
text: "Products".to_string(),
|
|
active: AtomicBool::new(false),
|
|
};
|
|
let nav_contact = NavLink {
|
|
href: "/contact/".to_string(),
|
|
text: "Contact".to_string(),
|
|
active: AtomicBool::new(false),
|
|
};
|
|
|
|
let nav_elements = vec![nav_home, nav_about, nav_products, nav_contact];
|
|
|
|
let estrogens = NavLink {
|
|
href: "/products/#estrogens".to_string(),
|
|
text: "Estrogens".to_string(),
|
|
active: AtomicBool::new(false),
|
|
};
|
|
let aa = NavLink {
|
|
href: "/products/#aa".to_string(),
|
|
text: "Anti-Androgens".to_string(),
|
|
active: AtomicBool::new(false),
|
|
};
|
|
let progestogens = NavLink {
|
|
href: "/products/#progestogens".to_string(),
|
|
text: "Progestogens".to_string(),
|
|
active: AtomicBool::new(false),
|
|
};
|
|
let product_classes = vec![estrogens, aa, progestogens];
|
|
|
|
// Render pages
|
|
|
|
render_page(
|
|
// Home
|
|
"index.html".to_string(),
|
|
tera.clone(),
|
|
"The best source for certified gay HRT & supplies".to_string(),
|
|
"Home".to_string(),
|
|
&nav_elements,
|
|
&product_classes,
|
|
);
|
|
render_page(
|
|
// About
|
|
"about/index.html".to_string(),
|
|
tera.clone(),
|
|
"About Us".to_string(),
|
|
"About".to_string(),
|
|
&nav_elements,
|
|
&product_classes,
|
|
);
|
|
}
|
|
|
|
fn render_page(
|
|
url: String,
|
|
tera: Tera,
|
|
subtitle: String,
|
|
active_page: String,
|
|
nav_elements: &Vec<NavLink>,
|
|
product_classes: &Vec<NavLink>,
|
|
) {
|
|
let mut output_file = std::fs::File::create(format!("static/{}", url)).expect("create failed");
|
|
|
|
for i in nav_elements {
|
|
if i.text == active_page {
|
|
i.active.store(true, Ordering::Relaxed);
|
|
} else {
|
|
i.active.store(false, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
let mut context = Context::new();
|
|
context.insert("title", "Catgirl Pharmacy");
|
|
context.insert("description", "Meow");
|
|
context.insert("subtitle", &subtitle);
|
|
for i in nav_elements {
|
|
context.insert(&i.text.to_ascii_lowercase(), &i);
|
|
}
|
|
context.insert("product_classes", &product_classes);
|
|
output_file
|
|
.write_all(tera.render("index.html", &context).unwrap().as_bytes())
|
|
.expect("write failed");
|
|
}
|