aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlukeflo2024-10-19 21:30:37 +0200
committerlukeflo2024-10-19 21:30:37 +0200
commit8beb373c4a587cdcea772f725f51c2ff2db7273d (patch)
tree184e429f9ccc95437d6b7fb74baeaafd9610ca88 /src
parentb5538d8c8408f1afbcfecb2a3b0407c3ad53ebe5 (diff)
downloadbibiman-8beb373c4a587cdcea772f725f51c2ff2db7273d.tar.gz
bibiman-8beb373c4a587cdcea772f725f51c2ff2db7273d.zip
reinvent BibiData, start integrating yaml
Diffstat (limited to 'src')
-rw-r--r--src/backend/bib.rs101
-rw-r--r--src/frontend/app.rs8
-rw-r--r--src/frontend/entries.rs30
3 files changed, 93 insertions, 46 deletions
diff --git a/src/backend/bib.rs b/src/backend/bib.rs
index 1f5d05d..29ce22e 100644
--- a/src/backend/bib.rs
+++ b/src/backend/bib.rs
@@ -17,6 +17,7 @@
use biblatex::{self, Bibliography};
use biblatex::{ChunksExt, Type};
+use hayagriva::io::from_yaml_str;
use itertools::Itertools;
use std::{fs, path::PathBuf};
@@ -36,6 +37,20 @@ pub struct BibiMain {
pub bibliography: Bibliography, // parsed bibliography
pub citekeys: Vec<String>, // list of all citekeys
pub keyword_list: Vec<String>, // list of all available keywords
+ pub entry_list: Vec<BibiData>, // List of all entries
+}
+
+#[derive(Debug, Clone)]
+pub struct BibiData {
+ pub authors: String,
+ pub title: String,
+ pub year: String,
+ pub pubtype: String,
+ pub keywords: String,
+ pub citekey: String,
+ pub abstract_text: String,
+ pub doi_url: String,
+ pub filepath: String,
}
impl BibiMain {
@@ -47,6 +62,7 @@ impl BibiMain {
let bibliography = biblatex::Bibliography::parse(&bibfilestring).unwrap();
let citekeys = Self::get_citekeys(&bibliography);
let keyword_list = Self::collect_tag_list(&citekeys, &bibliography);
+ let entry_list = Self::create_entry_list(&citekeys, &bibliography, &bibfile_format);
Self {
bibfile,
bibfile_format,
@@ -54,6 +70,15 @@ impl BibiMain {
bibliography,
citekeys,
keyword_list,
+ entry_list,
+ }
+ }
+
+ fn parse_bibliography(format: &FileFormat, bibfilestring: &str) -> Bibliography {
+ if let FileFormat::BibLatex = format {
+ biblatex::Bibliography::parse(bibfilestring).unwrap()
+ } else if let FileFormat::Hayagriva = format {
+ from_yaml_str(&bibfilestring).unwrap()
}
}
@@ -70,6 +95,27 @@ impl BibiMain {
}
}
+ fn create_entry_list(
+ citekeys: &[String],
+ bibliography: &Bibliography,
+ format: &FileFormat,
+ ) -> Vec<BibiData> {
+ citekeys
+ .into_iter()
+ .map(|k| BibiData {
+ authors: Self::get_authors(&k, &bibliography, format),
+ title: Self::get_title(&k, &bibliography),
+ year: Self::get_year(&k, &bibliography),
+ pubtype: Self::get_pubtype(&k, &bibliography),
+ keywords: Self::get_keywords(&k, &bibliography),
+ citekey: k.to_owned(),
+ abstract_text: Self::get_abstract(&k, &bibliography),
+ doi_url: Self::get_weblink(&k, &bibliography),
+ filepath: Self::get_filepath(&k, &bibliography),
+ })
+ .collect()
+ }
+
// get list of citekeys from the given bibfile
// this list is the base for further operations on the bibentries
// since it is the entry point of the biblatex crate.
@@ -112,38 +158,41 @@ impl BibiMain {
keyword_list
}
- pub fn get_authors(citekey: &str, biblio: &Bibliography) -> String {
- if biblio.get(&citekey).unwrap().author().is_ok() {
- let authors = biblio.get(&citekey).unwrap().author().unwrap();
- if authors.len() > 1 {
- let all_authors = authors.iter().map(|a| &a.name).join(", ");
- all_authors
- } else if authors.len() == 1 {
- let authors = authors[0].name.to_string();
- authors
- } else {
- let editors_authors = format!("empty");
- editors_authors
- }
- } else {
- if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() {
- let editors = biblio.get(&citekey).unwrap().editors().unwrap();
- if editors[0].0.len() > 1 {
- // let editors = format!("{} (ed.) et al.", editors[0].0[0].name);
- let mut editors = editors[0].0.iter().map(|e| &e.name).join(", ");
- editors.push_str(" (ed.)");
- editors
- } else if editors[0].0.len() == 1 {
- let editors = format!("{} (ed.)", editors[0].0[0].name);
- editors
+ pub fn get_authors(citekey: &str, biblio: &Bibliography, format: &FileFormat) -> String {
+ if let FileFormat::BibLatex = format {
+ if biblio.get(&citekey).unwrap().author().is_ok() {
+ let authors = biblio.get(&citekey).unwrap().author().unwrap();
+ if authors.len() > 1 {
+ let all_authors = authors.iter().map(|a| &a.name).join(", ");
+ all_authors
+ } else if authors.len() == 1 {
+ let authors = authors[0].name.to_string();
+ authors
} else {
let editors_authors = format!("empty");
editors_authors
}
} else {
- let editors_authors = format!("empty");
- editors_authors
+ if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() {
+ let editors = biblio.get(&citekey).unwrap().editors().unwrap();
+ if editors[0].0.len() > 1 {
+ // let editors = format!("{} (ed.) et al.", editors[0].0[0].name);
+ let mut editors = editors[0].0.iter().map(|e| &e.name).join(", ");
+ editors.push_str(" (ed.)");
+ editors
+ } else if editors[0].0.len() == 1 {
+ let editors = format!("{} (ed.)", editors[0].0[0].name);
+ editors
+ } else {
+ let editors_authors = format!("empty");
+ editors_authors
+ }
+ } else {
+ let editors_authors = format!("empty");
+ editors_authors
+ }
}
+ } else if let FileFormat::Hayagriva = format {
}
}
diff --git a/src/frontend/app.rs b/src/frontend/app.rs
index 10cfa9b..822c6f0 100644
--- a/src/frontend/app.rs
+++ b/src/frontend/app.rs
@@ -76,7 +76,7 @@ impl App {
let main_biblio = BibiMain::new(main_bibfile.clone());
let tag_list = TagList::new(main_biblio.keyword_list.clone());
let search_struct = BibiSearch::default();
- let entry_table = EntryTable::new(&main_biblio.citekeys, &main_biblio.bibliography);
+ let entry_table = EntryTable::new(main_biblio.entry_list.clone());
let current_area = CurrentArea::EntryArea;
Ok(Self {
running,
@@ -127,8 +127,7 @@ impl App {
self.main_biblio = BibiMain::new(self.main_bibfile.clone());
// self.tag_list = TagList::from_iter(self.main_biblio.keyword_list.clone());
self.tag_list = TagList::new(self.main_biblio.keyword_list.clone());
- self.entry_table =
- EntryTable::new(&self.main_biblio.citekeys, &self.main_biblio.bibliography);
+ self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone());
}
// Toggle moveable list between entries and tags
@@ -152,8 +151,7 @@ impl App {
}
pub fn reset_current_list(&mut self) {
- self.entry_table =
- EntryTable::new(&self.main_biblio.citekeys, &self.main_biblio.bibliography);
+ self.entry_table = EntryTable::new(self.main_biblio.entry_list.clone());
self.tag_list = TagList::new(self.main_biblio.keyword_list.clone());
if let CurrentArea::TagArea = self.current_area {
self.tag_list.tag_list_state.select(Some(0))
diff --git a/src/frontend/entries.rs b/src/frontend/entries.rs
index f64e35e..dea970e 100644
--- a/src/frontend/entries.rs
+++ b/src/frontend/entries.rs
@@ -18,7 +18,7 @@
use super::app::App;
use super::tui::Tui;
use crate::backend::{
- bib::{BibiMain, FileFormat},
+ bib::{BibiData, BibiMain, FileFormat},
search::BibiSearch,
};
use biblatex::Bibliography;
@@ -41,8 +41,8 @@ pub struct EntryTable {
}
impl EntryTable {
- pub fn new(citekeys: &Vec<String>, biblio: &Bibliography) -> Self {
- let entry_table_items = Self::set_entry_table(&citekeys, &biblio);
+ pub fn new(entry_list: Vec<BibiData>) -> Self {
+ let entry_table_items = Self::set_entry_table(entry_list);
let entry_table_state = TableState::default().with_selected(0);
let entry_scroll_state = ScrollbarState::new(entry_table_items.len());
let entry_info_scroll_state = ScrollbarState::default();
@@ -57,20 +57,20 @@ impl EntryTable {
}
}
- pub fn set_entry_table(citekeys: &[String], biblio: &Bibliography) -> Vec<EntryTableItem> {
- let mut entry_table: Vec<EntryTableItem> = citekeys
+ pub fn set_entry_table(entry_list: Vec<BibiData>) -> Vec<EntryTableItem> {
+ let mut entry_table: Vec<EntryTableItem> = entry_list
.into_iter()
- .map(|key| EntryTableItem {
- authors: BibiMain::get_authors(&key, &biblio),
+ .map(|e| EntryTableItem {
+ authors: e.authors,
short_author: String::new(),
- title: BibiMain::get_title(&key, &biblio),
- year: BibiMain::get_year(&key, &biblio),
- pubtype: BibiMain::get_pubtype(&key, &biblio),
- keywords: BibiMain::get_keywords(&key, &biblio),
- citekey: key.to_owned(),
- abstract_text: BibiMain::get_abstract(&key, &biblio),
- doi_url: BibiMain::get_weblink(&key, &biblio),
- filepath: BibiMain::get_filepath(&key, &biblio),
+ title: e.title,
+ year: e.year,
+ pubtype: e.pubtype,
+ keywords: e.keywords,
+ citekey: e.citekey,
+ abstract_text: e.abstract_text,
+ doi_url: e.doi_url,
+ filepath: e.filepath,
})
.collect();