├── cli ├── .gitignore ├── README.md ├── Cargo.toml └── src │ ├── main.rs │ └── makeschemas.rs ├── processor ├── .gitignore ├── examples │ ├── citation.yaml │ ├── style.csl.yaml │ ├── chicago-ad-experiment.yaml │ ├── chicago.bib.yaml │ └── ex1.bib.yaml ├── Cargo.toml ├── README.md ├── locales │ └── locale-en.yaml ├── benches │ └── proc_bench.rs ├── tests │ └── processor_test.rs └── src │ └── lib.rs ├── csln ├── src │ ├── style │ │ ├── .gitignore │ │ ├── README.md │ │ ├── mod.rs │ │ ├── locale.rs │ │ ├── template.rs │ │ └── options.rs │ ├── citation │ │ ├── .gitignore │ │ └── mod.rs │ ├── bibliography │ │ ├── .gitignore │ │ ├── README.md │ │ ├── mod.rs │ │ └── reference.rs │ └── lib.rs └── Cargo.toml ├── .gitignore ├── .rustfmt.toml ├── .vscode └── settings.json ├── Cargo.toml ├── .chglog ├── CHANGELOG.tpl.md └── config.yml ├── .github └── workflows │ └── quickstart.yml ├── CHANGELOG.md ├── README.md └── LICENSE /cli/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /processor/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /csln/src/style/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Cargo.lock 2 | target 3 | schemas 4 | *.bak 5 | -------------------------------------------------------------------------------- /csln/src/citation/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /csln/src/bibliography/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /csln/src/style/README.md: -------------------------------------------------------------------------------- 1 | This is a Rust library that implements a Style model. 2 | 3 | The `csln-schemas` binary will generate the input JSON schemas. 4 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | use_small_heuristics = "Max" 2 | max_width = 90 3 | chain_width = 70 4 | struct_lit_width = 50 5 | use_field_init_shorthand = true 6 | merge_derives = false 7 | -------------------------------------------------------------------------------- /csln/src/bibliography/README.md: -------------------------------------------------------------------------------- 1 | This is a Rust library that implements the [csl-next](https://github.com/bdarcus/csl-next) bibliography model. 2 | 3 | The `csln-schemas` binary will generate the input JSON schemas. 4 | -------------------------------------------------------------------------------- /csln/src/bibliography/mod.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | 3 | pub mod reference; 4 | pub use reference::InputReference; 5 | 6 | /// A bibliography is a collection of references. 7 | pub type InputBibliography = HashMap; 8 | -------------------------------------------------------------------------------- /processor/examples/citation.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | - mode: non-integral 3 | citation_items: 4 | - refId: "doe1" 5 | - refId: "doe2" 6 | - mode: integral 7 | citation_items: 8 | - refId: "doe2" 9 | suffix: ["page 42"] 10 | - mode: non-integral 11 | prefix: "see " 12 | citation_items: 13 | - refId: "doe3" 14 | -------------------------------------------------------------------------------- /cli/README.md: -------------------------------------------------------------------------------- 1 | Right now, this has two simple binaries: 2 | 3 | 1. `csln` runs the processor 4 | 2. `csln-schemas` creates the schemas 5 | 6 | I'm thinking to merge them in a single, richer, cli; something like: 7 | 8 | ```console 9 | csln make schemas -d /tmp/schemas 10 | csln process bibliography -t latex -b bib.yaml -s style.json 11 | csln process document -t djot -b bib.yaml -s style.json mymanuscript.dj 12 | csln find style abc 13 | csln make style xyz 14 | ``` 15 | 16 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "yaml.schemas": { 3 | "./schemas/style.json": [ 4 | "/*.csl.yaml" 5 | ], 6 | "./schemas/bibliography.json": [ 7 | "/*.bib.yaml" 8 | ], 9 | "./schemas/locale.json": [ 10 | "/locale-*.yaml" 11 | ], 12 | "./schemas/citation.json": [ 13 | "/citation*.yaml" 14 | ] 15 | 16 | }, 17 | "rust-analyzer.linkedProjects": [ 18 | "./csln/Cargo.toml", 19 | "./csln/Cargo.toml", 20 | "./csln/Cargo.toml", 21 | "./csln/Cargo.toml" 22 | ], 23 | } 24 | -------------------------------------------------------------------------------- /cli/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csln-cli" 3 | authors = ["Bruce D'Arcus "] 4 | license = "MPL-2.0" 5 | version = "0.1.0" 6 | edition = "2021" 7 | 8 | [[bin]] 9 | name = "csln-schemas" 10 | path = "src/makeschemas.rs" 11 | 12 | [[bin]] 13 | name = "csln" 14 | path = "src/main.rs" 15 | 16 | 17 | [dependencies] 18 | clap = { version = "4.4", features = ["derive"] } 19 | schemars = "0.8" 20 | serde_json = "1.0" 21 | csln = { path = "../csln", package = "csln" } 22 | processor = { path = "../processor", package = "csln-processor" } 23 | anyhow = "1.0.79" 24 | 25 | 26 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | resolver = "2" 3 | members = [ 4 | "cli", 5 | "csln", 6 | "processor", 7 | ] 8 | 9 | [workspace.lints.rust] 10 | unsafe_code = "forbid" 11 | 12 | [workspace.lints.clippy] 13 | # not sure on what to turn on and off 14 | complexity = { level = "allow", priority = -1 } 15 | expect_used = "warn" 16 | large_enum_variant = "allow" 17 | needless_borrow = "warn" 18 | needless_question_mark = "warn" 19 | needless_return = "warn" 20 | style = { level = "allow", priority = -1 } 21 | unwrap_used = "warn" 22 | 23 | [profile.release] 24 | lto = true 25 | codegen-units = 1 26 | panic = "abort" 27 | -------------------------------------------------------------------------------- /.chglog/CHANGELOG.tpl.md: -------------------------------------------------------------------------------- 1 | {{ range .Versions }} 2 | 3 | ## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }}) 4 | 5 | {{ range .CommitGroups -}} 6 | ### {{ .Title }} 7 | 8 | {{ range .Commits -}} 9 | * {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} ([{{ .Hash.Short }}]({{ $.Info.RepositoryURL }}/commit/{{ .Hash.Short }})) 10 | {{ end }} 11 | {{ end -}} 12 | 13 | {{- if .NoteGroups -}} 14 | {{ range .NoteGroups -}} 15 | ### {{ .Title }} 16 | 17 | {{ range .Notes }} 18 | {{ .Body }} 19 | {{ end }} 20 | {{ end -}} 21 | {{ end -}} 22 | {{ end -}} 23 | -------------------------------------------------------------------------------- /.chglog/config.yml: -------------------------------------------------------------------------------- 1 | style: github 2 | template: CHANGELOG.tpl.md 3 | info: 4 | title: CHANGELOG 5 | repository_url: https://github.com/bdarcus/csln 6 | options: 7 | commits: 8 | filters: 9 | Type: 10 | - feat 11 | - fix 12 | - refactor 13 | commit_groups: 14 | group_by: Type 15 | sort_by: RawTitle 16 | title_maps: 17 | feat: Added 18 | fix: Fixed 19 | refactor: Changed 20 | title_order: 21 | - feat 22 | - fix 23 | - refactor 24 | header: 25 | pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" 26 | pattern_maps: 27 | - Type 28 | - Scope 29 | - Subject 30 | issues: 31 | prefix: 32 | - # 33 | refs: 34 | actions: 35 | - Closes 36 | - Fixes 37 | notes: 38 | keywords: 39 | - BREAKING CHANGE 40 | -------------------------------------------------------------------------------- /processor/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csln-processor" 3 | authors = ["Bruce D'Arcus "] 4 | license = "MPL-2.0" 5 | version = "0.1.0" 6 | edition = "2021" 7 | 8 | [lib] 9 | name = "csln_processor" 10 | test = true 11 | doctest = true 12 | bench = true 13 | doc = true 14 | edition = "2021" # The edition of the target. 15 | crate-type = ["lib"] # The crate types to generate. 16 | 17 | [dependencies] 18 | schemars = "0.8.12" 19 | serde = "1.0.162" 20 | serde_derive = "1.0.162" 21 | serde_json = "1.0.96" 22 | serde_yaml = "0.9.21" 23 | edtf = { version = "0.2.0", features = ["chrono"] } 24 | csln = { path = "../csln", package = "csln" } 25 | itertools = "0.12" 26 | rayon = "1.7.0" 27 | icu = "1.2.0" 28 | icu_testdata = "1.2.0" 29 | icu_datetime = "1.2.1" 30 | chrono = "0.4.26" 31 | 32 | [dev-dependencies] 33 | criterion = { version = "0.5.1", features = ["html_reports"] } 34 | anyhow = "1.0.79" 35 | 36 | [[bench]] 37 | name = "proc_bench" 38 | harness = false 39 | 40 | [lints] 41 | workspace = true 42 | -------------------------------------------------------------------------------- /csln/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csln" 3 | authors = ["Bruce D'Arcus "] 4 | license = "MPL-2.0" 5 | version = "0.1.0" 6 | edition = "2021" 7 | 8 | [lib] 9 | name = "csln" 10 | test = true 11 | doctest = true 12 | bench = true 13 | doc = true 14 | edition = "2021" 15 | crate-type = ["lib"] 16 | 17 | [dependencies] 18 | schemars = { version = "0.8", features = ["url"] } 19 | serde = { version = "1.0", features = ["derive"] } 20 | serde_derive = "1.0" 21 | serde_json = "1.0" 22 | serde_yaml = "0.9" 23 | url = { version = "2.4.0", features = ["serde"] } 24 | edtf = { version = "0.2", features = ["chrono"] } 25 | chrono = { version = "0.4", features = ["unstable-locales"] } 26 | unic-langid = { version = "0.9.1", features = ["serde"] } 27 | itertools = "0.11.0" 28 | rayon = "1.7.0" 29 | anyhow = "1.0.79" 30 | #icu = { version = "1.2.0", features = ["icu_datetime_experimental"] } 31 | #icu_testdata = { version = "1.2.0", features = ["icu_datetime_experimental"] } 32 | #indexmap = { version = "2.0.0", features = ["std"] } 33 | 34 | [lints] 35 | workspace = true 36 | 37 | -------------------------------------------------------------------------------- /processor/README.md: -------------------------------------------------------------------------------- 1 | This is a Rust processor library for the [csl-next](https://github.com/bdarcus/csl-next) model. 2 | 3 | It is far from complete, but you can see its current state in the `csln` binary. 4 | 5 | The basic processing design is as follows: 6 | 7 | 1. sort bibliography references (the HashMap values) 8 | 2. group the sorted bibliography to derive processing hints, and return a `HashMap` of them 9 | 3. the `render_references` method then iterates through the `Style` templates, and above `Vector` and `HashMap`, and returns an AST 10 | 4. methods will then render from AST to different output formats 11 | 12 | A fragment of the current AST returned by `render_references()` is: 13 | 14 | ```js 15 | [ 16 | { 17 | "templateComponent": { 18 | "contributor": "author", 19 | "form": "long", 20 | "rendering": null 21 | }, 22 | "value": "Smith, John" 23 | }, 24 | { 25 | "templateComponent": { 26 | "date": "issued", 27 | "form": "year", 28 | "rendering": null 29 | }, 30 | "value": "2025" 31 | } 32 | ] 33 | ``` 34 | -------------------------------------------------------------------------------- /processor/locales/locale-en.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | locale: en 3 | terms: 4 | and: and 5 | and-symbol: "&" 6 | and-others: and others 7 | anonymous: 8 | long: anonymous 9 | short: anon 10 | at: at 11 | accessed: accessed 12 | available-at: available at 13 | by: by 14 | circa: 15 | long: circa 16 | short: c 17 | et-al: et al 18 | roles: 19 | editor: 20 | singular: 21 | long: editor 22 | short: ed 23 | plural: 24 | long: editors # is this right? 25 | short: eds 26 | verb: 27 | long: edited by 28 | short: ed 29 | dates: 30 | months: 31 | long: 32 | - January 33 | - February 34 | - March 35 | - April 36 | - May 37 | - June 38 | - July 39 | - August 40 | - September 41 | - October 42 | - November 43 | - December 44 | short: 45 | - Jan 46 | - Feb 47 | - Mar 48 | - Apr 49 | - May 50 | - Jun 51 | - Jul 52 | - Aug 53 | - Sep 54 | - Oct 55 | - Nov 56 | - Dec 57 | seasons: 58 | - "Spring" 59 | - "Summer" 60 | - "Fall" 61 | - "Winter" -------------------------------------------------------------------------------- /csln/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod style; 2 | use std::path::Path; 3 | 4 | use serde::de::DeserializeOwned; 5 | pub use style::Style; 6 | 7 | use std::fs; 8 | 9 | pub mod bibliography; 10 | pub use bibliography::InputBibliography; 11 | use style::locale::Locale; 12 | 13 | use anyhow::{Context, Result}; 14 | 15 | pub mod citation; 16 | 17 | pub trait Parsable: DeserializeOwned {} 18 | impl Parsable for Style {} 19 | impl Parsable for Locale {} 20 | impl Parsable for InputBibliography {} 21 | impl Parsable for citation::Citations {} 22 | 23 | pub fn from_file>(path: P) -> Result { 24 | let path = path.as_ref(); 25 | let contents = fs::read_to_string(path) 26 | .with_context(|| format!("Failed to read file: {}", path.display()))?; 27 | 28 | let value = if path.extension().and_then(|s| s.to_str()) == Some("json") { 29 | serde_json::from_str(&contents).with_context(|| { 30 | format!("Failed to parse JSON from file: {}", path.display()) 31 | })? 32 | } else if path.extension().and_then(|s| s.to_str()) == Some("yaml") { 33 | serde_yaml::from_str(&contents).with_context(|| { 34 | format!("Failed to parse YAML from file: {}", path.display()) 35 | })? 36 | } else { 37 | return Err(anyhow::anyhow!("Unsupported file extension")); 38 | }; 39 | 40 | Ok(value) 41 | } 42 | -------------------------------------------------------------------------------- /processor/benches/proc_bench.rs: -------------------------------------------------------------------------------- 1 | use criterion::{criterion_group, criterion_main, Criterion}; 2 | use csln::bibliography::InputBibliography as Bibliography; 3 | use csln::citation::Citation; 4 | use csln::from_file; 5 | use csln_processor::Processor; 6 | use std::time::Duration; 7 | 8 | fn proc_benchmark(c: &mut Criterion) { 9 | let style = match from_file("examples/style.csl.yaml") { 10 | Ok(style) => style, 11 | Err(_) => { 12 | println!("Failed to load style"); 13 | return; 14 | } 15 | }; 16 | let bibliography: Bibliography = from_file("examples/ex1.bib.yaml").expect("msg"); 17 | let locale = from_file("locales/locale-en.yaml"); 18 | let citations: Vec = Vec::new(); 19 | let processor: Processor = 20 | Processor::new(style, bibliography, citations, locale.expect("msg")); 21 | c.bench_function("sorting references", |b| { 22 | b.iter(|| { 23 | let refs = processor.get_references(); 24 | processor.sort_references(refs); 25 | }) 26 | }); 27 | c.bench_function("grouping references", |b| { 28 | b.iter(|| { 29 | processor.group_references(processor.get_references()); 30 | }) 31 | }); 32 | c.bench_function("rendering references", |b| { 33 | b.iter(|| { 34 | processor.process_references(); 35 | }) 36 | }); 37 | } 38 | 39 | criterion_group!( 40 | name = benches; 41 | config = Criterion::default().measurement_time(Duration::new(12, 0)).sample_size(80); 42 | targets = proc_benchmark 43 | ); 44 | criterion_main!(benches); 45 | -------------------------------------------------------------------------------- /cli/src/main.rs: -------------------------------------------------------------------------------- 1 | use anyhow::Context; 2 | use clap::Parser; 3 | use csln::citation::Citations; 4 | use csln::from_file; 5 | use processor::{ProcReferences, Processor}; 6 | 7 | #[derive(Parser, Default, Debug)] 8 | #[clap(author = "Bruce D'Arcus", version, about = "A CLI for CSLN")] 9 | pub struct Opts { 10 | #[clap(short, long)] 11 | /// The path to the CSLN style file 12 | style: String, 13 | #[clap(short, long)] 14 | /// The path to the CSLN bibliography file 15 | bibliography: String, 16 | #[clap(short, long)] 17 | /// The optional path to the CSLN citation file 18 | citations: Option, 19 | #[clap(short, long)] 20 | /// The path to the CSLN locale file 21 | locale: String, 22 | } 23 | 24 | fn main() -> anyhow::Result<()> { 25 | let opts = Opts::parse(); 26 | let style = from_file(&opts.style).context("Failed to load style file")?; 27 | let bibliography = from_file(&opts.bibliography).context("Failed to load bibliography file")?; 28 | let citations: Citations = if let Some(citation_path) = opts.citations { 29 | from_file(&citation_path).context("Failed to load citation file")? 30 | } else { 31 | Citations::default() 32 | }; 33 | let locale = from_file(&opts.locale).context("Failed to load locale file")?; 34 | let processor: Processor = Processor::new(style, bibliography, citations, locale); 35 | let rendered_refs: ProcReferences = processor.process_references(); 36 | let serialized_refs = serde_json::to_string_pretty(&rendered_refs) 37 | .context("Failed to serialize references")?; 38 | println!("{}", serialized_refs); 39 | Ok(()) 40 | } 41 | -------------------------------------------------------------------------------- /processor/examples/style.csl.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | info: 3 | title: APA 4 | options: 5 | substitute: 6 | contributor_role_form: short 7 | template: 8 | - editor 9 | - title 10 | processing: author-date # this sets sorting and grouping for author-date 11 | titles: 12 | component: 13 | quote: true 14 | default: 15 | emph: true 16 | contributors: 17 | display_as_sort: first 18 | and: symbol 19 | templates: 20 | title-apa: 21 | - title: primary 22 | container-title: 23 | # the below titles are mutually-exclusive, so at most one will be output 24 | - title: parent-monograph 25 | prefix: In 26 | emph: true 27 | - title: parent-serial 28 | author-apa-full: 29 | - contributor: author 30 | form: long 31 | howpublished-apa: 32 | - contributor: publisher 33 | form: short 34 | wrap: parentheses 35 | citation: 36 | template: 37 | - contributor: author 38 | form: short 39 | - date: issued 40 | form: year 41 | bibliography: 42 | template: 43 | - contributor: author 44 | form: long 45 | - date: issued 46 | form: year 47 | rendering: # not a fan of this 48 | wrap: parentheses 49 | - title: primary 50 | - contributor: editor 51 | form: verb 52 | - title: parent-monograph 53 | prefix: In 54 | emph: true 55 | - title: parent-serial 56 | - date: issued 57 | form: month-day 58 | - number: volume 59 | - variable: doi 60 | - contributor: publisher # location? 61 | form: long # make optional, with default? 62 | delimiter: colon # scope? delimiter vs item-delimiter? 63 | -------------------------------------------------------------------------------- /cli/src/makeschemas.rs: -------------------------------------------------------------------------------- 1 | use schemars::schema_for; 2 | use std::fs; 3 | use std::fs::File; 4 | use std::io::Write; 5 | 6 | use csln::bibliography::InputBibliography; 7 | use csln::citation::CitationList; 8 | use csln::style::locale::Locale; 9 | use csln::style::Style; 10 | 11 | fn main() { 12 | fs::create_dir_all("schemas").expect("Failed to create directory 'schemas'"); 13 | 14 | let style_schema = schema_for!(Style); 15 | let citation_schema = schema_for!(CitationList); 16 | let bib_schema = schema_for!(InputBibliography); 17 | let locale_schema = schema_for!(Locale); 18 | 19 | let style_json_output = serde_json::to_string_pretty(&style_schema).unwrap(); 20 | let citation_json_output = serde_json::to_string_pretty(&citation_schema).unwrap(); 21 | let bib_json_output = serde_json::to_string_pretty(&bib_schema).unwrap(); 22 | let locale_json_output = serde_json::to_string_pretty(&locale_schema).unwrap(); 23 | 24 | let mut citation_file = File::create("schemas/citation.json").unwrap(); 25 | let mut style_file = File::create("schemas/style.json").unwrap(); 26 | let mut bib_file = File::create("schemas/bibliography.json").unwrap(); 27 | let mut locale_file = File::create("schemas/locale.json").unwrap(); 28 | style_file.write_all(style_json_output.as_bytes()).unwrap(); 29 | citation_file.write_all(citation_json_output.as_bytes()).unwrap(); 30 | bib_file.write_all(bib_json_output.as_bytes()).unwrap(); 31 | locale_file.write_all(locale_json_output.as_bytes()).unwrap(); 32 | println!("Wrote bibliography schema to schemas/bibliography.json"); 33 | println!("Wrote citation schema to schemas/citation.json"); 34 | println!("Wrote style schema to schemas/style.json"); 35 | println!("Wrote locale schema to schemas/locale.json"); 36 | } 37 | -------------------------------------------------------------------------------- /.github/workflows/quickstart.yml: -------------------------------------------------------------------------------- 1 | # Based on https://github.com/actions-rs/meta/blob/master/recipes/quickstart.md 2 | # 3 | # While our "example" application has the platform-specific code, 4 | # for simplicity we are compiling and testing everything on the Ubuntu environment only. 5 | # For multi-OS testing see the `cross.yml` workflow. 6 | 7 | on: [push, pull_request] 8 | 9 | name: Quickstart 10 | 11 | jobs: 12 | check: 13 | name: Check 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: Checkout sources 17 | uses: actions/checkout@v4 18 | 19 | - name: Install stable toolchain 20 | uses: dtolnay/rust-toolchain@stable 21 | 22 | - name: Run cargo check 23 | run: cargo check 24 | continue-on-error: true # WARNING: only for this example, remove it! 25 | 26 | test: 27 | name: Test Suite 28 | runs-on: ubuntu-latest 29 | steps: 30 | - name: Checkout sources 31 | uses: actions/checkout@v4 32 | 33 | - name: Install stable toolchain 34 | uses: dtolnay/rust-toolchain@stable 35 | 36 | - name: Run cargo test 37 | run: cargo test 38 | continue-on-error: true # WARNING: only for this example, remove it! 39 | 40 | lints: 41 | name: Lints 42 | runs-on: ubuntu-latest 43 | steps: 44 | - name: Checkout sources 45 | uses: actions/checkout@v4 46 | 47 | - name: Install stable toolchain 48 | uses: dtolnay/rust-toolchain@stable 49 | with: 50 | components: rustfmt, clippy 51 | 52 | - name: Run cargo fmt 53 | run: cargo fmt --all -- --check 54 | continue-on-error: true # WARNING: only for this example, remove it! 55 | 56 | - name: Run cargo clippy 57 | run: cargo clippy -- -D warnings 58 | continue-on-error: true # WARNING: only for this example, remove it! 59 | -------------------------------------------------------------------------------- /csln/src/style/mod.rs: -------------------------------------------------------------------------------- 1 | /* 2 | SPDX-License-Identifier: MPL-2.0 3 | SPDX-FileCopyrightText: © 2023 Bruce D'Arcus 4 | */ 5 | 6 | use schemars::JsonSchema; 7 | use serde::{Deserialize, Serialize}; 8 | use std::collections::HashMap; 9 | 10 | pub mod locale; 11 | pub mod options; 12 | use options::Config; 13 | 14 | pub mod template; 15 | use template::TemplateComponent; 16 | 17 | /// The Style model. 18 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 19 | pub struct Style { 20 | /// Style metadata. 21 | pub info: Info, 22 | pub templates: Option>, 23 | /// Parameter groups. 24 | #[serde(default)] 25 | pub options: Option, 26 | /// The citation specification. 27 | pub citation: Option, 28 | /// The bibliography specification. 29 | pub bibliography: Option, 30 | } 31 | 32 | /// The Template model. 33 | pub type Template = Vec; 34 | 35 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 36 | /// The bibliography specification. 37 | pub struct Bibliography { 38 | pub options: Option, 39 | pub template: Template, 40 | } 41 | 42 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 43 | /// The citation specification. 44 | pub struct Citation { 45 | pub options: Option, 46 | pub template: Template, 47 | } 48 | 49 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 50 | /// Style metadata. 51 | pub struct Info { 52 | /// The categories the style belongs to; for purposes of indexing. 53 | pub categories: Option>, 54 | /// The description of the style. 55 | pub description: Option, 56 | /// The machine-readable token that uniquely identifies the style. 57 | pub id: Option, 58 | /// The human-readable name of the style. 59 | pub title: Option, 60 | } 61 | 62 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 63 | #[non_exhaustive] 64 | /// The categories the style belongs to; for purposes of indexing. 65 | pub enum Category { 66 | #[serde(rename = "biology")] 67 | Biology, 68 | #[serde(rename = "science")] 69 | Science, 70 | #[serde(rename = "social science")] 71 | SocialScience, 72 | } 73 | -------------------------------------------------------------------------------- /processor/examples/chicago-ad-experiment.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | info: 3 | title: Chicago 17, author-date 4 | description: | 5 | How simple can we make a Chicago author-date style? 6 | 7 | The goal here is to add: 8 | 9 | 1. configurable conditional logic without adding it to templates 10 | 2. presets in key places 11 | options: 12 | processing: author-date # preset for sorting, grouping, disambiguation 13 | # titles are unique in that their rendering is dependent, more so than other content, on their type 14 | # this is a very common pattern, where component titles (articles, chapters, etc) are quoted, but 15 | # titles otherwise render in italics 16 | titles: 17 | match: 18 | - class: component # chapter and article titles render the same 19 | style: [quote] 20 | default: 21 | style: [emph] 22 | contributors: 23 | delimiter: ", " 24 | andAs: text, 25 | citation: short # preset 26 | bibliography: full-chicago # preset name formatting and role 27 | substitute: 28 | role: short # non-author roles need to be included in the bibliography, but formatted differently than otherwise 29 | items: 30 | # this is the default value, so not needed 31 | - editor 32 | - title 33 | - translator 34 | dates: long 35 | numbers: 36 | label: contextual # Chicago 15.47-8 37 | citation: # this should allow presets; "citation-author-date-chicago" 38 | integral: 39 | # Doe (2020, 2021), Jones (2019) and Smtih (2021) argued X. 40 | author: # since we have author and substitution in the core, am less concerned about this 41 | delimiter: ", " 42 | andAs: text 43 | reference: 44 | delimiter: ", " 45 | items: 46 | - contributor: author 47 | - wrap: parentheses 48 | items: 49 | - date: issued 50 | form: year 51 | - prefix: ", " 52 | locators: true 53 | nonIntegral: 54 | author: 55 | delimiter: "; " 56 | wrap: parentheses 57 | items: 58 | - contributor: author 59 | - date: issued 60 | form: year 61 | - prefix: ", " 62 | locators: true 63 | bibliography: 64 | delimiter: ". " 65 | items: 66 | - contributor: author 67 | - date: issued 68 | form: year 69 | wrap: parentheses 70 | - title: title-part 71 | type: [chapter] 72 | style: [emph] 73 | - title: title-part # default, but how to know not to print if the above? 74 | style: [quote] 75 | - title: parent-monograph 76 | prefix: In 77 | style: [emph] 78 | - title: parent-serial 79 | style: [quote] 80 | -------------------------------------------------------------------------------- /csln/src/citation/mod.rs: -------------------------------------------------------------------------------- 1 | use schemars::JsonSchema; 2 | use serde::{Deserialize, Serialize}; 3 | 4 | pub type Citations = Vec; 5 | 6 | /// A vector of Citation objects. 7 | #[derive(Debug, Default, Serialize, Deserialize, JsonSchema)] 8 | pub struct CitationList(pub Vec); 9 | 10 | /* data Citation a = 11 | Citation { citationId :: Maybe Text 12 | , citationNoteNumber :: Maybe Int 13 | , citationItems :: [CitationItem a] } 14 | 15 | data CitationItem a = 16 | CitationItem 17 | { citationItemId :: ItemId 18 | , citationItemLabel :: Maybe Text 19 | , citationItemLocator :: Maybe Text 20 | , citationItemType :: CitationItemType 21 | , citationItemPrefix :: Maybe a 22 | , citationItemSuffix :: Maybe a 23 | , citationItemData :: Maybe (Reference a) 24 | } */ 25 | 26 | #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] 27 | pub struct Citation { 28 | pub note_number: Option, 29 | pub id: Option, 30 | /// Local citation rendering option; aka command or style. 31 | /// These are more general than author-date styles, and can apply to any citation style. 32 | pub mode: CitationModeType, 33 | /// The string that prefaces a list of citation references. 34 | pub prefix: Option, 35 | /// A vector of CitationItem objects. 36 | pub citation_items: Vec, 37 | /// A string that follows a list of qcitation references. 38 | pub suffix: Option, 39 | } 40 | 41 | #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] 42 | #[serde(rename_all = "kebab-case")] 43 | pub enum CitationModeType { 44 | /// Places the author inline in the text; also known as "narrative" or "in text" citations. 45 | Integral, 46 | /// Places the author in the citation and/or bibliography or reference entry. 47 | #[default] 48 | NonIntegral, 49 | } 50 | 51 | #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] 52 | #[serde(rename_all = "camelCase")] 53 | pub struct CitationItem { 54 | pub label: Option, 55 | /// A string that prefaces the citation reference. 56 | pub prefix: Option, 57 | /// The unique identifier token for the citation reference. 58 | pub ref_id: String, 59 | /// An array of locator key-values and/or strings. 60 | pub suffix: Option>, 61 | } 62 | 63 | #[allow(clippy::large_enum_variant)] // REVIEW is this a problem? 64 | /// A key-value object, or a string. 65 | #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] 66 | #[serde(untagged)] 67 | pub enum Locator { 68 | KeyValue(LocatorKeyValue), 69 | String(String), 70 | } 71 | 72 | pub type LocatorKeyValue = (LocatorTerm, String); 73 | 74 | #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] 75 | #[serde(rename_all = "camelCase")] 76 | pub enum LocatorTerm { 77 | Book, 78 | Chapter, 79 | Column, 80 | Figure, 81 | Folio, 82 | Line, 83 | Note, 84 | Number, 85 | Opus, 86 | #[default] 87 | Page, 88 | Paragraph, 89 | Part, 90 | Section, 91 | SubVerbo, 92 | Verse, 93 | Volume, 94 | } 95 | -------------------------------------------------------------------------------- /processor/examples/chicago.bib.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | # some exmples from Chicago 3 | biss: 4 | type: book 5 | author: 6 | family: Bissell 7 | given: Tom 8 | issued: "2011" 9 | title: 10 | main: Extra Lives 11 | sub: Why Video Games Matter 12 | publisher: 13 | location: New York 14 | name: Vintage Books 15 | hutt: 16 | type: chapter 17 | issued: "2011" 18 | author: 19 | family: Hutter 20 | given: Michael 21 | title: 22 | main: Infinite Surprises 23 | sub: Value in the Creative Industries 24 | parent: 25 | type: edited-book 26 | issued: "2011" # currerntly required in both places 27 | title: 28 | main: The Worth of Goods 29 | sub: Valuation and Pricing in the Economy 30 | editor: 31 | - family: Beckert 32 | given: Jens 33 | - family: Aspers 34 | given: Patrick 35 | publisher: 36 | location: New York 37 | name: Oxford University Press 38 | pages: 201-220 39 | lamp: 40 | type: article 41 | author: 42 | - family: Lampel 43 | given: Joseph 44 | - family: Lant 45 | given: Theresa 46 | - family: Shamsie 47 | given: Jamal 48 | issued: "2000" 49 | title: 50 | main: Balancing Act 51 | sub: Learning from Organizing Practices in Cultural Industries 52 | parent: 53 | type: academic-journal 54 | title: Organization Science 55 | volume: 11 56 | issue: 3 57 | pages: 263-269 58 | daum: 59 | type: edited-book 60 | editor: 61 | family: Daum 62 | given: Meghan 63 | issued: '2015' 64 | title: 65 | main: Selfish, Shallow, and Self-Absorbed 66 | sub: Sixteen Writers on the Decision Not to Have Kids 67 | publisher: 68 | name: Picador 69 | location: New York 70 | liu: 71 | type: article 72 | author: 73 | family: Liu 74 | given: Jui-Ch’i 75 | issued: '2015-24' 76 | title: 77 | main: Beholding the Feminine Sublime 78 | sub: Lee Miller’s War Photography 79 | parent: 80 | title: Signs 81 | type: academic-journal 82 | volume: 40 83 | issue: 2 # printed as 'no. 2'; not sure why 84 | pages: '308-19' 85 | doi: 10.1086/678242 86 | gund: 87 | # 15.48 exception: 88 | type: article 89 | author: 90 | - family: Gunderson 91 | given: Alex R 92 | - family: Leal 93 | given: Manuel 94 | issued: '2015-05' 95 | title: Patterns of Thermal Constraint on Ectotherm Activity 96 | parent: 97 | type: academic-journal 98 | title: American Naturalist 99 | issue: 185 # no volume, so preface with label to disambiguate 100 | pages: 653–64 101 | doi: 10.1086/680849 102 | glass: 103 | type: article 104 | author: 105 | - family: Glass 106 | given: Jennifer 107 | - family: Levchak 108 | given: Philip 109 | issued: '2014' 110 | title: 111 | main: Red States, Blue States, and Divorce 112 | sub: Understanding the Impact of Conservative Protestantism on Regional Variation in Divorce Rates 113 | parent: 114 | type: academic-journal 115 | title: American Journal of Sociology 116 | volume: 119 117 | issue: 4 118 | pages: 1002–46 119 | doi: 10.1086/674703 120 | meyer: 121 | # 15.47 exception (only an issue number, no volume): 122 | type: article 123 | author: 124 | family: Meyerovitch 125 | given: Eva 126 | issued: '1959' 127 | title: The Gnostic Manuscripts of Upper Egypt 128 | parent: 129 | type: academic-journal 130 | title: Diogenes 131 | issue: 25 132 | pages: 84–117 133 | 134 | -------------------------------------------------------------------------------- /processor/tests/processor_test.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | use anyhow::Context; 4 | use csln::citation::{Citation, CitationItem, Citations}; 5 | use csln::from_file; 6 | 7 | #[allow(dead_code)] 8 | // FIXME why these warnings? 9 | struct TestFixture { 10 | style: csln::style::Style, 11 | locale: csln::style::locale::Locale, 12 | bibliography: csln::bibliography::InputBibliography, 13 | citations: Vec, 14 | processor: csln_processor::Processor, 15 | } 16 | 17 | fn setup() -> TestFixture { 18 | let style: csln::style::Style = from_file("examples/style.csl.yaml").expect("Failed to load style file"); 19 | let locale: csln::style::locale::Locale = from_file("locales/locale-en.yaml").expect("Failed to load locale file"); 20 | let bibliography: csln::bibliography::InputBibliography = from_file("examples/ex1.bib.yaml").expect("Failed to load bibliography file"); 21 | let citations: Citations = 22 | from_file("examples/citation.yaml").context("Citation file?").unwrap_or_default(); 23 | let processor = 24 | csln_processor::Processor::new(style.clone(), bibliography.clone(), citations.clone(), locale.clone()); 25 | 26 | TestFixture { style, locale, bibliography, citations, processor } 27 | } 28 | 29 | #[test] 30 | fn gets_references() { 31 | let fixture = setup(); 32 | assert_eq!(fixture.processor.get_references().len(), 36); 33 | assert!(fixture.processor.get_reference("doe1").is_ok()); 34 | assert_eq!( 35 | fixture.processor.get_reference("doe1").unwrap().title(), 36 | Some(csln::bibliography::reference::Title::Single("Title 2".to_string())) 37 | ); 38 | assert!(fixture.processor.get_proc_hints().contains_key("doe1")); 39 | } 40 | 41 | #[test] 42 | fn sorts_references() { 43 | let fixture = setup(); 44 | let refs = fixture.processor.get_references(); 45 | let sorted_refs = fixture.processor.sort_references(refs); 46 | assert_eq!(sorted_refs.len(), 36); 47 | assert_eq!(sorted_refs.last().unwrap().title().unwrap().to_string(), "Title 4"); 48 | } 49 | 50 | #[test] 51 | fn process_citation_item() { 52 | // TODO make it for citations as a whole, and confirm no empty ones 53 | let fixture = setup(); 54 | let citation_item = CitationItem { 55 | ref_id: "doe1".to_string(), 56 | label: None, 57 | prefix: Some("Prefix".to_string()), 58 | suffix: None, 59 | }; 60 | let result = fixture.processor.process_citation_item(&citation_item); 61 | // confirm 62 | // assert_eq!(fixture.processor.get_reference("doe1"), "doe1".to_string()); 63 | assert_eq!(result.unwrap()[0].values.value.to_string(), "Doe, Jane".to_string()); 64 | } 65 | 66 | #[test] 67 | fn derives_proc_hints() { 68 | let fixture = setup(); 69 | let proc_hints = fixture.processor.get_proc_hints(); 70 | assert_eq!(proc_hints["doe7"].group_index, 1); 71 | assert_eq!(proc_hints["doe7"].group_length, 1); 72 | } 73 | 74 | #[test] 75 | fn loads_and_parses_locale_file() { 76 | let fixture = setup(); 77 | assert_eq!(fixture.locale.dates.months.long[0], "January"); 78 | assert_eq!(fixture.locale.dates.months.long[11], "December"); 79 | assert_eq!(fixture.locale.dates.months.short[0], "Jan"); 80 | assert_eq!(fixture.locale.dates.months.short[11], "Dec"); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /processor/examples/ex1.bib.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | un: 3 | type: book 4 | title: Title 4 5 | author: 6 | name: United Nations 7 | issued: '2020' 8 | smith1: 9 | type: book 10 | title: Title 3 11 | author: 12 | family: Smith 13 | given: John 14 | issued: '2023-10' 15 | doe1: 16 | type: book 17 | title: Title 2 18 | author: 19 | family: Doe 20 | given: Jane 21 | issued: '2023-10' 22 | doe2: 23 | type: book 24 | title: Title 1 25 | author: 26 | family: Doe 27 | given: Jane 28 | issued: '2020' 29 | doe3: 30 | type: article 31 | title: Title 0 32 | author: 33 | family: Doe 34 | given: Jane 35 | issued: '2020' 36 | parent: 37 | type: magazine 38 | title: Pub title 39 | brown1: 40 | type: book 41 | title: Title 5 42 | author: 43 | name: Brown, John 44 | issued: '2021' 45 | lee1: 46 | type: book 47 | title: Title 6 48 | author: 49 | family: Lee 50 | given: Sarah 51 | issued: '2022' 52 | lee2: 53 | type: document 54 | title: Title 7 55 | author: 56 | family: Lee 57 | given: Sarah 58 | issued: '2022' 59 | miller1: 60 | type: book 61 | title: Title 8 62 | author: 63 | family: Miller 64 | given: David 65 | issued: '2018' 66 | miller2: 67 | type: document 68 | title: Title 9 69 | author: 70 | family: Miller 71 | given: David 72 | issued: '2018' 73 | jones1: 74 | type: book 75 | title: Title 10 76 | author: 77 | family: Jones 78 | given: Michael 79 | issued: '2022' 80 | jones2: 81 | type: book 82 | title: Title 11 83 | author: 84 | family: Jones 85 | given: Michael 86 | issued: '2022' 87 | smith2: 88 | type: book 89 | title: Title 12 90 | author: 91 | family: Smith 92 | given: John 93 | issued: '2020' 94 | smith3: 95 | type: document 96 | title: Title 13 97 | author: 98 | family: Smith 99 | given: John 100 | issued: '2020' 101 | miller3: 102 | type: book 103 | title: Title 14 104 | author: 105 | family: Miller 106 | given: Sarah 107 | issued: '2017' 108 | miller4: 109 | type: article 110 | title: Title 15 111 | author: 112 | family: Miller 113 | given: Sarah 114 | issued: '2018' 115 | parent: 116 | type: academic-journal 117 | title: XYZ Journal 118 | jones3: 119 | type: book 120 | title: Title 16 121 | author: 122 | name: Jones, David 123 | issued: '2019' 124 | jones4: 125 | type: book 126 | title: Title 17 127 | author: 128 | name: Jones, David 129 | issued: '2019' 130 | brown2: 131 | type: book 132 | title: Title 18 133 | author: 134 | name: Brown, Sarah 135 | issued: '2019' 136 | brown3: 137 | type: document 138 | title: Title 19 139 | author: 140 | name: Brown, Sarah 141 | issued: '2019' 142 | lee3: 143 | type: book 144 | title: Title 20 145 | author: 146 | family: Lee 147 | given: David 148 | issued: '2006' 149 | lee4: 150 | type: document 151 | title: Title 21 152 | author: 153 | family: Lee 154 | given: David 155 | issued: '2006' 156 | doe4: 157 | type: book 158 | title: Title 22 159 | author: 160 | family: Doe 161 | given: John 162 | issued: '2013' 163 | doe5: 164 | type: book 165 | title: Title 23 166 | author: 167 | family: Doe 168 | given: John 169 | issued: '2013' 170 | smith4: 171 | type: book 172 | title: Title 24 173 | author: 174 | family: Smith 175 | given: Sarah 176 | issued: '2014' 177 | smith5: 178 | type: book 179 | title: Title 25 180 | author: 181 | family: Smith 182 | given: Sarah 183 | issued: '2015' 184 | miller5: 185 | type: book 186 | title: Title 26 187 | author: 188 | family: Miller 189 | given: John 190 | issued: '2016' 191 | miller6: 192 | type: document 193 | title: Title 27 194 | author: 195 | family: Miller 196 | given: John 197 | issued: '2032' 198 | jones5: 199 | type: book 200 | title: Title 28 201 | # for single author pieces, there's no point in a list 202 | # but if we need structured data, as we do with Western names,let's structure it 203 | author: 204 | family: Doe 205 | given: Jane 206 | issued: '2018' 207 | jones6: 208 | type: book 209 | title: Title 29 210 | author: 211 | family: Jones 212 | given: Sarah 213 | issued: '2018' 214 | brown4: 215 | type: book 216 | title: Title 30 217 | author: 218 | family: Brown 219 | given: David 220 | issued: '2021' 221 | brown5: 222 | type: document 223 | title: Title 31 224 | # here we need a list 225 | author: 226 | - family: Brown 227 | given: David 228 | - family: Lee 229 | given: Jane 230 | issued: '2021' 231 | lee5: 232 | type: book 233 | title: Title 32 234 | author: 235 | name: Lee, John 236 | issued: '2022' 237 | lee6: 238 | type: document 239 | title: Title 33 240 | author: 241 | family: Lee 242 | given: John 243 | issued: '2022' 244 | doe6: 245 | type: book 246 | title: Title 34 247 | author: 248 | family: Doe 249 | given: Sarah 250 | issued: 'non-EDTF date' 251 | doe7: 252 | type: document 253 | title: Title 35 254 | author: 255 | family: Doe 256 | given: Sarah 257 | issued: '2009' 258 | -------------------------------------------------------------------------------- /csln/src/style/locale.rs: -------------------------------------------------------------------------------- 1 | use schemars::JsonSchema; 2 | use serde::{Deserialize, Serialize}; 3 | use std::collections::HashMap; 4 | //use unic_langid::LanguageIdentifier; 5 | 6 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 7 | pub struct Locale { 8 | pub locale: String, 9 | // pub options: LocaleOptions, 10 | pub dates: DateTerms, 11 | pub roles: HashMap, 12 | //pub contributors: ContributorTerms, 13 | pub terms: Terms, // TODO 14 | } 15 | 16 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 17 | #[serde(rename_all = "kebab-case")] 18 | pub struct Terms { 19 | pub and: Option, 20 | pub and_symbol: Option, 21 | pub and_others: Option, 22 | pub anonymous: SimpleTerm, 23 | pub at: Option, 24 | pub accessed: Option, 25 | pub available_at: Option, 26 | pub by: Option, 27 | pub circa: SimpleTerm, 28 | pub et_al: Option, 29 | pub from: Option, 30 | pub ibid: Option, 31 | } 32 | 33 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 34 | pub struct AndAs { 35 | pub symbol: String, 36 | pub text: String, 37 | } 38 | 39 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 40 | pub struct SimpleTerm { 41 | /// The long form of the term. 42 | pub long: String, 43 | /// The short form of the term. 44 | pub short: String, 45 | } 46 | 47 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 48 | pub struct ContributorTerm { 49 | /// The long form of the term. 50 | pub singular: SimpleTerm, // REVIEW maybe swap this? 51 | /// The short form of the term. 52 | pub plural: SimpleTerm, 53 | /// The verb form of the term. 54 | pub verb: SimpleTerm, 55 | } 56 | 57 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 58 | #[serde(rename_all = "camelCase")] 59 | pub struct LocaleOptions { 60 | pub punctuation_in_quotes: bool, 61 | } 62 | 63 | /// A struct representing date terms. 64 | /// 65 | /// # Fields 66 | /// 67 | /// * `month` - vectors containing the full and abbreviated month names. 68 | /// * `seasons` - a map of seasons to their names. 69 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 70 | pub struct DateTerms { 71 | pub months: MonthNames, 72 | /// The ordered list of seasonal names, starting with Spring. 73 | /// The list must contain exactly four elements. 74 | // Note: this corresponds to EDTF level-1; level-2 has many more options. 75 | #[validate(range(min = 4, max = 4))] 76 | pub seasons: Vec, 77 | } 78 | 79 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 80 | pub struct MonthNames { 81 | /// The ordered list of full month names. 82 | /// The list must contain exactly 12 elements. 83 | #[validate(range(min = 12, max = 12))] 84 | pub long: MonthList, 85 | /// The ordered list of abbreviated month names. 86 | /// The list must contain exactly 12 elements. 87 | #[validate(range(min = 12, max = 12))] 88 | pub short: MonthList, 89 | } 90 | 91 | pub type MonthList = Vec; 92 | 93 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 94 | #[serde(rename_all = "kebab-case")] 95 | pub enum LocalizedTermNameLocator { 96 | Act, 97 | 98 | Appendix, 99 | ArticleLocator, 100 | 101 | Book, 102 | 103 | Canon, 104 | 105 | Chapter, 106 | 107 | Column, 108 | 109 | Elocation, 110 | 111 | Equation, 112 | 113 | Figure, 114 | 115 | Folio, 116 | 117 | Line, 118 | 119 | Note, 120 | 121 | Opus, 122 | 123 | Paragraph, 124 | 125 | Rule, 126 | 127 | Scene, 128 | 129 | SubVerbo, 130 | 131 | Table, 132 | 133 | Timestamp, 134 | 135 | TitleLocator, 136 | 137 | Verse, 138 | } 139 | 140 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 141 | pub enum LocalizedTermNameLocatorNumber { 142 | Issue, 143 | 144 | Page, 145 | 146 | Part, 147 | 148 | Section, 149 | 150 | Supplement, 151 | 152 | Version, 153 | 154 | Volume, 155 | } 156 | 157 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 158 | #[serde(rename_all = "kebab-case")] 159 | pub enum LocalizedTermNameMisc { 160 | Accessed, 161 | 162 | Ad, 163 | AdvanceOnlinePublication, 164 | 165 | Album, 166 | 167 | And, 168 | 169 | AndOthers, 170 | 171 | Anonymous, 172 | 173 | At, 174 | 175 | AudioRecording, 176 | 177 | AvailableAt, 178 | 179 | Bc, 180 | 181 | Bce, 182 | 183 | By, 184 | 185 | Ce, 186 | 187 | Circa, 188 | 189 | Cited, 190 | 191 | EtAl, 192 | 193 | Film, 194 | 195 | Forthcoming, 196 | 197 | From, 198 | 199 | Henceforth, 200 | 201 | Ibid, 202 | 203 | In, 204 | 205 | InPress, 206 | 207 | Internet, 208 | 209 | Interview, 210 | 211 | Letter, 212 | 213 | LocCit, 214 | 215 | NoDate, 216 | 217 | NoPlace, 218 | 219 | NoPublisher, 220 | 221 | On, 222 | 223 | Online, 224 | 225 | OpCit, 226 | 227 | OriginalWorkPublished, 228 | 229 | PersonalCommunication, 230 | 231 | Podcast, 232 | 233 | PodcastEpisode, 234 | 235 | Preprint, 236 | 237 | PresentedAt, 238 | 239 | RadioBroadcast, 240 | 241 | RadioSeries, 242 | 243 | RadioSeriesEpisode, 244 | 245 | Reference, 246 | 247 | Retrieved, 248 | 249 | ReviewOf, 250 | 251 | Scale, 252 | 253 | SpecialIssue, 254 | 255 | SpecialSection, 256 | 257 | TelevisionBroadcast, 258 | 259 | TelevisionSeries, 260 | 261 | TelevisionSeriesEpisode, 262 | 263 | Video, 264 | 265 | WorkingPaper, 266 | } 267 | -------------------------------------------------------------------------------- /csln/src/style/template.rs: -------------------------------------------------------------------------------- 1 | use schemars::JsonSchema; 2 | use serde::{Deserialize, Serialize}; 3 | 4 | /// Rendering instructions for a template component. 5 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 6 | pub struct Rendering { 7 | pub emph: Option, 8 | pub quote: Option, 9 | pub strong: Option, 10 | pub prefix: Option, 11 | pub suffix: Option, 12 | pub wrap: Option, 13 | } 14 | 15 | /// The punctuation to wrap a template component in. 16 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 17 | #[serde(rename_all = "camelCase")] 18 | pub enum WrapPunctuation { 19 | Parentheses, 20 | Brackets, 21 | #[default] 22 | None, 23 | } 24 | 25 | /// The Template component model. Each item is for a specific datatype. 26 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 27 | #[serde(untagged)] 28 | #[non_exhaustive] 29 | pub enum TemplateComponent { 30 | Contributor(TemplateContributor), 31 | Date(TemplateDate), 32 | List(TemplateList), 33 | Title(TemplateTitle), 34 | Number(TemplateNumber), 35 | SimpleString(TemplateSimpleString), 36 | } 37 | 38 | impl TemplateComponent { 39 | pub fn rendering(&self) -> Option { 40 | match self { 41 | TemplateComponent::Contributor(c) => c.rendering.clone(), 42 | TemplateComponent::Date(d) => d.rendering.clone(), 43 | TemplateComponent::List(_l) => None, 44 | TemplateComponent::Title(t) => t.rendering.clone(), 45 | TemplateComponent::Number(n) => n.rendering.clone(), 46 | TemplateComponent::SimpleString(s) => s.rendering.clone(), 47 | } 48 | } 49 | 50 | // TODO do I need this? 51 | pub fn is_author(&self) -> bool { 52 | match self { 53 | TemplateComponent::Contributor(c) => c.contributor == ContributorRole::Author, 54 | _ => false, 55 | } 56 | } 57 | } 58 | 59 | /// A simple string component, to render a string variable. 60 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 61 | pub struct TemplateSimpleString { 62 | pub variable: Variables, 63 | pub rendering: Option, 64 | } 65 | 66 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 67 | #[serde(rename_all = "lowercase")] 68 | pub enum Variables { 69 | // TODO: add more variables 70 | Doi, 71 | Isbn, 72 | Issn, 73 | } 74 | 75 | /// A number component, to render a number. 76 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 77 | pub struct TemplateNumber { 78 | pub number: Numbers, 79 | pub form: Option, 80 | pub rendering: Option, 81 | } 82 | 83 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 84 | #[serde(rename_all = "lowercase")] 85 | pub enum Numbers { 86 | Volume, 87 | Issue, 88 | Pages, 89 | } 90 | 91 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 92 | #[serde(rename_all = "lowercase")] 93 | pub enum NumberForm { 94 | #[default] 95 | Numeric, 96 | Ordinal, 97 | } 98 | 99 | /// To render is a list of more than one item; primarily to enable use of a delimiter to join the items. 100 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 101 | pub struct TemplateList { 102 | pub delimiter: Option, 103 | pub prefix: Option, 104 | pub suffix: Option, 105 | pub wrap: Option, 106 | pub items: Vec, 107 | } 108 | 109 | /// The punctuation to use as a delimiter between items in a list. 110 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 111 | #[serde(rename_all = "kebab-case")] 112 | pub enum DelimiterPunctuation { 113 | Comma, 114 | Semicolon, 115 | Period, 116 | Colon, 117 | Ampersand, 118 | VerticalLine, 119 | Slash, 120 | Hyphen, 121 | Space, 122 | None, 123 | } 124 | 125 | /// A contributor component, to render a list of contributors. 126 | // TODO incomplete 127 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 128 | pub struct TemplateContributor { 129 | pub contributor: ContributorRole, 130 | pub form: ContributorForm, 131 | pub rendering: Option, 132 | } 133 | 134 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 135 | #[serde(rename_all = "camelCase")] 136 | pub enum ContributorForm { 137 | Long, 138 | Short, 139 | Verb, 140 | VerbShort, 141 | } 142 | 143 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq, Eq, Hash)] 144 | #[serde(rename_all = "camelCase")] 145 | pub enum ContributorRole { 146 | Author, 147 | Editor, 148 | Translator, 149 | Director, 150 | Publisher, 151 | Recipient, 152 | Interviewer, 153 | Interviewee, 154 | Inventor, 155 | Counsel, 156 | Composer, 157 | } 158 | 159 | /// A date component, to render a date. 160 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 161 | pub struct TemplateDate { 162 | pub date: Dates, 163 | pub form: DateForm, 164 | pub rendering: Option, 165 | } 166 | 167 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 168 | #[serde(rename_all = "kebab-case")] 169 | pub enum Dates { 170 | Issued, 171 | Accessed, 172 | OriginalPublished, 173 | } 174 | 175 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 176 | #[serde(rename_all = "kebab-case")] 177 | pub enum DateForm { 178 | Year, 179 | YearMonth, 180 | Full, 181 | MonthDay, 182 | } 183 | 184 | /// A title component, to render a title. 185 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 186 | pub struct TemplateTitle { 187 | pub title: Titles, 188 | pub form: Option, 189 | pub rendering: Option, 190 | } 191 | 192 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 193 | #[serde(rename_all = "kebab-case")] 194 | #[non_exhaustive] 195 | pub enum Titles { 196 | /// The primary title for the cited work. 197 | Primary, 198 | /// The title of a book or other monograph that the cited work is a part of. 199 | ParentMonograph, 200 | /// The titles of a periodical or other serial that the cited work is a part of. 201 | ParentSerial, 202 | } 203 | 204 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 205 | #[serde(rename_all = "camelCase")] 206 | pub enum TitleForm { 207 | Short, 208 | Long, 209 | } 210 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## [0.2.0](https://github.com/bdarcus/csln/compare/0.1.0...0.2.0) (2023-08-01) 4 | 5 | ### Added 6 | 7 | * basic conditional ([1ca55bb](https://github.com/bdarcus/csln/commit/1ca55bb)) 8 | * **bib:** identifiers ([664808c](https://github.com/bdarcus/csln/commit/664808c)) 9 | * **bib:** contributor and, et al ([452123c](https://github.com/bdarcus/csln/commit/452123c)) 10 | * **bib:** date methods ([42846fa](https://github.com/bdarcus/csln/commit/42846fa)) 11 | * **bib:** structured and multilingual titles ([eec9f89](https://github.com/bdarcus/csln/commit/eec9f89)) 12 | * **citation:** the model ([2274c3d](https://github.com/bdarcus/csln/commit/2274c3d)) 13 | * **cli:** clapify ([7a1bf74](https://github.com/bdarcus/csln/commit/7a1bf74)) 14 | * **proc:** add refs_to_string placeholder ([5a6c114](https://github.com/bdarcus/csln/commit/5a6c114)) 15 | * **proc:** titles renderin ([826a72a](https://github.com/bdarcus/csln/commit/826a72a)) 16 | * **proc:** numbers ([0361c5c](https://github.com/bdarcus/csln/commit/0361c5c)) 17 | * **proc:** publisher ([4b46098](https://github.com/bdarcus/csln/commit/4b46098)) 18 | * **proc:** template rendering ([c82aa8b](https://github.com/bdarcus/csln/commit/c82aa8b)) 19 | * **proc:** verb and standard role forms ([c98d368](https://github.com/bdarcus/csln/commit/c98d368)) 20 | * **proc:** author substitution ([a6bf2b8](https://github.com/bdarcus/csln/commit/a6bf2b8)) 21 | * **proc:** get_cited_references, etc ([0804344](https://github.com/bdarcus/csln/commit/0804344)) 22 | * **proc:** contributor roles ([57e87e9](https://github.com/bdarcus/csln/commit/57e87e9)) 23 | * **style:** Titles options ([4c951b3](https://github.com/bdarcus/csln/commit/4c951b3)) 24 | * **style:** simple string variables ([0ca7200](https://github.com/bdarcus/csln/commit/0ca7200)) 25 | * **style:** locale model, example ([c0d5c74](https://github.com/bdarcus/csln/commit/c0d5c74)) 26 | 27 | ### Fixed 28 | 29 | * **bib:** editor, reference component ([ea65bd9](https://github.com/bdarcus/csln/commit/ea65bd9)) 30 | * **bib:** import warning ([af5b71c](https://github.com/bdarcus/csln/commit/af5b71c)) 31 | * **proc:** clippy warnings ([1247955](https://github.com/bdarcus/csln/commit/1247955)) 32 | * **proc:** check config before adding year suffix ([530e1d2](https://github.com/bdarcus/csln/commit/530e1d2)) 33 | * **proc:** correct year suffix ([2c8f780](https://github.com/bdarcus/csln/commit/2c8f780)) 34 | * **proc:** sorting ([318aac9](https://github.com/bdarcus/csln/commit/318aac9)) 35 | * **style:** add quote, make fields public ([9d4c7bc](https://github.com/bdarcus/csln/commit/9d4c7bc)) 36 | * **style:** remove sort, group from top ([d85e1e0](https://github.com/bdarcus/csln/commit/d85e1e0)) 37 | 38 | ### Changed 39 | 40 | * add csln-types crate ([ef35de2](https://github.com/bdarcus/csln/commit/ef35de2)) 41 | * add csln-types crate ([8a2afde](https://github.com/bdarcus/csln/commit/8a2afde)) 42 | * option definitions ([f0cff31](https://github.com/bdarcus/csln/commit/f0cff31)) 43 | * comment out types ([cddf018](https://github.com/bdarcus/csln/commit/cddf018)) 44 | * move logic to InputReference, etc. ([a19dc30](https://github.com/bdarcus/csln/commit/a19dc30)) 45 | * types -> core ([b3ed80b](https://github.com/bdarcus/csln/commit/b3ed80b)) 46 | * **bib:** enrich contributor model ([5002757](https://github.com/bdarcus/csln/commit/5002757)) 47 | * **bib:** SimpleName, string -> struct ([6e02648](https://github.com/bdarcus/csln/commit/6e02648)) 48 | * **bib:** allow string subtitle ([9ec91f6](https://github.com/bdarcus/csln/commit/9ec91f6)) 49 | * **citation:** clean up, etc ([686646f](https://github.com/bdarcus/csln/commit/686646f)) 50 | * **proc:** consolidate Render traits ([01d7739](https://github.com/bdarcus/csln/commit/01d7739)) 51 | * **proc:** substitution, suppression ([90ba768](https://github.com/bdarcus/csln/commit/90ba768)) 52 | * **proc:** ProcTemplate from type to struct ([cb26c1c](https://github.com/bdarcus/csln/commit/cb26c1c)) 53 | * **proc:** remove string_for_key ([316c866](https://github.com/bdarcus/csln/commit/316c866)) 54 | * **proc:** add process_template method ([6e3992c](https://github.com/bdarcus/csln/commit/6e3992c)) 55 | * **style:** StyleTemplate* -> Template* ([488f755](https://github.com/bdarcus/csln/commit/488f755)) 56 | * **style:** disamb -> processing ([bed20c1](https://github.com/bdarcus/csln/commit/bed20c1)) 57 | * **style:** option adjustments, docs ([423a703](https://github.com/bdarcus/csln/commit/423a703)) 58 | * **style:** make contrib config optional ([f66c50e](https://github.com/bdarcus/csln/commit/f66c50e)) 59 | * **style:** title -> primary ([27cf738](https://github.com/bdarcus/csln/commit/27cf738)) 60 | * **style:** remove template conditional ([e9f6c75](https://github.com/bdarcus/csln/commit/e9f6c75)) 61 | * **types:** remove ([a22dae8](https://github.com/bdarcus/csln/commit/a22dae8)) 62 | 63 | 64 | 65 | ## 0.1.0 (2023-06-06) 66 | 67 | ### Added 68 | 69 | * **citation:** add the model ([7e586e3](https://github.com/bdarcus/csln/commit/7e586e3)) 70 | * **cli:** use render_references ([9368dc2](https://github.com/bdarcus/csln/commit/9368dc2)) 71 | * **proc:** options, dates ([4a2a813](https://github.com/bdarcus/csln/commit/4a2a813)) 72 | * **proc:** set disabm_condition ([438e484](https://github.com/bdarcus/csln/commit/438e484)) 73 | * **proc:** add start of disambiguation ([3b36cf5](https://github.com/bdarcus/csln/commit/3b36cf5)) 74 | * **proc:** render_references, render_renderence ([2d4f3f7](https://github.com/bdarcus/csln/commit/2d4f3f7)) 75 | * **proc:** grouping, etc. ([e9d8740](https://github.com/bdarcus/csln/commit/e9d8740)) 76 | 77 | ### Fixed 78 | 79 | * **proc:** suffix is a letter ([1650d36](https://github.com/bdarcus/csln/commit/1650d36)) 80 | * **proc:** missing id field ([eb068e4](https://github.com/bdarcus/csln/commit/eb068e4)) 81 | * **proc:** render_references return type ([ae4f13c](https://github.com/bdarcus/csln/commit/ae4f13c)) 82 | * **proc:** clippy warning ([31b855f](https://github.com/bdarcus/csln/commit/31b855f)) 83 | * **proc:** start at 1 for group index ([172e2f7](https://github.com/bdarcus/csln/commit/172e2f7)) 84 | * **proc:** sorting ([d840a3f](https://github.com/bdarcus/csln/commit/d840a3f)) 85 | * **test:** update ([bc87a59](https://github.com/bdarcus/csln/commit/bc87a59)) 86 | 87 | ### Changed 88 | 89 | * **bib:** use edtf for date parsing ([f73cb7c](https://github.com/bdarcus/csln/commit/f73cb7c)) 90 | * **proc:** move file loading to style, bib ([197fbee](https://github.com/bdarcus/csln/commit/197fbee)) 91 | * **proc:** more -> iter/map ([d0d5308](https://github.com/bdarcus/csln/commit/d0d5308)) 92 | * **proc:** switch to map, group_by ([228918c](https://github.com/bdarcus/csln/commit/228918c)) 93 | * **proc:** impl render traits ([7ad2c3a](https://github.com/bdarcus/csln/commit/7ad2c3a)) 94 | * **proc:** ProcTemplate/Component, docstrings ([b6d5504](https://github.com/bdarcus/csln/commit/b6d5504)) 95 | * **proc:** remove ProcReference ([04d37e7](https://github.com/bdarcus/csln/commit/04d37e7)) 96 | * **proc:** split proc hints ([12c60e5](https://github.com/bdarcus/csln/commit/12c60e5)) 97 | 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Vision 2 | 3 | At a high-level, the vision of the project is to provide a simpler, easier-to-extend, and more featureful successor to CSL, with a model defined in Rust code, and JSON schemas generated from it. 4 | 5 | More specifically, the idea is to: 6 | 7 | 1. Adapt what we've learned in almost 20 years of experience with [CSL 1.0][CSL] to modern programming idioms and formats. 8 | 2. Simplify the template part of the language, and put more, and extensible, logic in option groups, so it's easier to work with for users, style editors, and developers alike. 9 | 3. Add new features while we're at it, like multi-lingual support, advanced dates and times, narrative citations, and so forth. 10 | 4. Align code and schemas by generating the latter from the former, and so also provide a common meeting point for developers and domain experts. 11 | 12 | More concretely, the goal is a suite of models, libraries and tools that make extremely performant advanced citation and bibliography processing available everywhere: 13 | 14 | - desktop and web 15 | - batch-processing for formats like pandoc markdown, djot, LaTeX, and org-mode 16 | - interactive real-time processing for GUI contexts like Zotero 17 | - easy-to-use style creation wizards, both command-line and web 18 | 19 | ## Principles 20 | 21 | For the `Style` model: 22 | 23 | 1. As with [CSL 1.0][CSL], styling is agnostic of input and output formats, including whether one is using an author-date citation style, numeric, or note-based. 24 | 2. Keep the template language as simple as possible, in the hopes we can keep it stable going forward, while still enabling innnovation. In a GUI, behavior (sorting, substitution, etc) would be configured in those options, and not in the templates. 25 | 3. Add new functionality primarily via option groups. 26 | 27 | For the `InputReference` and `Citation` models: 28 | 29 | 3. No string-parsing, with the sole exception of the [EDTF date format][EDTF], which is now ISO-standardized as an extension profile of ISO 8601, with well-defined parsing rules, and parsing libraries available in multiple languages. 30 | 4. Provide structure where needed, but offer alternatives where not. EDTF is available for diverse date-time encoding, but dates fields will fallback to a plain string. Likewise, the `Contributor` model offers similar flexibility, and power where needed. 31 | 32 | ## Caveats and Status 33 | 34 | This is not particularly close to ready for actual use, and needs more development, testing, and input. 35 | 36 | A very high-level summary of where this at ATM: 37 | 38 | - complete-ish draft models for bibliography, citations, styles, locales 39 | - YAML and JSON serialization and deserialization of these models, and a `csln-schemas` binary that will create JSON schemas to validate them 40 | - a processor which can create formatted string output using the above inputs, but which is designed for pluggable renderers (see [#105](https://github.com/bdarcus/csln/issues/105)); includes basic author substitution, basic EDTF date parsing and formatting, and a few other things I'm likely forgetting 41 | - a `csln` CLI that uses the above; it's Rust, so a single binary, and very fast. 42 | 43 | ## The model 44 | 45 | ### Influences 46 | 47 | 1. The [CSL 1.0 specification][CSL-spec] [options][CSL-options], and its template language (aka [layout][CSL-templates] and [rendering elements][CSL-render]), most notably from names, dates, and other formatting. 48 | 2. Patterns observed in the [CSL 1.0 styles repository][CSL-styles]. 49 | 3. The [BibLaTeX preamble][BLTX] options. 50 | 4. The [Typst Hayagriva][haya] project has some interesting details; particularly its input data model, and its [selector macro][sel]. 51 | 52 | ### Comparison to CSL 1.0 and BibLaTeX 53 | 54 | To understand the difference between this model and [CSL 1.0][CSL], look at [style::options][CSLNO]. 55 | There, you will note configuration options for many details that in CSL 1.0 are configured within the template language: 56 | 57 | - dates 58 | - contributors 59 | - substitution 60 | 61 | Plus, I've added `localization` support as such a configuration option group, with the idea it can be more easily-expanded there, than by burdening the template language with those details. 62 | 63 | In that sense, this design is closer to [BibLaTeX][BLTX], which has a very long list of flat options that handle much of the configuration. 64 | Like that project, here we standardize on [EDTF dates][EDTF]. 65 | 66 | On the citation end, CSL in general has been most akin to the BibLaTeX `autocite` commands rather than the lower-level ones. This is to ensure documents are portable across radically-different output styles. But this model adds a basic distinction between "integral" (aka narrative or text) citations, and "non-integral." 67 | 68 | ## Project Organization 69 | 70 | I've separated the code into discrete crates, with the intention to ultimately publish them. 71 | 72 | I'm hoping to have demonstrated enough so far that this is a promising direction for the future of CSL, at least on the technical end, that folks might be willing to help build this out. 73 | Ideally, I want to develop this project sufficiently to move it to the [GitHub CSL org][CSLO] for further development and future maintenance. 74 | Doing so, however, will require sorting out details of how that process is managed and funded going forward. 75 | 76 | ## Contributing 77 | 78 | I would _love_ to have help on this, both because I'm an amateur programmer and a Rust newbie, and because the vision I am sketching out here will take a lot of work to realize. 79 | 80 | ### Getting Started 81 | 82 | To build and test the project: 83 | 84 | ```bash 85 | # Clone the repository 86 | git clone https://github.com/bdarcus/csln.git 87 | cd csln 88 | 89 | # Build the project 90 | cargo build 91 | 92 | # Run tests 93 | cargo test 94 | 95 | # Run clippy for code quality checks 96 | cargo clippy --all-targets --all-features 97 | 98 | # Format code 99 | cargo fmt 100 | 101 | # Generate JSON schemas 102 | cargo run --bin csln-schemas 103 | ``` 104 | 105 | ### Project Structure 106 | 107 | - `csln/` - Core library with data models for styles, bibliography, and citations 108 | - `cli/` - Command-line interface for processing citations 109 | - `processor/` - Citation and bibliography processing engine 110 | 111 | ### How to Help 112 | 113 | Please contact me via discussions or the issue tracker, or by email, if you'd like to contribute. 114 | 115 | I licensed the code here under the same terms as [citeproc-rs][CSLRS], in case code might be shared between them. 116 | I also understand the Mozilla 2.0 license is compatible with Apache. 117 | 118 | A note on citeproc-rs: 119 | 120 | In reviewing the code, it strikes me pieces of it obviously complement this code base. 121 | In particular, it has been optimized for the Zotero use-case, where it provides real-time formatting, while I have focused of the batch-processing case. 122 | 123 | [CSL]: https://citationstyles.org/ 124 | [CSLNJS]: https://github.com/bdarcus/csl-next 125 | [CSLNO]: https://github.com/bdarcus/csln/blob/main/csln/src/style/options.rs 126 | [CSLRS]: https://github.com/zotero/citeproc-rs 127 | [CSLO]: https://github.com/citation-style-language 128 | [CSL-spec]: https://docs.citationstyles.org/en/stable/specification.html 129 | [CSL-styles]: https://github.com/citation-style-language/styles 130 | [CSL-macros]: https://docs.citationstyles.org/en/stable/specification.html#macros 131 | [CSL-templates]: https://docs.citationstyles.org/en/stable/specification.html#layout-1 132 | [CSL-render]: https://docs.citationstyles.org/en/stable/specification.html#rendering-elements 133 | [CSL-options]: https://docs.citationstyles.org/en/stable/specification.html#options 134 | [BLTX]: https://github.com/plk/biblatex 135 | [EDTF]: https://www.loc.gov/standards/datetime/ 136 | [haya]: https://github.com/typst/hayagriva 137 | [sel]: https://github.com/typst/hayagriva/blob/main/docs/selectors.md 138 | -------------------------------------------------------------------------------- /csln/src/style/options.rs: -------------------------------------------------------------------------------- 1 | /* 2 | SPDX-License-Identifier: MPL-2.0 3 | SPDX-FileCopyrightText: © 2023 Bruce D'Arcus 4 | */ 5 | 6 | //! This submodule defines the configuration groups and options available in CSLN styles. 7 | //! 8 | //! The details are adapted from: 9 | //! 10 | //! 1. The [CSL 1.0 specification][CSL-spec] [options][CSL-options], and its template language (aka [layout][CSL-templates] and [rendering elements][CSL-render]), most notably from names, dates, and other formatting. 11 | //! 2. Patterns observed in the [CSL 1.0 styles repository][CSL-styles]. 12 | //! 3. The [BibLaTeX preamble][BLTX] options. 13 | //! 14 | //! In this model, much more logic is configured in these options, and the `template` submodule is comparatively simple. 15 | //! The intent is to make it easier to write and maintain styles, as well as softtware that uses them. 16 | //! 17 | //! ## Style Options 18 | //! 19 | //! The [`Config`] struct defines the configuration groups and options available in CSLN styles. 20 | //! 21 | //! ## Status 22 | //! 23 | //! Still early, with more work needed on adding options, and testing. 24 | //! 25 | //! [CSL-spec]: https://docs.citationstyles.org/en/stable/specification.html 26 | //! [CSL-styles]: https://github.com/citation-style-language/styles 27 | //! [CSL-macros]: https://docs.citationstyles.org/en/stable/specification.html#macros 28 | //! [CSL-templates]: https://docs.citationstyles.org/en/stable/specification.html#layout-1 29 | //! [CSL-render]: https://docs.citationstyles.org/en/stable/specification.html#rendering-elements 30 | //! [CSL-options]: https://docs.citationstyles.org/en/stable/specification.html#options 31 | //! [BLTX]: https://github.com/plk/biblatex 32 | //! 33 | 34 | use crate::style::template::Rendering; 35 | use schemars::JsonSchema; 36 | use serde::{Deserialize, Serialize}; 37 | 38 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 39 | pub struct Config { 40 | pub substitute: Option, 41 | pub processing: Option, 42 | pub localize: Option, 43 | pub contributors: Option, 44 | pub dates: Option, 45 | pub titles: Option, 46 | } 47 | 48 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 49 | pub struct TitlesConfig { 50 | component: Option, 51 | monograph: Option, 52 | default: Option, 53 | } 54 | 55 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 56 | #[serde(rename_all = "kebab-case")] 57 | #[non_exhaustive] 58 | pub enum Processing { 59 | #[default] 60 | // FIX again, this pattern doesn't work 61 | AuthorDate, 62 | Numeric, 63 | Custom(ProcessingCustom), 64 | } 65 | 66 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 67 | pub struct ProcessingCustom { 68 | pub sort: Option, 69 | pub group: Option, 70 | pub disambiguate: Option, 71 | } 72 | 73 | impl Processing { 74 | pub fn config(&self) -> ProcessingCustom { 75 | match self { 76 | Processing::AuthorDate => ProcessingCustom { 77 | sort: Some(Sort { 78 | shorten_names: false, 79 | render_substitutions: false, 80 | template: vec![ 81 | SortSpec { key: SortKey::Author, ascending: true }, 82 | SortSpec { key: SortKey::Year, ascending: true }, 83 | ], 84 | }), 85 | group: Some(Group { template: vec![SortKey::Author, SortKey::Year] }), 86 | disambiguate: Some(Disambiguation { names: true, year_suffix: true }), 87 | }, 88 | Processing::Numeric => { 89 | ProcessingCustom { sort: None, group: None, disambiguate: None } 90 | } 91 | Processing::Custom(custom) => custom.clone(), 92 | } 93 | } 94 | } 95 | 96 | #[test] 97 | fn author_date_config() { 98 | let config = Processing::AuthorDate.config(); 99 | let sort = config.sort.unwrap_or_default(); 100 | assert_eq!(sort.template[0].key, SortKey::Author); 101 | assert_eq!(sort.template[1].key, SortKey::Year); 102 | assert!(config.disambiguate.unwrap_or_default().year_suffix); 103 | } 104 | 105 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 106 | #[serde(rename_all = "camelCase")] 107 | pub struct Disambiguation { 108 | pub names: bool, 109 | pub year_suffix: bool, 110 | } 111 | 112 | impl Default for Disambiguation { 113 | fn default() -> Self { 114 | Self { names: true, year_suffix: false } 115 | } 116 | } 117 | 118 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 119 | pub struct Date { 120 | pub month: MonthFormat, 121 | } 122 | 123 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 124 | #[serde(rename_all = "lowercase")] 125 | pub enum MonthFormat { 126 | #[default] 127 | Long, 128 | Short, 129 | Numeric, 130 | } 131 | 132 | impl Default for Date { 133 | fn default() -> Self { 134 | Self { month: MonthFormat::Long } 135 | } 136 | } 137 | 138 | #[test] 139 | fn date_default_config() { 140 | let config = Config::default(); 141 | assert_eq!(config.dates.unwrap_or_default().month, MonthFormat::Long); 142 | } 143 | 144 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 145 | pub struct ContributorConfig { 146 | /// When to display a contributor's name in sort order. 147 | pub display_as_sort: Option, 148 | /// Shorten the list of contributors. 149 | pub shorten: Option, 150 | /// The delimiter or separator to use between contributors. 151 | pub delimiter: Option, 152 | /// Whether to separate the last two contributors with a natural language conjunction, and if so what form it should take. 153 | pub and: Option, 154 | /// When and how to display contributor roles. 155 | pub role: Option, 156 | } 157 | 158 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 159 | #[serde(rename_all = "lowercase")] 160 | pub enum DisplayAsSort { 161 | All, 162 | First, 163 | #[default] 164 | None, 165 | } 166 | 167 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 168 | #[serde(rename_all = "lowercase")] 169 | #[non_exhaustive] 170 | pub enum AndOptions { 171 | #[default] // REVIEW: is this correct? 172 | Text, 173 | Symbol, 174 | None, 175 | } 176 | 177 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 178 | #[serde(rename_all = "camelCase")] 179 | pub struct RoleOptions { 180 | /// Contributor roles for which to omit the role description. 181 | /// 182 | /// The default value is `["author"]`, which omits the role for authors. 183 | pub omit: Vec, 184 | pub form: String, // TODO 185 | pub rendering: Option, 186 | } 187 | 188 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 189 | #[serde(rename_all = "kebab-case")] 190 | pub enum DelimiterLastOptions { 191 | /// Delimiter is only used if preceding name is inverted as a result of the`asSort` parameter. E.g. with `asSort` set to “first”. 192 | AfterInvertedName, 193 | /// Delimiter is always used when more than two, regardless of shortening. 194 | Always, 195 | /// Delimiter is never used. 196 | Never, 197 | #[default] 198 | /// The delimiter is only used when shortening is applied. 199 | Contextual, 200 | } 201 | 202 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 203 | #[serde(rename_all = "camelCase")] 204 | pub struct ShortenListOptions { 205 | pub min: u8, 206 | pub use_first: u8, 207 | pub and_others: AndOtherOptions, // REVIEW wrong place? 208 | pub delimiter_precedes_last: DelimiterLastOptions, 209 | } 210 | 211 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 212 | pub enum AndOtherOptions { 213 | #[default] 214 | EtAl, 215 | Text, 216 | } 217 | 218 | impl Default for ShortenListOptions { 219 | // REVIEW these defaults 220 | fn default() -> Self { 221 | Self { 222 | min: 5, 223 | use_first: 3, 224 | and_others: AndOtherOptions::default(), 225 | delimiter_precedes_last: DelimiterLastOptions::default(), 226 | } 227 | } 228 | } 229 | 230 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 231 | pub struct Localize { 232 | pub scope: Scope, 233 | } 234 | 235 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 236 | #[serde(rename_all = "kebab-case")] 237 | pub enum Scope { 238 | Global, 239 | PerItem, 240 | } 241 | 242 | impl Default for Localize { 243 | fn default() -> Self { 244 | Self { scope: Scope::Global } 245 | } 246 | } 247 | 248 | #[test] 249 | fn localize_config_default() { 250 | let config = Config::default(); 251 | assert_eq!(config.localize.unwrap_or_default().scope, Scope::Global); 252 | } 253 | 254 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 255 | pub struct Group { 256 | pub template: Vec, 257 | } 258 | 259 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 260 | pub struct Substitute { 261 | pub contributor_role_form: Option, 262 | pub template: Vec, 263 | } 264 | 265 | impl Default for Substitute { 266 | fn default() -> Self { 267 | Self { 268 | contributor_role_form: None, 269 | template: vec![ 270 | SubstituteKey::Editor, 271 | SubstituteKey::Title, 272 | SubstituteKey::Translator, 273 | ], 274 | } 275 | } 276 | } 277 | 278 | #[test] 279 | fn substitute_default() { 280 | let config = Config::default(); 281 | assert_eq!(config.substitute.unwrap_or_default().template.len(), 3); 282 | } 283 | 284 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 285 | #[serde(rename_all = "camelCase")] 286 | pub struct Sort { 287 | /// Shorten name lists for sorting the same as for display. 288 | // REVIEW: may need more options here. 289 | #[serde(default = "default_shorten_names")] 290 | pub shorten_names: bool, 291 | /// Use same substitutions for sorting as for rendering. 292 | #[serde(default = "default_render_substitutions")] 293 | pub render_substitutions: bool, 294 | pub template: Vec, 295 | } 296 | 297 | fn default_shorten_names() -> bool { 298 | false 299 | } 300 | 301 | fn default_render_substitutions() -> bool { 302 | false 303 | } 304 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 305 | pub struct SortSpec { 306 | pub key: SortKey, 307 | #[serde(default = "default_ascending")] 308 | pub ascending: bool, 309 | } 310 | 311 | fn default_ascending() -> bool { 312 | true 313 | } 314 | 315 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 316 | #[serde(rename_all = "lowercase")] 317 | #[non_exhaustive] 318 | pub enum SortKey { 319 | #[default] 320 | Author, 321 | Year, 322 | Title, 323 | } 324 | 325 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 326 | #[serde(rename_all = "lowercase")] 327 | pub enum SubstituteKey { 328 | Editor, 329 | Title, 330 | Translator, 331 | } 332 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /csln/src/bibliography/reference.rs: -------------------------------------------------------------------------------- 1 | /* 2 | SPDX-License-Identifier: MPL-2.0 3 | SPDX-FileCopyrightText: © 2023 Bruce D'Arcus 4 | */ 5 | 6 | //! A reference is a bibliographic item, such as a book, article, or web page. 7 | //! It is the basic unit of bibliographic data. 8 | //! 9 | //! The model includes the following core data types. 10 | //! Each is designed to be as simple as possible, while also allowing more complex data structures. 11 | //! 12 | //! ## Title 13 | //! 14 | //! A title can be a single string, a structured title, or a multilingual title. 15 | //! 16 | //! ## Contributor 17 | //! 18 | //! A contributor can be a single string, a structured name, or a list of contributors. 19 | //! 20 | //! ## Date 21 | //! 22 | //! Dates can either be EDTF strings, for flexible dates and date-times, or literal strings. 23 | //! Literal strings can be used for examples like "Han Dynasty". 24 | //! 25 | //! ## Parent References 26 | //! 27 | //! A reference can be a component of a larger work, such as a chapter in a book, or an article. 28 | //! The parent is represented inline as a Monograph or Serial. 29 | //! I would like to add ability to reference a parent by ID, but that is not yet implemented. 30 | 31 | use crate::style::locale::Locale; 32 | use crate::style::options::{AndOptions, AndOtherOptions, DisplayAsSort}; 33 | use crate::style::{locale::MonthList, options::Config}; 34 | use edtf::level_1::Edtf; 35 | use fmt::Display; 36 | use schemars::JsonSchema; 37 | use serde::{Deserialize, Serialize}; 38 | use std::fmt; 39 | use std::fmt::Formatter; 40 | use url::Url; 41 | //use icu::calendar::DateTime; 42 | 43 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 44 | #[serde(untagged)] 45 | /// The Reference model. 46 | pub enum InputReference { 47 | /// A monograph, such as a book or a report, is a monolithic work published or produced as a complete entity. 48 | Monograph(Monograph), 49 | /// A component of a larger Monography, such as a chapter in a book. 50 | /// The parent monograph is referenced by its ID. 51 | CollectionComponent(CollectionComponent), 52 | /// A componet of a larger serial publication; for example a journal or newspaper article. 53 | /// The parent serial is referenced by its ID. 54 | SerialComponent(SerialComponent), 55 | /// A collection of works, such as an anthology or proceedings. 56 | Collection(Collection), 57 | } 58 | 59 | impl InputReference { 60 | // REVIEW: is this sensible? 61 | 62 | /// Return the reference ID. 63 | /// If the reference does not have an ID, return None. 64 | pub fn id(&self) -> Option { 65 | match self { 66 | InputReference::Monograph(r) => r.id.clone(), 67 | InputReference::CollectionComponent(r) => r.id.clone(), 68 | InputReference::SerialComponent(r) => r.id.clone(), 69 | InputReference::Collection(r) => r.id.clone(), 70 | } 71 | } 72 | 73 | /// Return the author. 74 | /// If the reference does not have an author, return None. 75 | pub fn author(&self) -> Option { 76 | match self { 77 | InputReference::Monograph(r) => Some(r.author.clone()?), 78 | InputReference::CollectionComponent(r) => Some(r.author.clone()?), 79 | InputReference::SerialComponent(r) => Some(r.author.clone()?), 80 | _ => None, 81 | } 82 | } 83 | 84 | /// Return the editor. 85 | /// If the reference does not have an editor, return None. 86 | pub fn editor(&self) -> Option { 87 | match self { 88 | // REVIEW: return string instead? 89 | InputReference::Collection(r) => r.editor.clone(), 90 | InputReference::CollectionComponent(r) => r.parent.editor.clone(), 91 | _ => None, 92 | } 93 | } 94 | 95 | /// Return the translator. 96 | /// If the reference does not have a translator, return None. 97 | pub fn translator(&self) -> Option { 98 | match self { 99 | // REVIEW: return string instead? 100 | InputReference::Monograph(r) => r.translator.clone(), 101 | InputReference::CollectionComponent(r) => r.translator.clone(), 102 | InputReference::SerialComponent(r) => r.translator.clone(), 103 | InputReference::Collection(r) => r.translator.clone(), 104 | } 105 | } 106 | 107 | /// Return the publisher. 108 | /// If the reference does not have a publisher, return None. 109 | pub fn publisher(&self) -> Option { 110 | match self { 111 | // REVIEW: return string instead? 112 | InputReference::Monograph(r) => r.publisher.clone(), 113 | InputReference::CollectionComponent(r) => r.parent.publisher.clone(), 114 | InputReference::Collection(r) => r.publisher.clone(), 115 | _ => None, 116 | } 117 | } 118 | 119 | /// Return the title. 120 | /// If the reference does not have a title, return None. 121 | pub fn title(&self) -> Option { 122 | match self { 123 | InputReference::Monograph(r) => Some(r.title.clone()), 124 | InputReference::CollectionComponent(r) => r.title.clone(), 125 | InputReference::SerialComponent(r) => r.title.clone(), 126 | InputReference::Collection(r) => r.title.clone(), 127 | } 128 | } 129 | 130 | /// Return the issued date. 131 | /// If the reference does not have an issued date, return None. 132 | pub fn issued(&self) -> Option<EdtfString> { 133 | match self { 134 | InputReference::Monograph(r) => Some(r.issued.clone()), 135 | InputReference::CollectionComponent(r) => Some(r.issued.clone()), 136 | InputReference::SerialComponent(r) => Some(r.issued.clone()), 137 | InputReference::Collection(r) => Some(r.issued.clone()), 138 | } 139 | } 140 | 141 | pub fn set_id(&mut self, id: String) { 142 | match self { 143 | InputReference::Monograph(monograph) => monograph.id = Some(id), 144 | InputReference::CollectionComponent(monograph_component) => { 145 | monograph_component.id = Some(id) 146 | } 147 | InputReference::SerialComponent(serial_component) => { 148 | serial_component.id = Some(id) 149 | } 150 | InputReference::Collection(collection) => collection.id = Some(id), 151 | } 152 | } 153 | } 154 | 155 | /// A value that could be either a number or a string. 156 | // Borrowed from Hayagriva 157 | #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, Deserialize, Serialize)] 158 | #[serde(untagged)] 159 | pub enum NumOrStr { 160 | /// It's a number! 161 | Number(i64), 162 | /// It's a string! 163 | Str(String), 164 | } 165 | 166 | impl Display for NumOrStr { 167 | fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { 168 | match self { 169 | Self::Number(i) => write!(f, "{}", i), 170 | Self::Str(s) => write!(f, "{}", s), 171 | } 172 | } 173 | } 174 | 175 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 176 | /// A monograph, such as a book or a report, is a monolithic work published or produced as a complete entity. 177 | pub struct Monograph { 178 | pub id: Option<RefID>, 179 | pub r#type: MonographType, 180 | pub title: Title, 181 | pub author: Option<Contributor>, 182 | pub translator: Option<Contributor>, 183 | pub issued: EdtfString, 184 | pub publisher: Option<Contributor>, 185 | pub url: Option<Url>, 186 | pub accessed: Option<EdtfString>, 187 | pub note: Option<String>, 188 | pub isbn: Option<String>, 189 | pub doi: Option<String>, 190 | pub edition: Option<String>, 191 | } 192 | 193 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 194 | #[serde(rename_all = "kebab-case")] 195 | pub struct Collection { 196 | pub id: Option<RefID>, 197 | pub r#type: CollectionType, 198 | pub title: Option<Title>, 199 | pub editor: Option<Contributor>, 200 | pub translator: Option<Contributor>, 201 | pub issued: EdtfString, 202 | pub publisher: Option<Contributor>, 203 | pub url: Option<Url>, 204 | pub accessed: Option<EdtfString>, 205 | pub note: Option<String>, 206 | pub isbn: Option<String>, 207 | } 208 | 209 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 210 | #[serde(rename_all = "kebab-case")] 211 | #[non_exhaustive] 212 | pub enum CollectionType { 213 | Anthology, 214 | Proceedings, 215 | EditedBook, 216 | EditedVolume, 217 | } 218 | 219 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 220 | /// A componet of a larger serial publication; for example a journal or newspaper article. 221 | /// The parent serial is referenced by its ID. 222 | pub struct SerialComponent { 223 | pub id: Option<RefID>, 224 | pub r#type: SerialComponentType, 225 | pub title: Option<Title>, 226 | pub author: Option<Contributor>, 227 | pub translator: Option<Contributor>, 228 | pub issued: EdtfString, 229 | /// The parent work, such a magazine or journal. 230 | pub parent: Serial, 231 | pub url: Option<Url>, 232 | pub accessed: Option<EdtfString>, 233 | pub note: Option<String>, 234 | pub doi: Option<String>, 235 | pub pages: Option<String>, 236 | pub volume: Option<NumOrStr>, 237 | pub issue: Option<NumOrStr>, 238 | } 239 | 240 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 241 | #[serde(untagged)] 242 | pub enum ParentReference { 243 | Monograph(Monograph), 244 | Serial(Serial), 245 | } 246 | 247 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 248 | #[serde(rename_all = "kebab-case")] 249 | pub enum SerialComponentType { 250 | Article, 251 | Post, 252 | Review, 253 | } 254 | 255 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 256 | pub struct Serial { 257 | pub r#type: SerialType, 258 | pub title: Title, 259 | } 260 | 261 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 262 | #[serde(rename_all = "kebab-case")] 263 | #[non_exhaustive] 264 | pub enum SerialType { 265 | AcademicJournal, 266 | Blog, 267 | Magazine, 268 | Newspaper, 269 | Newsletter, 270 | Proceedings, 271 | Podcast, 272 | BroadcastProgram, 273 | } 274 | 275 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 276 | #[serde(rename_all = "kebab-case")] 277 | #[non_exhaustive] 278 | pub enum MonographComponentType { 279 | Chapter, 280 | /// A generic part of a monograph, such as a preface or an appendix. 281 | Document, 282 | Section, 283 | Part, 284 | } 285 | 286 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 287 | #[serde(rename_all = "kebab-case")] 288 | #[non_exhaustive] 289 | pub enum MonographType { 290 | #[default] 291 | Book, 292 | /// A standalone generic item. 293 | Document, 294 | Report, 295 | } 296 | 297 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 298 | /// A component of a larger Monography, such as a chapter in a book. 299 | /// The parent monograph is referenced by its ID. 300 | pub struct CollectionComponent { 301 | pub id: Option<RefID>, 302 | pub r#type: MonographComponentType, 303 | pub title: Option<Title>, 304 | pub author: Option<Contributor>, 305 | pub translator: Option<Contributor>, 306 | pub issued: EdtfString, 307 | /// The parent work, as either a Monograph. 308 | // I would like to allow this to be either a Monograph or a RefID, but I can't figure out how to do that. 309 | pub parent: Collection, 310 | pub pages: Option<NumOrStr>, 311 | pub url: Option<Url>, 312 | pub accessed: Option<EdtfString>, 313 | pub note: Option<String>, 314 | pub doi: Option<String>, 315 | } 316 | 317 | pub type RefID = String; 318 | 319 | /// A locale string. 320 | pub type LangID = String; 321 | 322 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 323 | #[serde(untagged)] 324 | #[non_exhaustive] 325 | /// A collection of formattable strings consisting of a title, a translated title, and a shorthand. 326 | // REVIEW this needs a bit more work. 327 | pub enum Title { 328 | /// A title in a single language. 329 | Single(String), 330 | /// A structured title. 331 | Structured(StructuredTitle), 332 | /// A title in multiple languages. 333 | Multi(Vec<(LangID, String)>), 334 | /// A structured title in multiple languages. 335 | MultiStructured(Vec<(LangID, StructuredTitle)>), 336 | /// An abbreviated title. 337 | // Borrowed from Hayagriva 338 | Shorthand(String, String), 339 | } 340 | 341 | /// Where title parts are meaningful, use this struct; CSLN processors will not parse title strings. 342 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 343 | pub struct StructuredTitle { 344 | pub full: Option<String>, 345 | pub main: String, 346 | pub sub: Subtitle, 347 | } 348 | 349 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 350 | #[serde(untagged)] 351 | /// The subtitle can either be a string, as is the common case, or a vector of strings. 352 | pub enum Subtitle { 353 | String(String), 354 | Vector(Vec<String>), 355 | } 356 | 357 | impl fmt::Display for Title { 358 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 359 | match self { 360 | Title::Single(s) => write!(f, "{}", s), 361 | Title::Multi(_m) => todo!("multilingual title"), 362 | Title::Structured(s) => { 363 | let subtitle = match &s.sub { 364 | Subtitle::String(s) => s.clone(), 365 | Subtitle::Vector(v) => v.join(", "), 366 | }; 367 | write!(f, "{}: {}", s.main.clone(), subtitle) 368 | } 369 | Title::MultiStructured(_m) => todo!("multilingual structured title"), 370 | Title::Shorthand(s, t) => write!(f, "{} ({})", s, t), 371 | } 372 | } 373 | } 374 | 375 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 376 | /// A string conforming to the EDTF specification. 377 | pub struct EdtfString(pub String); 378 | 379 | #[derive(Debug, PartialEq)] 380 | /// Date inputs must be valid EDTF strings, or a literal string. 381 | pub enum RefDate { 382 | Edtf(Edtf), 383 | Literal(String), 384 | } 385 | 386 | impl EdtfString { 387 | /// Parse the string as an EDTF date etc, or return the string as a literal. 388 | pub fn parse(&self) -> RefDate { 389 | match Edtf::parse(&self.0) { 390 | Ok(edtf) => RefDate::Edtf(edtf), 391 | Err(_) => RefDate::Literal(self.0.clone()), 392 | } 393 | } 394 | 395 | fn component_to_u32(&self, component: Option<edtf::level_1::Component>) -> u32 { 396 | match component { 397 | Some(component) => component.value().unwrap_or(0), 398 | None => 0, 399 | } 400 | } 401 | 402 | pub fn year(&self) -> String { 403 | let parsed_date = self.parse(); 404 | match parsed_date { 405 | RefDate::Edtf(edtf) => match edtf { 406 | Edtf::Date(date) => date.year().to_string(), 407 | Edtf::YYear(year) => format!("{}", year.value()), 408 | Edtf::DateTime(datetime) => datetime.date().year().to_string(), 409 | Edtf::Interval(start, _end) => format!("{}", start.year()), 410 | Edtf::IntervalFrom(date, _terminal) => format!("{}", date.year()), 411 | Edtf::IntervalTo(_terminal, date) => format!("{}", date.year()), 412 | }, 413 | RefDate::Literal(_) => "".to_string(), 414 | } 415 | } 416 | 417 | fn month_to_string(month: u32, months: MonthList) -> String { 418 | if month > 0 { 419 | let index = month - 1; 420 | if index < months.len() as u32 { 421 | months[index as usize].clone() 422 | } else { 423 | "".to_string() 424 | } 425 | } else { 426 | "".to_string() 427 | } 428 | } 429 | 430 | pub fn month(&self, months: MonthList) -> String { 431 | let parsed_date = self.parse(); 432 | let month: Option<u32> = match parsed_date { 433 | RefDate::Edtf(edtf) => match edtf { 434 | Edtf::Date(date) => Some(self.component_to_u32(date.month())), 435 | Edtf::YYear(_year) => None, 436 | // types errors below that I couldn't figure out how to fix 437 | Edtf::DateTime(datetime) => Some(datetime.date().month()), 438 | Edtf::Interval(_start, _end) => todo!(), 439 | Edtf::IntervalFrom(_date, _terminal) => todo!(), 440 | Edtf::IntervalTo(_terminal, _date) => todo!(), 441 | }, 442 | RefDate::Literal(_) => None, 443 | }; 444 | match month { 445 | Some(month) => EdtfString::month_to_string(month, months), 446 | None => "".to_string(), 447 | } 448 | } 449 | 450 | pub fn year_month(&self, months: MonthList) -> String { 451 | let month = self.month(months); 452 | let year = self.year(); 453 | if month.is_empty() || year.is_empty() { 454 | "".to_string() 455 | } else { 456 | format!("{} {}", month, year) 457 | } 458 | } 459 | 460 | pub fn month_day(&self, months: MonthList) -> String { 461 | let month = self.month(months); 462 | // TODO 463 | let day = "1"; 464 | if month.is_empty() { 465 | "".to_string() 466 | } else { 467 | format!("{} {}", month, day) 468 | } 469 | } 470 | } 471 | 472 | #[test] 473 | fn year_months() { 474 | let months: MonthList = vec![ 475 | "January".to_string(), 476 | "February".to_string(), 477 | "March".to_string(), 478 | "April".to_string(), 479 | "May".to_string(), 480 | "June".to_string(), 481 | "July".to_string(), 482 | "August".to_string(), 483 | "September".to_string(), 484 | "October".to_string(), 485 | "November".to_string(), 486 | "December".to_string(), 487 | ]; 488 | let date = EdtfString("2020-01-01".to_string()); 489 | assert_eq!(date.year_month(months), "January 2020"); 490 | } 491 | 492 | #[test] 493 | fn literal_dates() { 494 | let date_string = EdtfString("foo bar".to_string()); 495 | assert_eq!(date_string.parse(), RefDate::Literal("foo bar".to_string())); 496 | } 497 | 498 | impl RefDate { 499 | pub fn and_then<F, T>(self, f: F) -> Option<T> 500 | where 501 | F: FnOnce(Edtf) -> Option<T>, 502 | { 503 | match self { 504 | RefDate::Edtf(edtf) => f(edtf), 505 | RefDate::Literal(_) => None, 506 | } 507 | } 508 | 509 | // TODO do we want this or string? 510 | pub fn year(&self) -> i32 { 511 | match self { 512 | RefDate::Edtf(edtf) => match edtf { 513 | Edtf::Date(date) => date.year(), 514 | Edtf::YYear(year) => year.value() as i32, 515 | Edtf::DateTime(datetime) => datetime.date().year(), 516 | // REVIEW: the intervals need more thought. 517 | Edtf::Interval(start, _end) => start.year(), 518 | Edtf::IntervalFrom(date, _terminal) => date.year(), 519 | Edtf::IntervalTo(_terminal, date) => date.year(), 520 | }, 521 | // Since we need this for sorting, return 0 for now. 522 | RefDate::Literal(_) => 0, 523 | } 524 | } 525 | } 526 | 527 | #[test] 528 | fn year_from_edtf_dates() { 529 | let date = EdtfString("2020-01-01".to_string()).parse(); 530 | assert_eq!(date.year(), 2020); 531 | let date = EdtfString("2021-10".to_string()).parse(); 532 | assert_eq!(date.year(), 2021); 533 | let date = EdtfString("2022".to_string()).parse(); 534 | assert_eq!(date.year(), 2022); 535 | } 536 | 537 | #[test] 538 | fn month_from_edtf_dates() { 539 | let months: MonthList = vec![ 540 | "January".to_string(), 541 | "February".to_string(), 542 | "March".to_string(), 543 | "April".to_string(), 544 | "May".to_string(), 545 | "June".to_string(), 546 | "July".to_string(), 547 | "August".to_string(), 548 | "September".to_string(), 549 | "October".to_string(), 550 | "November".to_string(), 551 | "December".to_string(), 552 | ]; 553 | let date = EdtfString("2020-01-01".to_string()); 554 | assert_eq!(date.month(months), "January"); 555 | } 556 | 557 | impl fmt::Display for EdtfString { 558 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 559 | // TODO: finish this 560 | let parsed_date: Edtf = match Edtf::parse(&self.0) { 561 | Ok(edtf) => edtf, 562 | Err(_) => return write!(f, "{:?}", self), 563 | }; 564 | write!(f, "{}", parsed_date) 565 | } 566 | } 567 | 568 | /// A contributor can be a person or an organzation. 569 | // REVIEW for now, we keep this simple-but-flexible. We may want to add more structure later. 570 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 571 | #[serde(untagged)] 572 | pub enum Contributor { 573 | SimpleName(SimpleName), 574 | StructuredName(StructuredName), 575 | ContributorList(ContributorList), 576 | } 577 | 578 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 579 | pub struct SimpleName { 580 | pub name: String, 581 | pub location: Option<String>, 582 | } 583 | 584 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 585 | /// The contributor list model. 586 | pub struct ContributorList(pub Vec<Contributor>); 587 | 588 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)] 589 | #[serde(rename_all = "camelCase")] 590 | /// Structured personal contributor names. 591 | pub struct StructuredName { 592 | pub given: String, 593 | pub family: String, 594 | } 595 | 596 | impl fmt::Display for Contributor { 597 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 598 | match self { 599 | Contributor::SimpleName(c) => write!(f, "{}", c.name), 600 | Contributor::StructuredName(contributor) => { 601 | write!(f, "{} {}", contributor.given, contributor.family) 602 | } 603 | Contributor::ContributorList(contributors) => { 604 | write!(f, "{}", contributors) 605 | } 606 | } 607 | } 608 | } 609 | 610 | impl StructuredName { 611 | /// Return the initials of the name. 612 | pub fn initials(&self, with: Option<String>) -> String { 613 | let with = with.unwrap_or_default(); 614 | let initials = self 615 | .given 616 | .split_whitespace() 617 | .map(|name| name.chars().next().unwrap_or_default()) 618 | .collect::<Vec<char>>(); 619 | let initials_string = initials 620 | .iter() 621 | .map(|&c| c.to_string()) 622 | .collect::<Vec<String>>() 623 | .join(&with) 624 | + &with; 625 | initials_string 626 | } 627 | } 628 | 629 | #[test] 630 | fn initials() { 631 | let name = StructuredName { 632 | given: "Jane Mary".to_string(), 633 | family: "Smith".to_string(), 634 | }; 635 | assert_eq!(name.initials(None), "JM"); 636 | assert_eq!(name.initials(Some(".".to_string())), "J.M."); 637 | } 638 | 639 | #[test] 640 | fn contributor_name() { 641 | let contributor = 642 | Contributor::SimpleName(SimpleName { name: "ABC".to_string(), location: None }); 643 | assert_eq!(contributor.to_string(), "ABC"); 644 | let contributor = Contributor::StructuredName(StructuredName { 645 | given: "John".to_string(), 646 | family: "Smith".to_string(), 647 | }); 648 | assert_eq!(contributor.to_string(), "John Smith"); 649 | let contributor = Contributor::ContributorList(ContributorList(vec![ 650 | Contributor::SimpleName(SimpleName { 651 | name: "John Smith".to_string(), 652 | location: None, 653 | }), 654 | Contributor::SimpleName(SimpleName { 655 | name: "Jane Smith".to_string(), 656 | location: None, 657 | }), 658 | ])); 659 | assert_eq!(contributor.to_string(), "John Smith, Jane Smith"); 660 | } 661 | 662 | impl fmt::Display for ContributorList { 663 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 664 | let contributors: Vec<String> = 665 | self.0.iter().map(|c| c.to_string()).collect::<Vec<String>>(); 666 | write!(f, "{}", contributors.join(", ")) 667 | } 668 | } 669 | 670 | impl Contributor { 671 | // if as_sorted is true, the name will be displayed as sorted, overriding the configuration option. 672 | pub fn names(&self, options: Config, as_sorted: bool) -> Vec<String> { 673 | match self { 674 | Contributor::SimpleName(c) => vec![c.name.to_string()], 675 | Contributor::StructuredName(contributor) => { 676 | // FIXME when there's only one, always uses else here 677 | if as_sorted { 678 | vec![format!("{}, {}", contributor.family, contributor.given)] 679 | } else { 680 | vec![format!("{} {}", contributor.given, contributor.family)] 681 | } 682 | } 683 | Contributor::ContributorList(contributors) => { 684 | contributors.names_list(options) 685 | } 686 | } 687 | } 688 | 689 | /// Join a vector of strings with commas and "and". 690 | pub fn name_list_and(&self, and: String) -> Vec<String> { 691 | let names = self.names(Config::default(), false); 692 | let mut result = names; 693 | if result.len() > 1 { 694 | let last = result.pop().expect("List should have at least one element"); 695 | result.push(format!("{} {}", and, last)); 696 | } 697 | result 698 | } 699 | 700 | pub fn name_list_shorten(&self, names: &[&str], use_first: u8) -> Vec<String> { 701 | names 702 | .iter() 703 | .take(use_first as usize) 704 | .map(|&s| s.to_string()) 705 | .collect::<Vec<String>>() 706 | } 707 | 708 | fn format_list( 709 | &self, 710 | names: Vec<String>, 711 | and_str: String, 712 | oxford_comma: bool, 713 | ) -> String { 714 | let last = names.last().map(ToString::to_string).unwrap_or_default(); 715 | match names.len() { 716 | 0 => String::new(), 717 | 1 => last, 718 | 2 => format!("{} {} {}", names[0], and_str, last), 719 | _ => { 720 | let all_but_last = names[..names.len() - 1] 721 | .iter() 722 | .map(ToString::to_string) 723 | .collect::<Vec<_>>() 724 | .join(", "); 725 | if oxford_comma { 726 | format!("{}, {} {}", all_but_last, and_str, last) 727 | } else { 728 | format!("{} {} {}", all_but_last, and_str, last) 729 | } 730 | } 731 | } 732 | } 733 | 734 | pub fn format(&self, options: Config, locale: Locale) -> String { 735 | let as_sorted: bool = matches!(self, Contributor::StructuredName(_)); 736 | let names = self.names(options.clone(), as_sorted); 737 | let contributor_options = options.contributors.clone().unwrap_or_default(); 738 | let shorten: bool = 739 | contributor_options.shorten.unwrap_or_default().min <= names.len() as u8; 740 | if shorten { 741 | let shorten_options = options 742 | .contributors 743 | .unwrap_or_default() 744 | .shorten 745 | .clone() 746 | .unwrap_or_default(); 747 | let use_first = shorten_options.use_first; 748 | let and_others = shorten_options.and_others; 749 | let and_others_string = match and_others { 750 | AndOtherOptions::EtAl => { 751 | locale.terms.et_al.unwrap_or("et al".to_string()) 752 | } // TODO localize 753 | AndOtherOptions::Text => { 754 | locale.terms.and_others.unwrap_or("and others".to_string()) 755 | } 756 | }; 757 | let names_str: Vec<&str> = names.iter().map(AsRef::as_ref).collect(); 758 | let result = self.name_list_shorten(&names_str, use_first); 759 | let result_with_and_others = 760 | format!("{} {}", result.join(", "), and_others_string); 761 | result_with_and_others 762 | } else { 763 | let and_options = contributor_options.and; 764 | let and_string = match and_options { 765 | Some(AndOptions::Symbol) => "&".to_string(), 766 | Some(AndOptions::Text) => "and".to_string(), 767 | _ => "".to_string(), // FIXME localize 768 | // Add more variants as needed 769 | }; 770 | self.format_list(names, and_string, true) 771 | } 772 | } 773 | } 774 | 775 | impl ContributorList { 776 | // ... 777 | 778 | fn as_sorted(options: Config, index: usize) -> bool { 779 | let display_as_sort = options 780 | .contributors 781 | .clone() 782 | .unwrap_or_default() 783 | .display_as_sort 784 | .clone(); 785 | index == 0 && display_as_sort == Some(DisplayAsSort::First) 786 | || display_as_sort == Some(DisplayAsSort::All) 787 | } 788 | 789 | pub fn names_list(&self, options: Config) -> Vec<String> { 790 | self.0 791 | .iter() 792 | .enumerate() 793 | .flat_map(|(i, c)| { 794 | c.names(options.clone(), Self::as_sorted(options.clone(), i)) 795 | }) 796 | .collect::<Vec<String>>() 797 | } 798 | } 799 | 800 | #[test] 801 | fn display_and_sort_names() { 802 | let simple = Contributor::SimpleName(SimpleName { 803 | name: "John Doe".to_string(), 804 | location: None, 805 | }); 806 | let structured = Contributor::StructuredName(StructuredName { 807 | given: "John".to_string(), 808 | family: "Doe".to_string(), 809 | }); 810 | let options = Config::default(); 811 | // FIXME use this format method in this test 812 | assert_eq!(simple.names(options, false).join(" "), "John Doe"); 813 | let options = Config::default(); 814 | assert_eq!( 815 | simple.names(options, true).join(" "), 816 | "John Doe", 817 | "as_sorted=true should not affect a simple name" 818 | ); 819 | let options = Config::default(); 820 | assert_eq!(structured.names(options, false).join(" "), "John Doe"); 821 | let options = Config::default(); 822 | assert_eq!(structured.names(options, true).join(", "), "Doe, John"); 823 | } 824 | -------------------------------------------------------------------------------- /processor/src/lib.rs: -------------------------------------------------------------------------------- 1 | /* 2 | SPDX-License-Identifier: MPL-2.0 3 | SPDX-FileCopyrightText: © 2023 Bruce D'Arcus 4 | */ 5 | 6 | //! CSLN Processor 7 | //! 8 | //! This crate provides the core citation and bibliography processing functionality 9 | //! for the Citation Style Language Next (CSLN) project. It takes style definitions, 10 | //! bibliographic data, and citation information and produces formatted output. 11 | //! 12 | //! The processor is designed to be pluggable with different renderers and supports 13 | //! advanced features like disambiguation, sorting, and localization. 14 | 15 | use csln::bibliography::reference::InputReference; 16 | use csln::bibliography::reference::{EdtfString, RefID}; 17 | use csln::bibliography::InputBibliography as Bibliography; 18 | use csln::citation::{Citation, CitationItem, Citations}; 19 | use csln::style::locale::Locale; 20 | use csln::style::options::{Config, MonthFormat, SortKey, SubstituteKey}; 21 | use csln::style::template::{ 22 | ContributorForm, ContributorRole, DateForm, Dates, Numbers, TemplateComponent, 23 | TemplateContributor, TemplateDate, TemplateNumber, TemplateSimpleString, 24 | TemplateTitle, Titles, Variables, WrapPunctuation, 25 | }; 26 | use csln::style::Style; 27 | use icu::datetime::DateTimeFormatterOptions; 28 | use itertools::Itertools; 29 | use rayon::prelude::*; 30 | use schemars::JsonSchema; 31 | use serde::{Deserialize, Serialize}; 32 | //use std::cmp::Ordering; 33 | //use anyhow::Result; 34 | use std::collections::HashMap; 35 | use std::fmt::{self, Debug, Display, Formatter}; 36 | use std::option::Option; 37 | 38 | /* 39 | This is the processor code. 40 | 41 | The basic design is the same as the csl-next typescript implementation: 42 | 43 | The processor takes a style, a bibliography, and a locale, and renders the output. 44 | 45 | The primary target is a JSON AST, represented by the ProcTemplateComponent struct. 46 | */ 47 | 48 | // TODO: This will need to be generalized later. See: 49 | // https://github.com/bdarcus/csln/issues/105 50 | pub fn refs_to_string(proc_templates: Vec<ProcTemplate>) -> String { 51 | proc_templates 52 | .iter() 53 | .map(|proc_template| { 54 | proc_template 55 | .iter() 56 | .map(|proc_template_component| proc_template_component.to_string()) 57 | .collect::<Vec<String>>() 58 | .join(". ") 59 | + "." 60 | }) 61 | .collect::<Vec<String>>() 62 | .join("\n\n") 63 | } 64 | 65 | /// The processor struct, which takes a style, a bibliography, and a locale, and renders the output. 66 | #[derive(Debug, Default, Deserialize, Serialize)] 67 | pub struct Processor { 68 | /// The input style. 69 | style: Style, 70 | /// The input bibliography. 71 | bibliography: Bibliography, 72 | /// The input citations. 73 | citations: Citations, 74 | /// The output locale. 75 | locale: Locale, 76 | } 77 | 78 | /// The intermediate representation of a StyleTemplate, which is used to render the output. 79 | pub type ProcTemplate = Vec<ProcTemplateComponent>; 80 | 81 | /// The intermediate representation of a StyleTemplateComponent, which is used to render the output. 82 | /// This struct will have two fields: a StyleComponent and a String. 83 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 84 | #[serde(rename_all = "camelCase")] 85 | pub struct ProcTemplateComponent { 86 | /// The original input style template component, which provides rendering instructions. 87 | pub template_component: TemplateComponent, 88 | /// The string to render. 89 | pub values: ProcValues, 90 | } 91 | 92 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 93 | #[serde(rename_all = "camelCase")] 94 | /// Holds one or more processed strings, ready for final rendering. 95 | pub struct ProcValues { 96 | /// The primary string to render. 97 | pub value: String, 98 | /// The prefix to render. 99 | pub prefix: Option<String>, 100 | /// The suffix to render. 101 | pub suffix: Option<String>, 102 | } 103 | 104 | #[test] 105 | fn render_proc_template_component() { 106 | use csln::style::template::Rendering; 107 | let template_component = TemplateComponent::SimpleString(TemplateSimpleString { 108 | variable: Variables::Doi, 109 | rendering: Some(Rendering { 110 | emph: Some(true), 111 | quote: Some(true), 112 | strong: Some(true), 113 | prefix: Some("doi: ".to_string()), 114 | suffix: Some(" ||".to_string()), 115 | wrap: Some(WrapPunctuation::Parentheses), 116 | }), 117 | }); 118 | let value = "10/1234".to_string(); 119 | let proc_template_component = ProcTemplateComponent::new( 120 | template_component, 121 | ProcValues { value, prefix: None, suffix: None }, 122 | ); 123 | assert_eq!(proc_template_component.to_string(), "(doi: 10/1234 ||)".to_string()); 124 | } 125 | 126 | impl Display for ProcTemplateComponent { 127 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 128 | let rendering = self.template_component.rendering(); 129 | let prefix: String = rendering 130 | .clone() // REVIEW this compiles, but too much cloning 131 | .unwrap_or_default() 132 | .prefix 133 | .unwrap_or_default(); 134 | let suffix: String = 135 | rendering.clone().unwrap_or_default().suffix.unwrap_or_default(); 136 | let wrap: WrapPunctuation = 137 | rendering.unwrap_or_default().wrap.unwrap_or_default(); 138 | let wrap_punct: (String, String) = match wrap { 139 | WrapPunctuation::None => ("".to_string(), "".to_string()), 140 | WrapPunctuation::Parentheses => ("(".to_string(), ")".to_string()), 141 | WrapPunctuation::Brackets => ("[".to_string(), "]".to_string()), 142 | }; 143 | // REVIEW: is this where to plugin different renderers? 144 | // Also, how to handle the different affixes, including within the values? 145 | let result = wrap_punct.0 146 | + &prefix 147 | + &self.values.prefix.clone().unwrap_or_default() 148 | + &self.values.value 149 | + &self.values.suffix.clone().unwrap_or_default() 150 | + &suffix 151 | + &wrap_punct.1; 152 | write!(f, "{}", result) 153 | } 154 | } 155 | 156 | impl ProcTemplateComponent { 157 | pub fn new(template_component: TemplateComponent, values: ProcValues) -> Self { 158 | ProcTemplateComponent { template_component, values } 159 | } 160 | } 161 | 162 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 163 | #[serde(rename_all = "kebab-case")] 164 | /// Holds the intermediate processing hints for a reference that can be used 165 | /// to render the output; particularly for disambiguation. 166 | pub struct ProcHints { 167 | /// Whether or not the reference needs to be disambiguated. 168 | pub disamb_condition: bool, 169 | /// The index of the reference in the group, starting at 1. 170 | pub group_index: usize, 171 | /// The number of references in the group. 172 | pub group_length: usize, 173 | /// The key of the group. 174 | pub group_key: String, 175 | } 176 | 177 | impl ProcHints { 178 | pub fn new( 179 | disamb_condition: bool, 180 | group_index: usize, 181 | group_length: usize, 182 | group_key: String, 183 | ) -> Self { 184 | ProcHints { 185 | disamb_condition, 186 | group_index, 187 | group_length, 188 | group_key, 189 | } 190 | } 191 | } 192 | 193 | impl Default for ProcHints { 194 | fn default() -> Self { 195 | ProcHints { 196 | disamb_condition: false, 197 | group_index: 0, 198 | group_length: 0, 199 | group_key: "".to_string(), 200 | } 201 | } 202 | } 203 | 204 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 205 | /// Configuration options. 206 | pub struct RenderOptions { 207 | // Options for the style, including default options. 208 | global: Config, 209 | // Options for the citaton or bibliography, that may override the style options. 210 | local: Config, 211 | // Locale for the output. 212 | locale: Locale, 213 | } 214 | 215 | /// The intermediate representation of a TemplateComponent, which is used to render the output. 216 | pub trait ProcessComponent<T> { 217 | fn process( 218 | &self, 219 | reference: &InputReference, 220 | component: &T, 221 | options: RenderOptions, 222 | ) -> Option<ProcTemplateComponent>; 223 | } 224 | 225 | pub trait ComponentValues { 226 | fn values( 227 | &self, 228 | reference: &InputReference, 229 | hints: &ProcHints, 230 | options: &RenderOptions, 231 | ) -> Option<ProcValues>; 232 | } 233 | 234 | impl ComponentValues for TemplateComponent { 235 | fn values( 236 | &self, 237 | reference: &InputReference, 238 | hints: &ProcHints, 239 | options: &RenderOptions, 240 | ) -> Option<ProcValues> { 241 | let proc_values = match self { 242 | TemplateComponent::Title(title) => title.values(reference, hints, options), 243 | TemplateComponent::Contributor(contributor) => { 244 | contributor.values(reference, hints, options) 245 | } 246 | TemplateComponent::Date(date) => date.values(reference, hints, options), 247 | TemplateComponent::Number(number) => number.values(reference, hints, options), 248 | TemplateComponent::SimpleString(string) => { 249 | string.values(reference, hints, options) 250 | } 251 | TemplateComponent::List(_list) => todo!(), 252 | _ => None, 253 | }; 254 | Some(ProcValues { 255 | value: proc_values.as_ref()?.value.clone(), 256 | prefix: proc_values.as_ref()?.prefix.clone(), 257 | suffix: proc_values.as_ref()?.suffix.clone(), 258 | }) 259 | } 260 | } 261 | 262 | impl ComponentValues for TemplateNumber { 263 | fn values( 264 | &self, 265 | reference: &InputReference, 266 | _hints: &ProcHints, 267 | _options: &RenderOptions, 268 | ) -> Option<ProcValues> { 269 | let number: Option<String> = match &self.number { 270 | Numbers::Volume => match reference { 271 | InputReference::SerialComponent(serial_component) => { 272 | Some(serial_component.volume.as_ref()?.to_string()) 273 | } 274 | _ => None, 275 | }, 276 | Numbers::Issue => match reference { 277 | InputReference::SerialComponent(serial_component) => { 278 | Some(serial_component.issue.as_ref()?.to_string()) 279 | } 280 | _ => None, 281 | }, 282 | Numbers::Pages => match reference { 283 | InputReference::SerialComponent(serial_component) => { 284 | Some(serial_component.pages.as_ref()?.to_string()) 285 | } 286 | InputReference::CollectionComponent(monograph_component) => { 287 | Some(monograph_component.pages.as_ref()?.to_string()) 288 | } 289 | _ => None, 290 | }, 291 | }; 292 | Some(ProcValues { 293 | value: number.unwrap_or_default(), 294 | prefix: None, 295 | suffix: None, 296 | }) 297 | } 298 | } 299 | 300 | impl ComponentValues for TemplateSimpleString { 301 | fn values( 302 | &self, 303 | reference: &InputReference, 304 | _hints: &ProcHints, 305 | _options: &RenderOptions, 306 | ) -> Option<ProcValues> { 307 | let value = match self.variable { 308 | Variables::Doi => match reference { 309 | InputReference::SerialComponent(serial_component) => { 310 | Some(serial_component.doi.as_ref()?.to_string()) 311 | } 312 | InputReference::CollectionComponent(monograph_component) => { 313 | Some(monograph_component.doi.as_ref()?.to_string()) 314 | } 315 | _ => None, 316 | }, 317 | Variables::Isbn => match reference { 318 | InputReference::Monograph(monograph_component) => { 319 | Some(monograph_component.isbn.as_ref()?.to_string()) 320 | } 321 | _ => None, 322 | }, 323 | _ => None, // TODO completes 324 | }; 325 | Some(ProcValues { 326 | value: value.unwrap_or_default(), 327 | prefix: None, 328 | suffix: None, 329 | }) 330 | } 331 | } 332 | 333 | impl ComponentValues for TemplateTitle { 334 | fn values( 335 | &self, 336 | reference: &InputReference, 337 | _hints: &ProcHints, 338 | _options: &RenderOptions, 339 | ) -> Option<ProcValues> { 340 | let value = match &self.title { 341 | Titles::ParentMonograph => { 342 | if let InputReference::CollectionComponent(collection_component) = 343 | reference 344 | { 345 | Some(collection_component.parent.title.as_ref()?.to_string()) 346 | } else { 347 | None 348 | } 349 | } 350 | Titles::ParentSerial => { 351 | if let InputReference::SerialComponent(serial_component) = reference { 352 | Some(serial_component.parent.title.to_string()) 353 | } else { 354 | None 355 | } 356 | } 357 | Titles::Primary => match reference { 358 | InputReference::Monograph(monograph) => Some(monograph.title.to_string()), 359 | InputReference::Collection(collection) => { 360 | Some(collection.title.as_ref()?.to_string()) 361 | } 362 | InputReference::CollectionComponent(monograph_component) => { 363 | Some(monograph_component.title.as_ref()?.to_string()) 364 | } 365 | InputReference::SerialComponent(serial_component) => { 366 | Some(serial_component.title.as_ref()?.to_string()) 367 | } 368 | }, 369 | _ => None, 370 | }; 371 | Some(ProcValues { 372 | value: value.unwrap_or_default(), 373 | prefix: None, 374 | suffix: None, 375 | }) 376 | } 377 | } 378 | 379 | pub fn role_to_string( 380 | role: &ContributorRole, 381 | locale: Locale, 382 | form: ContributorForm, 383 | length: usize, 384 | ) -> Option<String> { 385 | let term = locale.roles.get(role)?; // FIXME causes panic 386 | match form { 387 | ContributorForm::Long => { 388 | if length > 1 { 389 | Some(term.plural.long.clone()) 390 | } else { 391 | Some(term.singular.long.clone()) 392 | } 393 | } 394 | ContributorForm::Short => { 395 | if length > 1 { 396 | Some(term.plural.short.clone()) 397 | } else { 398 | Some(term.singular.short.clone()) 399 | } 400 | } 401 | ContributorForm::Verb => Some(term.verb.long.clone()), 402 | ContributorForm::VerbShort => Some(term.verb.short.clone()), 403 | } 404 | } 405 | 406 | #[test] 407 | fn role_form_to_string() { 408 | use csln::style::locale::{ContributorTerm, Locale, SimpleTerm}; 409 | let mut locale = Locale::default(); 410 | locale.roles.insert( 411 | ContributorRole::Editor, 412 | ContributorTerm { 413 | singular: SimpleTerm { 414 | long: "editor".to_string(), 415 | short: "ed".to_string(), 416 | }, 417 | plural: SimpleTerm { 418 | long: "editors".to_string(), 419 | short: "eds".to_string(), 420 | }, 421 | verb: SimpleTerm { 422 | long: "edited by".to_string(), 423 | short: "ed".to_string(), 424 | }, 425 | }, 426 | ); 427 | let role = ContributorRole::Editor; 428 | let form = ContributorForm::Long; 429 | let length = 1; 430 | let result = role_to_string(&role, locale, form, length); 431 | assert_eq!(result, Some("editor".to_string())); 432 | } 433 | 434 | impl ComponentValues for TemplateContributor { 435 | fn values( 436 | &self, 437 | reference: &InputReference, 438 | _hints: &ProcHints, 439 | options: &RenderOptions, 440 | ) -> Option<ProcValues> { 441 | let locale = options.locale.clone(); 442 | match &self.contributor { 443 | ContributorRole::Author => { 444 | let author = reference.author(); 445 | if author.is_some() { 446 | Some(ProcValues { 447 | value: author?.format(options.global.clone(), locale), 448 | prefix: None, 449 | suffix: None, 450 | }) 451 | } else { 452 | // TODO generalize the substitution 453 | let add_role_form = 454 | // REVIEW is this correct? 455 | options.global.substitute.clone()?.contributor_role_form; 456 | let editor = reference.editor()?; 457 | let editor_length = editor.names(options.global.clone(), true).len(); 458 | // get the role string; if it's in fact author, it will be None 459 | let suffix = add_role_form.map(|role_form| { 460 | role_to_string( 461 | &ContributorRole::Editor, 462 | locale.clone(), 463 | role_form, 464 | editor_length, 465 | ) 466 | }); 467 | let suffix_padded = suffix.and_then(|s| Some(format!(" {}", s?))); // TODO extract this into separate method 468 | Some(ProcValues { 469 | value: editor.format(options.global.clone(), locale), 470 | prefix: None, 471 | suffix: suffix_padded, 472 | }) 473 | } 474 | } 475 | ContributorRole::Editor => { 476 | match reference { 477 | &InputReference::Collection(_) => None, 478 | _ => { 479 | let editor = &reference.editor()?; 480 | let form = &self.form; 481 | let editor_length = 482 | editor.names(options.global.clone(), true).len(); 483 | // TODO handle verb and non-verb forms 484 | 485 | match form { 486 | ContributorForm::Verb | ContributorForm::VerbShort => { 487 | let prefix = role_to_string( 488 | &self.contributor, 489 | locale.clone(), 490 | form.clone(), 491 | editor_length, 492 | ); 493 | let prefix_padded = prefix.and_then(|s| { 494 | if s.is_empty() { 495 | None 496 | } else { 497 | Some(format!("{} ", s)) 498 | } 499 | }); 500 | Some(ProcValues { 501 | value: editor.format(options.global.clone(), locale), 502 | prefix: prefix_padded, 503 | suffix: None, 504 | }) 505 | } 506 | _ => { 507 | let suffix = role_to_string( 508 | &self.contributor, 509 | locale.clone(), 510 | form.clone(), 511 | editor_length, 512 | ); 513 | let suffix_padded = suffix.and_then(|s| { 514 | if s.is_empty() { 515 | None 516 | } else { 517 | Some(format!(" {}", s)) 518 | } 519 | }); 520 | Some(ProcValues { 521 | value: editor.format(options.global.clone(), locale), 522 | prefix: None, 523 | suffix: suffix_padded, // TODO handle None 524 | }) 525 | } 526 | } 527 | } 528 | } 529 | } 530 | ContributorRole::Translator => Some(ProcValues { 531 | value: reference.translator()?.format(options.global.clone(), locale), 532 | prefix: None, 533 | suffix: None, 534 | }), 535 | ContributorRole::Publisher => Some(ProcValues { 536 | value: reference.publisher()?.format(options.global.clone(), locale), 537 | prefix: None, 538 | suffix: None, 539 | }), 540 | // TODO implement the rest 541 | _ => None, 542 | } 543 | } 544 | } 545 | 546 | impl ComponentValues for TemplateDate { 547 | fn values( 548 | &self, 549 | reference: &InputReference, 550 | hints: &ProcHints, 551 | options: &RenderOptions, 552 | ) -> Option<ProcValues> { 553 | let locale: &Locale = &options.locale; 554 | let input_date: EdtfString = match &self.date { 555 | Dates::Issued => reference.issued()?, 556 | Dates::OriginalPublished => todo!("original-published"), 557 | Dates::Accessed => todo!("accessed"), 558 | }; 559 | let parsed_date = input_date.parse(); 560 | //print!("date form: {:?}", reference.issued); 561 | let formatted_date: String = match self.form { 562 | DateForm::Year => parsed_date 563 | .year() // this line causes a panic if the date is not a year 564 | .to_string(), 565 | DateForm::YearMonth => { 566 | input_date.year_month(locale.dates.months.long.clone()) 567 | } 568 | DateForm::MonthDay => input_date.month_day(locale.dates.months.long.clone()), 569 | DateForm::Full => todo!(), 570 | }; 571 | 572 | // TODO: implement this along with localized dates 573 | fn _config_fmt(options: &RenderOptions) -> DateTimeFormatterOptions { 574 | let date_options = match options.global.dates.clone() { 575 | Some(dates) => dates, 576 | None => return DateTimeFormatterOptions::default(), // or handle the None case accordingly 577 | }; 578 | match date_options.month { 579 | MonthFormat::Long => todo!("long"), 580 | MonthFormat::Short => todo!("short"), 581 | MonthFormat::Numeric => todo!("numeric"), 582 | }; 583 | } 584 | 585 | fn int_to_letter(n: u32) -> String { 586 | let c = n + 96; 587 | match char::from_u32(c) { 588 | Some(ch) => ch.to_string(), 589 | None => "".to_string(), 590 | } 591 | } 592 | 593 | let suffix = if hints.disamb_condition 594 | // TODO need to check form here also 595 | // && self.form == style::template::DateForm::Year 596 | // REVIEW: ugly, and needs to be smarter 597 | && options.global.processing.clone().unwrap_or_default().config().disambiguate.unwrap_or_default().year_suffix 598 | && formatted_date.len() == 4 599 | { 600 | int_to_letter((hints.group_index % 26) as u32) 601 | } else { 602 | "".to_string() 603 | }; 604 | Some(ProcValues { 605 | value: formatted_date, 606 | prefix: None, 607 | suffix: Some(suffix), // put the suffix here, in case we need to do something with it 608 | }) 609 | } 610 | } 611 | 612 | // #[test] 613 | // fn render_year() { 614 | // let component = StyleTemplateDate { 615 | // date: Dates::Issued, 616 | // form: DateForm::Year, 617 | // rendering: None, 618 | // }; 619 | // let reference = InputReference { 620 | // id: Some("test".to_string()), 621 | // issued: Some(RefDate::Structured(Edtf::from_str("2020").unwrap())), 622 | // ..Default::default() 623 | // }; 624 | // let options = RenderOptions { 625 | // global: &StyleOptions::default(), 626 | // local: &StyleOptions::default(), 627 | // }; 628 | // let rendered_date = component.render(&reference, &ProcHints::default(), &options); 629 | // assert_eq!(rendered_date, "2020"); 630 | // } 631 | 632 | /// The intermediate representation of renderered citations and bibliography.. 633 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 634 | pub struct ProcReferences { 635 | pub bibliography: ProcBibliography, 636 | /// Process the citations, if there are any. 637 | pub citations: Option<ProcCitations>, 638 | } 639 | 640 | pub type ProcBibliography = Vec<ProcTemplate>; 641 | pub type ProcCitationItem = Vec<ProcTemplateComponent>; 642 | pub type ProcCitation = Vec<ProcCitationItem>; 643 | pub type ProcCitations = Vec<ProcCitation>; 644 | 645 | impl Processor { 646 | /// Render references to AST. 647 | #[inline] 648 | pub fn process_references(&self) -> ProcReferences { 649 | let sorted_references = self.sort_references(self.get_references()); 650 | let bibliography: ProcBibliography = sorted_references 651 | .par_iter() 652 | .map(|reference| self.process_reference(reference)) 653 | .collect(); 654 | let citations = if self.citations.is_empty() { 655 | None 656 | } else { 657 | Some(self.process_citations(&self.citations)) 658 | }; 659 | ProcReferences { bibliography, citations } 660 | } 661 | 662 | fn process_citations(&self, citations: &Citations) -> ProcCitations { 663 | citations 664 | .iter() 665 | .map(|citation| self.process_citation(citation)) 666 | .collect() 667 | } 668 | 669 | fn process_citation(&self, citation: &Citation) -> ProcCitation { 670 | // TODO handle the prefix and suffix, though am uncertain how to best do that 671 | let pcitation = citation 672 | .citation_items 673 | .iter() 674 | .filter_map(|citation_item| self.process_citation_item(citation_item)) 675 | .collect(); 676 | println!("pcitation: {:?}", pcitation); 677 | pcitation 678 | } 679 | 680 | pub fn process_citation_item( 681 | &self, 682 | citation_item: &CitationItem, 683 | ) -> Option<ProcCitationItem> { 684 | let citation_style = self.style.citation.clone(); 685 | // FIXME below is returning None 686 | let reference = match self.get_reference(&citation_item.ref_id) { 687 | Ok(reference) => reference, 688 | Err(_) => return None, // or handle the error in a different way 689 | }; 690 | let proc_template = 691 | self.process_template(&reference, citation_style?.template.as_slice()); 692 | println!("proc_template: {:?}", proc_template); 693 | Some(proc_template) 694 | } 695 | 696 | /// Render a reference to AST. 697 | fn process_reference( 698 | &self, 699 | reference: &InputReference, 700 | ) -> Vec<ProcTemplateComponent> { 701 | let bibliography_style = self.style.bibliography.clone().unwrap(); 702 | // TODO bibliography should probably be Optional 703 | self.process_template(reference, bibliography_style.template.as_slice()) 704 | } 705 | 706 | fn get_render_options(&self, style: Style, locale: Locale) -> RenderOptions { 707 | RenderOptions { 708 | global: style.options.unwrap_or_default(), 709 | local: Config::default(), 710 | locale, 711 | } 712 | } 713 | 714 | fn process_template( 715 | &self, 716 | reference: &InputReference, 717 | template: &[TemplateComponent], 718 | ) -> ProcTemplate { 719 | template 720 | .iter() 721 | .filter_map(|component| self.process_template_component(component, reference)) 722 | .collect() 723 | } 724 | 725 | fn process_template_component( 726 | &self, 727 | component: &TemplateComponent, 728 | reference: &InputReference, 729 | ) -> Option<ProcTemplateComponent> { 730 | let hints = self.get_proc_hints(); 731 | let reference_id: Option<RefID> = reference.id(); 732 | let hint: ProcHints = 733 | // TODO why would reference_id be None? 734 | hints.get(&reference_id.unwrap_or_default()).cloned().unwrap_or_default(); 735 | let options = self.get_render_options(self.style.clone(), self.locale.clone()); 736 | let values = component.values(reference, &hint, &options)?; 737 | let template_component = component.clone(); 738 | // TODO add role here if specified in the style 739 | // TODO affixes from style? 740 | if !values.value.is_empty() { 741 | Some(ProcTemplateComponent { 742 | template_component, 743 | values: ProcValues { 744 | value: values.value, 745 | prefix: values.prefix, 746 | suffix: values.suffix, 747 | }, 748 | }) 749 | } else { 750 | None 751 | } 752 | } 753 | 754 | /// Get references from the bibliography. 755 | pub fn get_references(&self) -> Vec<InputReference> { 756 | self.bibliography 757 | .iter() 758 | .map(|(key, reference)| match reference { 759 | InputReference::Monograph(monograph) => { 760 | let mut input_reference = 761 | InputReference::Monograph(monograph.clone()); 762 | input_reference.set_id(key.clone()); 763 | input_reference 764 | } 765 | InputReference::CollectionComponent(collection_component) => { 766 | let mut input_reference = 767 | InputReference::CollectionComponent(collection_component.clone()); 768 | input_reference.set_id(key.clone()); 769 | input_reference 770 | } 771 | InputReference::SerialComponent(serial_component) => { 772 | let mut input_reference = 773 | InputReference::SerialComponent(serial_component.clone()); 774 | input_reference.set_id(key.clone()); 775 | input_reference 776 | } 777 | InputReference::Collection(collection) => { 778 | let mut input_reference = 779 | InputReference::Collection(collection.clone()); 780 | input_reference.set_id(key.clone()); 781 | input_reference 782 | } 783 | }) 784 | .collect() 785 | } 786 | 787 | /// Get a reference from the bibliography by id/citekey. 788 | pub fn get_reference(&self, id: &str) -> Result<InputReference, String> { 789 | match self.bibliography.get(id) { 790 | Some(reference) => Ok(reference.clone()), 791 | None => Err(format!("Invalid reference ID: {}", id)), 792 | } 793 | } 794 | 795 | pub fn get_cited_references(&self) -> Vec<InputReference> { 796 | let mut cited_references = Vec::new(); 797 | for key in &self.get_cited_keys() { 798 | if let Ok(reference) = self.get_reference(key) { 799 | cited_references.push(reference); 800 | } 801 | } 802 | cited_references 803 | } 804 | 805 | /// Return a list of all the keys cited in the document, in order. 806 | pub fn get_cited_keys(&self) -> Vec<String> { 807 | self.citations 808 | .iter() 809 | .flat_map(|c| { 810 | c.citation_items 811 | .iter() 812 | .map(|cr| cr.ref_id.clone()) 813 | .collect::<Vec<String>>() 814 | }) 815 | .collect() 816 | } 817 | 818 | /// Sort the references according to instructions in the style. 819 | #[inline] 820 | pub fn sort_references( 821 | &self, 822 | references: Vec<InputReference>, 823 | ) -> Vec<InputReference> { 824 | let mut references: Vec<InputReference> = references; 825 | let options: Config = self.style.options.clone().unwrap_or_default(); 826 | if let Some(sort_config) = 827 | options.processing.clone().unwrap_or_default().config().sort 828 | { 829 | sort_config.template.iter().rev().for_each(|sort| match sort.key { 830 | SortKey::Author => { 831 | references.par_sort_by(|a, b| { 832 | let a_author = match a.author() { 833 | Some(author) => author.names(options.clone(), true).join("-"), 834 | None => match self.get_author_substitute(a) { 835 | Some((substitute, _)) => substitute, 836 | None => "".to_string(), 837 | }, 838 | }; 839 | 840 | let b_author = match b.author() { 841 | Some(author) => author.names(options.clone(), true).join("-"), 842 | None => match self.get_author_substitute(b) { 843 | Some((substitute, _)) => substitute, 844 | None => "".to_string(), 845 | }, 846 | }; 847 | a_author.to_lowercase().cmp(&b_author.to_lowercase()) 848 | }); 849 | } 850 | SortKey::Year => { 851 | references.par_sort_by(|a: &InputReference, b: &InputReference| { 852 | let a_year = a.issued().as_ref().unwrap().year(); 853 | let b_year = b.issued().as_ref().unwrap().year(); 854 | b_year.cmp(&a_year) 855 | }); 856 | } 857 | _ => {} 858 | }); 859 | } 860 | references 861 | } 862 | 863 | /// Process the references and return a HashMap of ProcHints. 864 | pub fn get_proc_hints(&self) -> HashMap<String, ProcHints> { 865 | let refs = self.get_references(); 866 | let sorted_refs = self.sort_references(refs); 867 | let grouped_refs = self.group_references(sorted_refs); 868 | let proc_hints = grouped_refs 869 | .iter() 870 | .flat_map(|(key, group)| { 871 | let group_len = group.len(); 872 | group.iter().enumerate().map( 873 | move |(index, reference)| -> (String, ProcHints) { 874 | // TODO will need to generalize. 875 | let disambiguate = group_len > 1; 876 | let proc_hint = ProcHints { 877 | disamb_condition: disambiguate, 878 | group_index: index + 1, 879 | group_length: group_len, 880 | group_key: key.clone(), 881 | }; 882 | let ref_id = match reference { 883 | InputReference::Monograph(monograph) => monograph.id.clone(), 884 | InputReference::CollectionComponent(collection_component) => { 885 | collection_component.id.clone() 886 | } 887 | InputReference::SerialComponent(serial_component) => { 888 | serial_component.id.clone() 889 | } 890 | InputReference::Collection(collection) => { 891 | collection.id.clone() 892 | } 893 | }; 894 | (ref_id.unwrap(), proc_hint) 895 | }, 896 | ) 897 | }) 898 | .collect(); 899 | proc_hints 900 | } 901 | 902 | /// Return a string to use for grouping for a given reference, using instructions in the style. 903 | fn make_group_key(&self, reference: &InputReference) -> String { 904 | let options: csln::style::options::Config = match self.style.options { 905 | Some(ref options) => options.clone(), 906 | None => Config::default(), // TODO is this right? 907 | }; 908 | let group_config = options.processing.unwrap_or_default().config().group.unwrap(); 909 | let options = self.style.options.clone(); 910 | let as_sorted = false; 911 | let group_key = group_config 912 | .template 913 | // This is likely unnecessary, but just in case. 914 | .par_iter() 915 | .map(|key| match key { 916 | SortKey::Author => match reference.author() { 917 | Some(author) => { 918 | author.names(options.clone().unwrap(), as_sorted).join("-") 919 | } 920 | None => "".to_string(), 921 | }, 922 | SortKey::Year => { 923 | reference.issued().as_ref().unwrap().parse().year().to_string() 924 | } 925 | SortKey::Title => reference.title().as_ref().unwrap().to_string(), 926 | _ => "".to_string(), // REVIEW is this right? 927 | }) 928 | .collect::<Vec<String>>() 929 | .join(":"); 930 | group_key 931 | } 932 | 933 | pub fn get_author_substitute( 934 | &self, 935 | reference: &InputReference, 936 | ) -> Option<(String, SubstituteKey)> { 937 | let options = self.style.options.as_ref().unwrap().clone(); // FIXME default? 938 | let substitute_config = options.substitute.clone(); // FIXME default? the below line panics 939 | substitute_config 940 | .unwrap_or_default() 941 | .template 942 | .iter() 943 | .find_map(|substitute_key| match *substitute_key { 944 | SubstituteKey::Editor => { 945 | let names = 946 | reference.editor()?.format(options.clone(), self.locale.clone()); 947 | Some((names, substitute_key.clone())) 948 | } 949 | _ => None, 950 | }) 951 | } 952 | 953 | // #[cfg(test)] 954 | // fn author_substitution() { 955 | // use csln::bibliography::reference::{Collection, StructuredName}; 956 | // let component = TemplateContributor { 957 | // contributor: ContributorRole::Author, 958 | // rendering: None, 959 | // form: csln::style::template::ContributorForm::Long, 960 | // }; 961 | // let reference = Collection { 962 | // id: Some("test".to_string()), 963 | // editor: Some(csln::bibliography::reference::Contributor::StructuredName( 964 | // StructuredName { 965 | // family: "Editor".to_string(), 966 | // given: "Jane".to_string(), 967 | // }, 968 | // )), 969 | // r#type: csln::bibliography::reference::CollectionType::EditedBook, 970 | // issued: EdtfString("2020".to_string()), 971 | // title: None, 972 | // url: None, 973 | // accessed: None, 974 | // translator: None, 975 | // publisher: None, 976 | // note: None, 977 | // isbn: None, 978 | // }; 979 | // (assert_eq!( 980 | // component.value( 981 | // &InputReference::Collection(reference), 982 | // &ProcHints::default(), 983 | // &RenderOptions::default() 984 | // ), 985 | // Some("Jane Editor".to_string()) 986 | // )); 987 | // } 988 | 989 | /// Group references according to instructions in the style. 990 | #[inline] 991 | pub fn group_references( 992 | &self, 993 | references: Vec<InputReference>, 994 | ) -> HashMap<String, Vec<InputReference>> { 995 | references 996 | .into_iter() 997 | .group_by(|reference| self.make_group_key(reference)) 998 | .into_iter() 999 | .map(|(key, group)| (key, group.collect())) 1000 | .collect() 1001 | } 1002 | 1003 | pub fn new( 1004 | style: Style, 1005 | bibliography: Bibliography, 1006 | citations: Citations, 1007 | locale: Locale, 1008 | ) -> Processor { 1009 | Processor { style, bibliography, citations, locale } 1010 | } 1011 | } 1012 | --------------------------------------------------------------------------------