core: Add error for invalid source markdown.
This commit is contained in:
parent
8fdf7f67c8
commit
d9e8848607
|
@ -1,5 +1,5 @@
|
|||
use std::{error, fmt, io};
|
||||
use crate::files::FileType;
|
||||
use std::{error, fmt, io};
|
||||
|
||||
use colored::Colorize;
|
||||
|
||||
|
@ -9,6 +9,7 @@ pub enum RookeriesError {
|
|||
ServerPortBind(io::Error, u16),
|
||||
MissingSiteToml,
|
||||
CopyFileFailure(io::Error, String, FileType),
|
||||
ReadMarkdownSourceFailure(io::Error, String),
|
||||
}
|
||||
|
||||
impl fmt::Display for RookeriesError {
|
||||
|
@ -32,12 +33,11 @@ impl fmt::Display for RookeriesError {
|
|||
"rookeries init".bright_yellow(),
|
||||
),
|
||||
RookeriesError::CopyFileFailure(ref e, ref filename, ref file_type) => {
|
||||
writeln!(
|
||||
f,
|
||||
"Could not copy over {} {}.",
|
||||
file_type,
|
||||
filename.red(),
|
||||
)?;
|
||||
writeln!(f, "Could not copy over {} {}.", file_type, filename.red(),)?;
|
||||
write!(f, "Exact cause: {}", e)
|
||||
}
|
||||
RookeriesError::ReadMarkdownSourceFailure(ref e, ref filename) => {
|
||||
writeln!(f, "Could not read Markdown source {}.", filename.red(),)?;
|
||||
write!(f, "Exact cause: {}", e)
|
||||
}
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ impl error::Error for RookeriesError {
|
|||
RookeriesError::ServerPortBind(ref e, _) => Some(e),
|
||||
RookeriesError::MissingSiteToml => None,
|
||||
RookeriesError::CopyFileFailure(ref e, _, _) => Some(e),
|
||||
RookeriesError::ReadMarkdownSourceFailure(ref e, _) => Some(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ pub enum FileType {
|
|||
|
||||
impl fmt::Display for FileType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let name = match self {
|
||||
let name = match self {
|
||||
FileType::Plugin => "plugin",
|
||||
FileType::HtmlTemplate => "HTML template",
|
||||
FileType::SampleMarkdown => "sample Markdown",
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -22,8 +22,8 @@ use chrono::prelude::*;
|
|||
use colored::Colorize;
|
||||
use docopt::Docopt;
|
||||
use env_logger::{Builder, Env};
|
||||
use rookeries::files::FileType;
|
||||
use rookeries::errors::RookeriesError;
|
||||
use rookeries::files::FileType;
|
||||
use rookeries::{errors, Page, Site};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
@ -90,7 +90,7 @@ fn get_project_directory(args: &Args) -> std::result::Result<PathBuf, errors::Ro
|
|||
Ok(project_directory.canonicalize()?)
|
||||
}
|
||||
|
||||
fn copy_file (
|
||||
fn copy_file(
|
||||
source_directory: &PathBuf,
|
||||
target_directory: &PathBuf,
|
||||
filename: &str,
|
||||
|
@ -230,12 +230,13 @@ fn build_site(args: &Args) -> Result {
|
|||
));
|
||||
|
||||
// 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...");
|
||||
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.
|
||||
let path = entry.ok().unwrap().path();
|
||||
if path.is_file() && path.extension() != None && path.extension().unwrap() == "md" {
|
||||
let path = entry?.path();
|
||||
if path.is_file() && path.extension() != None && path.extension()? == "md" {
|
||||
success_message(&format!("Found {}", path.display()));
|
||||
site_source_pages.push(path.into_os_string());
|
||||
}
|
||||
|
@ -251,15 +252,18 @@ fn build_site(args: &Args) -> Result {
|
|||
.iter()
|
||||
.map(|source_page| {
|
||||
let source_path = Path::new(source_page);
|
||||
let slug = String::from(source_path.file_stem().unwrap().to_str().unwrap());
|
||||
let content = read_to_string(source_path).unwrap();
|
||||
let slug = String::from(source_path.file_stem()?.to_str()?);
|
||||
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.
|
||||
let title = match &page_overrides.get(&slug) {
|
||||
Some(page_entry) => {
|
||||
let page_title = match page_entry.get("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)
|
||||
}
|
||||
None => format!("Rookeries :: {}", &slug),
|
||||
|
@ -281,18 +285,17 @@ fn build_site(args: &Args) -> Result {
|
|||
.collect();
|
||||
|
||||
// Create a build directory for the resulting files.
|
||||
// TODO: Break out in standalone function and make more reliable.
|
||||
header_message("Preparing build directory...");
|
||||
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...");
|
||||
remove_dir_all(&build_directory).expect("Failure!");
|
||||
create_dir(&build_directory).expect("Failure!");
|
||||
success_message("Created a build directory.");
|
||||
} else if !build_directory.exists() {
|
||||
create_dir(&build_directory).expect("Failure!");
|
||||
remove_dir_all(&build_directory)?;
|
||||
create_dir(&build_directory)?;
|
||||
success_message("Created a build directory.");
|
||||
} 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.
|
||||
|
|
Loading…
Reference in New Issue