diff options
Diffstat (limited to 'support/texlab/src/citation')
-rw-r--r-- | support/texlab/src/citation/name.rs | 279 | ||||
-rw-r--r-- | support/texlab/src/citation/name/parser.lalrpop | 150 | ||||
-rw-r--r-- | support/texlab/src/citation/ris.rs | 24 |
3 files changed, 14 insertions, 439 deletions
diff --git a/support/texlab/src/citation/name.rs b/support/texlab/src/citation/name.rs deleted file mode 100644 index 9e22383a7d..0000000000 --- a/support/texlab/src/citation/name.rs +++ /dev/null @@ -1,279 +0,0 @@ -// Ported from: https://github.com/michel-kraemer/citeproc-java/blob/master/citeproc-java/grammars/InternalName.g4 -// Michel Kraemer -// Apache License 2.0 -use citeproc_io::Name; - -use parser::NamesParser; - -mod parser { - #![allow(warnings)] - include!(concat!(env!("OUT_DIR"), "/citation/name/parser.rs")); -} - -pub fn parse(input: &str) -> Vec<Name> { - let parser = NamesParser::new(); - parser.parse(input).unwrap_or_else(|_| { - vec![Name::Literal { - literal: input.into(), - }] - }) -} - -#[cfg(test)] -mod tests { - use super::*; - use citeproc_io::PersonName; - - #[test] - fn test_family_only() { - let name = Name::Person(PersonName { - family: Some("Thompson".into()), - given: None, - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Thompson"), vec![name]); - } - - #[test] - fn test_simple() { - let name = Name::Person(PersonName { - family: Some("Thompson".into()), - given: Some("Ken".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Ken Thompson"), vec![name]); - } - - #[test] - fn test_middle_name() { - let name = Name::Person(PersonName { - family: Some("Ritchie".into()), - given: Some("Dennis M.".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Dennis M. Ritchie"), vec![name]); - } - - #[test] - fn test_initials() { - let name = Name::Person(PersonName { - family: Some("Johnson".into()), - given: Some("S. C.".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("S. C. Johnson"), vec![name]); - } - - #[test] - fn test_non_dropping_particle() { - let name = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: Some("Michael".into()), - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Michael van Gerwen"), vec![name]); - } - - #[test] - fn test_non_dropping_particle_family_only() { - let name = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: None, - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("van Gerwen"), vec![name]); - } - - #[test] - fn test_comma() { - let name = Name::Person(PersonName { - family: Some("Thompson".into()), - given: Some("Ken".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Thompson, Ken"), vec![name]); - } - - #[test] - fn test_comma_junior() { - let name = Name::Person(PersonName { - family: Some("Friedman".into()), - given: Some("George".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: Some("Jr.".into()), - }); - assert_eq!(parse("Friedman, Jr., George"), vec![name]); - } - - #[test] - fn test_comma_no_junior() { - let name = Name::Person(PersonName { - family: Some("Familya Familyb".into()), - given: Some("Given".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Familya Familyb, Given"), vec![name]); - } - - #[test] - fn test_comma_initials() { - let name = Name::Person(PersonName { - family: Some("Ritchie".into()), - given: Some("Dennis M.".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Ritchie, Dennis M."), vec![name]); - } - - #[test] - fn test_comma_non_dropping_particle() { - let name = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: Some("Michael".into()), - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("van Gerwen, Michael"), vec![name]); - } - - #[test] - fn test_comma_non_dropping_particles() { - let name = Name::Person(PersonName { - family: Some("Voort".into()), - given: Some("Vincent".into()), - non_dropping_particle: Some("Van der".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!(parse("Van der Voort, Vincent"), vec![name]); - } - - #[test] - fn test_and() { - let name1 = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: Some("Michael".into()), - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - let name2 = Name::Person(PersonName { - family: Some("Voort".into()), - given: Some("Vincent".into()), - non_dropping_particle: Some("van der".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!( - parse("Michael van Gerwen and Vincent van der Voort"), - vec![name1, name2] - ); - } - - #[test] - fn test_and_comma1() { - let name1 = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: Some("Michael".into()), - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - let name2 = Name::Person(PersonName { - family: Some("Voort".into()), - given: Some("Vincent".into()), - non_dropping_particle: Some("Van der".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!( - parse("van Gerwen, Michael and Van der Voort, Vincent"), - vec![name1, name2] - ); - } - - #[test] - fn test_and_comma2() { - let name1 = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: Some("Michael".into()), - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - let name2 = Name::Person(PersonName { - family: Some("Voort".into()), - given: Some("Vincent".into()), - non_dropping_particle: Some("van der".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!( - parse("van Gerwen, Michael and van der Voort, Vincent"), - vec![name1, name2] - ); - } - - #[test] - fn test_and_comma_mix() { - let name1 = Name::Person(PersonName { - family: Some("Gerwen".into()), - given: Some("Michael".into()), - non_dropping_particle: Some("van".into()), - dropping_particle: None, - suffix: None, - }); - let name2 = Name::Person(PersonName { - family: Some("Voort".into()), - given: Some("Vincent".into()), - non_dropping_particle: Some("van der".into()), - dropping_particle: None, - suffix: None, - }); - assert_eq!( - parse("van Gerwen, Michael and Vincent van der Voort"), - vec![name1, name2] - ); - } - - #[test] - fn test_junior() { - let name = Name::Person(PersonName { - family: Some("Friedman".into()), - given: Some("George".into()), - non_dropping_particle: None, - dropping_particle: None, - suffix: Some("Jr.".into()), - }); - assert_eq!(parse("George Friedman, Jr."), vec![name]); - } - - #[test] - fn test_non_parseable() { - let literal = "Jerry Peek and Tim O'Reilly and Mike Loukides and other authors of the Nutshell handbooks"; - let name = Name::Literal { - literal: literal.into(), - }; - assert_eq!(parse(literal), vec![name]); - } -} diff --git a/support/texlab/src/citation/name/parser.lalrpop b/support/texlab/src/citation/name/parser.lalrpop deleted file mode 100644 index e7bce8a302..0000000000 --- a/support/texlab/src/citation/name/parser.lalrpop +++ /dev/null @@ -1,150 +0,0 @@ -// Ported from: https://github.com/michel-kraemer/citeproc-java/blob/master/citeproc-java/grammars/InternalName.g4 -// Michel Kraemer -// Apache License 2.0 -use citeproc_io::{Name, PersonName}; -use itertools::Itertools; - -grammar; - -pub Names: Vec<Name> = And<Name>; - -And<T>: Vec<T> = { - <v:(<T> "and")*> <e:T?> => match e { - None => v, - Some(e) => { - let mut v = v; - v.push(e); - v - } - } -}; - -Name: Name = { - <np1:UWord+> <np2:Von> <fam:Last> "," <fst:First> => { - let (fst1, fst2) = fst; - let name = PersonName { - family: Some(fam.join(" ")), - given: fst2, - non_dropping_particle: Some(format!("{} {}", np1.join(" "), np2)), - dropping_particle: None, - suffix: fst1, - }; - Name::Person(name) - }, - <np:Von> <fam:Last> "," <fst:First> => { - let (fst1, fst2) = fst; - let name = PersonName { - family: Some(fam.join(" ")), - given: fst2, - non_dropping_particle: Some(np), - dropping_particle: None, - suffix: fst1, - }; - Name::Person(name) - }, - <last:Last> "," <fst:First> => { - let (fst1, fst2) = fst; - let first_text = format!( - "{} {}", - fst1.as_ref().map(|s| s.as_str()).unwrap_or_default(), - fst2.as_ref().map(|s| s.as_str()).unwrap_or_default(), - ).trim().to_owned(); - - let name = if first_text == "Jr." { - if last.len() == 1 { - PersonName { - family: Some(last.join(" ")), - given: None, - non_dropping_particle: None, - dropping_particle: None, - suffix: Some(first_text), - } - } else { - let mut last = last.into_iter(); - let given = last.next().map(ToOwned::to_owned); - PersonName { - family: Some(last.join(" ")), - given, - non_dropping_particle: None, - dropping_particle: None, - suffix: Some(first_text), - } - } - } else { - PersonName { - family: Some(last.join(" ")), - given: fst2, - non_dropping_particle: None, - dropping_particle: None, - suffix: fst1, - } - }; - Name::Person(name) - }, - <np:Von> <fam:Last> => { - let name = PersonName { - family: Some(fam.join(" ")), - given: None, - non_dropping_particle: Some(np), - dropping_particle: None, - suffix: None, - }; - Name::Person(name) - }, - <giv:UWord+> <np:Von> <fam:Last> => { - let name = PersonName { - family: Some(fam.join(" ")), - given: Some(giv.join(" ")), - non_dropping_particle: Some(np), - dropping_particle: None, - suffix: None, - }; - Name::Person(name) - }, - <giv:UWord+> <fam:Word> => { - let name = PersonName { - family: Some(fam.into()), - given: Some(giv.join(" ")), - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }; - Name::Person(name) - }, - <fam:Word> => { - let name = PersonName { - family: Some(fam.into()), - given: None, - non_dropping_particle: None, - dropping_particle: None, - suffix: None, - }; - Name::Person(name) - }, -}; - -First: (Option<String>, Option<String>) = { - <a:Word*> "," <b:Word*> => (Some(a.join(" ")), Some(b.join(" "))), - <a:Word*> => (None, Some(a.join(" "))), -}; - -Last: Vec<&'input str> = { - LWord => vec![(<>)], - UWord+ => (<>), -}; - -Von: String = { - LWord => String::from(<>), - <a:Von> <b:LWord> => format!("{} {}", a, b), - <a:Von> <b:UWord+> <c:LWord> => format!("{} {} {}", a, b.join(" "), c), -}; - -Word: &'input str = { - UWord => (<>), - LWord => (<>), -}; - - -UWord: &'input str = r"[A-Z\u00C0-\uFFFF(?][A-Z\u00C0-\uFFFF(?a-z\-)&/.]+" => (<>); - -LWord: &'input str = r"[a-z\-)&/.][A-Z\u00C0-\uFFFF(?a-z\-)&/.]+" => (<>); diff --git a/support/texlab/src/citation/ris.rs b/support/texlab/src/citation/ris.rs index 5b666160a3..041fb654a0 100644 --- a/support/texlab/src/citation/ris.rs +++ b/support/texlab/src/citation/ris.rs @@ -1,13 +1,11 @@ // Ported from: https://github.com/michel-kraemer/citeproc-java/tree/master/citeproc-java/templates // Michel Kraemer // Apache License 2.0 -use citeproc_io::{Date, DateOrRange, Name, NumericValue, Reference}; +use citeproc_io::{unicode::is_latin_cyrillic, Date, DateOrRange, Name, NumberLike, Reference}; use csl::*; use fnv::FnvHashMap; use serde::{Deserialize, Serialize}; -use super::name; - #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] #[serde(rename_all = "UPPERCASE")] pub enum RisType { @@ -303,7 +301,7 @@ impl Into<Reference> for RisReference { let csl_type = self.ty.expect("RIS type is missing").csl(); let mut date: FnvHashMap<DateVariable, DateOrRange> = FnvHashMap::default(); let mut name: FnvHashMap<NameVariable, Vec<Name>> = FnvHashMap::default(); - let mut number: FnvHashMap<NumberVariable, NumericValue> = FnvHashMap::default(); + let mut number: FnvHashMap<NumberVariable, NumberLike> = FnvHashMap::default(); let mut ordinary: FnvHashMap<Variable, String> = FnvHashMap::default(); if let Some(access_date) = self.access_date { @@ -389,7 +387,7 @@ impl Into<Reference> for RisReference { (Some(start_page), Some(end_page)) => { number.insert( NumberVariable::Page, - NumericValue::Str(format!("{}-{}", start_page, end_page)), + NumberLike::Str(format!("{}-{}", start_page, end_page).into()), ); } (Some(page), None) | (None, Some(page)) => { @@ -434,24 +432,30 @@ impl Into<Reference> for RisReference { } } -fn parse_number(value: String) -> NumericValue { +fn parse_number(value: String) -> NumberLike { match value.parse() { - Ok(value) => NumericValue::num(value), - Err(_) => NumericValue::Str(value), + Ok(value) => NumberLike::Num(value), + Err(_) => NumberLike::Str(value.into()), } } fn parse_authors(authors: Vec<String>) -> Vec<Name> { authors .into_iter() - .flat_map(|author| name::parse(&author)) + .map(|author| Name::Literal { + is_latin_cyrillic: is_latin_cyrillic(&author), + literal: author.into(), + }) .collect() } fn parse_date_or_range(value: String) -> DateOrRange { parse_date(&value) .map(DateOrRange::Single) - .unwrap_or_else(|| DateOrRange::Literal(value)) + .unwrap_or_else(|| DateOrRange::Literal { + literal: value.into(), + circa: false, + }) } fn parse_date(value: &str) -> Option<Date> { |