diff options
| author | lukeflo | 2025-10-10 14:57:53 +0200 |
|---|---|---|
| committer | lukeflo | 2025-10-10 14:57:53 +0200 |
| commit | c69b1789fabaf149916d160922d7026f2cbe33f1 (patch) | |
| tree | da75e91e446d02f96946fb59cfb70b549180c778 | |
| parent | 4779dbc5fe3712bce31bbb5f1f43c28c4c839420 (diff) | |
| download | bibiman-c69b1789fabaf149916d160922d7026f2cbe33f1.tar.gz bibiman-c69b1789fabaf149916d160922d7026f2cbe33f1.zip | |
implement const of ignored special chars for citekey formatting
* the list contains 33 special chars at the moment
* it will only affect already existing special chars in biblatex fields
* delimiter specified for citekey formatting are not affected
* char count is also not affected, ignored chars are not counted
| -rw-r--r-- | src/bibiman/citekeys.rs | 40 | ||||
| -rw-r--r-- | src/config.rs | 5 | ||||
| -rw-r--r-- | tests/test-config.toml | 2 |
3 files changed, 27 insertions, 20 deletions
diff --git a/src/bibiman/citekeys.rs b/src/bibiman/citekeys.rs index 5121741..7c06886 100644 --- a/src/bibiman/citekeys.rs +++ b/src/bibiman/citekeys.rs @@ -31,7 +31,10 @@ use owo_colors::{ }; use serde::{Deserialize, Serialize}; -use crate::{bibiman::sanitize::sanitize_single_string_fully, config::BibiConfig}; +use crate::{ + bibiman::sanitize::sanitize_single_string_fully, + config::{BibiConfig, IGNORED_SPECIAL_CHARS}, +}; #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum CitekeyCase { @@ -354,26 +357,25 @@ fn preformat_field(field: &str, entry: &Entry) -> String { /// Cut of word at char count index if its set fn format_word(word: &str, count: Option<usize>) -> String { - if let Some(len) = count - && len < word.chars().count() - { - // Since chars can consist of multiple bytes, we need this more complex - // loop to collect a specified number of chars (e.g. ÄÖÜäöü¢æø etc...) - // instead of simple byte indexing - let mut word_slice = String::new(); - let word_chars = word.chars(); - let mut counter = 0; - for c in word_chars { - if counter == len { - break; - } - word_slice.push(c); - counter += 1; + // Since chars can consist of multiple bytes, we need this more complex + // loop to collect a specified number of chars (e.g. ÄÖÜäöü¢æø etc...) + // instead of simple byte indexing + let mut word_slice = String::new(); + let word_chars = word.chars(); + let mut counter = 0; + for c in word_chars { + if let Some(len) = count + && counter == len + { + break; } - word_slice - } else { - word.to_string() + if IGNORED_SPECIAL_CHARS.contains(&c) { + continue; + } + word_slice.push(c); + counter += 1; } + word_slice } /// Split a formatting pattern of kind diff --git a/src/config.rs b/src/config.rs index a5df61c..a4e89be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,6 +35,11 @@ use crate::{ cliargs::CLIArgs, }; +pub const IGNORED_SPECIAL_CHARS: [char; 33] = [ + '?', '!', '\\', '\'', '.', '-', '–', ':', ',', '[', ']', '(', ')', '{', '}', '§', '$', '%', + '&', '/', '`', '´', '#', '+', '*', '=', '|', '<', '>', '^', '°', '_', '"', +]; + const DEFAULT_CONFIG: &str = r##" # [general] ## Default files/dirs which are loaded on startup diff --git a/tests/test-config.toml b/tests/test-config.toml index b484b69..558d216 100644 --- a/tests/test-config.toml +++ b/tests/test-config.toml @@ -61,5 +61,5 @@ custom_column = "series" # year_color = "135" [citekey_formatter] -fields = [ "author;2;;-;_", "title;3;3;_;_", "year" ] +fields = [ "author;2;;-;_", "title;3;6;_;_", "year" ] case = "lowercase" |
