175 lines
5.3 KiB
Rust
175 lines
5.3 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use crate::config_toml::{GlobalConfigToml, PageConfig, PageConfigToml};
|
|
use crate::error::Error;
|
|
use crate::file_writer;
|
|
use crate::logger::{LogLevel, Logger};
|
|
use crate::options::{NewOpt, Opt};
|
|
|
|
use pathdiff;
|
|
|
|
const PLACEHOLDER_MARKDOWN: &'static str = r#"# Placeholder title
|
|
This is the markdown file, where you will insert this page's contents.
|
|
- This is an example list
|
|
- And in lists
|
|
- You can even make inner lists, as well as **bold**, and _italics_
|
|
|
|
See [CommonMark](http://commonmark.org/help/) for reference."#;
|
|
|
|
pub fn generate_new_page(logger: &Logger, opt: &Opt, new_opt: &NewOpt) -> Result<(), Error> {
|
|
logger.log(
|
|
LogLevel::DETAILER,
|
|
format!("Starting to create paths for given files"),
|
|
);
|
|
|
|
let toml_path = ensure_extension(new_opt.toml_path.clone(), "toml", false)?;
|
|
|
|
let markdown_path;
|
|
if let Some(markdown) = new_opt.markdown_path.clone() {
|
|
markdown_path = ensure_extension(markdown, "md", false)?;
|
|
} else {
|
|
markdown_path = ensure_extension(toml_path.clone(), "md", true)?;
|
|
}
|
|
|
|
logger.log(
|
|
LogLevel::INFO,
|
|
format!(
|
|
"Creating page config at {:?} and markdown at {:?}",
|
|
toml_path, markdown_path
|
|
),
|
|
);
|
|
|
|
logger.log(
|
|
LogLevel::DETAIL,
|
|
format!("Creating a new .toml file at {:?}", toml_path),
|
|
);
|
|
|
|
let html_path;
|
|
if let Some(path) = new_opt.html_path.clone() {
|
|
html_path = ensure_extension(path, "html", false)?;
|
|
} else {
|
|
html_path = ensure_extension(toml_path.clone(), "html", true)?;
|
|
}
|
|
|
|
let title: String;
|
|
if let Some(t) = new_opt.title.clone() {
|
|
title = t;
|
|
} else {
|
|
let mut parts = toml_path.file_name().unwrap().to_str().unwrap().split(".");
|
|
title = parts.next().unwrap().to_owned();
|
|
}
|
|
|
|
let mut relative_markdown_path = markdown_path.clone();
|
|
if let Some(toml_parent) = toml_path.parent() {
|
|
if let Some(relative) = pathdiff::diff_paths(markdown_path.as_path(), toml_parent) {
|
|
relative_markdown_path = relative;
|
|
}
|
|
}
|
|
|
|
logger.log(
|
|
LogLevel::DETAILER,
|
|
format!("Generating page config.toml contents"),
|
|
);
|
|
|
|
let page_config = PageConfigToml {
|
|
page: PageConfig {
|
|
html_path: html_path.to_str().unwrap().to_owned(),
|
|
title: title,
|
|
description: String::new(),
|
|
content_path: relative_markdown_path.to_str().unwrap().to_owned(),
|
|
favicon: None,
|
|
before_navbar_url: None,
|
|
before_content_url: None,
|
|
after_content_url: None,
|
|
javascript: None,
|
|
css: None,
|
|
},
|
|
};
|
|
|
|
match file_writer::write_toml(&toml_path, &page_config, opt.overwrite) {
|
|
Ok(_) => logger.log(
|
|
LogLevel::INFO,
|
|
format!("{:?} created successfully", toml_path),
|
|
),
|
|
Err(err) => {
|
|
return Err(Error::new(
|
|
err.severity(),
|
|
format!("Failed to serialize page config: {}", err.description()),
|
|
))
|
|
}
|
|
}
|
|
|
|
logger.log(
|
|
LogLevel::DETAIL,
|
|
format!("Creating a new .md file at {:?}", markdown_path),
|
|
);
|
|
|
|
file_writer::write_file(&markdown_path, PLACEHOLDER_MARKDOWN, opt.overwrite)?;
|
|
|
|
if !new_opt.no_modify_config {
|
|
if !opt.overwrite && PathBuf::from("config.toml").exists() {
|
|
return Err(Error::new(LogLevel::SEVERE, "Add --overwrite flag to overwrite config.toml, or --no-modify-config to not modify config.toml automatically."));
|
|
}
|
|
|
|
logger.log(
|
|
LogLevel::INFO,
|
|
format!("Adding page config path to config.toml"),
|
|
);
|
|
let mut config = GlobalConfigToml::get_config()?;
|
|
config
|
|
.website
|
|
.built_pages
|
|
.push(toml_path.to_str().unwrap().to_owned());
|
|
|
|
logger.log(
|
|
LogLevel::DETAIL,
|
|
format!("Re-serializing config and writing it"),
|
|
);
|
|
|
|
match file_writer::write_toml(&PathBuf::from("config.toml"), &config, opt.overwrite) {
|
|
Ok(_) => {}
|
|
Err(err) => {
|
|
return Err(Error::new(
|
|
err.severity(),
|
|
format!("Failed to write to config.toml: {}", err.description()),
|
|
))
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn ensure_extension<T: Into<String>>(
|
|
mut path: PathBuf,
|
|
extension: T,
|
|
replace_extension: bool,
|
|
) -> Result<PathBuf, Error> {
|
|
let extension = extension.into();
|
|
|
|
if let (Some(parent), Some(file_name)) = (path.clone().parent(), path.clone().file_name()) {
|
|
let mut filename = file_name.to_str().unwrap().to_owned();
|
|
let clone = filename.clone();
|
|
let split = clone.split(".");
|
|
if split.clone().count() == 1 as usize {
|
|
path = parent.join(format!("{}.{}", filename, extension));
|
|
} else if replace_extension {
|
|
let amount = split.clone().count() - 1;
|
|
filename = split
|
|
.clone()
|
|
.take(amount)
|
|
.map(|i| format!("{}.", i))
|
|
.collect::<String>();
|
|
|
|
path = parent.join(format!("{}{}", filename, extension));
|
|
}
|
|
|
|
Ok(path)
|
|
} else {
|
|
Err(Error::new(
|
|
LogLevel::SEVERE,
|
|
format!("Could not find parent/file_name for {:?}", path),
|
|
))
|
|
}
|
|
}
|