commit a2017fb4e08547282ce6a2c4a6fa2517e3a8c977
parent f88c50623a60ccd5197c48bc3776d87a1ae01a5e
Author: Jackson G. Kaindume <seestem@merely.tech>
Date: Fri, 19 Aug 2022 00:01:30 +0200
add gallery section
Diffstat:
3 files changed, 146 insertions(+), 0 deletions(-)
diff --git a/examples/frosh.toml b/examples/frosh.toml
@@ -21,3 +21,7 @@ variant = "frosh"
heading = "Welcome to frosh"
sub_heading = "Frosh is super cool!"
paragraph = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
+
+[sections.gallery]
+variant = "frosh"
+entries = [{src="https://duckduckgo.com/assets/logo_homepage.alt.v108.svg"}, {src="https://duckduckgo.com/assets/logo_homepage.alt.v108.svg"}, {src="https://duckduckgo.com/assets/logo_homepage.alt.v108.svg"}, {src="https://duckduckgo.com/assets/logo_homepage.alt.v108.svg"}]
diff --git a/src/sections/gallery.rs b/src/sections/gallery.rs
@@ -0,0 +1,133 @@
+use crate::common::Export;
+use crate::sections::{Section, SectionMetadata};
+use serde::Deserialize;
+
+#[derive(Deserialize, Clone)]
+pub struct Gallery {
+ /// Variant of about to use
+ pub variant: String,
+ /// Heading of about section
+ pub heading: Option<String>,
+ /// Images to be displayed in the gallery
+ pub entries: Vec<GalleryEntry>,
+}
+
+impl Gallery {
+ fn parse_entries(gallery: &Gallery) -> String {
+ let mut html = "<ul>".to_string();
+
+ for entry in &gallery.entries {
+ let mut alt_text = "".to_string();
+ let mut width = "100".to_string();
+ let mut height = "".to_string();
+
+ if let Some(a) = &entry.alt_text {
+ alt_text = a.clone()
+ }
+
+ if let Some(w) = entry.width {
+ width = format!("{}", w);
+ }
+
+ if let Some(h) = entry.height {
+ height = format!("{}", h);
+ }
+
+ html = format!(
+ r#"{}<img src='{}' alt='{}' width='{}' height='{}'>"#,
+ html, entry.src, alt_text, width, height
+ );
+ }
+
+ format!("{}</ul>", html)
+ }
+}
+
+impl Section for Gallery {
+ fn export(&self, metadata: &SectionMetadata) -> Export {
+ let mut html = "".to_string();
+ let mut css = "".to_string();
+ let mut heading = "".to_string();
+
+ if let Some(head) = &self.heading {
+ heading = format!("<h3>{}</h3>", head);
+ }
+
+ match self.variant.as_str() {
+ "basic" => {
+ css = format!(
+ r#".htoml_gallery{{
+ margin-top: 0;
+ padding: 2em;
+ height: 200px;
+ color: {};
+ }}"#,
+ metadata.tertiary_color
+ );
+
+ html = format!(
+ r#"
+ <section class='htoml_gallery'>
+ {}
+ </section>"#,
+ heading,
+ );
+ }
+ "frosh" => {
+ css = format!(
+ r#".htoml_gallery{{
+ margin: 10em 0 10em;
+ padding: 2em;
+ color: {};
+ background-color: {};
+ }}
+
+ img{{
+ background-color: {};
+ margin: 2em;
+ padding: .5em
+ }}
+
+ .htoml_gallery img:hover{{
+ cursor: zoom-in
+ }}
+ "#,
+ metadata.primary_color, metadata.tertiary_color, metadata.primary_color
+ );
+ html = format!(
+ r#"
+ <section class='htoml_gallery'>
+ {}
+ {}
+ </section>"#,
+ heading,
+ Gallery::parse_entries(self)
+ );
+ }
+ _ => panic!("Invalid Gallery variant"),
+ }
+ Export { html, css }
+ }
+}
+
+/// Item of Gallery
+#[derive(Deserialize, Clone)]
+pub struct GalleryEntry {
+ /// Specifies the path to the image
+ pub src: String,
+ /// Specifies an alternate text for the image
+ pub alt_text: Option<String>,
+ /// Specifies the height of an image
+ pub height: Option<i64>,
+ /// Specifies the width of an image
+ pub width: Option<i64>,
+}
+
+#[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
@@ -2,6 +2,7 @@ use crate::common::Export;
use serde::Deserialize;
pub mod about;
+pub mod gallery;
pub mod hero;
pub mod navbar;
@@ -30,6 +31,8 @@ pub struct Sections {
pub hero: Option<hero::Hero>,
/// About Section
pub about: Option<about::About>,
+ /// Gallery Section
+ pub gallery: Option<gallery::Gallery>,
}
impl Sections {
@@ -56,6 +59,12 @@ impl Sections {
css = format!("{}{}", css, about_export.css);
}
+ if let Some(gallery) = &self.gallery {
+ let gallery_export = gallery.export(metadata);
+ html = format!("{}{}", html, gallery_export.html);
+ css = format!("{}{}", css, gallery_export.css);
+ }
+
Export { html, css }
}
}