htoml

HTML page templates described with a toml config
Log | Files | Refs | README

commit 0ffb5d7ed8f24e1b74da8a90ee97678677fd0eba
parent bdaa7234851265924f444c471eb3d2fa0880840c
Author: Jackson G. Kaindume <seestem@merely.tech>
Date:   Mon, 15 Aug 2022 20:08:40 +0200

add hero section

Diffstat:
MREADME.md | 6++++++
Asrc/sections/hero.rs | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/sections/mod.rs | 9+++++++++
3 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -39,6 +39,12 @@ variant = "basic" # Links for the navigation links = [{url= "/", text= "Home"}, {url= "/about", text= "About"}, {url= "/contact", text= "Contact Us"}] +# Hero section [Optional] +[sections.hero] +# Variant to use +variant = "basic" +heading = "Awesome Hero" +sub_heading = "This is a cool sub heading" ``` ## Usage diff --git a/src/sections/hero.rs b/src/sections/hero.rs @@ -0,0 +1,72 @@ +use crate::common::Export; +use crate::sections::{Section, SectionMetadata}; +use serde::Deserialize; +use std::path::PathBuf; + +#[derive(Deserialize, Clone)] +pub struct Hero { + /// Variant of hero to use + pub variant: String, + /// Path to background image of the section + pub background_image: Option<PathBuf>, + /// Heading of hero section + pub heading: String, + /// Sub heading of hero section + pub sub_heading: String, +} + +impl Section for Hero { + fn export(&self, metadata: &SectionMetadata) -> Export { + let mut html = "".to_string(); + let mut css = format!( + r#" + .htoml_hero{{ + background-color: {}; + margin-top: 0; + padding: 2em; + height: 200px; + color: {}; + }} + + .htoml_hero h1{{ + margin-top: 0; + }} + "#, + metadata.tertiary_color, metadata.primary_color + ); + + let mut background_image = ""; + + if let Some(img) = &self.background_image { + if img.as_path().exists() { + if let Some(s) = img.as_path().to_str() { + background_image = s; + } + } + }; + + match self.variant.as_str() { + "basic" => { + html = format!( + r#" + <section class='htoml_hero'> + <h1>{}<h1> + <p>{}</p> + </section>"#, + self.heading, self.sub_heading + ); + } + _ => panic!("Invalid Hero variant"), + } + Export { html, css } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +} diff --git a/src/sections/mod.rs b/src/sections/mod.rs @@ -1,5 +1,7 @@ use crate::common::Export; use serde::Deserialize; + +pub mod hero; pub mod navbar; /// A section of an HTML webpage @@ -23,6 +25,8 @@ pub struct SectionMetadata { pub struct Sections { /// Navigation bar section pub navbar: Option<navbar::Navbar>, + /// Hero section + pub hero: Option<hero::Hero>, } impl Sections { @@ -36,6 +40,11 @@ impl Sections { html = format!("{}{}", html, navbar_export.html); css = format!("{}{}", css, navbar_export.css); } + + if let Some(hero) = &self.hero { + let hero_export = hero.export(metadata); + html = format!("{}{}", html, hero_export.html); + css = format!("{}{}", css, hero_export.css); } Export { html, css }