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]
website_name = "TestWebsite" website_name = "Test Website Name!"
built_pages = ["test_page/about.toml", "test_page/other.toml"] built_pages = ["test_page/about.toml", "test_page/other.toml"]
use_default_css = true use_default_css = true
javascript = [] javascript = []
css = [] 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] [navbar]
items = ["test", "other"] items = ["about", "other", "google"]
[navbar.item.about]
title = "About"
link = "/"
[navbar.item.other] [navbar.item.other]
title = "Other" title = "Other"
link = "/other.html" link = "/other"
[navbar.item.test] [navbar.item.google]
title = "Home" title = "Google!"
link = "/home.html" link = "https://google.com"
image_url = "/img/image.png"

View File

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

View File

@ -1,12 +1,73 @@
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::PathBuf;
use std::error::Error as STDError;
use pulldown_cmark::{html, Parser}; use pulldown_cmark::{html, Parser};
use config::{Config, SinglePageConfigs}; use config::{Config, SinglePageConfigs};
use template::Template; use template::Template;
use error::Error; 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> { pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, Error> {
if let Some(parent) = config.page_config_path.parent() { if let Some(parent) = config.page_config_path.parent() {

View File

@ -76,6 +76,9 @@ impl Template {
pub fn page_data_from( pub fn page_data_from(
config: SinglePageConfigs, config: SinglePageConfigs,
navbar_content: String, navbar_content: String,
before_navbar: String,
before_content: String,
after_content: String,
content: String, content: String,
) -> Data { ) -> Data {
let favicon = config.page_config.page.favicon.unwrap_or( let favicon = config.page_config.page.favicon.unwrap_or(
@ -109,6 +112,10 @@ impl Template {
"google_verification".to_owned() => google_verification, "google_verification".to_owned() => google_verification,
"twitter_author".to_owned() => twitter_author, "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(), "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(), "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" content_path = "other.md"
#favicon = "optional_favicon.png" #favicon = "optional_favicon.png"
#before_navbar_url = "/navbar.html" #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" #after_content_url = "/after_content.html"
#javascript = ["Additional JavaScript"] #javascript = ["Additional JavaScript"]
#css = ["Additional CSS"] #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>