From f40ee315f9b0696248d34a1fcd7ca7a34d6eec2a Mon Sep 17 00:00:00 2001 From: Teascade Date: Mon, 16 Apr 2018 15:21:31 +0300 Subject: [PATCH] make navbars render --- config.toml | 14 +++++++- .../test.png => public/img/teascade.png | Bin public/test.html | 14 +++++++- src/config_toml.rs | 17 ++++++++++ src/main.rs | 20 +++++++++-- src/renderer.rs | 32 +++++++++++++++++- src/template.rs | 18 +++++++++- ...template.html => image-item-template.html} | 2 +- src/templates/navbar/item-template.html | 3 ++ .../navbar/navbar-item-template.html | 3 -- src/templates/page-template.html | 2 +- test_page.toml | 1 + 12 files changed, 114 insertions(+), 12 deletions(-) rename src/templates/test.png => public/img/teascade.png (100%) rename src/templates/navbar/{navbar-image-item-template.html => image-item-template.html} (59%) create mode 100644 src/templates/navbar/item-template.html delete mode 100644 src/templates/navbar/navbar-item-template.html diff --git a/config.toml b/config.toml index 19052d5..acceb88 100644 --- a/config.toml +++ b/config.toml @@ -7,4 +7,16 @@ css = [] #favicon = "path.png" #twitter_author = "@teascade" #google_robots = "all" -#google_site_verification = "" \ No newline at end of file +#google_site_verification = "" + +[navbar] +items = ["home", "potato", "games"] + + [navbar.item.home] + title = "Home" + link = "home" + image_url = "img/teascade.png" + + [navbar.item.games] + title = "Games" + link = "/games" \ No newline at end of file diff --git a/src/templates/test.png b/public/img/teascade.png similarity index 100% rename from src/templates/test.png rename to public/img/teascade.png diff --git a/public/test.html b/public/test.html index ee72fff..b07c191 100644 --- a/public/test.html +++ b/public/test.html @@ -36,7 +36,19 @@ diff --git a/src/config_toml.rs b/src/config_toml.rs index 4544977..3826be4 100644 --- a/src/config_toml.rs +++ b/src/config_toml.rs @@ -3,10 +3,12 @@ use toml; use std::path::PathBuf; use std::fs::File; use std::io::{Error, ErrorKind, Read}; +use std::collections::HashMap; #[derive(Deserialize, Clone, Debug)] pub struct GlobalConfigToml { pub website: WebsiteConfig, + pub navbar: Option, } #[derive(Deserialize, Clone, Debug)] @@ -22,6 +24,20 @@ pub struct WebsiteConfig { pub google_site_verification: Option, } +#[derive(Deserialize, Clone, Debug)] +pub struct NavbarConfig { + pub items: Vec, + #[serde(rename = "item")] + pub item_map: HashMap, +} + +#[derive(Deserialize, Clone, Debug)] +pub struct NavbarItem { + pub title: String, + pub link: String, + pub image_url: Option, +} + impl GlobalConfigToml { pub fn get_config() -> Result { let mut file = File::open("config.toml")?; @@ -85,6 +101,7 @@ impl PageConfigToml { #[derive(Deserialize, Clone, Debug)] pub struct PageConfig { + pub html_path: String, pub title: String, pub description: String, pub content_path: String, diff --git a/src/main.rs b/src/main.rs index d26af7c..1fea15a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,8 @@ use std::io::prelude::*; const DEFAULT_CSS: &'static str = include_str!("templates/default-css.css"); const PAGE_TEMPLATE: &'static str = include_str!("templates/page-template.html"); +const NAVBAR_ITEM: &'static str = include_str!("templates/navbar/item-template.html"); +const NAVBAR_IMAGE_ITEM: &'static str = include_str!("templates/navbar/image-item-template.html"); fn main() { println!("Fetching configs"); @@ -33,13 +35,25 @@ fn main() { file.write_all(DEFAULT_CSS.as_bytes()).ok(); } - println!("Generating page template"); - let template = Template::new(PAGE_TEMPLATE); + println!("Generating page templates"); + let page_template = Template::new(PAGE_TEMPLATE); + + let mut navbar_content = String::new(); + if config.global_config.navbar.is_some() { + let navbar_item_template = Template::new(NAVBAR_ITEM); + let navbar_image_item_template = Template::new(NAVBAR_IMAGE_ITEM); + println!("Rendering navbar"); + match renderer::render_navbar(&config, navbar_item_template, navbar_image_item_template) { + Ok(navbar) => navbar_content = navbar, + Err(err) => panic!(err), + } + } println!("Rendering public/test.html"); let rendered = match config.get_configs_for(0) { - Ok(page_config) => template.render(&Template::page_data_from( + Ok(page_config) => page_template.render(&Template::page_data_from( page_config.clone(), + navbar_content, renderer::render_markdown_content(&page_config).unwrap(), )), Err(err) => panic!(err), diff --git a/src/renderer.rs b/src/renderer.rs index dedf957..766a6da 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -3,7 +3,8 @@ use std::io::Read; use pulldown_cmark::{html, Parser}; -use config::SinglePageConfigs; +use config::{Config, SinglePageConfigs}; +use template::Template; pub fn render_markdown_content(config: &SinglePageConfigs) -> Result { if let Some(parent) = config.page_config_path.parent() { @@ -27,3 +28,32 @@ pub fn render_markdown_content(config: &SinglePageConfigs) -> Result Result { + let mut navbar_str = String::new(); + + if let Some(navbar) = config.global_config.navbar.clone() { + for item_str in navbar.items { + if let Some(item) = navbar.item_map.get(&item_str) { + let data = Template::navbar_item_data_from(item.clone()); + if item.image_url.is_some() { + navbar_str += &*image_item_template.render(&data); + } else { + navbar_str += &*item_template.render(&data); + } + } else { + return Err(format!( + "Navbar item does not exist: navbar.item.{}", + item_str + )); + } + } + Ok(navbar_str) + } else { + Err("Attempted to render navbar without navbar".to_owned()) + } +} diff --git a/src/template.rs b/src/template.rs index e84ad0f..5e726bf 100644 --- a/src/template.rs +++ b/src/template.rs @@ -3,6 +3,7 @@ use regex::{Captures, Regex}; use std::collections::HashMap; use config::SinglePageConfigs; +use config_toml::NavbarItem; type Data = HashMap; @@ -62,7 +63,21 @@ impl Template { result } - pub fn page_data_from(config: SinglePageConfigs, content: String) -> Data { + pub fn navbar_item_data_from(item: NavbarItem) -> Data { + let map = hashmap!( + "title".to_owned() => item.title, + "link".to_owned() => item.link, + "image".to_owned() => item.image_url.unwrap_or(String::new()) + ); + + map + } + + pub fn page_data_from( + config: SinglePageConfigs, + navbar_content: String, + content: String, + ) -> Data { let favicon = config.page_config.page.favicon.unwrap_or( config .global_config @@ -97,6 +112,7 @@ impl Template { "use_default_css".to_owned() => config.global_config.website.use_default_css.to_string(), "navbar".to_owned() => "true".to_owned(), + "navbar_content".to_owned() => navbar_content, "content".to_owned() => content ); diff --git a/src/templates/navbar/navbar-image-item-template.html b/src/templates/navbar/image-item-template.html similarity index 59% rename from src/templates/navbar/navbar-image-item-template.html rename to src/templates/navbar/image-item-template.html index 71ed53a..e57b7cd 100644 --- a/src/templates/navbar/navbar-image-item-template.html +++ b/src/templates/navbar/image-item-template.html @@ -1,6 +1,6 @@
  • - {{image-alt}} + {{title}}
  • \ No newline at end of file diff --git a/src/templates/navbar/item-template.html b/src/templates/navbar/item-template.html new file mode 100644 index 0000000..4402bbe --- /dev/null +++ b/src/templates/navbar/item-template.html @@ -0,0 +1,3 @@ +
  • + {{title}} +
  • \ No newline at end of file diff --git a/src/templates/navbar/navbar-item-template.html b/src/templates/navbar/navbar-item-template.html deleted file mode 100644 index ff5b06d..0000000 --- a/src/templates/navbar/navbar-item-template.html +++ /dev/null @@ -1,3 +0,0 @@ -
  • - {{name}} -
  • \ No newline at end of file diff --git a/src/templates/page-template.html b/src/templates/page-template.html index 408e2f5..7726665 100644 --- a/src/templates/page-template.html +++ b/src/templates/page-template.html @@ -36,7 +36,7 @@ {{if navbar}} {{endif}} diff --git a/test_page.toml b/test_page.toml index 57960b8..953a0d3 100644 --- a/test_page.toml +++ b/test_page.toml @@ -1,4 +1,5 @@ [page] +html_path = "test.html" title = "Test Page" description = "This is a test page." content_path = "content.md"