From c555c8666268efdff4d7107d5a527b899679559e Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 14 Oct 2024 22:04:26 +0200 Subject: check file format, use new keys() method of biblatex crate --- Cargo.lock | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index 0d3503e..e9ff8b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,6 +79,7 @@ dependencies = [ "crossterm", "editor-command", "futures", + "hayagriva", "itertools", "nucleo-matcher", "ratatui", @@ -176,6 +177,43 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "citationberg" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92fea693c83bd967604be367dc1e1b4895625eabafec2eec66c51092e18e700e" +dependencies = [ + "quick-xml", + "serde", +] + [[package]] name = "clipboard-win" version = "5.4.0" @@ -301,6 +339,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "derive-new" version = "0.6.0" @@ -312,6 +356,17 @@ dependencies = [ "syn", ] +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dlib" version = "0.5.2" @@ -444,6 +499,15 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + [[package]] name = "futures" version = "0.3.31" @@ -549,6 +613,16 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.15.0" @@ -560,6 +634,27 @@ dependencies = [ "foldhash", ] +[[package]] +name = "hayagriva" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3de14f15f4243222e84000669030b3a2fe3a0c6755fd4e38e8a92de039ec21ef" +dependencies = [ + "biblatex", + "ciborium", + "citationberg", + "indexmap", + "numerals", + "paste", + "serde", + "serde_yaml", + "thiserror", + "unic-langid", + "unicode-segmentation", + "unscanny", + "url", +] + [[package]] name = "heck" version = "0.5.0" @@ -581,6 +676,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "image" version = "0.25.2" @@ -608,6 +713,7 @@ checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", "hashbrown", + "serde", ] [[package]] @@ -944,6 +1050,12 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "petgraph" version = "0.6.5" @@ -1001,6 +1113,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7649a7b4df05aed9ea7ec6f628c67c9953a43869b8bc50929569b2999d443fe" dependencies = [ "memchr", + "serde", ] [[package]] @@ -1091,6 +1204,39 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -1266,6 +1412,15 @@ dependencies = [ "weezl", ] +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", +] + [[package]] name = "tinyvec" version = "1.8.0" @@ -1378,6 +1533,31 @@ dependencies = [ "petgraph", ] +[[package]] +name = "unic-langid" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23dd9d1e72a73b25e07123a80776aae3e7b0ec461ef94f9151eed6ec88005a44" +dependencies = [ + "unic-langid-impl", +] + +[[package]] +name = "unic-langid-impl" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a5422c1f65949306c99240b81de9f3f15929f5a8bfe05bb44b034cc8bf593e5" +dependencies = [ + "serde", + "tinystr", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" + [[package]] name = "unicode-ident" version = "1.0.13" @@ -1416,12 +1596,30 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "unscanny" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9df2af067a7953e9c3831320f35c1cc0600c30d44d9f7a12b01db1cd88d6b47" +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + [[package]] name = "valuable" version = "0.1.0" -- cgit v1.2.3 From b5538d8c8408f1afbcfecb2a3b0407c3ad53ebe5 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Fri, 18 Oct 2024 17:07:25 +0200 Subject: Handle short and long author fields --- Cargo.lock | 4 +-- Cargo.toml | 2 +- src/backend/bib.rs | 20 +++++------ src/backend/search.rs | 1 + src/frontend/entries.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++--- src/frontend/ui.rs | 21 ++++++------ src/main.rs | 6 ++-- tests/biblatex-test.bib | 2 +- 8 files changed, 114 insertions(+), 31 deletions(-) (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index e9ff8b6..8a84eaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -636,9 +636,9 @@ dependencies = [ [[package]] name = "hayagriva" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3de14f15f4243222e84000669030b3a2fe3a0c6755fd4e38e8a92de039ec21ef" +checksum = "7a3635c2577f77499c9dc3dceeef2e64e6c146e711b1861507a0f15b20641348" dependencies = [ "biblatex", "ciborium", diff --git a/Cargo.toml b/Cargo.toml index 035a7c8..e9b5158 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ color-eyre = "0.6.3" crossterm = { version = "0.28.1", features = ["event-stream"] } editor-command = "0.1.1" futures = "0.3.30" -hayagriva = "0.7.0" +hayagriva = "0.8.0" itertools = "0.13.0" nucleo-matcher = "0.3.1" ratatui = { version = "0.28.1", features = ["unstable-rendered-line-info"]} diff --git a/src/backend/bib.rs b/src/backend/bib.rs index aca08b3..1f5d05d 100644 --- a/src/backend/bib.rs +++ b/src/backend/bib.rs @@ -17,6 +17,7 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; +use itertools::Itertools; use std::{fs, path::PathBuf}; #[derive(Debug)] @@ -74,16 +75,13 @@ impl BibiMain { // since it is the entry point of the biblatex crate. pub fn get_citekeys(bibstring: &Bibliography) -> Vec { let citekeys: Vec = bibstring.keys().map(|k| k.to_owned()).collect(); - // let mut citekeys: Vec = - // bibstring.iter().map(|entry| entry.to_owned().key).collect(); - // citekeys.sort_by_key(|name| name.to_lowercase()); citekeys } // collect all keywords present in the bibliography // sort them and remove duplicates // this list is for fast filtering entries by topics/keyowrds - pub fn collect_tag_list(citekeys: &Vec, biblio: &Bibliography) -> Vec { + pub fn collect_tag_list(citekeys: &[String], biblio: &Bibliography) -> Vec { // Initialize vector collecting all keywords let mut keyword_list = vec![]; @@ -118,8 +116,8 @@ impl BibiMain { if biblio.get(&citekey).unwrap().author().is_ok() { let authors = biblio.get(&citekey).unwrap().author().unwrap(); if authors.len() > 1 { - let authors = format!("{} et al.", authors[0].name); - authors + 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 @@ -128,12 +126,14 @@ impl BibiMain { editors_authors } } else { - if biblio.get(&citekey).unwrap().editors().is_ok() { + if !biblio.get(&citekey).unwrap().editors().unwrap().is_empty() { let editors = biblio.get(&citekey).unwrap().editors().unwrap(); - if editors.len() > 1 { - let editors = format!("{} (ed.) et al.", editors[0].0[0].name); + 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.len() == 1 { + } else if editors[0].0.len() == 1 { let editors = format!("{} (ed.)", editors[0].0[0].name); editors } else { diff --git a/src/backend/search.rs b/src/backend/search.rs index 4576d2d..65d97d7 100644 --- a/src/backend/search.rs +++ b/src/backend/search.rs @@ -116,6 +116,7 @@ mod tests { fn test_vector_join() { let bibvec: EntryTableItem = EntryTableItem { authors: "Author".to_string(), + short_author: "".to_string(), title: "Title".to_string(), year: "1999".to_string(), pubtype: "article".to_string(), diff --git a/src/frontend/entries.rs b/src/frontend/entries.rs index 63a2a1b..f64e35e 100644 --- a/src/frontend/entries.rs +++ b/src/frontend/entries.rs @@ -17,7 +17,10 @@ use super::app::App; use super::tui::Tui; -use crate::backend::{bib::BibiMain, search::BibiSearch}; +use crate::backend::{ + bib::{BibiMain, FileFormat}, + search::BibiSearch, +}; use biblatex::Bibliography; use color_eyre::eyre::{Context, Ok, Result}; use core::panic; @@ -54,11 +57,12 @@ impl EntryTable { } } - pub fn set_entry_table(citekeys: &Vec, biblio: &Bibliography) -> Vec { + pub fn set_entry_table(citekeys: &[String], biblio: &Bibliography) -> Vec { let mut entry_table: Vec = citekeys .into_iter() .map(|key| EntryTableItem { authors: BibiMain::get_authors(&key, &biblio), + short_author: String::new(), title: BibiMain::get_title(&key, &biblio), year: BibiMain::get_year(&key, &biblio), pubtype: BibiMain::get_pubtype(&key, &biblio), @@ -114,6 +118,7 @@ impl EntryTable { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct EntryTableItem { pub authors: String, + pub short_author: String, pub title: String, pub year: String, pub pubtype: String, @@ -127,8 +132,32 @@ pub struct EntryTableItem { impl EntryTableItem { // This functions decides which fields are rendered in the entry table // Fields which should be usable but not visible can be left out - pub fn ref_vec(&self) -> Vec<&String> { - vec![&self.authors, &self.title, &self.year, &self.pubtype] + pub fn ref_vec(&mut self) -> Vec<&str> { + self.short_author = match self.authors.split_once(",") { + Some((first, _rest)) => { + if self.authors.contains("(ed.)") { + let first_author = format!("{} et al. (ed.)", first); + first_author + } else { + let first_author = format!("{} et al.", first); + first_author + } + } + None => String::from(""), + }; + + vec![ + { + if self.short_author.is_empty() { + &self.authors + } else { + &self.short_author + } + }, + &self.title, + &self.year, + &self.pubtype, + ] } pub fn authors(&self) -> &str { @@ -150,6 +179,14 @@ impl EntryTableItem { pub fn citekey(&self) -> &str { &self.citekey } + + pub fn doi_url(&self) -> &str { + &self.doi_url + } + + pub fn filepath(&self) -> &str { + &self.filepath + } } impl App { @@ -355,6 +392,8 @@ impl App { #[cfg(test)] mod tests { + use super::EntryTableItem; + #[test] fn check_os() { let os = std::env::consts::OS; @@ -365,4 +404,46 @@ mod tests { std::env::consts::OS ) } + + #[test] + fn shorten_authors() { + let mut entry: EntryTableItem = EntryTableItem { + authors: "Miller, Schmitz, Bernard".to_string(), + short_author: "".to_string(), + title: "A title".to_string(), + year: "2000".to_string(), + pubtype: "article".to_string(), + keywords: "key1, key2".to_string(), + citekey: "miller_2000".to_string(), + abstract_text: "An abstract".to_string(), + doi_url: "www.text.org".to_string(), + filepath: "/home/test".to_string(), + }; + + let entry_vec = EntryTableItem::ref_vec(&mut entry); + + let mut entry_editors: EntryTableItem = EntryTableItem { + authors: "Miller, Schmitz, Bernard (ed.)".to_string(), + short_author: "".to_string(), + title: "A title".to_string(), + year: "2000".to_string(), + pubtype: "article".to_string(), + keywords: "key1, key2".to_string(), + citekey: "miller_2000".to_string(), + abstract_text: "An abstract".to_string(), + doi_url: "www.text.org".to_string(), + filepath: "/home/test".to_string(), + }; + + let entry_vec_editors = EntryTableItem::ref_vec(&mut entry_editors); + + assert_eq!( + entry_vec, + vec!["Miller et al.", "A title", "2000", "article"] + ); + assert_eq!( + entry_vec_editors, + vec!["Miller et al. (ed.)", "A title", "2000", "article"] + ) + } } diff --git a/src/frontend/ui.rs b/src/frontend/ui.rs index 1767cea..d94a654 100644 --- a/src/frontend/ui.rs +++ b/src/frontend/ui.rs @@ -28,7 +28,10 @@ use ratatui::{ }, }; -use crate::{frontend::app::App, frontend::keywords::TagListItem}; +use crate::{ + backend::bib::BibiMain, + frontend::{app::App, keywords::TagListItem}, +}; use super::app::{CurrentArea, FormerArea}; @@ -284,7 +287,7 @@ impl App { let rows = self .entry_table .entry_table_items - .iter() + .iter_mut() .enumerate() .map(|(_i, data)| { let item = data.ref_vec(); @@ -349,15 +352,16 @@ impl App { let mut lines = vec![]; lines.push(Line::from(vec![ Span::styled("Authors: ", style_value), - Span::styled(cur_entry.authors.clone(), Style::new().green()), + // Span::styled(cur_entry.authors.clone(), Style::new().green()), + Span::styled(cur_entry.authors(), Style::new().green()), ])); lines.push(Line::from(vec![ Span::styled("Title: ", style_value), - Span::styled(cur_entry.title.clone(), Style::new().magenta()), + Span::styled(cur_entry.title(), Style::new().magenta()), ])); lines.push(Line::from(vec![ Span::styled("Year: ", style_value), - Span::styled(cur_entry.year.clone(), Style::new().light_magenta()), + Span::styled(cur_entry.year(), Style::new().light_magenta()), ])); if !cur_entry.doi_url.is_empty() || !cur_entry.filepath.is_empty() { lines.push(Line::raw("")); @@ -366,7 +370,7 @@ impl App { lines.push(Line::from(vec![ Span::styled("DOI/URL: ", style_value_sec), Span::styled( - cur_entry.doi_url.clone(), + cur_entry.doi_url(), Style::default().fg(TEXT_FG_COLOR).underlined(), ), ])); @@ -374,10 +378,7 @@ impl App { if !cur_entry.filepath.is_empty() { lines.push(Line::from(vec![ Span::styled("File: ", style_value_sec), - Span::styled( - cur_entry.filepath.clone(), - Style::default().fg(TEXT_FG_COLOR), - ), + Span::styled(cur_entry.filepath(), Style::default().fg(TEXT_FG_COLOR)), ])); } lines.push(Line::from("")); diff --git a/src/main.rs b/src/main.rs index 6ef7ee1..b90aeea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,9 +42,9 @@ async fn main() -> Result<()> { std::process::exit(0); } - if !parsed_args.bibfilearg.is_file() { - panic!("No \'.bib\' file passed, aborting") - } + // if !parsed_args.bibfilearg.is_file() { + // panic!("No \'.bib\' file passed, aborting") + // } init_error_hooks()?; diff --git a/tests/biblatex-test.bib b/tests/biblatex-test.bib index 7a1c226..b89416b 100644 --- a/tests/biblatex-test.bib +++ b/tests/biblatex-test.bib @@ -15,7 +15,7 @@ publisher = {Gale}, volume = {61}, pages = {204--208}, - editor = {Matuz, Roger}, + editor = {Matuz, Roger and Miller, Helen}, keywords = {narration}, langid = {english}, langidopts = {variant=american}, -- cgit v1.2.3