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/new/chapter.rs | 2 +- src/new/figure.rs | 28 ++++++++++++++++++++++++++++ src/new/mod.rs | 4 +++- src/new/replacement.rs | 9 +++++++-- 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/new/figure.rs (limited to 'src/new') 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