diff options
| author | lukeflo | 2025-10-08 22:30:46 +0200 |
|---|---|---|
| committer | lukeflo | 2025-10-08 22:30:46 +0200 |
| commit | 952dc94b412ffcff26a59c37f3112079c78058ff (patch) | |
| tree | 7ac4ee0c4c9df8c67b0e619c915913df8969890f | |
| parent | 8b858f92da69cfb8fa43ec861cda46eeb6ef4bbe (diff) | |
| download | bibiman-952dc94b412ffcff26a59c37f3112079c78058ff.tar.gz bibiman-952dc94b412ffcff26a59c37f3112079c78058ff.zip | |
use vector for old new key pairs
| -rw-r--r-- | src/bibiman/citekeys.rs | 64 |
1 files changed, 59 insertions, 5 deletions
diff --git a/src/bibiman/citekeys.rs b/src/bibiman/citekeys.rs index 118ae3e..b389da2 100644 --- a/src/bibiman/citekeys.rs +++ b/src/bibiman/citekeys.rs @@ -15,6 +15,12 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// +use std::{ + fs::File, + io::Write, + path::{Path, PathBuf}, +}; + use biblatex::{Bibliography, ChunksExt, Entry, Type}; use color_eyre::eyre::eyre; use owo_colors::OwoColorize; @@ -32,9 +38,11 @@ pub enum CitekeyCase { #[derive(Debug, Default, Clone)] pub(crate) struct CitekeyFormatting { + bibfile_path: PathBuf, bib_entries: Bibliography, fields: Vec<String>, case: Option<CitekeyCase>, + old_new_keys_map: Vec<(String, String)>, } impl CitekeyFormatting { @@ -43,7 +51,11 @@ impl CitekeyFormatting { /// multi-file setups. /// The `Bibliography` inserted will be edited in place with the new citekeys. /// Thus, in the end the `bib_entries` field will hold the updated `Bibliography` - pub fn new(cfg: &BibiConfig, bib_entries: Bibliography) -> color_eyre::Result<Self> { + pub fn new<P: AsRef<Path>>( + cfg: &BibiConfig, + path: P, + bib_entries: Bibliography, + ) -> color_eyre::Result<Self> { let fields = cfg .citekey_formatter .fields @@ -56,22 +68,39 @@ impl CitekeyFormatting { )); } Ok(Self { + bibfile_path: path.as_ref().to_path_buf(), bib_entries, fields, case: cfg.citekey_formatter.case.clone(), + old_new_keys_map: Vec::new(), }) } /// Process the actual formatting. The citekey of every entry will be updated. pub fn do_formatting(&mut self) { + let mut old_new_keys: Vec<(String, String)> = Vec::new(); for entry in self.bib_entries.iter_mut() { - entry.key = build_citekey(entry, &self.fields); + old_new_keys.push(( + entry.key.clone(), + build_citekey(entry, &self.fields, self.case.as_ref()), + )); } + + self.old_new_keys_map = old_new_keys; + } + + /// Write entries with updated citekeys to bibfile + pub fn update_file(&self) -> color_eyre::Result<()> { + let mut file = File::open(&self.bibfile_path)?; + + file.write_all(self.bib_entries.to_biblatex_string().as_bytes())?; + + Ok(()) } } /// Build the citekey from the patterns defined in the config file -fn build_citekey(entry: &Entry, pattern_fields: &[String]) -> String { +fn build_citekey(entry: &Entry, pattern_fields: &[String], case: Option<&CitekeyCase>) -> String { let mut new_citekey = String::new(); for pattern in pattern_fields.iter() { let (field, word_count, char_count, inner_delimiter, trailing_delimiter) = @@ -105,7 +134,14 @@ fn build_citekey(entry: &Entry, pattern_fields: &[String]) -> String { }; new_citekey = new_citekey + &formatted_field_str; } - new_citekey + if let Some(case_format) = case { + match case_format { + CitekeyCase::Lower => new_citekey.to_lowercase(), + CitekeyCase::Upper => new_citekey.to_uppercase(), + } + } else { + new_citekey + } } /// Preformat some fields which are very common to be used in citekeys @@ -203,6 +239,8 @@ fn split_formatting_pat( #[cfg(test)] mod tests { + use std::path::PathBuf; + use biblatex::Bibliography; use itertools::Itertools; @@ -244,6 +282,7 @@ mod tests { "; let bibliography = Bibliography::parse(src).unwrap(); let mut formatting_struct = CitekeyFormatting { + bibfile_path: PathBuf::new(), bib_entries: bibliography, fields: vec![ "entrytype;;;;:".into(), @@ -253,6 +292,7 @@ mod tests { "year".into(), ], case: None, + old_new_keys_map: Vec::new(), }; formatting_struct.do_formatting(); let keys = formatting_struct.bib_entries.keys().collect_vec(); @@ -260,9 +300,23 @@ mod tests { keys[0], "book:Bhambra-Holmwood_Col_and_Mod_Soc_Camb:and:Medf_2021" ); + formatting_struct.case = Some(CitekeyCase::Lower); + formatting_struct.do_formatting(); + let keys = formatting_struct.bib_entries.keys().collect_vec(); assert_eq!( - keys[0].to_lowercase(), + keys[0], "book:bhambra-holmwood_col_and_mod_soc_camb:and:medf_2021" ); + // let bib_string = formatting_struct.bib_entries.to_biblatex_string(); + // let new_entry = r" + // @book{book:Bhambra-Holmwood_Col_and_Mod_Soc_Camb:and:Medf_2021, + // title = {Colonialism and \textbf{Modern Social Theory}}, + // author = {Bhambra, Gurminder K. and Holmwood, John}, + // location = {Cambridge and Medford}, + // publisher = {Polity Press}, + // date = {2021}, + // } + // "; + // assert_eq!(new_entry, bib_string); } } |
