From 49d9a57bd15565116a51380d3552201b4a2de57b Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sun, 12 Oct 2025 14:05:47 +0200 Subject: pop trailing delimiter if last field is empty --- src/bibiman/citekeys.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/bibiman/citekeys.rs b/src/bibiman/citekeys.rs index 1f16b48..065d57f 100644 --- a/src/bibiman/citekeys.rs +++ b/src/bibiman/citekeys.rs @@ -272,14 +272,27 @@ fn formatting_help() { /// Build the citekey from the patterns defined in the config file fn build_citekey(entry: &Entry, pattern_fields: &[String], case: Option<&CitekeyCase>) -> String { + // mut string the citekey is built from let mut new_citekey = String::new(); + + // count different fields of pattern vec let fields = pattern_fields.len(); + + // loop over pattern fields process them for (idx, pattern) in pattern_fields.iter().enumerate() { - let (field, word_count, char_count, inner_delimiter, trailing_delimiter) = + // parse single values from pattern field + let (field_name, word_count, char_count, inner_delimiter, trailing_delimiter) = split_formatting_pat(pattern); + + // built the part of the citekey from the current pattern field let formatted_field_str = { let mut formatted_str = String::new(); - let field = preformat_field(field, entry); + + // preformat the field depending on biblatex value + let field = preformat_field(field_name, entry); + + // split at whitespaces, count fields and set counter for processed + // splits let mut split_field = field.split_whitespace(); let mut words_passed = 0; let field_count = field.split_whitespace().count(); @@ -290,10 +303,19 @@ fn build_citekey(entry: &Entry, pattern_fields: &[String], case: Option<&Citekey } else { field_count }; + + // loop over single parts of current field and add correct delimiter loop { + // terminate loop for current field if its empty. If its also the + // last of the pattern vec, pop the trailing delimiter if field.is_empty() { + if idx + 1 == fields { + let _ = new_citekey.pop(); + } break; } + + // process the single slices and add correct delimiter if let Some(field_slice) = split_field.next() { formatted_str = formatted_str + &format_word(field_slice, char_count); words_passed += 1; -- cgit v1.2.3