htoml

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

commit f0c280553c88f15c1d1b96e88c7fae0a1a8e69ae
Author: Jackson G. Kaindume <seestem@merely.tech>
Date:   Sat,  6 Aug 2022 00:07:49 +0200

Initial commit

Diffstat:
A.gitignore | 2++
ACargo.lock | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACargo.toml | 9+++++++++
AHTOML_example.toml | 20++++++++++++++++++++
AREADME | 14++++++++++++++
Aindex.html | 3+++
Asrc/common.rs | 18++++++++++++++++++
Asrc/htoml.rs | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/main.rs | 5+++++
Asrc/sections/mod.rs | 34++++++++++++++++++++++++++++++++++
Asrc/sections/navbar.rs | 39+++++++++++++++++++++++++++++++++++++++
11 files changed, 297 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +*~ +/target diff --git a/Cargo.lock b/Cargo.lock @@ -0,0 +1,75 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "htoml" +version = "0.1.0" +dependencies = [ + "serde", + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.142" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e590c437916fb6b221e1d00df6e3294f3fccd70ca7e92541c475d6ed6ef5fee2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.142" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34b5b8d809babe02f538c2cfec6f2c1ed10804c0e5a6a041a049a4f5588ccc2e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" diff --git a/Cargo.toml b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "htoml" +version = "0.1.0" +edition = "2021" + +[dependencies] +toml = "0.5.9" +serde = { version = "1.0.132", features = ["derive"] } +\ No newline at end of file diff --git a/HTOML_example.toml b/HTOML_example.toml @@ -0,0 +1,19 @@ +# An example of a HTOML description + +# Title of the HTML page +title = "Homepage" + +# Path to html output +output = "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"},] +\ No newline at end of file diff --git a/README b/README @@ -0,0 +1,13 @@ +[v0.1.0] [Last update:2022-08-04] + 𝙝𝙩𝙤𝙢𝙡 +---------------------------------------------------------------------- + HTML page templates described with in a toml config +---------------------------------------------------------------------- + + source code: https://codeberg.org/seestem/htoml + requirements: rustc 1.62+ + + +---------------------------------------------------------------------- + +====================================================================== +\ No newline at end of file diff --git a/index.html b/index.html @@ -0,0 +1,3 @@ +<p>[v0.1.0] [Last update:2022-08-04] 𝙝𝙩𝙤𝙢𝙡 ———————————————————————- HTML page templates described with in a toml config ———————————————————————- www: https://codeberg.org/bef7/htoml requirements: rustc 1.62+</p> +<hr /> +<p>======================================================================</p> diff --git a/src/common.rs b/src/common.rs @@ -0,0 +1,18 @@ +use serde::Deserialize; + +/// An anchor link +#[derive(Deserialize, Clone)] +pub struct Link { + /// Actual URL + pub url: String, + /// Link label text + pub text: String, +} + +/// Exported HTML and CSS code +pub struct Export { + /// Exported HTML + pub html: String, + /// Exported CSS + pub css: String, +} diff --git a/src/htoml.rs b/src/htoml.rs @@ -0,0 +1,78 @@ +use crate::sections::{self, Sections}; +use serde::Deserialize; +use std::path::PathBuf; + +/// Configuration of webpages +#[derive(Deserialize, Clone)] +pub struct Htoml { + /// Title of the HTML page + pub title: String, + /// Path to html output + pub output: PathBuf, + /// Sections of the HTML page + pub sections: Sections, +} + +impl Htoml { + /// Epxort toml to HTML + pub fn export(&self) -> String { + let sections_export = self.sections.export(); + + format!( + r#"<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <meta http-equiv="X-UA-Compatible" content="ie=edge"> + <title>{}</title> + <style> + {} + </style> + </head> + <body> + {} + </body> +</html>"#, + self.title, sections_export.css, sections_export.html + ) + } +} + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use super::*; + + #[test] + fn serde() { + let htoml: Htoml = toml::from_str( + r#" + title = "Homepage" + output = "index.html" + + [sections] + + [sections.navbar] + variant = "basic" + links = [{url= "/", text= "Home"}, {url= "/about", text= "About"},] + "#, + ) + .unwrap(); + + assert_eq!(htoml.title, "Homepage".to_string()); + assert_eq!(htoml.output, PathBuf::from_str("index.html").unwrap()); + + if let Some(navbar) = htoml.clone().sections.navbar { + assert_eq!(&navbar.variant, "basic"); + assert_eq!(navbar.links.len(), 2); + assert_eq!(&navbar.links[0].url, "/"); + assert_eq!(&navbar.links[0].text, "Home"); + assert_eq!(&navbar.links[1].url, "/about"); + assert_eq!(&navbar.links[1].text, "About"); + } else { + panic!("Navbar not found"); + } + } +} diff --git a/src/main.rs b/src/main.rs @@ -0,0 +1,5 @@ +pub mod common; +pub mod htoml; +pub mod sections; + +fn main() {} diff --git a/src/sections/mod.rs b/src/sections/mod.rs @@ -0,0 +1,34 @@ +use crate::common::Export; +use serde::Deserialize; +pub mod navbar; + +/// A section of an HTML webpage +pub trait Section { + /// Export section to HTML string + fn export(&self) -> Export; +} + +#[derive(Deserialize, Clone)] +pub struct Sections { + /// Navigation bar section + pub navbar: Option<navbar::Navbar>, +} + +impl Sections { + /// Export sections to HTML + pub fn export(&self) -> 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 => (), + } + + Export { html, css } + } +} diff --git a/src/sections/navbar.rs b/src/sections/navbar.rs @@ -0,0 +1,39 @@ +use crate::common::{Export, Link}; +use crate::sections::Section; +use serde::Deserialize; + +#[derive(Deserialize, Clone)] +pub struct Navbar { + /// Variant of navbar to use + pub variant: String, + /// Navbar links + pub links: Vec<Link>, +} + +impl Section for Navbar { + fn export(&self) -> Export { + let mut html = "".to_string(); + let mut css = "".to_string(); + + match self.variant.as_str() { + "basic" => { + for link in &self.links { + html = format!("{}\n<a href='{}'>{}</a>", html, link.url, link.text); + } + + html = format!("<nav>{}</nav>", html); + } + _ => panic!("Invalid Navbar variant"), + } + Export { html, css } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +}