aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorlukeflo2025-10-15 08:50:54 +0200
committerlukeflo2025-10-15 08:50:54 +0200
commit7d59b6c816fa81c13cdf8348a2e7db0076015cd1 (patch)
tree06e863cc52411cf041ca0d118c1334c7d3e3bac2 /src/config.rs
parent34170cfa62df5443a0d8675106c553efec035687 (diff)
parentdb882623358d9141927bd31f6825472f2cdca4b6 (diff)
downloadbibiman-7d59b6c816fa81c13cdf8348a2e7db0076015cd1.tar.gz
bibiman-7d59b6c816fa81c13cdf8348a2e7db0076015cd1.zip
Merge pull request 'implement basic citekey formatting' (#58) from format-citekeys-#55 into main
Reviewed-on: https://codeberg.org/lukeflo/bibiman/pulls/58
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs130
1 files changed, 125 insertions, 5 deletions
diff --git a/src/config.rs b/src/config.rs
index 00a35b7..47e145c 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -16,21 +16,65 @@
/////
use std::{
- fs::{create_dir_all, File},
- io::{stdin, Write},
+ fs::{File, create_dir_all},
+ io::{Write, stdin},
path::PathBuf,
str::FromStr,
+ sync::LazyLock,
};
use color_eyre::{eyre::Result, owo_colors::OwoColorize};
use figment::{
- providers::{Format, Serialized, Toml},
Figment,
+ providers::{Format, Serialized, Toml},
};
use ratatui::style::Color;
use serde::{Deserialize, Serialize};
-use crate::{bibiman::bibisetup::CustomField, cliargs::CLIArgs};
+use crate::{
+ bibiman::{bibisetup::CustomField, citekeys::CitekeyCase},
+ cliargs::CLIArgs,
+};
+
+pub const IGNORED_SPECIAL_CHARS: [char; 33] = [
+ '?', '!', '\\', '\'', '.', '-', '–', ':', ',', '[', ']', '(', ')', '{', '}', '§', '$', '%',
+ '&', '/', '`', '´', '#', '+', '*', '=', '|', '<', '>', '^', '°', '_', '"',
+];
+
+pub static IGNORED_WORDS: LazyLock<Vec<String>> = LazyLock::new(|| {
+ vec![
+ String::from("the"),
+ String::from("a"),
+ String::from("an"),
+ String::from("of"),
+ String::from("for"),
+ String::from("in"),
+ String::from("at"),
+ String::from("to"),
+ String::from("and"),
+ String::from("him"),
+ String::from("her"),
+ String::from("his"),
+ String::from("hers"),
+ String::from("der"),
+ String::from("die"),
+ String::from("das"),
+ String::from("ein"),
+ String::from("eine"),
+ String::from("eines"),
+ String::from("des"),
+ String::from("auf"),
+ String::from("und"),
+ String::from("für"),
+ String::from("vor"),
+ String::from("er"),
+ String::from("sie"),
+ String::from("es"),
+ String::from("ihm"),
+ String::from("ihr"),
+ String::from("ihnen"),
+ ]
+});
const DEFAULT_CONFIG: &str = r##"
# [general]
@@ -95,6 +139,55 @@ const DEFAULT_CONFIG: &str = r##"
# author_color = "38"
# title_color = "37"
# year_color = "135"
+
+# [citekey_formatter]
+## Define the patterns for creating citekeys. Every item of the array consists of
+## five components separated by semicolons. Despite the field name every component
+## can be left blank:
+## - name of the biblatex field ("author", "title"...)
+## - number of max words from the given field
+## - number of chars used from each word
+## - delimiter to separate words of the same field
+## - trailing delimiter separating the current field from the following
+# fields = [ "author;2;;-;_", "title;3;6;_;_", "year" ]
+
+## Convert chars to specified case. Possible values:
+## "upper", "uppercase", "lower", "lowercase"
+# case = "lowercase"
+
+## Map all unicode chars to their pure ascii equivalent
+# ascii_only = true
+
+## List of special chars that'll be ignored when building citekeys.
+## A custom list will overwrite the default list
+# ignored_chars = [
+# "?", "!", "\\", "\'", ".", "-", "–", ":", ",", "[", "]", "(", ")", "{", "}", "§", "$", "%", "&", "/", "`", "´", "#", "+", "*", "=", "|", "<", ">", "^", "°", "_", """,
+# ]
+
+## List of words that'll be ignored when building citekeys.
+## A custom list will overwrite the default list
+# ignored_words = [
+# "the",
+# "a",
+# "an",
+# "of",
+# "for",
+# "in",
+# "at",
+# "to",
+# "and",
+# "der",
+# "die",
+# "das",
+# "ein",
+# "eine",
+# "eines",
+# "des",
+# "auf",
+# "und",
+# "für",
+# "vor",
+# ]
"##;
/// Main struct of the config file. Contains substructs/headings in toml
@@ -102,6 +195,7 @@ const DEFAULT_CONFIG: &str = r##"
pub struct BibiConfig {
pub general: General,
pub colors: Colors,
+ pub citekey_formatter: CitekeyFormatter,
}
/// Substruct [general] in config.toml
@@ -143,6 +237,15 @@ pub struct Colors {
pub year_color: Color,
}
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
+pub struct CitekeyFormatter {
+ pub fields: Option<Vec<String>>,
+ pub case: Option<CitekeyCase>,
+ pub ascii_only: bool,
+ pub ignored_chars: Option<Vec<char>>,
+ pub ignored_words: Option<Vec<String>>,
+}
+
impl Default for BibiConfig {
fn default() -> Self {
Self {
@@ -161,6 +264,13 @@ impl Default for BibiConfig {
custom_column: CustomField::Pubtype,
},
colors: Self::dark_colors(),
+ citekey_formatter: CitekeyFormatter {
+ fields: None,
+ case: None,
+ ascii_only: true,
+ ignored_chars: None,
+ ignored_words: None,
+ },
}
}
}
@@ -187,6 +297,13 @@ impl BibiConfig {
} else {
Self::dark_colors()
},
+ citekey_formatter: CitekeyFormatter {
+ fields: None,
+ case: None,
+ ascii_only: true,
+ ignored_chars: None,
+ ignored_words: None,
+ },
}
}
@@ -344,8 +461,8 @@ fn select_opener() -> String {
#[cfg(test)]
mod tests {
use figment::{
- providers::{Format, Toml},
Figment,
+ providers::{Format, Toml},
};
use super::BibiConfig;
@@ -382,6 +499,9 @@ mod tests {
author_color = "38"
title_color = "37"
year_color = "135"
+
+ [citekey_formatter]
+ ascii_only = true
"#,
)?;