make navbars render
This commit is contained in:
parent
7249af10b8
commit
f40ee315f9
14
config.toml
14
config.toml
@ -7,4 +7,16 @@ css = []
|
||||
#favicon = "path.png"
|
||||
#twitter_author = "@teascade"
|
||||
#google_robots = "all"
|
||||
#google_site_verification = ""
|
||||
#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"
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
@ -36,7 +36,19 @@
|
||||
|
||||
<nav>
|
||||
<ul>
|
||||
|
||||
<li class="image">
|
||||
<a href="home">
|
||||
<img src="img/teascade.png" alt="Home">
|
||||
</img>
|
||||
</a>
|
||||
</li><li class="image">
|
||||
<a href="home">
|
||||
<img src="img/teascade.png" alt="Home">
|
||||
</img>
|
||||
</a>
|
||||
</li><li>
|
||||
<a href="games">Games</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
|
@ -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<NavbarConfig>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
@ -22,6 +24,20 @@ pub struct WebsiteConfig {
|
||||
pub google_site_verification: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct NavbarConfig {
|
||||
pub items: Vec<String>,
|
||||
#[serde(rename = "item")]
|
||||
pub item_map: HashMap<String, NavbarItem>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct NavbarItem {
|
||||
pub title: String,
|
||||
pub link: String,
|
||||
pub image_url: Option<String>,
|
||||
}
|
||||
|
||||
impl GlobalConfigToml {
|
||||
pub fn get_config() -> Result<GlobalConfigToml, Error> {
|
||||
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,
|
||||
|
20
src/main.rs
20
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),
|
||||
|
@ -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<String, String> {
|
||||
if let Some(parent) = config.page_config_path.parent() {
|
||||
@ -27,3 +28,32 @@ pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, Str
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_navbar(
|
||||
config: &Config,
|
||||
item_template: Template,
|
||||
image_item_template: Template,
|
||||
) -> Result<String, String> {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ use regex::{Captures, Regex};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use config::SinglePageConfigs;
|
||||
use config_toml::NavbarItem;
|
||||
|
||||
type Data = HashMap<String, String>;
|
||||
|
||||
@ -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
|
||||
);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<li class="image">
|
||||
<a href="{{link}}">
|
||||
<img src="{{image}}" alt="{{image-alt}}">
|
||||
<img src="{{image}}" alt="{{title}}">
|
||||
</img>
|
||||
</a>
|
||||
</li>
|
3
src/templates/navbar/item-template.html
Normal file
3
src/templates/navbar/item-template.html
Normal file
@ -0,0 +1,3 @@
|
||||
<li>
|
||||
<a href="{{link}}">{{title}}</a>
|
||||
</li>
|
@ -1,3 +0,0 @@
|
||||
<li>
|
||||
<a href="{{link}}">{{name}}</a>
|
||||
</li>
|
@ -36,7 +36,7 @@
|
||||
{{if navbar}}
|
||||
<nav>
|
||||
<ul>
|
||||
{{items}}
|
||||
{{navbar_content}}
|
||||
</ul>
|
||||
</nav>
|
||||
{{endif}}
|
||||
|
@ -1,4 +1,5 @@
|
||||
[page]
|
||||
html_path = "test.html"
|
||||
title = "Test Page"
|
||||
description = "This is a test page."
|
||||
content_path = "content.md"
|
||||
|
Loading…
Reference in New Issue
Block a user