From 800ffaf658c3defe165dd24c3ecd65ceabc7096a Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Sun, 29 Sep 2024 11:48:59 +0200 Subject: feat(new/figure)!: Init BREAKING CHANGE: The config file now needs to contain an `templates.figure` key. You can simply add this. --- src/cli.rs | 11 +++++++++-- src/config_file.rs | 1 + src/constants.rs | 12 ++++++++---- src/main.rs | 17 ++++++++--------- src/new/chapter.rs | 2 +- src/new/figure.rs | 28 ++++++++++++++++++++++++++++ src/new/mod.rs | 4 +++- src/new/replacement.rs | 9 +++++++-- 8 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 src/new/figure.rs (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index b3ee30c..12419d6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -39,13 +39,20 @@ pub enum What { /// chapter already. #[arg(long, short)] chapter: Option, - /// Name of the new Section + + /// Name of the new section name: String, }, /// Adds a chapter Chapter { - /// Name of the new Chapter + /// Name of the new chapter + name: String, + }, + + /// Adds a figure + Figure { + /// Name of the new figure name: String, }, } diff --git a/src/config_file.rs b/src/config_file.rs index f9ea659..8894721 100644 --- a/src/config_file.rs +++ b/src/config_file.rs @@ -10,4 +10,5 @@ pub struct Config { pub struct Template { pub section: String, pub chapter: String, + pub figure: String, } diff --git a/src/constants.rs b/src/constants.rs index 6536f63..7c23a09 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,8 +1,12 @@ pub const REPLACEMENT_CHAPTER: &str = "lpm::new_chapter_name"; +pub const REPLACEMENT_FIGURE: &str = "lpm::new_figure_name"; +pub const REPLACMENT_SECTION: &str = "lpm::new_section_name"; -pub const REPLACEMENT_CHAPTER_SECTION: &str = "lpm::current_chapter_name::title_case"; -pub const REPLACMENT_SECTION_TITLE: &str = "lpm::new_section_name"; pub const DATE: &str = "lpm::current_date"; -pub const NEXT_CHAPTER: &str = "% lpm::next_chapter_marker"; -pub const NEXT_CHAPTER_INCLUDE_ONLY: &str = "% lpm::next_chapter_includeonly_marker"; +pub const REPLACEMENT_CHAPTER_SECTION: &str = "lpm::current_chapter_name::title_case"; + +pub mod marker { + pub const NEXT_CHAPTER: &str = "% lpm::next_chapter_marker"; + pub const NEXT_CHAPTER_INCLUDE_ONLY: &str = "% lpm::next_chapter_includeonly_marker"; +} diff --git a/src/main.rs b/src/main.rs index ce886bf..a8c5a4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,12 @@ use std::{env, ffi::OsString, fs, path::PathBuf}; use anyhow::{bail, Context}; use clap::Parser; -use cli::Command; +use cli::{Command, What}; use log::debug; +use new::figure::generate_new_figure; use crate::{ - cli::{ - Args, - What::{Chapter, Section}, - }, + cli::Args, config_file::Config, new::{chapter::generate_new_chapter, section::generate_new_section}, }; @@ -17,8 +15,8 @@ use crate::{ pub mod bundle; pub mod cli; pub mod config_file; -pub mod new; pub mod constants; +pub mod new; // The copyright header tells you, where this file is from. pub mod file_tree; @@ -48,9 +46,9 @@ fn main() -> anyhow::Result<()> { None } Command::New(new_command) => match new_command { - Section { name, chapter } => { + What::Section { name, chapter } => { let chapter = if let Some(val) = chapter { - // The user probably has not added the preceeding chapter number to the chapter + // The user probably has not added the preceding chapter number to the chapter // string if val.starts_with(|c: char| c.is_numeric()) { eprintln!( @@ -76,7 +74,8 @@ fn main() -> anyhow::Result<()> { &chapter, )?) } - Chapter { name } => Some(generate_new_chapter(config, &project_root, name)?), + What::Chapter { name } => Some(generate_new_chapter(config, &project_root, name)?), + What::Figure { name } => Some(generate_new_figure(&config, name, &project_root)?), }, }; diff --git a/src/new/chapter.rs b/src/new/chapter.rs index 9fb5301..429816f 100644 --- a/src/new/chapter.rs +++ b/src/new/chapter.rs @@ -5,7 +5,7 @@ use log::error; use crate::{ config_file::Config, - constants::{NEXT_CHAPTER, NEXT_CHAPTER_INCLUDE_ONLY}, + constants::marker::{NEXT_CHAPTER, NEXT_CHAPTER_INCLUDE_ONLY}, file_tree::{FileTree, GeneratedFile}, }; diff --git a/src/new/figure.rs b/src/new/figure.rs new file mode 100644 index 0000000..3099f94 --- /dev/null +++ b/src/new/figure.rs @@ -0,0 +1,28 @@ +use std::path::Path; + +use crate::{ + config_file::Config, + file_tree::{FileTree, GeneratedFile}, +}; + +use super::{replacement::untemplatize_figure, MangledName}; + +pub fn generate_new_figure( + config: &Config, + name: String, + project_root: &Path, +) -> anyhow::Result { + let mut file_tree = FileTree::new(); + + let new_figure_text = untemplatize_figure(&config.templates.figure, &name); + + file_tree.add_file(GeneratedFile::new_clobber( + project_root + .join("figures") + .join(format!("{}.tex", MangledName::new(&name))), + new_figure_text, + false, + )); + + Ok(file_tree) +} diff --git a/src/new/mod.rs b/src/new/mod.rs index fed4ff3..888930e 100644 --- a/src/new/mod.rs +++ b/src/new/mod.rs @@ -3,9 +3,11 @@ use std::fmt::Display; use convert_case::{Case, Casing}; use deunicode::deunicode; -pub mod chapter; pub mod replacement; + +pub mod chapter; pub mod section; +pub mod figure; #[derive(PartialEq, Eq, PartialOrd, Ord)] pub struct MangledName(String); diff --git a/src/new/replacement.rs b/src/new/replacement.rs index 6878e61..0e9f27f 100644 --- a/src/new/replacement.rs +++ b/src/new/replacement.rs @@ -4,7 +4,7 @@ use chrono::{Local, TimeDelta, TimeZone}; use log::debug; use crate::constants::{ - DATE, REPLACEMENT_CHAPTER, REPLACEMENT_CHAPTER_SECTION, REPLACMENT_SECTION_TITLE, + DATE, REPLACEMENT_CHAPTER, REPLACEMENT_CHAPTER_SECTION, REPLACEMENT_FIGURE, REPLACMENT_SECTION, }; fn get_current_date() -> String { @@ -36,7 +36,7 @@ fn get_current_date() -> String { pub fn untemplatize_section(input: &str, new_section_name: &str, new_chapter_name: &str) -> String { input .replace(REPLACEMENT_CHAPTER_SECTION, &new_chapter_name) - .replace(REPLACMENT_SECTION_TITLE, &new_section_name) + .replace(REPLACMENT_SECTION, &new_section_name) .replace(DATE, &get_current_date()) } @@ -45,3 +45,8 @@ pub fn untemplatize_chapter(input: &str, new_chapter_name: &str) -> String { .replace(REPLACEMENT_CHAPTER, &new_chapter_name) .replace(DATE, &get_current_date()) } +pub fn untemplatize_figure(input: &str, new_figure_name: &str) -> String { + input + .replace(REPLACEMENT_FIGURE, &new_figure_name) + .replace(DATE, &get_current_date()) +} -- cgit 1.4.1