diff options
Diffstat (limited to 'src/new/chapter.rs')
-rw-r--r-- | src/new/chapter.rs | 143 |
1 files changed, 92 insertions, 51 deletions
diff --git a/src/new/chapter.rs b/src/new/chapter.rs index 88f2a85..749202f 100644 --- a/src/new/chapter.rs +++ b/src/new/chapter.rs @@ -1,67 +1,108 @@ -use std::{ - fs::{self, File}, - io::{self, Write}, -}; +use std::{fs, path::Path}; use convert_case::{Case, Casing}; -use crate::data::Data; +use crate::{ + config_file::Config, + file_tree::{FileTree, GeneratedFile}, +}; -use super::{get_project_root, CHAPTER}; +pub fn generate_new_chapter( + config: Config, + project_root: &Path, + name: String, +) -> anyhow::Result<FileTree> { + let mut file_tree = FileTree::new(); + file_tree.add_file(new_main_file(project_root, &config, &name)?); + file_tree.add_file(new_chapter_file(&config, &name, project_root)); + file_tree.add_file(new_lpm_toml_file(config, name, project_root)); -pub fn generate_new_chapter(name: String) -> io::Result<()> { - let project_root = get_project_root().unwrap(); + Ok(file_tree) +} - let raw_data_file = fs::read_to_string(project_root.join("lpm.toml")).unwrap(); - let mut data_file: Data = toml::from_str(&raw_data_file).unwrap(); - let mut main_file = fs::read_to_string(project_root.join("main.tex")).unwrap(); +fn new_lpm_toml_file(mut config: Config, name: String, project_root: &Path) -> GeneratedFile { + config.last_chapter.user_name = name.to_case(Case::Snake); + config.last_chapter.number += 1; - fs::create_dir(project_root.join(format!("content/{}", name.to_case(Case::Snake),))).unwrap(); - let mut new_chapter = File::create(project_root.join(format!( - "content/{}/chapter_{:02}.tex", - name.to_case(Case::Snake), - data_file.last_chapter.number + 1 - ))) - .unwrap(); - new_chapter - .write_all(CHAPTER.replace("REPLACEMENT_CHAPTER", &name).as_bytes()) - .unwrap(); + GeneratedFile::new( + project_root.join("lpm.toml"), + toml::to_string(&config).expect("We changed it ourselfes, the conversion should work"), + ) +} - fs::create_dir(project_root.join(format!("content/{}/sections", name.to_case(Case::Snake),))) - .unwrap(); +fn new_chapter_file(config: &Config, name: &str, project_root: &Path) -> GeneratedFile { + let chapter_text = config + .templates + .chapter + .replace("REPLACEMENT_CHAPTER", &name); - main_file = main_file.replace( - &format!( - "\\includeonly{{content/{}/{}}}", - &data_file.last_chapter.user_name, - &format!("chapter_{:02}", data_file.last_chapter.number) - ), - &format!( - "\\includeonly{{content/{}/{}}}", - name.to_case(Case::Snake), - &format!("chapter_{:02}", data_file.last_chapter.number + 1) - ), - ); - let find_index = main_file.find("% NEXT_CHAPTER").unwrap(); - main_file.insert_str( + GeneratedFile::new( + project_root + .join("content") + .join(format! {"{:02}_{}", config.last_chapter.number + 1, name.to_case(Case::Snake)}) + .join("chapter.tex"), + chapter_text, + ) +} + +fn new_main_file( + project_root: &Path, + config: &Config, + name: &str, +) -> anyhow::Result<GeneratedFile> { + let mut main_text = fs::read_to_string(project_root.join("main.tex"))?; + + if &config.last_chapter.user_name == "static" && config.last_chapter.number == 0 { + // This is the first added chapter; The `\includeonly` will be empty. + main_text = main_text.replace( + "\\includeonly{}", + &format!( + "\\includeonly{{content/{}/{}}}", + &format!( + "{:02}_{}", + config.last_chapter.number + 1, + &name.to_case(Case::Snake) + ), + "chapter.tex", + ), + ) + } else { + main_text = main_text.replace( + &format!( + "\\includeonly{{content/{}/{}}}", + &format!( + "{:02}_{}", + config.last_chapter.number, &config.last_chapter.user_name + ), + "chapter.tex", + ), + &format!( + "\\includeonly{{content/{}/{}}}", + &format!( + "{:02}_{}", + config.last_chapter.number + 1, + &name.to_case(Case::Snake) + ), + "chapter.tex", + ), + ) + }; + + let find_index = main_text + .find("% NEXT_CHAPTER") + .expect("The % NEXT_CHAPTER maker must exist"); + main_text.insert_str( find_index, &format!( "\\include{{content/{}/{}}}\n ", - name.to_case(Case::Snake), - &format!("chapter_{:02}", data_file.last_chapter.number + 1) + &format!( + "{:02}_{}", + config.last_chapter.number + 1, + &name.to_case(Case::Snake) + ), + "chapter.tex", ), ); - data_file.last_chapter.user_name = name.to_case(Case::Snake); - data_file.last_chapter.number += 1; - - fs::write( - project_root.join("lpm.toml"), - toml::to_string(&data_file).expect("We changed it ourselfes, the conversion should work"), - ) - .unwrap(); - - fs::write(project_root.join("main.tex"), main_file).unwrap(); - - Ok(()) + Ok(GeneratedFile::new(project_root.join("main.tex"), main_text)) } |