├── .chglog ├── CHANGELOG.tpl.md └── config.yml ├── .github └── workflows │ └── quickstart.yml ├── .gitignore ├── .rustfmt.toml ├── .vscode └── settings.json ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── cli ├── .gitignore ├── Cargo.toml ├── README.md └── src │ ├── main.rs │ └── makeschemas.rs ├── csln ├── Cargo.toml └── src │ ├── bibliography │ ├── .gitignore │ ├── README.md │ ├── mod.rs │ └── reference.rs │ ├── citation │ ├── .gitignore │ └── mod.rs │ ├── lib.rs │ └── style │ ├── .gitignore │ ├── README.md │ ├── locale.rs │ ├── mod.rs │ ├── options.rs │ └── template.rs └── processor ├── .gitignore ├── Cargo.toml ├── README.md ├── benches └── proc_bench.rs ├── examples ├── chicago-ad-experiment.yaml ├── chicago.bib.yaml ├── citation.yaml ├── ex1.bib.yaml └── style.csl.yaml ├── locales └── locale-en.yaml ├── src └── lib.rs └── tests └── processor_test.rs /.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 | -------------------------------------------------------------------------------- /.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@v2 18 | 19 | - name: Install stable toolchain 20 | uses: actions-rs/toolchain@v1 21 | with: 22 | profile: minimal 23 | toolchain: stable 24 | override: true 25 | 26 | - name: Run cargo check 27 | uses: actions-rs/cargo@v1 28 | continue-on-error: true # WARNING: only for this example, remove it! 29 | with: 30 | command: check 31 | 32 | test: 33 | name: Test Suite 34 | runs-on: ubuntu-latest 35 | steps: 36 | - name: Checkout sources 37 | uses: actions/checkout@v2 38 | 39 | - name: Install stable toolchain 40 | uses: actions-rs/toolchain@v1 41 | with: 42 | profile: minimal 43 | toolchain: stable 44 | override: true 45 | 46 | - name: Run cargo test 47 | uses: actions-rs/cargo@v1 48 | continue-on-error: true # WARNING: only for this example, remove it! 49 | with: 50 | command: test 51 | 52 | lints: 53 | name: Lints 54 | runs-on: ubuntu-latest 55 | steps: 56 | - name: Checkout sources 57 | uses: actions/checkout@v2 58 | 59 | - name: Install stable toolchain 60 | uses: actions-rs/toolchain@v1 61 | with: 62 | profile: minimal 63 | toolchain: stable 64 | override: true 65 | components: rustfmt, clippy 66 | 67 | - name: Run cargo fmt 68 | uses: actions-rs/cargo@v1 69 | continue-on-error: true # WARNING: only for this example, remove it! 70 | with: 71 | command: fmt 72 | args: --all -- --check 73 | 74 | - name: Run cargo clippy 75 | uses: actions-rs/cargo@v1 76 | continue-on-error: true # WARNING: only for this example, remove it! 77 | with: 78 | command: clippy 79 | args: -- -D warnings 80 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Cargo.lock 2 | target 3 | schemas 4 | *.bak 5 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 = "allow" 15 | expect_used = "warn" 16 | large_enum_variant = "allow" 17 | needless_borrow = "warn" 18 | needless_question_mark = "warn" 19 | needless_return = "warn" 20 | style = "allow" 21 | unwrap_used = "warn" 22 | 23 | [profile.release] 24 | lto = true 25 | codegen-units = 1 26 | panic = "abort" 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | ## Contribution 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 | Please contact me via discussions or the issue tracker, or by email, if you'd like to contribute. 81 | 82 | I licensed the code here under the same terms as [citeproc-rs][CSLRS], in case code might be shared between them. 83 | I also understand the Mozilla 2.0 license is compatible with Apache. 84 | 85 | A note on citeproc-rs: 86 | 87 | In reviewing the code, it strikes me pieces of it obviously complement this code base. 88 | 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. 89 | 90 | [CSL]: https://citationstyles.org/ 91 | [CSLNJS]: https://github.com/bdarcus/csl-next 92 | [CSLNO]: https://github.com/bdarcus/csln/blob/main/csln/src/style/options.rs 93 | [CSLRS]: https://github.com/zotero/citeproc-rs 94 | [CSLO]: https://github.com/citation-style-language 95 | [CSL-spec]: https://docs.citationstyles.org/en/stable/specification.html 96 | [CSL-styles]: https://github.com/citation-style-language/styles 97 | [CSL-macros]: https://docs.citationstyles.org/en/stable/specification.html#macros 98 | [CSL-templates]: https://docs.citationstyles.org/en/stable/specification.html#layout-1 99 | [CSL-render]: https://docs.citationstyles.org/en/stable/specification.html#rendering-elements 100 | [CSL-options]: https://docs.citationstyles.org/en/stable/specification.html#options 101 | [BLTX]: https://github.com/plk/biblatex 102 | [EDTF]: https://www.loc.gov/standards/datetime/ 103 | [haya]: https://github.com/typst/hayagriva 104 | [sel]: https://github.com/typst/hayagriva/blob/main/docs/selectors.md 105 | -------------------------------------------------------------------------------- /cli/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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() { 25 | let opts = Opts::parse(); 26 | let style = from_file(&opts.style).context("Style file?"); 27 | let bibliography = from_file(&opts.bibliography).context("Bibliography file?"); 28 | let citations: Citations = if opts.citations.is_none() { 29 | Citations::default() 30 | } else { 31 | from_file(opts.citations.unwrap()).unwrap_or_default() 32 | }; 33 | let locale = from_file(&opts.locale).context("Locale file?"); 34 | let processor: Processor = Processor::new( 35 | style.expect("msg"), // REVIEW why? 36 | bibliography.expect("msg"), 37 | citations, 38 | locale.expect("msg"), 39 | ); 40 | let rendered_refs: ProcReferences = processor.process_references(); 41 | let serialized_refs = serde_json::to_string_pretty(&rendered_refs); 42 | //println!("{}", refs_to_string(rendered_refs)); 43 | if serialized_refs.is_err() { 44 | println!("Error: {:?}", serialized_refs); 45 | } else { 46 | println!("{}", serialized_refs.unwrap()); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /csln/src/bibliography/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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(), 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().unwrap(); 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 | -------------------------------------------------------------------------------- /csln/src/citation/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /csln/src/citation/mod.rs: -------------------------------------------------------------------------------- 1 | use schemars::JsonSchema; 2 | use serde::{Deserialize, Serialize}; 3 | 4 | pub type Citations = Vec<Citation>; 5 | 6 | /// A vector of Citation objects. 7 | #[derive(Debug, Default, Serialize, Deserialize, JsonSchema)] 8 | pub struct CitationList(pub Vec<Citation>); 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<i32>, 29 | pub id: Option<String>, 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<String>, 35 | /// A vector of CitationItem objects. 36 | pub citation_items: Vec<CitationItem>, 37 | /// A string that follows a list of qcitation references. 38 | pub suffix: Option<String>, 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<String>, 55 | /// A string that prefaces the citation reference. 56 | pub prefix: Option<String>, 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<Vec<Locator>>, 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 | -------------------------------------------------------------------------------- /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<T: Parsable, P: AsRef<Path>>(path: P) -> Result<T> { 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 | -------------------------------------------------------------------------------- /csln/src/style/.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 | -------------------------------------------------------------------------------- /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<super::template::ContributorRole, ContributorTerm>, 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<String>, 20 | pub and_symbol: Option<String>, 21 | pub and_others: Option<String>, 22 | pub anonymous: SimpleTerm, 23 | pub at: Option<String>, 24 | pub accessed: Option<String>, 25 | pub available_at: Option<String>, 26 | pub by: Option<String>, 27 | pub circa: SimpleTerm, 28 | pub et_al: Option<String>, 29 | pub from: Option<String>, 30 | pub ibid: Option<String>, 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<String>, 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<String>; 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/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<HashMap<String, Template>>, 23 | /// Parameter groups. 24 | #[serde(default)] 25 | pub options: Option<Config>, 26 | /// The citation specification. 27 | pub citation: Option<Citation>, 28 | /// The bibliography specification. 29 | pub bibliography: Option<Bibliography>, 30 | } 31 | 32 | /// The Template model. 33 | pub type Template = Vec<TemplateComponent>; 34 | 35 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 36 | /// The bibliography specification. 37 | pub struct Bibliography { 38 | pub options: Option<options::Config>, 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<Config>, 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<Vec<Category>>, 54 | /// The description of the style. 55 | pub description: Option<String>, 56 | /// The machine-readable token that uniquely identifies the style. 57 | pub id: Option<String>, 58 | /// The human-readable name of the style. 59 | pub title: Option<String>, 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 | -------------------------------------------------------------------------------- /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<Substitute>, 41 | pub processing: Option<Processing>, 42 | pub localize: Option<Localize>, 43 | pub contributors: Option<ContributorConfig>, 44 | pub dates: Option<Date>, 45 | pub titles: Option<TitlesConfig>, 46 | } 47 | 48 | #[derive(JsonSchema, Debug, Default, PartialEq, Clone, Serialize, Deserialize)] 49 | pub struct TitlesConfig { 50 | component: Option<Rendering>, 51 | monograph: Option<Rendering>, 52 | default: Option<Rendering>, 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<Sort>, 69 | pub group: Option<Group>, 70 | pub disambiguate: Option<Disambiguation>, 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<DisplayAsSort>, 148 | /// Shorten the list of contributors. 149 | pub shorten: Option<ShortenListOptions>, 150 | /// The delimiter or separator to use between contributors. 151 | pub delimiter: Option<String>, 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<AndOptions>, 154 | /// When and how to display contributor roles. 155 | pub role: Option<RoleOptions>, 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<String>, 184 | pub form: String, // TODO 185 | pub rendering: Option<Rendering>, 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<SortKey>, 257 | } 258 | 259 | #[derive(JsonSchema, Debug, PartialEq, Clone, Serialize, Deserialize)] 260 | pub struct Substitute { 261 | pub contributor_role_form: Option<super::template::ContributorForm>, 262 | pub template: Vec<SubstituteKey>, 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<SortSpec>, 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 | -------------------------------------------------------------------------------- /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<bool>, 8 | pub quote: Option<bool>, 9 | pub strong: Option<bool>, 10 | pub prefix: Option<String>, 11 | pub suffix: Option<String>, 12 | pub wrap: Option<WrapPunctuation>, 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<Rendering> { 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<Rendering>, 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<NumberForm>, 80 | pub rendering: Option<Rendering>, 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<DelimiterPunctuation>, 103 | pub prefix: Option<String>, 104 | pub suffix: Option<String>, 105 | pub wrap: Option<WrapPunctuation>, 106 | pub items: Vec<TemplateComponent>, 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<Rendering>, 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<Rendering>, 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<TitleForm>, 189 | pub rendering: Option<Rendering>, 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 | -------------------------------------------------------------------------------- /processor/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | -------------------------------------------------------------------------------- /processor/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csln-processor" 3 | authors = ["Bruce D'Arcus <bdarcus@gmail.com>"] 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 | 35 | [[bench]] 36 | name = "proc_bench" 37 | harness = false 38 | 39 | [lints] 40 | workspace = true 41 | -------------------------------------------------------------------------------- /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/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<Citation> = 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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" -------------------------------------------------------------------------------- /processor/src/lib.rs: -------------------------------------------------------------------------------- 1 | /* 2 | SPDX-License-Identifier: MPL-2.0 3 | SPDX-FileCopyrightText: © 2023 Bruce D'Arcus 4 | */ 5 | 6 | use csln::bibliography::reference::InputReference; 7 | use csln::bibliography::reference::{EdtfString, RefID}; 8 | use csln::bibliography::InputBibliography as Bibliography; 9 | use csln::citation::{Citation, CitationItem, Citations}; 10 | use csln::style::locale::Locale; 11 | use csln::style::options::{Config, MonthFormat, SortKey, SubstituteKey}; 12 | use csln::style::template::{ 13 | ContributorForm, ContributorRole, DateForm, Dates, Numbers, TemplateComponent, 14 | TemplateContributor, TemplateDate, TemplateNumber, TemplateSimpleString, 15 | TemplateTitle, Titles, Variables, WrapPunctuation, 16 | }; 17 | use csln::style::Style; 18 | use icu::datetime::DateTimeFormatterOptions; 19 | use itertools::Itertools; 20 | use rayon::prelude::*; 21 | use schemars::JsonSchema; 22 | use serde::{Deserialize, Serialize}; 23 | //use std::cmp::Ordering; 24 | //use anyhow::Result; 25 | use std::collections::HashMap; 26 | use std::fmt::{self, Debug, Display, Formatter}; 27 | use std::option::Option; 28 | 29 | /* 30 | This is the processor code. 31 | 32 | The basic design is the same as the csl-next typescript implementation: 33 | 34 | The processor takes a style, a bibliography, and a locale, and renders the output. 35 | 36 | The primary target is a JSON AST, represented by the ProcTemplateComponent struct. 37 | */ 38 | 39 | // TODO: This will need to be generalized later. See: 40 | // https://github.com/bdarcus/csln/issues/105 41 | pub fn refs_to_string(proc_templates: Vec<ProcTemplate>) -> String { 42 | proc_templates 43 | .iter() 44 | .map(|proc_template| { 45 | proc_template 46 | .iter() 47 | .map(|proc_template_component| proc_template_component.to_string()) 48 | .collect::<Vec<String>>() 49 | .join(". ") 50 | + "." 51 | }) 52 | .collect::<Vec<String>>() 53 | .join("\n\n") 54 | } 55 | 56 | /// The processor struct, which takes a style, a bibliography, and a locale, and renders the output. 57 | #[derive(Debug, Default, Deserialize, Serialize)] 58 | pub struct Processor { 59 | /// The input style. 60 | style: Style, 61 | /// The input bibliography. 62 | bibliography: Bibliography, 63 | /// The input citations. 64 | citations: Citations, 65 | /// The output locale. 66 | locale: Locale, 67 | } 68 | 69 | /// The intermediate representation of a StyleTemplate, which is used to render the output. 70 | pub type ProcTemplate = Vec<ProcTemplateComponent>; 71 | 72 | /// The intermediate representation of a StyleTemplateComponent, which is used to render the output. 73 | /// This struct will have two fields: a StyleComponent and a String. 74 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 75 | #[serde(rename_all = "camelCase")] 76 | pub struct ProcTemplateComponent { 77 | /// The original input style template component, which provides rendering instructions. 78 | pub template_component: TemplateComponent, 79 | /// The string to render. 80 | pub values: ProcValues, 81 | } 82 | 83 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 84 | #[serde(rename_all = "camelCase")] 85 | /// Holds one or more processed strings, ready for final rendering. 86 | pub struct ProcValues { 87 | /// The primary string to render. 88 | pub value: String, 89 | /// The prefix to render. 90 | pub prefix: Option<String>, 91 | /// The suffix to render. 92 | pub suffix: Option<String>, 93 | } 94 | 95 | #[test] 96 | fn render_proc_template_component() { 97 | use csln::style::template::Rendering; 98 | let template_component = TemplateComponent::SimpleString(TemplateSimpleString { 99 | variable: Variables::Doi, 100 | rendering: Some(Rendering { 101 | emph: Some(true), 102 | quote: Some(true), 103 | strong: Some(true), 104 | prefix: Some("doi: ".to_string()), 105 | suffix: Some(" ||".to_string()), 106 | wrap: Some(WrapPunctuation::Parentheses), 107 | }), 108 | }); 109 | let value = "10/1234".to_string(); 110 | let proc_template_component = ProcTemplateComponent::new( 111 | template_component, 112 | ProcValues { value, prefix: None, suffix: None }, 113 | ); 114 | assert_eq!(proc_template_component.to_string(), "(doi: 10/1234 ||)".to_string()); 115 | } 116 | 117 | impl Display for ProcTemplateComponent { 118 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 119 | let rendering = self.template_component.rendering(); 120 | let prefix: String = rendering 121 | .clone() // REVIEW this compiles, but too much cloning 122 | .unwrap_or_default() 123 | .prefix 124 | .unwrap_or_default(); 125 | let suffix: String = 126 | rendering.clone().unwrap_or_default().suffix.unwrap_or_default(); 127 | let wrap: WrapPunctuation = 128 | rendering.unwrap_or_default().wrap.unwrap_or_default(); 129 | let wrap_punct: (String, String) = match wrap { 130 | WrapPunctuation::None => ("".to_string(), "".to_string()), 131 | WrapPunctuation::Parentheses => ("(".to_string(), ")".to_string()), 132 | WrapPunctuation::Brackets => ("[".to_string(), "]".to_string()), 133 | }; 134 | // REVIEW: is this where to plugin different renderers? 135 | // Also, how to handle the different affixes, including within the values? 136 | let result = wrap_punct.0 137 | + &prefix 138 | + &self.values.prefix.clone().unwrap_or_default() 139 | + &self.values.value 140 | + &self.values.suffix.clone().unwrap_or_default() 141 | + &suffix 142 | + &wrap_punct.1; 143 | write!(f, "{}", result) 144 | } 145 | } 146 | 147 | impl ProcTemplateComponent { 148 | pub fn new(template_component: TemplateComponent, values: ProcValues) -> Self { 149 | ProcTemplateComponent { template_component, values } 150 | } 151 | } 152 | 153 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 154 | #[serde(rename_all = "kebab-case")] 155 | /// Holds the intermediate processing hints for a reference that can be used 156 | /// to render the output; particularly for disambiguation. 157 | pub struct ProcHints { 158 | /// Whether or not the reference needs to be disambiguated. 159 | pub disamb_condition: bool, 160 | /// The index of the reference in the group, starting at 1. 161 | pub group_index: usize, 162 | /// The number of references in the group. 163 | pub group_length: usize, 164 | /// The key of the group. 165 | pub group_key: String, 166 | } 167 | 168 | impl ProcHints { 169 | pub fn new( 170 | disamb_condition: bool, 171 | group_index: usize, 172 | group_length: usize, 173 | group_key: String, 174 | ) -> Self { 175 | ProcHints { 176 | disamb_condition, 177 | group_index, 178 | group_length, 179 | group_key, 180 | } 181 | } 182 | } 183 | 184 | impl Default for ProcHints { 185 | fn default() -> Self { 186 | ProcHints { 187 | disamb_condition: false, 188 | group_index: 0, 189 | group_length: 0, 190 | group_key: "".to_string(), 191 | } 192 | } 193 | } 194 | 195 | #[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema)] 196 | /// Configuration options. 197 | pub struct RenderOptions { 198 | // Options for the style, including default options. 199 | global: Config, 200 | // Options for the citaton or bibliography, that may override the style options. 201 | local: Config, 202 | // Locale for the output. 203 | locale: Locale, 204 | } 205 | 206 | /// The intermediate representation of a TemplateComponent, which is used to render the output. 207 | pub trait ProcessComponent<T> { 208 | fn process( 209 | &self, 210 | reference: &InputReference, 211 | component: &T, 212 | options: RenderOptions, 213 | ) -> Option<ProcTemplateComponent>; 214 | } 215 | 216 | pub trait ComponentValues { 217 | fn values( 218 | &self, 219 | reference: &InputReference, 220 | hints: &ProcHints, 221 | options: &RenderOptions, 222 | ) -> Option<ProcValues>; 223 | } 224 | 225 | impl ComponentValues for TemplateComponent { 226 | fn values( 227 | &self, 228 | reference: &InputReference, 229 | hints: &ProcHints, 230 | options: &RenderOptions, 231 | ) -> Option<ProcValues> { 232 | let proc_values = match self { 233 | TemplateComponent::Title(title) => title.values(reference, hints, options), 234 | TemplateComponent::Contributor(contributor) => { 235 | contributor.values(reference, hints, options) 236 | } 237 | TemplateComponent::Date(date) => date.values(reference, hints, options), 238 | TemplateComponent::Number(number) => number.values(reference, hints, options), 239 | TemplateComponent::SimpleString(string) => { 240 | string.values(reference, hints, options) 241 | } 242 | TemplateComponent::List(_list) => todo!(), 243 | _ => None, 244 | }; 245 | Some(ProcValues { 246 | value: proc_values.as_ref()?.value.clone(), 247 | prefix: proc_values.as_ref()?.prefix.clone(), 248 | suffix: proc_values.as_ref()?.suffix.clone(), 249 | }) 250 | } 251 | } 252 | 253 | impl ComponentValues for TemplateNumber { 254 | fn values( 255 | &self, 256 | reference: &InputReference, 257 | _hints: &ProcHints, 258 | _options: &RenderOptions, 259 | ) -> Option<ProcValues> { 260 | let number: Option<String> = match &self.number { 261 | Numbers::Volume => match reference { 262 | InputReference::SerialComponent(serial_component) => { 263 | Some(serial_component.volume.as_ref()?.to_string()) 264 | } 265 | _ => None, 266 | }, 267 | Numbers::Issue => match reference { 268 | InputReference::SerialComponent(serial_component) => { 269 | Some(serial_component.issue.as_ref()?.to_string()) 270 | } 271 | _ => None, 272 | }, 273 | Numbers::Pages => match reference { 274 | InputReference::SerialComponent(serial_component) => { 275 | Some(serial_component.pages.as_ref()?.to_string()) 276 | } 277 | InputReference::CollectionComponent(monograph_component) => { 278 | Some(monograph_component.pages.as_ref()?.to_string()) 279 | } 280 | _ => None, 281 | }, 282 | }; 283 | Some(ProcValues { 284 | value: number.unwrap_or_default(), 285 | prefix: None, 286 | suffix: None, 287 | }) 288 | } 289 | } 290 | 291 | impl ComponentValues for TemplateSimpleString { 292 | fn values( 293 | &self, 294 | reference: &InputReference, 295 | _hints: &ProcHints, 296 | _options: &RenderOptions, 297 | ) -> Option<ProcValues> { 298 | let value = match self.variable { 299 | Variables::Doi => match reference { 300 | InputReference::SerialComponent(serial_component) => { 301 | Some(serial_component.doi.as_ref()?.to_string()) 302 | } 303 | InputReference::CollectionComponent(monograph_component) => { 304 | Some(monograph_component.doi.as_ref()?.to_string()) 305 | } 306 | _ => None, 307 | }, 308 | Variables::Isbn => match reference { 309 | InputReference::Monograph(monograph_component) => { 310 | Some(monograph_component.isbn.as_ref()?.to_string()) 311 | } 312 | _ => None, 313 | }, 314 | _ => None, // TODO completes 315 | }; 316 | Some(ProcValues { 317 | value: value.unwrap_or_default(), 318 | prefix: None, 319 | suffix: None, 320 | }) 321 | } 322 | } 323 | 324 | impl ComponentValues for TemplateTitle { 325 | fn values( 326 | &self, 327 | reference: &InputReference, 328 | _hints: &ProcHints, 329 | _options: &RenderOptions, 330 | ) -> Option<ProcValues> { 331 | let value = match &self.title { 332 | Titles::ParentMonograph => { 333 | if let InputReference::CollectionComponent(collection_component) = 334 | reference 335 | { 336 | Some(collection_component.parent.title.as_ref()?.to_string()) 337 | } else { 338 | None 339 | } 340 | } 341 | Titles::ParentSerial => { 342 | if let InputReference::SerialComponent(serial_component) = reference { 343 | Some(serial_component.parent.title.to_string()) 344 | } else { 345 | None 346 | } 347 | } 348 | Titles::Primary => match reference { 349 | InputReference::Monograph(monograph) => Some(monograph.title.to_string()), 350 | InputReference::Collection(collection) => { 351 | Some(collection.title.as_ref()?.to_string()) 352 | } 353 | InputReference::CollectionComponent(monograph_component) => { 354 | Some(monograph_component.title.as_ref()?.to_string()) 355 | } 356 | InputReference::SerialComponent(serial_component) => { 357 | Some(serial_component.title.as_ref()?.to_string()) 358 | } 359 | }, 360 | _ => None, 361 | }; 362 | Some(ProcValues { 363 | value: value.unwrap_or_default(), 364 | prefix: None, 365 | suffix: None, 366 | }) 367 | } 368 | } 369 | 370 | pub fn role_to_string( 371 | role: &ContributorRole, 372 | locale: Locale, 373 | form: ContributorForm, 374 | length: usize, 375 | ) -> Option<String> { 376 | let term = locale.roles.get(role)?; // FIXME causes panic 377 | match form { 378 | ContributorForm::Long => { 379 | if length > 1 { 380 | Some(term.plural.long.clone()) 381 | } else { 382 | Some(term.singular.long.clone()) 383 | } 384 | } 385 | ContributorForm::Short => { 386 | if length > 1 { 387 | Some(term.plural.short.clone()) 388 | } else { 389 | Some(term.singular.short.clone()) 390 | } 391 | } 392 | ContributorForm::Verb => Some(term.verb.long.clone()), 393 | ContributorForm::VerbShort => Some(term.verb.short.clone()), 394 | } 395 | } 396 | 397 | #[test] 398 | fn role_form_to_string() { 399 | use csln::style::locale::{ContributorTerm, Locale, SimpleTerm}; 400 | let mut locale = Locale::default(); 401 | locale.roles.insert( 402 | ContributorRole::Editor, 403 | ContributorTerm { 404 | singular: SimpleTerm { 405 | long: "editor".to_string(), 406 | short: "ed".to_string(), 407 | }, 408 | plural: SimpleTerm { 409 | long: "editors".to_string(), 410 | short: "eds".to_string(), 411 | }, 412 | verb: SimpleTerm { 413 | long: "edited by".to_string(), 414 | short: "ed".to_string(), 415 | }, 416 | }, 417 | ); 418 | let role = ContributorRole::Editor; 419 | let form = ContributorForm::Long; 420 | let length = 1; 421 | let result = role_to_string(&role, locale, form, length); 422 | assert_eq!(result, Some("editor".to_string())); 423 | } 424 | 425 | impl ComponentValues for TemplateContributor { 426 | fn values( 427 | &self, 428 | reference: &InputReference, 429 | _hints: &ProcHints, 430 | options: &RenderOptions, 431 | ) -> Option<ProcValues> { 432 | let locale = options.locale.clone(); 433 | match &self.contributor { 434 | ContributorRole::Author => { 435 | let author = reference.author(); 436 | if author.is_some() { 437 | Some(ProcValues { 438 | value: author?.format(options.global.clone(), locale), 439 | prefix: None, 440 | suffix: None, 441 | }) 442 | } else { 443 | // TODO generalize the substitution 444 | let add_role_form = 445 | // REVIEW is this correct? 446 | options.global.substitute.clone()?.contributor_role_form; 447 | let editor = reference.editor()?; 448 | let editor_length = editor.names(options.global.clone(), true).len(); 449 | // get the role string; if it's in fact author, it will be None 450 | let suffix = add_role_form.map(|role_form| { 451 | role_to_string( 452 | &ContributorRole::Editor, 453 | locale.clone(), 454 | role_form, 455 | editor_length, 456 | ) 457 | }); 458 | let suffix_padded = suffix.and_then(|s| Some(format!(" {}", s?))); // TODO extract this into separate method 459 | Some(ProcValues { 460 | value: editor.format(options.global.clone(), locale), 461 | prefix: None, 462 | suffix: suffix_padded, 463 | }) 464 | } 465 | } 466 | ContributorRole::Editor => { 467 | match reference { 468 | &InputReference::Collection(_) => None, 469 | _ => { 470 | let editor = &reference.editor()?; 471 | let form = &self.form; 472 | let editor_length = 473 | editor.names(options.global.clone(), true).len(); 474 | // TODO handle verb and non-verb forms 475 | 476 | match form { 477 | ContributorForm::Verb | ContributorForm::VerbShort => { 478 | let prefix = role_to_string( 479 | &self.contributor, 480 | locale.clone(), 481 | form.clone(), 482 | editor_length, 483 | ); 484 | let prefix_padded = prefix.and_then(|s| { 485 | if s.is_empty() { 486 | None 487 | } else { 488 | Some(format!("{} ", s)) 489 | } 490 | }); 491 | Some(ProcValues { 492 | value: editor.format(options.global.clone(), locale), 493 | prefix: prefix_padded, 494 | suffix: None, 495 | }) 496 | } 497 | _ => { 498 | let suffix = role_to_string( 499 | &self.contributor, 500 | locale.clone(), 501 | form.clone(), 502 | editor_length, 503 | ); 504 | let suffix_padded = suffix.and_then(|s| { 505 | if s.is_empty() { 506 | None 507 | } else { 508 | Some(format!(" {}", s)) 509 | } 510 | }); 511 | Some(ProcValues { 512 | value: editor.format(options.global.clone(), locale), 513 | prefix: None, 514 | suffix: suffix_padded, // TODO handle None 515 | }) 516 | } 517 | } 518 | } 519 | } 520 | } 521 | ContributorRole::Translator => Some(ProcValues { 522 | value: reference.translator()?.format(options.global.clone(), locale), 523 | prefix: None, 524 | suffix: None, 525 | }), 526 | ContributorRole::Publisher => Some(ProcValues { 527 | value: reference.publisher()?.format(options.global.clone(), locale), 528 | prefix: None, 529 | suffix: None, 530 | }), 531 | // TODO implement the rest 532 | _ => None, 533 | } 534 | } 535 | } 536 | 537 | impl ComponentValues for TemplateDate { 538 | fn values( 539 | &self, 540 | reference: &InputReference, 541 | hints: &ProcHints, 542 | options: &RenderOptions, 543 | ) -> Option<ProcValues> { 544 | let locale: &Locale = &options.locale; 545 | let input_date: EdtfString = match &self.date { 546 | Dates::Issued => reference.issued()?, 547 | Dates::OriginalPublished => todo!("original-published"), 548 | Dates::Accessed => todo!("accessed"), 549 | }; 550 | let parsed_date = input_date.parse(); 551 | //print!("date form: {:?}", reference.issued); 552 | let formatted_date: String = match self.form { 553 | DateForm::Year => parsed_date 554 | .year() // this line causes a panic if the date is not a year 555 | .to_string(), 556 | DateForm::YearMonth => { 557 | input_date.year_month(locale.dates.months.long.clone()) 558 | } 559 | DateForm::MonthDay => input_date.month_day(locale.dates.months.long.clone()), 560 | DateForm::Full => todo!(), 561 | }; 562 | 563 | // TODO: implement this along with localized dates 564 | fn _config_fmt(options: &RenderOptions) -> DateTimeFormatterOptions { 565 | let date_options = match options.global.dates.clone() { 566 | Some(dates) => dates, 567 | None => return DateTimeFormatterOptions::default(), // or handle the None case accordingly 568 | }; 569 | match date_options.month { 570 | MonthFormat::Long => todo!("long"), 571 | MonthFormat::Short => todo!("short"), 572 | MonthFormat::Numeric => todo!("numeric"), 573 | }; 574 | } 575 | 576 | fn int_to_letter(n: u32) -> String { 577 | let c = n + 96; 578 | match char::from_u32(c) { 579 | Some(ch) => ch.to_string(), 580 | None => "".to_string(), 581 | } 582 | } 583 | 584 | let suffix = if hints.disamb_condition 585 | // TODO need to check form here also 586 | // && self.form == style::template::DateForm::Year 587 | // REVIEW: ugly, and needs to be smarter 588 | && options.global.processing.clone().unwrap_or_default().config().disambiguate.unwrap_or_default().year_suffix 589 | && formatted_date.len() == 4 590 | { 591 | int_to_letter((hints.group_index % 26) as u32) 592 | } else { 593 | "".to_string() 594 | }; 595 | Some(ProcValues { 596 | value: formatted_date, 597 | prefix: None, 598 | suffix: Some(suffix), // put the suffix here, in case we need to do something with it 599 | }) 600 | } 601 | } 602 | 603 | // #[test] 604 | // fn render_year() { 605 | // let component = StyleTemplateDate { 606 | // date: Dates::Issued, 607 | // form: DateForm::Year, 608 | // rendering: None, 609 | // }; 610 | // let reference = InputReference { 611 | // id: Some("test".to_string()), 612 | // issued: Some(RefDate::Structured(Edtf::from_str("2020").unwrap())), 613 | // ..Default::default() 614 | // }; 615 | // let options = RenderOptions { 616 | // global: &StyleOptions::default(), 617 | // local: &StyleOptions::default(), 618 | // }; 619 | // let rendered_date = component.render(&reference, &ProcHints::default(), &options); 620 | // assert_eq!(rendered_date, "2020"); 621 | // } 622 | 623 | /// The intermediate representation of renderered citations and bibliography.. 624 | #[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)] 625 | pub struct ProcReferences { 626 | pub bibliography: ProcBibliography, 627 | /// Process the citations, if there are any. 628 | pub citations: Option<ProcCitations>, 629 | } 630 | 631 | pub type ProcBibliography = Vec<ProcTemplate>; 632 | pub type ProcCitationItem = Vec<ProcTemplateComponent>; 633 | pub type ProcCitation = Vec<ProcCitationItem>; 634 | pub type ProcCitations = Vec<ProcCitation>; 635 | 636 | impl Processor { 637 | /// Render references to AST. 638 | #[inline] 639 | pub fn process_references(&self) -> ProcReferences { 640 | let sorted_references = self.sort_references(self.get_references()); 641 | let bibliography: ProcBibliography = sorted_references 642 | .par_iter() 643 | .map(|reference| self.process_reference(reference)) 644 | .collect(); 645 | let citations = if self.citations.is_empty() { 646 | None 647 | } else { 648 | Some(self.process_citations(&self.citations)) 649 | }; 650 | ProcReferences { bibliography, citations } 651 | } 652 | 653 | fn process_citations(&self, citations: &Citations) -> ProcCitations { 654 | citations 655 | .iter() 656 | .map(|citation| self.process_citation(citation)) 657 | .collect() 658 | } 659 | 660 | fn process_citation(&self, citation: &Citation) -> ProcCitation { 661 | // TODO handle the prefix and suffix, though am uncertain how to best do that 662 | let pcitation = citation 663 | .citation_items 664 | .iter() 665 | .filter_map(|citation_item| self.process_citation_item(citation_item)) 666 | .collect(); 667 | println!("pcitation: {:?}", pcitation); 668 | pcitation 669 | } 670 | 671 | pub fn process_citation_item( 672 | &self, 673 | citation_item: &CitationItem, 674 | ) -> Option<ProcCitationItem> { 675 | let citation_style = self.style.citation.clone(); 676 | // FIXME below is returning None 677 | let reference = match self.get_reference(&citation_item.ref_id) { 678 | Ok(reference) => reference, 679 | Err(_) => return None, // or handle the error in a different way 680 | }; 681 | let proc_template = 682 | self.process_template(&reference, citation_style?.template.as_slice()); 683 | println!("proc_template: {:?}", proc_template); 684 | Some(proc_template) 685 | } 686 | 687 | /// Render a reference to AST. 688 | fn process_reference( 689 | &self, 690 | reference: &InputReference, 691 | ) -> Vec<ProcTemplateComponent> { 692 | let bibliography_style = self.style.bibliography.clone().unwrap(); 693 | // TODO bibliography should probably be Optional 694 | self.process_template(reference, bibliography_style.template.as_slice()) 695 | } 696 | 697 | fn get_render_options(&self, style: Style, locale: Locale) -> RenderOptions { 698 | RenderOptions { 699 | global: style.options.unwrap_or_default(), 700 | local: Config::default(), 701 | locale, 702 | } 703 | } 704 | 705 | fn process_template( 706 | &self, 707 | reference: &InputReference, 708 | template: &[TemplateComponent], 709 | ) -> ProcTemplate { 710 | template 711 | .iter() 712 | .filter_map(|component| self.process_template_component(component, reference)) 713 | .collect() 714 | } 715 | 716 | fn process_template_component( 717 | &self, 718 | component: &TemplateComponent, 719 | reference: &InputReference, 720 | ) -> Option<ProcTemplateComponent> { 721 | let hints = self.get_proc_hints(); 722 | let reference_id: Option<RefID> = reference.id(); 723 | let hint: ProcHints = 724 | // TODO why would reference_id be None? 725 | hints.get(&reference_id.unwrap_or_default()).cloned().unwrap_or_default(); 726 | let options = self.get_render_options(self.style.clone(), self.locale.clone()); 727 | let values = component.values(reference, &hint, &options)?; 728 | let template_component = component.clone(); 729 | // TODO add role here if specified in the style 730 | // TODO affixes from style? 731 | if !values.value.is_empty() { 732 | Some(ProcTemplateComponent { 733 | template_component, 734 | values: ProcValues { 735 | value: values.value, 736 | prefix: values.prefix, 737 | suffix: values.suffix, 738 | }, 739 | }) 740 | } else { 741 | None 742 | } 743 | } 744 | 745 | /// Get references from the bibliography. 746 | pub fn get_references(&self) -> Vec<InputReference> { 747 | self.bibliography 748 | .iter() 749 | .map(|(key, reference)| match reference { 750 | InputReference::Monograph(monograph) => { 751 | let mut input_reference = 752 | InputReference::Monograph(monograph.clone()); 753 | input_reference.set_id(key.clone()); 754 | input_reference 755 | } 756 | InputReference::CollectionComponent(collection_component) => { 757 | let mut input_reference = 758 | InputReference::CollectionComponent(collection_component.clone()); 759 | input_reference.set_id(key.clone()); 760 | input_reference 761 | } 762 | InputReference::SerialComponent(serial_component) => { 763 | let mut input_reference = 764 | InputReference::SerialComponent(serial_component.clone()); 765 | input_reference.set_id(key.clone()); 766 | input_reference 767 | } 768 | InputReference::Collection(collection) => { 769 | let mut input_reference = 770 | InputReference::Collection(collection.clone()); 771 | input_reference.set_id(key.clone()); 772 | input_reference 773 | } 774 | }) 775 | .collect() 776 | } 777 | 778 | /// Get a reference from the bibliography by id/citekey. 779 | pub fn get_reference(&self, id: &str) -> Result<InputReference, String> { 780 | match self.bibliography.get(id) { 781 | Some(reference) => Ok(reference.clone()), 782 | None => Err(format!("Invalid reference ID: {}", id)), 783 | } 784 | } 785 | 786 | pub fn get_cited_references(&self) -> Vec<InputReference> { 787 | let mut cited_references = Vec::new(); 788 | for key in &self.get_cited_keys() { 789 | if let Ok(reference) = self.get_reference(key) { 790 | cited_references.push(reference); 791 | } 792 | } 793 | cited_references 794 | } 795 | 796 | /// Return a list of all the keys cited in the document, in order. 797 | pub fn get_cited_keys(&self) -> Vec<String> { 798 | self.citations 799 | .iter() 800 | .flat_map(|c| { 801 | c.citation_items 802 | .iter() 803 | .map(|cr| cr.ref_id.clone()) 804 | .collect::<Vec<String>>() 805 | }) 806 | .collect() 807 | } 808 | 809 | /// Sort the references according to instructions in the style. 810 | #[inline] 811 | pub fn sort_references( 812 | &self, 813 | references: Vec<InputReference>, 814 | ) -> Vec<InputReference> { 815 | let mut references: Vec<InputReference> = references; 816 | let options: Config = self.style.options.clone().unwrap_or_default(); 817 | if let Some(sort_config) = 818 | options.processing.clone().unwrap_or_default().config().sort 819 | { 820 | sort_config.template.iter().rev().for_each(|sort| match sort.key { 821 | SortKey::Author => { 822 | references.par_sort_by(|a, b| { 823 | let a_author = match a.author() { 824 | Some(author) => author.names(options.clone(), true).join("-"), 825 | None => match self.get_author_substitute(a) { 826 | Some((substitute, _)) => substitute, 827 | None => "".to_string(), 828 | }, 829 | }; 830 | 831 | let b_author = match b.author() { 832 | Some(author) => author.names(options.clone(), true).join("-"), 833 | None => match self.get_author_substitute(b) { 834 | Some((substitute, _)) => substitute, 835 | None => "".to_string(), 836 | }, 837 | }; 838 | a_author.to_lowercase().cmp(&b_author.to_lowercase()) 839 | }); 840 | } 841 | SortKey::Year => { 842 | references.par_sort_by(|a: &InputReference, b: &InputReference| { 843 | let a_year = a.issued().as_ref().unwrap().year(); 844 | let b_year = b.issued().as_ref().unwrap().year(); 845 | b_year.cmp(&a_year) 846 | }); 847 | } 848 | _ => {} 849 | }); 850 | } 851 | references 852 | } 853 | 854 | /// Process the references and return a HashMap of ProcHints. 855 | pub fn get_proc_hints(&self) -> HashMap<String, ProcHints> { 856 | let refs = self.get_references(); 857 | let sorted_refs = self.sort_references(refs); 858 | let grouped_refs = self.group_references(sorted_refs); 859 | let proc_hints = grouped_refs 860 | .iter() 861 | .flat_map(|(key, group)| { 862 | let group_len = group.len(); 863 | group.iter().enumerate().map( 864 | move |(index, reference)| -> (String, ProcHints) { 865 | // TODO will need to generalize. 866 | let disambiguate = group_len > 1; 867 | let proc_hint = ProcHints { 868 | disamb_condition: disambiguate, 869 | group_index: index + 1, 870 | group_length: group_len, 871 | group_key: key.clone(), 872 | }; 873 | let ref_id = match reference { 874 | InputReference::Monograph(monograph) => monograph.id.clone(), 875 | InputReference::CollectionComponent(collection_component) => { 876 | collection_component.id.clone() 877 | } 878 | InputReference::SerialComponent(serial_component) => { 879 | serial_component.id.clone() 880 | } 881 | InputReference::Collection(collection) => { 882 | collection.id.clone() 883 | } 884 | }; 885 | (ref_id.unwrap(), proc_hint) 886 | }, 887 | ) 888 | }) 889 | .collect(); 890 | proc_hints 891 | } 892 | 893 | /// Return a string to use for grouping for a given reference, using instructions in the style. 894 | fn make_group_key(&self, reference: &InputReference) -> String { 895 | let options: csln::style::options::Config = match self.style.options { 896 | Some(ref options) => options.clone(), 897 | None => Config::default(), // TODO is this right? 898 | }; 899 | let group_config = options.processing.unwrap_or_default().config().group.unwrap(); 900 | let options = self.style.options.clone(); 901 | let as_sorted = false; 902 | let group_key = group_config 903 | .template 904 | // This is likely unnecessary, but just in case. 905 | .par_iter() 906 | .map(|key| match key { 907 | SortKey::Author => match reference.author() { 908 | Some(author) => { 909 | author.names(options.clone().unwrap(), as_sorted).join("-") 910 | } 911 | None => "".to_string(), 912 | }, 913 | SortKey::Year => { 914 | reference.issued().as_ref().unwrap().parse().year().to_string() 915 | } 916 | SortKey::Title => reference.title().as_ref().unwrap().to_string(), 917 | _ => "".to_string(), // REVIEW is this right? 918 | }) 919 | .collect::<Vec<String>>() 920 | .join(":"); 921 | group_key 922 | } 923 | 924 | pub fn get_author_substitute( 925 | &self, 926 | reference: &InputReference, 927 | ) -> Option<(String, SubstituteKey)> { 928 | let options = self.style.options.as_ref().unwrap().clone(); // FIXME default? 929 | let substitute_config = options.substitute.clone(); // FIXME default? the below line panics 930 | substitute_config 931 | .unwrap_or_default() 932 | .template 933 | .iter() 934 | .find_map(|substitute_key| match *substitute_key { 935 | SubstituteKey::Editor => { 936 | let names = 937 | reference.editor()?.format(options.clone(), self.locale.clone()); 938 | Some((names, substitute_key.clone())) 939 | } 940 | _ => None, 941 | }) 942 | } 943 | 944 | // #[cfg(test)] 945 | // fn author_substitution() { 946 | // use csln::bibliography::reference::{Collection, StructuredName}; 947 | // let component = TemplateContributor { 948 | // contributor: ContributorRole::Author, 949 | // rendering: None, 950 | // form: csln::style::template::ContributorForm::Long, 951 | // }; 952 | // let reference = Collection { 953 | // id: Some("test".to_string()), 954 | // editor: Some(csln::bibliography::reference::Contributor::StructuredName( 955 | // StructuredName { 956 | // family: "Editor".to_string(), 957 | // given: "Jane".to_string(), 958 | // }, 959 | // )), 960 | // r#type: csln::bibliography::reference::CollectionType::EditedBook, 961 | // issued: EdtfString("2020".to_string()), 962 | // title: None, 963 | // url: None, 964 | // accessed: None, 965 | // translator: None, 966 | // publisher: None, 967 | // note: None, 968 | // isbn: None, 969 | // }; 970 | // (assert_eq!( 971 | // component.value( 972 | // &InputReference::Collection(reference), 973 | // &ProcHints::default(), 974 | // &RenderOptions::default() 975 | // ), 976 | // Some("Jane Editor".to_string()) 977 | // )); 978 | // } 979 | 980 | /// Group references according to instructions in the style. 981 | #[inline] 982 | pub fn group_references( 983 | &self, 984 | references: Vec<InputReference>, 985 | ) -> HashMap<String, Vec<InputReference>> { 986 | references 987 | .into_iter() 988 | .group_by(|reference| self.make_group_key(reference)) 989 | .into_iter() 990 | .map(|(key, group)| (key, group.collect())) 991 | .collect() 992 | } 993 | 994 | pub fn new( 995 | style: Style, 996 | bibliography: Bibliography, 997 | citations: Citations, 998 | locale: Locale, 999 | ) -> Processor { 1000 | Processor { style, bibliography, citations, locale } 1001 | } 1002 | } 1003 | -------------------------------------------------------------------------------- /processor/tests/processor_test.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | use csln::citation::{Citation, CitationItem, Citations}; 4 | use csln::from_file; 5 | 6 | #[allow(dead_code)] 7 | // FIXME why these warnings? 8 | struct TestFixture { 9 | style: csln::style::Style, 10 | locale: csln::style::locale::Locale, 11 | bibliography: csln::bibliography::InputBibliography, 12 | citations: Vec<Citation>, 13 | processor: csln_processor::Processor, 14 | } 15 | 16 | fn setup() -> TestFixture { 17 | let style = from_file("examples/style.csl.yaml"); 18 | let locale = from_file("locales/locale-en.yaml"); 19 | let bibliography = from_file("examples/ex1.bib.yaml"); 20 | let citations: Citations = 21 | from_file("examples/citation.yaml").context("Citation file?"); 22 | let processor = 23 | csln_processor::Processor::new(style, bibliography, citations, locale); 24 | 25 | TestFixture { style, locale, bibliography, citations, processor } 26 | } 27 | 28 | #[test] 29 | fn gets_references() { 30 | let fixture = setup(); 31 | assert_eq!(fixture.processor.get_references().len(), 36); 32 | assert!(fixture.processor.get_reference("doe1").is_ok()); 33 | assert_eq!( 34 | fixture.processor.get_reference("doe1").unwrap().title(), 35 | Some(csln::bibliography::reference::Title::Single("Title 2".to_string())) 36 | ); 37 | assert!(fixture.processor.get_proc_hints().contains_key("doe1")); 38 | } 39 | 40 | #[test] 41 | fn sorts_references() { 42 | let fixture = setup(); 43 | let refs = fixture.processor.get_references(); 44 | let sorted_refs = fixture.processor.sort_references(refs); 45 | assert_eq!(sorted_refs.len(), 36); 46 | assert_eq!(sorted_refs.last().unwrap().title().unwrap().to_string(), "Title 4"); 47 | } 48 | 49 | #[test] 50 | fn process_citation_item() { 51 | // TODO make it for citations as a whole, and confirm no empty ones 52 | let fixture = setup(); 53 | let citation_item = CitationItem { 54 | ref_id: "doe1".to_string(), 55 | label: None, 56 | prefix: Some("Prefix".to_string()), 57 | suffix: None, 58 | }; 59 | let result = fixture.processor.process_citation_item(&citation_item); 60 | // confirm 61 | // assert_eq!(fixture.processor.get_reference("doe1"), "doe1".to_string()); 62 | assert_eq!(result.unwrap()[0].values.value.to_string(), "Doe, Jane".to_string()); 63 | } 64 | 65 | #[test] 66 | fn derives_proc_hints() { 67 | let fixture = setup(); 68 | let proc_hints = fixture.processor.get_proc_hints(); 69 | assert_eq!(proc_hints["doe7"].group_index, 1); 70 | assert_eq!(proc_hints["doe7"].group_length, 1); 71 | } 72 | 73 | #[test] 74 | fn loads_and_parses_locale_file() { 75 | let fixture = setup(); 76 | assert_eq!(fixture.locale.dates.months.long[0], "January"); 77 | assert_eq!(fixture.locale.dates.months.long[11], "December"); 78 | assert_eq!(fixture.locale.dates.months.short[0], "Jan"); 79 | assert_eq!(fixture.locale.dates.months.short[11], "Dec"); 80 | } 81 | } 82 | --------------------------------------------------------------------------------