diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-09-29 10:11:35 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-09-29 10:11:35 +0200 |
commit | d0fe56f4e98fa552c5e271713a815d2382e614f7 (patch) | |
tree | 5564085c201fdac129e96fa6036c3da0b1c11a0b /src/new/replacement.rs | |
parent | feat(bundle): Support bundling a document into one TeX file (diff) | |
download | lpm-d0fe56f4e98fa552c5e271713a815d2382e614f7.tar.gz lpm-d0fe56f4e98fa552c5e271713a815d2382e614f7.zip |
feat(templates): Provide a consistent syntax for replacements
All replacements now start with `lpm::` and thus provide a future way to extend them. This change also adds the ability to access the chapter name in a new section and the current data in a new chapter.
Diffstat (limited to 'src/new/replacement.rs')
-rw-r--r-- | src/new/replacement.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/new/replacement.rs b/src/new/replacement.rs new file mode 100644 index 0000000..6878e61 --- /dev/null +++ b/src/new/replacement.rs @@ -0,0 +1,47 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +use chrono::{Local, TimeDelta, TimeZone}; +use log::debug; + +use crate::constants::{ + DATE, REPLACEMENT_CHAPTER, REPLACEMENT_CHAPTER_SECTION, REPLACMENT_SECTION_TITLE, +}; + +fn get_current_date() -> String { + let start = SystemTime::now(); + let seconds_since_epoch: TimeDelta = TimeDelta::from_std( + start + .duration_since(UNIX_EPOCH) + .expect("Time went backwards"), + ) + .expect("Time does not go backwards"); + + debug!( + "Adding a date with timestamp: {}", + seconds_since_epoch.num_seconds() + ); + + let our_date = format!( + "{}", + Local + .timestamp_opt(seconds_since_epoch.num_seconds(), 0) + // only has unwrap, no expect. But should always work + .unwrap() + .format("%Y-%m-%d %H:%M:%S%.f %:z") + ); + + our_date +} + +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(DATE, &get_current_date()) +} + +pub fn untemplatize_chapter(input: &str, new_chapter_name: &str) -> String { + input + .replace(REPLACEMENT_CHAPTER, &new_chapter_name) + .replace(DATE, &get_current_date()) +} |