Add injections for before navbar and after/before content

This commit is contained in:
Sofia 2018-04-18 21:59:44 +03:00
parent 1bad6a2309
commit e8f8650dce
9 changed files with 107 additions and 12 deletions

View File

@ -1,17 +1,28 @@
[website]
website_name = "TestWebsite"
website_name = "Test Website Name!"
built_pages = ["test_page/about.toml", "test_page/other.toml"]
use_default_css = true
javascript = []
css = []
before_navbar_url = "test_page/resources/before_navbar.html"
before_content_url = "test_page/resources/before_content.html"
after_content_url = "test_page/resources/after_content.html"
#favicon = "favicon.png"
#twitter_author = "@teascade"
#google_robots = "all"
#google_site_verification = ""
[navbar]
items = ["test", "other"]
[navbar.item.other]
title = "Other"
link = "/other.html"
items = ["about", "other", "google"]
[navbar.item.test]
title = "Home"
link = "/home.html"
image_url = "/img/image.png"
[navbar.item.about]
title = "About"
link = "/"
[navbar.item.other]
title = "Other"
link = "/other"
[navbar.item.google]
title = "Google!"
link = "https://google.com"

View File

@ -46,12 +46,24 @@ pub fn build(logger: &Logger, opt: &Opt, _: &BuildOpt) -> Result<(), Error> {
let configs = config.get_configs()?;
let mut renders = Vec::new();
for config in configs.clone() {
logger.log(LogLevel::DETAILER, "Rendering injections.");
let (before_navbar, before_content, after_content) =
renderer::render_injections(&logger, &config)?;
logger.log(
LogLevel::DETAILER,
format!("Rendering {}", config.page_config.page.html_path),
);
let markdown = renderer::render_markdown_content(&config)?;
let data = Template::page_data_from(config.clone(), navbar_content.clone(), markdown);
let data = Template::page_data_from(
config.clone(),
navbar_content.clone(),
before_navbar,
before_content,
after_content,
markdown,
);
let rendered = page_template.render(&data);
renders.push(rendered);
}

View File

@ -1,12 +1,73 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::error::Error as STDError;
use pulldown_cmark::{html, Parser};
use config::{Config, SinglePageConfigs};
use template::Template;
use error::Error;
use logger::LogLevel;
use logger::{LogLevel, Logger};
fn get_file_contents(path: Option<String>) -> Result<String, Error> {
match path {
Some(url) => match File::open(PathBuf::from(&url)) {
Ok(mut file) => {
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
Ok(content)
}
Err(err) => Err(Error::new(
LogLevel::WARNING,
format!("Failed to open {}, {}", url, err.description().to_owned()),
)),
},
None => Ok(String::new()),
}
}
pub fn render_injections(
logger: &Logger,
config: &SinglePageConfigs,
) -> Result<(String, String, String), Error> {
let before_navbar_url = match config.page_config.page.before_navbar_url.clone() {
Some(url) => Some(url),
None => config.global_config.website.before_navbar_url.clone(),
};
let before_content_url = match config.page_config.page.before_content_url.clone() {
Some(url) => Some(url),
None => config.global_config.website.before_content_url.clone(),
};
let after_content_url = match config.page_config.page.after_content_url.clone() {
Some(url) => Some(url),
None => config.global_config.website.after_content_url.clone(),
};
let before_navbar = match get_file_contents(before_navbar_url) {
Ok(content) => content,
Err(error) => {
logger.log(error.severity(), error.description());
String::new()
}
};
let before_content = match get_file_contents(before_content_url) {
Ok(content) => content,
Err(error) => {
logger.log(error.severity(), error.description());
String::new()
}
};
let after_content = match get_file_contents(after_content_url) {
Ok(content) => content,
Err(error) => {
logger.log(error.severity(), error.description());
String::new()
}
};
Ok((before_navbar, before_content, after_content))
}
pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, Error> {
if let Some(parent) = config.page_config_path.parent() {

View File

@ -76,6 +76,9 @@ impl Template {
pub fn page_data_from(
config: SinglePageConfigs,
navbar_content: String,
before_navbar: String,
before_content: String,
after_content: String,
content: String,
) -> Data {
let favicon = config.page_config.page.favicon.unwrap_or(
@ -109,6 +112,10 @@ impl Template {
"google_verification".to_owned() => google_verification,
"twitter_author".to_owned() => twitter_author,
"before_navbar".to_owned() => before_navbar,
"before_content".to_owned() => before_content,
"after_content".to_owned() => after_content,
"use_default_css".to_owned() => config.global_config.website.use_default_css.to_string(),
"navbar".to_owned() => config.global_config.navbar.is_some().to_string(),

View File

@ -5,7 +5,7 @@ description = "This is the .. other page"
content_path = "other.md"
#favicon = "optional_favicon.png"
#before_navbar_url = "/navbar.html"
#before_content_url = "/before_content.html"
before_content_url = "test_page/resources/before_content_other.html"
#after_content_url = "/after_content.html"
#javascript = ["Additional JavaScript"]
#css = ["Additional CSS"]

View File

@ -0,0 +1 @@
<h1>Hi! I'm after the content!</h1>

View File

@ -0,0 +1 @@
<h1>Hi! I'm before the content!</h1>

View File

@ -0,0 +1 @@
<h1>I'm different :)</h1>

View File

@ -0,0 +1 @@
<h1>Hi! I'm before the navbar!</h1>