From 52b2bab5fd38802d68f565bfe98ed1218fe527a5 Mon Sep 17 00:00:00 2001 From: Teascade Date: Tue, 17 Apr 2018 00:43:39 +0300 Subject: [PATCH] Add pages to config.toml when they are created with new --- config.toml | 25 +++++++++---------------- src/new_page.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/config.toml b/config.toml index 7906670..5f9892d 100644 --- a/config.toml +++ b/config.toml @@ -4,25 +4,18 @@ built_pages = ["test_page/about.toml", "test_page/other.toml"] use_default_css = true javascript = [] css = [] -#before_navbar_url = "/navbar.html" -#before_content_url = "/before_content.html" -#after_content_url = "/after_content.html" -#favicon = "favicon.png" -#twitter_author = "@teascade" -#google_robots = "all" -#google_site_verification = "" [navbar] items = ["about", "other", "google"] - [navbar.item.about] - title = "About" - link = "/" +[navbar.item.google] +title = "Google!" +link = "https://google.com" - [navbar.item.other] - title = "Other" - link = "/other" +[navbar.item.about] +title = "About" +link = "/" - [navbar.item.google] - title = "Google!" - link = "https://google.com" \ No newline at end of file +[navbar.item.other] +title = "Other" +link = "/other" diff --git a/src/new_page.rs b/src/new_page.rs index 8735e52..65d886d 100644 --- a/src/new_page.rs +++ b/src/new_page.rs @@ -5,7 +5,7 @@ use options::NewOps; use logger::{LogLevel, Logger}; use file_writer; use error::Error; -use config_toml::{PageConfig, PageConfigToml}; +use config_toml::{GlobalConfigToml, PageConfig, PageConfigToml}; use toml; use pathdiff; @@ -34,6 +34,14 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> { 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), @@ -61,6 +69,11 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> { } } + 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(), @@ -96,6 +109,34 @@ pub fn generate_new_page(ops: NewOps, logger: &Logger) -> Result<(), Error> { file_writer::write_file(markdown_path, PLACEHOLDER_MARKDOWN, ops.overwrite)?; + 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 toml::ser::to_string(&config) { + Ok(text) => file_writer::write_file(PathBuf::from("config.toml"), text, ops.overwrite)?, + Err(err) => { + return Err(Error::new( + LogLevel::SEVERE, + format!( + "Failed to serialize config.toml: {}", + err.description().to_owned() + ), + )) + } + } + Ok(()) }