core: Add error for invalid source markdown.

This commit is contained in:
Dorian 2019-09-03 17:54:16 -04:00
parent 8fdf7f67c8
commit d9e8848607
3 changed files with 27 additions and 23 deletions

View File

@ -1,5 +1,5 @@
use std::{error, fmt, io};
use crate::files::FileType; use crate::files::FileType;
use std::{error, fmt, io};
use colored::Colorize; use colored::Colorize;
@ -9,6 +9,7 @@ pub enum RookeriesError {
ServerPortBind(io::Error, u16), ServerPortBind(io::Error, u16),
MissingSiteToml, MissingSiteToml,
CopyFileFailure(io::Error, String, FileType), CopyFileFailure(io::Error, String, FileType),
ReadMarkdownSourceFailure(io::Error, String),
} }
impl fmt::Display for RookeriesError { impl fmt::Display for RookeriesError {
@ -32,12 +33,11 @@ impl fmt::Display for RookeriesError {
"rookeries init".bright_yellow(), "rookeries init".bright_yellow(),
), ),
RookeriesError::CopyFileFailure(ref e, ref filename, ref file_type) => { RookeriesError::CopyFileFailure(ref e, ref filename, ref file_type) => {
writeln!( writeln!(f, "Could not copy over {} {}.", file_type, filename.red(),)?;
f, write!(f, "Exact cause: {}", e)
"Could not copy over {} {}.", }
file_type, RookeriesError::ReadMarkdownSourceFailure(ref e, ref filename) => {
filename.red(), writeln!(f, "Could not read Markdown source {}.", filename.red(),)?;
)?;
write!(f, "Exact cause: {}", e) write!(f, "Exact cause: {}", e)
} }
} }
@ -51,6 +51,7 @@ impl error::Error for RookeriesError {
RookeriesError::ServerPortBind(ref e, _) => Some(e), RookeriesError::ServerPortBind(ref e, _) => Some(e),
RookeriesError::MissingSiteToml => None, RookeriesError::MissingSiteToml => None,
RookeriesError::CopyFileFailure(ref e, _, _) => Some(e), RookeriesError::CopyFileFailure(ref e, _, _) => Some(e),
RookeriesError::ReadMarkdownSourceFailure(ref e, _) => Some(e),
} }
} }
} }

View File

@ -22,8 +22,8 @@ use chrono::prelude::*;
use colored::Colorize; use colored::Colorize;
use docopt::Docopt; use docopt::Docopt;
use env_logger::{Builder, Env}; use env_logger::{Builder, Env};
use rookeries::files::FileType;
use rookeries::errors::RookeriesError; use rookeries::errors::RookeriesError;
use rookeries::files::FileType;
use rookeries::{errors, Page, Site}; use rookeries::{errors, Page, Site};
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
@ -230,12 +230,13 @@ fn build_site(args: &Args) -> Result {
)); ));
// Build a list of the pages to build out. // Build a list of the pages to build out.
// TODO: Figure out something more flexible and less likely to error out.
header_message("Searching for pages..."); header_message("Searching for pages...");
let mut site_source_pages: Vec<OsString> = Vec::new(); let mut site_source_pages: Vec<OsString> = Vec::new();
for entry in read_dir(&project_directory.as_path()).unwrap() { for entry in read_dir(&project_directory.as_path())? {
// TODO: Make the search recursive through directories. // TODO: Make the search recursive through directories.
let path = entry.ok().unwrap().path(); let path = entry?.path();
if path.is_file() && path.extension() != None && path.extension().unwrap() == "md" { if path.is_file() && path.extension() != None && path.extension()? == "md" {
success_message(&format!("Found {}", path.display())); success_message(&format!("Found {}", path.display()));
site_source_pages.push(path.into_os_string()); site_source_pages.push(path.into_os_string());
} }
@ -251,15 +252,18 @@ fn build_site(args: &Args) -> Result {
.iter() .iter()
.map(|source_page| { .map(|source_page| {
let source_path = Path::new(source_page); let source_path = Path::new(source_page);
let slug = String::from(source_path.file_stem().unwrap().to_str().unwrap()); let slug = String::from(source_path.file_stem()?.to_str()?);
let content = read_to_string(source_path).unwrap(); let content = read_to_string(source_path).map_err(|err| {
RookeriesError::ReadMarkdownSourceFailure(err, source_page.into_string()?)
})?;
// TODO: Be able to extract the title from the header of the markdown content. // TODO: Be able to extract the title from the header of the markdown content.
let title = match &page_overrides.get(&slug) { let title = match &page_overrides.get(&slug) {
Some(page_entry) => { Some(page_entry) => {
let page_title = match page_entry.get("title") { let page_title = match page_entry.get("title") {
Some(title_) => { Some(title_) => {
let title_str = serde_json::to_string(title_).unwrap(); let title_str =
serde_json::to_string(title_).unwrap_or("UNKNOWN".into_string());
title_str.replacen("\"", "", 2) title_str.replacen("\"", "", 2)
} }
None => format!("Rookeries :: {}", &slug), None => format!("Rookeries :: {}", &slug),
@ -281,18 +285,17 @@ fn build_site(args: &Args) -> Result {
.collect(); .collect();
// Create a build directory for the resulting files. // Create a build directory for the resulting files.
// TODO: Break out in standalone function and make more reliable.
header_message("Preparing build directory..."); header_message("Preparing build directory...");
let build_directory = project_directory.clone().join("build"); let build_directory = project_directory.clone().join("build");
if build_directory.exists() && build_directory.is_dir() { if build_directory.exists() {
caution_message("Recreating build directory..."); caution_message("Recreating build directory...");
remove_dir_all(&build_directory).expect("Failure!"); remove_dir_all(&build_directory)?;
create_dir(&build_directory).expect("Failure!"); create_dir(&build_directory)?;
success_message("Created a build directory.");
} else if !build_directory.exists() {
create_dir(&build_directory).expect("Failure!");
success_message("Created a build directory."); success_message("Created a build directory.");
} else { } else {
fatal_condition("Can not create a build directory!!!"); create_dir(&build_directory)?;
success_message("Created a build directory.");
} }
// TODO: Create the html setup for the pages. // TODO: Create the html setup for the pages.