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"
|
#favicon = "path.png"
|
||||||
#twitter_author = "@teascade"
|
#twitter_author = "@teascade"
|
||||||
#google_robots = "all"
|
#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>
|
<nav>
|
||||||
<ul>
|
<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>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
|
@ -3,10 +3,12 @@ use toml;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Error, ErrorKind, Read};
|
use std::io::{Error, ErrorKind, Read};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
pub struct GlobalConfigToml {
|
pub struct GlobalConfigToml {
|
||||||
pub website: WebsiteConfig,
|
pub website: WebsiteConfig,
|
||||||
|
pub navbar: Option<NavbarConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
@ -22,6 +24,20 @@ pub struct WebsiteConfig {
|
|||||||
pub google_site_verification: Option<String>,
|
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 {
|
impl GlobalConfigToml {
|
||||||
pub fn get_config() -> Result<GlobalConfigToml, Error> {
|
pub fn get_config() -> Result<GlobalConfigToml, Error> {
|
||||||
let mut file = File::open("config.toml")?;
|
let mut file = File::open("config.toml")?;
|
||||||
@ -85,6 +101,7 @@ impl PageConfigToml {
|
|||||||
|
|
||||||
#[derive(Deserialize, Clone, Debug)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
pub struct PageConfig {
|
pub struct PageConfig {
|
||||||
|
pub html_path: String,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
pub content_path: 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 DEFAULT_CSS: &'static str = include_str!("templates/default-css.css");
|
||||||
const PAGE_TEMPLATE: &'static str = include_str!("templates/page-template.html");
|
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() {
|
fn main() {
|
||||||
println!("Fetching configs");
|
println!("Fetching configs");
|
||||||
@ -33,13 +35,25 @@ fn main() {
|
|||||||
file.write_all(DEFAULT_CSS.as_bytes()).ok();
|
file.write_all(DEFAULT_CSS.as_bytes()).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Generating page template");
|
println!("Generating page templates");
|
||||||
let template = Template::new(PAGE_TEMPLATE);
|
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");
|
println!("Rendering public/test.html");
|
||||||
let rendered = match config.get_configs_for(0) {
|
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(),
|
page_config.clone(),
|
||||||
|
navbar_content,
|
||||||
renderer::render_markdown_content(&page_config).unwrap(),
|
renderer::render_markdown_content(&page_config).unwrap(),
|
||||||
)),
|
)),
|
||||||
Err(err) => panic!(err),
|
Err(err) => panic!(err),
|
||||||
|
@ -3,7 +3,8 @@ use std::io::Read;
|
|||||||
|
|
||||||
use pulldown_cmark::{html, Parser};
|
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> {
|
pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, String> {
|
||||||
if let Some(parent) = config.page_config_path.parent() {
|
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 std::collections::HashMap;
|
||||||
|
|
||||||
use config::SinglePageConfigs;
|
use config::SinglePageConfigs;
|
||||||
|
use config_toml::NavbarItem;
|
||||||
|
|
||||||
type Data = HashMap<String, String>;
|
type Data = HashMap<String, String>;
|
||||||
|
|
||||||
@ -62,7 +63,21 @@ impl Template {
|
|||||||
result
|
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(
|
let favicon = config.page_config.page.favicon.unwrap_or(
|
||||||
config
|
config
|
||||||
.global_config
|
.global_config
|
||||||
@ -97,6 +112,7 @@ impl Template {
|
|||||||
"use_default_css".to_owned() => config.global_config.website.use_default_css.to_string(),
|
"use_default_css".to_owned() => config.global_config.website.use_default_css.to_string(),
|
||||||
"navbar".to_owned() => "true".to_owned(),
|
"navbar".to_owned() => "true".to_owned(),
|
||||||
|
|
||||||
|
"navbar_content".to_owned() => navbar_content,
|
||||||
"content".to_owned() => content
|
"content".to_owned() => content
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<li class="image">
|
<li class="image">
|
||||||
<a href="{{link}}">
|
<a href="{{link}}">
|
||||||
<img src="{{image}}" alt="{{image-alt}}">
|
<img src="{{image}}" alt="{{title}}">
|
||||||
</img>
|
</img>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</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}}
|
{{if navbar}}
|
||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<ul>
|
||||||
{{items}}
|
{{navbar_content}}
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
{{endif}}
|
{{endif}}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
[page]
|
[page]
|
||||||
|
html_path = "test.html"
|
||||||
title = "Test Page"
|
title = "Test Page"
|
||||||
description = "This is a test page."
|
description = "This is a test page."
|
||||||
content_path = "content.md"
|
content_path = "content.md"
|
||||||
|
Loading…
Reference in New Issue
Block a user