aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlukeflo2025-06-29 12:26:27 +0200
committerlukeflo2025-06-29 12:26:27 +0200
commitfdfbf550144ef69ecc6003760fbb39466ec08637 (patch)
treec13a111fdfd18b1f078adb867ae69d1ab0de2ea4 /src
parent2e4ae7a05a73cb1df63343e14fbb8a5fd3c7bf41 (diff)
downloadbibiman-fdfbf550144ef69ecc6003760fbb39466ec08637.tar.gz
bibiman-fdfbf550144ef69ecc6003760fbb39466ec08637.zip
Some basic function rewriting for matching different file types
Diffstat (limited to 'src')
-rw-r--r--src/bibiman/bibisetup.rs137
-rw-r--r--src/config.rs12
2 files changed, 91 insertions, 58 deletions
diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs
index bf5baf5..8d8de53 100644
--- a/src/bibiman/bibisetup.rs
+++ b/src/bibiman/bibisetup.rs
@@ -55,6 +55,7 @@ pub struct BibiData {
pub filepath: Option<Vec<OsString>>,
pub file_field: bool,
pub subtitle: Option<String>,
+ pub notes: Option<Vec<OsString>>,
}
impl BibiData {
@@ -200,7 +201,7 @@ impl BibiSetup {
cfg: &BibiConfig,
) -> Vec<BibiData> {
let mut pdf_files = if cfg.general.pdf_path.is_some() {
- collect_pdf_file_paths(cfg.general.pdf_path.as_ref().unwrap())
+ collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), Some(vec!["pdf"]))
} else {
None
};
@@ -225,6 +226,7 @@ impl BibiSetup {
filepath: filepaths.0,
file_field: filepaths.1,
subtitle: Self::get_subtitle(k, bibliography),
+ notes: None,
}
})
.collect()
@@ -373,12 +375,21 @@ impl BibiSetup {
true,
)
} else if pdf_files.is_some() {
- (Self::merge_filepath_or_none(&citekey, pdf_files), false)
+ (
+ Self::merge_filepath_or_none_two(&citekey, pdf_files, vec!["pdf"]),
+ false,
+ )
} else {
(None, false)
}
}
+ // pub fn get_notepath(
+ // citekey: &str,
+ // note_files: &mut Option<HashMap<String, Vec<PathBuf>>>,
+ // ) -> Option<Vec<OsString>> {
+ // }
+
pub fn get_subtitle(citekey: &str, biblio: &Bibliography) -> Option<String> {
if biblio.get(citekey).unwrap().subtitle().is_ok() {
Some(
@@ -399,53 +410,7 @@ impl BibiSetup {
pdf_files: &mut Option<HashMap<String, Vec<PathBuf>>>,
) -> Option<Vec<OsString>> {
let pdf_file = {
- // let mut idx = 0;
let citekey = citekey.to_owned().to_ascii_lowercase() + ".pdf";
- // let filename = citekey.to_owned() + ".pdf";
- // for f in args.pdf_files.unwrap().iter() {
- // if f.file_name().unwrap().to_str().unwrap() == &filename {
- // break f;
- // }
- // }
-
- // loop {
- // if idx + 1 > pdf_files.as_ref().unwrap().len() {
- // break None;
- // }
- // let cur_entry = pdf_files.as_ref().unwrap()[idx].clone();
- // if cur_entry.is_file()
- // && cur_entry
- // .file_name()
- // .unwrap()
- // .to_ascii_lowercase()
- // .to_str()
- // .unwrap()
- // == citekey
- // {
- // let path = cur_entry.to_owned().into_os_string();
- // pdf_files.as_mut().unwrap().swap_remove(idx);
- // break Some(path);
- // } else {
- // idx += 1
- // }
- // }
-
- // for file in pdf_files.as_ref().unwrap().iter() {
- // let filename = file.file_name().unwrap().to_ascii_lowercase();
- // if filename.to_str().unwrap() == citekey {
- // break;
- // } else if pdf_files.as_ref().unwrap().len() > idx {
- // break;
- // } else {
- // idx += 1;
- // }
- // }
-
- // if pdf_files.as_ref().unwrap()[idx].is_file() {
- // Some(pdf_files.as_ref().unwrap()[idx].to_owned().into_os_string())
- // } else {
- // None
- // }
if pdf_files.as_ref().unwrap().contains_key(&citekey) {
let path_vec = pdf_files
@@ -462,31 +427,74 @@ impl BibiSetup {
pdf_file
}
+
+ fn merge_filepath_or_none_two(
+ citekey: &str,
+ files: &mut Option<HashMap<String, Vec<PathBuf>>>,
+ extensions: Vec<&str>,
+ ) -> Option<Vec<OsString>> {
+ let mut file = Vec::new();
+
+ for e in extensions.iter() {
+ let basename = citekey.to_owned().to_ascii_lowercase() + "." + e;
+ if files.as_ref().unwrap().contains_key(&basename) {
+ let _ = files
+ .as_ref()
+ .unwrap()
+ .get(&basename)
+ .unwrap()
+ .to_owned()
+ .into_iter()
+ .for_each(|p| file.push(p.into_os_string()));
+ }
+ }
+
+ if file.is_empty() {
+ None
+ } else {
+ Some(file)
+ }
+ }
}
-/// This function walks the given dir and collects all pdf files into a `HashMap`
-/// of the format `[String, Vec<PathBuf>]`, where `String` represents the basename
-/// of the file and the `Vec<PathBuf>` holds all filepaths ending with this basename.
+/// This function walks the given dir and collects all files matching one of the
+/// passed extensions into a `HashMap` of the format `[String, Vec<PathBuf>]`,
+/// where `String` represents the basename of the file and the `Vec<PathBuf>` holds
+/// all filepaths ending with this basename.
///
/// In most cases the latter is only a single path, but there might be some concepts
/// with subdirs were some entries have multiple files associated with them.
-pub fn collect_pdf_file_paths(pdf_dir: &PathBuf) -> Option<HashMap<String, Vec<PathBuf>>> {
+///
+/// Passing [`None`] as argument for extensions will result in collecting all files
+/// from the given directory and its subdirectories!
+pub fn collect_file_paths(
+ file_dir: &PathBuf,
+ extensions: Option<Vec<&str>>,
+) -> Option<HashMap<String, Vec<PathBuf>>> {
let mut files: HashMap<String, Vec<PathBuf>> = HashMap::new();
// Expand tilde to /home/user
- let pdf_dir = if pdf_dir.starts_with("~") {
- &app::expand_home(&pdf_dir)
+ let file_dir = if file_dir.starts_with("~") {
+ &app::expand_home(&file_dir)
} else {
- pdf_dir
+ file_dir
};
// Walk the passed dir and collect all pdf files into hashmap
- if pdf_dir.is_dir() {
- for file in WalkDir::new(pdf_dir) {
+ if file_dir.is_dir() {
+ for file in WalkDir::new(file_dir) {
let f = file.unwrap().into_path();
if f.is_file()
&& f.extension().is_some()
- && f.extension().unwrap_or_default().to_ascii_lowercase() == "pdf"
+ && extensions.as_ref().is_some_and(|v| {
+ v.contains(
+ &f.extension()
+ .unwrap_or_default()
+ .to_ascii_lowercase()
+ .to_str()
+ .unwrap_or_default(),
+ )
+ })
{
let filename = f
.file_name()
@@ -500,6 +508,19 @@ pub fn collect_pdf_file_paths(pdf_dir: &PathBuf) -> Option<HashMap<String, Vec<P
} else {
files.insert(filename, vec![f]);
}
+ } else if f.is_file() && extensions.is_none() {
+ let filename = f
+ .file_name()
+ .unwrap()
+ .to_ascii_lowercase()
+ .into_string()
+ .unwrap();
+
+ if let Some(paths) = files.get_mut(&filename) {
+ paths.push(f);
+ } else {
+ files.insert(filename, vec![f]);
+ }
}
}
}
diff --git a/src/config.rs b/src/config.rs
index f5f2dd0..6abcfd1 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -56,6 +56,12 @@ const DEFAULT_CONFIG: &str = r##"
## Use absolute paths (~ for HOME works). Otherwise, loading might not work.
# pdf_path = "/path/to/pdf/folder"
+## Path to folder (with subfolders) containing note files with the basename of
+## the format "citekey.extension". Other basenames are not accepted. The possible
+## extensions can be set through the "note_extensions" array.
+# note_path = "/path/to/notes/folder"
+# note_extensions = [ "md", "txt", "org" ]
+
# [colors]
## Default values for dark-themed terminal
## Possible values are:
@@ -90,6 +96,8 @@ pub struct General {
pub url_opener: String,
pub file_prefix: Option<PathBuf>,
pub pdf_path: Option<PathBuf>,
+ pub note_path: Option<PathBuf>,
+ pub note_extensions: Option<Vec<String>>,
}
/// Substruct [colors] in config.toml
@@ -117,6 +125,8 @@ impl Default for BibiConfig {
url_opener: select_opener(),
file_prefix: None,
pdf_path: None,
+ note_path: None,
+ note_extensions: None,
},
colors: Self::dark_colors(),
}
@@ -133,6 +143,8 @@ impl BibiConfig {
url_opener: select_opener(),
file_prefix: None,
pdf_path: None,
+ note_path: None,
+ note_extensions: None,
},
colors: if args.light_theme {
Self::light_colors()