Make writing and rendering proper

This commit is contained in:
Sofia 2018-04-16 17:33:26 +03:00
parent a21e689a6f
commit 510d744ccb
4 changed files with 63 additions and 27 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
/target/
/target/
/public/

View File

@ -6,7 +6,7 @@ use error::Error;
use std::fs::{create_dir_all, File};
use std::io::prelude::*;
use std::path::Path;
use std::path::{Path, PathBuf};
const DEFAULT_CSS: &'static str = include_str!("templates/default-css.css");
const PAGE_TEMPLATE: &'static str = include_str!("templates/page-template.html");
@ -24,13 +24,15 @@ fn write_file<T: Into<String>, U: Into<String>>(path_str: T, content: U) -> Resu
let path_str = path_str.into();
let content = content.into();
let path = Path::new(&path_str);
if let Some(parent) = path.parent() {
create_dir_all(parent)?;
let mut path = PathBuf::from(&path_str);
if let (Some(parent), Some(file_name)) = (path.clone().parent(), path.clone().file_name()) {
let parent = Path::new("public").join(parent);
create_dir_all(parent.clone())?;
path = parent.join(file_name);
} else {
return Err(Error::new(
LogLevel::SEVERE,
format!("Could not find parent folder for {}", path_str),
format!("Could not find parent folder or file name for {}", path_str),
));
}
if path.exists() {
@ -54,8 +56,9 @@ pub fn build(logger: &Logger) -> Result<(), Error> {
let config = fetch_config()?;
if config.global_config.website.use_default_css {
logger.log(LogLevel::DETAIL, "Adding public/css/default.css");
write_file("public/css/default.css", DEFAULT_CSS)?;
let css_path = "css/default.css";
logger.log(LogLevel::DETAIL, format!("Adding public/{}", css_path));
write_file(css_path, DEFAULT_CSS)?;
}
logger.log(LogLevel::INFO, "Generating page templates");
@ -71,19 +74,32 @@ pub fn build(logger: &Logger) -> Result<(), Error> {
}
logger.log(LogLevel::INFO, "Rendering");
logger.log(LogLevel::DETAILER, "Rendering public/test.html");
let rendered = match config.get_configs_for(0) {
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),
};
let configs = config.get_configs()?;
let mut renders = Vec::new();
for config in configs.clone() {
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 rendered = page_template.render(&data);
renders.push(rendered);
}
logger.log(LogLevel::INFO, "Writing");
logger.log(LogLevel::DETAILER, "Writing public/test.html");
write_file("public/test.html", rendered)?;
for (idx, config) in configs.clone().iter().enumerate() {
let html_path = config.clone().page_config.page.html_path;
logger.log(LogLevel::DETAILER, format!("Writing public/{}", html_path));
if let Some(render) = renders.get(idx) {
write_file(html_path, render.clone())?;
} else {
return Err(Error::new(
LogLevel::SEVERE,
format!("Internal error; Render not found for file: {}", html_path),
));
}
}
logger.log(LogLevel::INFO, "Done!");
Ok(())

View File

@ -1,6 +1,7 @@
use config_toml::{GlobalConfigToml, PageConfigToml};
use std::io::Error;
use std::path::PathBuf;
use error::Error;
use logger::LogLevel;
#[derive(Clone)]
pub struct SinglePageConfigs {
@ -34,7 +35,7 @@ impl Config {
})
}
pub fn get_configs_for(&self, idx: u32) -> Result<SinglePageConfigs, String> {
pub fn get_configs_for(&self, idx: u32) -> Result<SinglePageConfigs, Error> {
if let (Some(page_config), Some(page_path)) = (
self.page_configs.get(idx as usize),
self.page_config_paths.get(idx as usize),
@ -45,7 +46,19 @@ impl Config {
page_config_path: page_path.clone(),
})
} else {
Err(format!("Failed to get page config, idx: {}", idx))
Err(Error::new(
LogLevel::SEVERE,
format!("Failed to get page config, idx: {}", idx),
))
}
}
pub fn get_configs(&self) -> Result<Vec<SinglePageConfigs>, Error> {
let mut configs = Vec::new();
for idx in 0..self.page_configs.len() {
let config = self.get_configs_for(idx as u32)?;
configs.push(config);
}
Ok(configs)
}
}

View File

@ -8,7 +8,7 @@ use template::Template;
use error::Error;
use logger::LogLevel;
pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, String> {
pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, Error> {
if let Some(parent) = config.page_config_path.parent() {
let path = parent.with_file_name(&config.page_config.page.content_path);
match File::open(&path) {
@ -21,12 +21,18 @@ pub fn render_markdown_content(config: &SinglePageConfigs) -> Result<String, Str
html::push_html(&mut markdown, parser);
Ok(markdown)
}
Err(_) => Err(format!("Failed to open file: {}", path.to_str().unwrap())),
Err(_) => Err(Error::new(
LogLevel::SEVERE,
format!("Failed to open file: {}", path.to_str().unwrap()),
)),
}
} else {
Err(format!(
"Failed to get page config parent: {}",
config.page_config_path.to_str().unwrap()
Err(Error::new(
LogLevel::SEVERE,
format!(
"Failed to get page config parent: {}",
config.page_config_path.to_str().unwrap()
),
))
}
}