htoml

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

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

add section metadata

Diffstat:
MHTOML_example.toml | 22++++++++++++++++++----
MREADME.md | 7+++----
Msrc/htoml.rs | 17+++++++++++++++--
Msrc/sections/mod.rs | 27++++++++++++++++++---------
Msrc/sections/navbar.rs | 22++++++++++++----------
5 files changed, 66 insertions(+), 29 deletions(-)

diff --git a/HTOML_example.toml b/HTOML_example.toml @@ -6,14 +6,28 @@ title = "Homepage" # Path to html output output = "publish/index.html" +# Primary color (CSS colors) +primary_color = "teal" + +# Secondary color (CSS colors) +secondary_color = "white" + +# Tertiary color (CSS colors) +tertiary_color = "cyan" + # Different sections for the HTML document [sections] # Navigation section [Optional] [sections.navbar] - # Variant to use variant = "basic" - # Links for the navigation -links = [{url= "/", text= "Home"}, {url= "/about", text= "About"}, {url= "/contact", text= "Contact Us"}] -\ No newline at end of file +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" +\ No newline at end of file diff --git a/README.md b/README.md @@ -27,19 +27,18 @@ ___ title = "Homepage" # Path to html output -output = "index.html" +output = "publish/index.html" # Different sections for the HTML document [sections] # Navigation section [Optional] [sections.navbar] - # Variant to use variant = "basic" - # Links for the navigation -links = [{url= "/", text= "Home"}, {url= "/about", text= "About"},] +links = [{url= "/", text= "Home"}, {url= "/about", text= "About"}, {url= "/contact", text= "Contact Us"}] + ``` ## Usage diff --git a/src/htoml.rs b/src/htoml.rs @@ -1,4 +1,4 @@ -use crate::sections::Sections; +use crate::sections::{SectionMetadata, Sections}; use serde::Deserialize; use std::path::PathBuf; @@ -11,12 +11,23 @@ pub struct Htoml { pub output: PathBuf, /// Sections of the HTML page pub sections: Sections, + /// Primary color (CSS colors) + pub primary_color: String, + /// Secondary color (CSS colors) + pub secondary_color: String, + /// Tertiary color (CSS colors) + pub tertiary_color: String, } impl Htoml { /// Epxort toml to HTML pub fn export(&self) -> String { - let sections_export = self.sections.export(); + let metadata = SectionMetadata { + primary_color: self.primary_color.clone(), + secondary_color: self.secondary_color.clone(), + tertiary_color: self.tertiary_color.clone(), + }; + let sections_export = self.sections.export(&metadata); format!( r#"<!DOCTYPE html> @@ -54,6 +65,8 @@ mod tests { r#" title = "Homepage" output = "index.html" + primary_color = "teal" + secondary_color = "white" [sections] diff --git a/src/sections/mod.rs b/src/sections/mod.rs @@ -5,7 +5,18 @@ pub mod navbar; /// A section of an HTML webpage pub trait Section { /// Export section to HTML string - fn export(&self) -> Export; + fn export(&self, metadata: &SectionMetadata) -> Export; +} + +/// Metadata passed to Section.export method +#[derive(Deserialize, Clone)] +pub struct SectionMetadata { + /// Primary color + pub primary_color: String, + /// Secondary color + pub secondary_color: String, + /// Tertiary color + pub tertiary_color: String, } #[derive(Deserialize, Clone)] @@ -16,17 +27,15 @@ pub struct Sections { impl Sections { /// Export sections to HTML - pub fn export(&self) -> Export { + pub fn export(&self, metadata: &SectionMetadata) -> Export { let mut html = "".to_string(); let mut css = "".to_string(); - match &self.navbar { - Some(navbar) => { - let navbar_export = navbar.export(); - html = navbar_export.html; - css = navbar_export.css; - } - None => (), + if let Some(navbar) = &self.navbar { + let navbar_export = navbar.export(metadata); + html = format!("{}{}", html, navbar_export.html); + css = format!("{}{}", css, navbar_export.css); + } } Export { html, css } diff --git a/src/sections/navbar.rs b/src/sections/navbar.rs @@ -1,5 +1,5 @@ use crate::common::{Export, Link}; -use crate::sections::Section; +use crate::sections::{Section, SectionMetadata}; use serde::Deserialize; #[derive(Deserialize, Clone)] @@ -11,20 +11,22 @@ pub struct Navbar { } impl Section for Navbar { - fn export(&self) -> Export { + fn export(&self, metadata: &SectionMetadata) -> Export { let mut html = "".to_string(); - let mut css = r#" - .htoml_nav{ - background: teal; + let mut css = format!( + r#" + .htoml_nav{{ + background: {}; padding: .5em; text-align: right; - } + }} - .htoml_nav a{ + .htoml_nav a{{ color: #ffffff; - } - "# - .to_string(); + }} + "#, + metadata.primary_color + ); match self.variant.as_str() { "basic" => {