Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/src/csl/locale.rs

8.7 KiB, 1 run

created by r1870400018:72718, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! CSL locales and the order they are consulted in.
2//!
3//! The order is hayagriva 0.10.1's (`lookup_locale`), which Typst 0.15.1 shows and which differs from
4//! CSL 1.0.2 §Locale Fallback in two places. The in-style step tries the requested code, then its
5//! two-letter language, then a `locale` without `xml:lang`. The file step tries the requested code,
6//! then the primary dialect of its language, then `en-US`. The primary dialect comes from
7//! citationberg's table, not from `locales.json`: the two agree except that the table has no entry
8//! for `ar`, `cy`, `gl`, `hi`, `hy`, `id`, `ms`, `tl` and the three-letter codes, and gives `sr` as
9//! `sr-RS`, a file hayagriva does not serve. Each of those therefore falls to `en-US` in Typst, and
10//! here.
11
12use crate::{
13 csl::style::{
14 Date,
15 Rd,
16 Style,
17 TermForm,
18 },
19 xml::{
20 Elem as XElem,
21 Xml,
22 },
23};
24
25use oxedyne_fe2o3_core::prelude::*;
26
27use std::collections::BTreeMap;
28
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub enum Gender { Masculine, Feminine, Neuter }
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum OrdMatch { LastDigit, LastTwoDigits, WholeNumber }
35
36#[derive(Clone, Debug, PartialEq)]
37pub struct Term {
38 pub name: String,
39 pub form: TermForm,
40 pub gender: Option<Gender>, // of a noun term such as `month-01`
41 pub gender_form:Option<Gender>, // of an ordinal suffix
42 pub matching: Option<OrdMatch>,
43 pub single: String,
44 pub multiple: Option<String>,
45}
46
47impl Term {
48 pub fn text(&self, plural: bool) -> &str {
49 match (plural, &self.multiple) {
50 (true, Some(m)) => m,
51 _ => &self.single,
52 }
53 }
54}
55
56#[derive(Clone, Debug, Default, PartialEq)]
57pub struct Locale {
58 pub lang: Option<String>,
59 pub terms: Vec<Term>,
60 pub dates: Vec<Date>, // each carries its `form`
61 pub punctuation_in_quote: Option<bool>,
62 pub limit_ordinals_to_day_1: Option<bool>,
63}
64
65impl Locale {
66
67 /// Reads a standalone locale file.
68 pub fn parse(src: &str) -> Outcome<Self> {
69 let xml = res!(Xml::parse(src));
70 let root = res!(xml.root());
71 if root.name.local() != "locale" {
72 return Err(err!("CSL locale: the root element is <{}>, not <locale>.", root.name.qname; Invalid, Input));
73 }
74 Self::read(&xml, root)
75 }
76
77 /// Reads a `locale` element, standalone or inside a style.
78 pub(crate) fn read(xml: &Xml, e: &XElem) -> Outcome<Self> {
79 let rd = Rd { xml };
80 let mut out = Self {
81 lang: e.attr("xml:lang").map(str::to_string),
82 ..Default::default()
83 };
84 for k in e.elems() {
85 match k.name.local() {
86 "style-options" => {
87 out.punctuation_in_quote = res!(rd.bool(k, "punctuation-in-quote"));
88 out.limit_ordinals_to_day_1 = res!(rd.bool(k, "limit-day-ordinals-to-day-1"));
89 }
90 "date" => out.dates.push(res!(rd.date(k, true))),
91 "terms" => for t in k.children("term") {
92 out.terms.push(res!(Self::term(&rd, t)));
93 },
94 _ => {} // info
95 }
96 }
97 Ok(out)
98 }
99
100 fn term(rd: &Rd, t: &XElem) -> Outcome<Term> {
101 let gender = |attr: &str| -> Outcome<Option<Gender>> {
102 Ok(match t.attr(attr) {
103 None => None,
104 Some("masculine") => Some(Gender::Masculine),
105 Some("feminine") => Some(Gender::Feminine),
106 Some("neuter") => Some(Gender::Neuter),
107 Some(v) => return Err(rd.bad(t, attr, v)),
108 })
109 };
110 let (single, multiple) = match (t.child("single"), t.child("multiple")) {
111 (None, None) => (rd.xml.text_of(t).trim().to_string(), None),
112 (s, m) => (
113 s.map(|s| rd.xml.text_of(s).trim().to_string()).unwrap_or_default(),
114 m.map(|m| rd.xml.text_of(m).trim().to_string()),
115 ),
116 };
117 Ok(Term {
118 name: res!(rd.req(t, "name")).to_string(),
119 form: res!(rd.term_form(t)),
120 gender: res!(gender("gender")),
121 gender_form: res!(gender("gender-form")),
122 matching: match t.attr("match") {
123 None => None,
124 Some("last-digit") => Some(OrdMatch::LastDigit),
125 Some("last-two-digits") => Some(OrdMatch::LastTwoDigits),
126 Some("whole-number") => Some(OrdMatch::WholeNumber),
127 Some(v) => return Err(rd.bad(t, "match", v)),
128 },
129 single,
130 multiple,
131 })
132 }
133}
134
135/// The primary dialect of a language, as citationberg 0.7 gives it.
136pub fn primary_dialect(code: &str) -> Option<&'static str> {
137 let base = res_base(code)?;
138 let dialect = PRIMARY.iter().find(|(l, _)| *l == base).map(|(_, d)| *d)?;
139 match dialect == code {
140 true => None,
141 false => Some(dialect),
142 }
143}
144
145/// The two-letter base language of a code, which is all citationberg reads a fallback from.
146fn res_base(code: &str) -> Option<&str> {
147 let first = code.split('-').next()?;
148 match first.len() {
149 2 => Some(first),
150 _ => None,
151 }
152}
153
154const PRIMARY: &[(&str, &str)] = &[
155 ("af", "af-ZA"), ("bg", "bg-BG"), ("ca", "ca-AD"), ("cs", "cs-CZ"), ("da", "da-DK"), ("de", "de-DE"),
156 ("el", "el-GR"), ("en", "en-US"), ("es", "es-ES"), ("et", "et-EE"), ("fa", "fa-IR"), ("fi", "fi-FI"),
157 ("fr", "fr-FR"), ("he", "he-IL"), ("hr", "hr-HR"), ("hu", "hu-HU"), ("is", "is-IS"), ("it", "it-IT"),
158 ("ja", "ja-JP"), ("km", "km-KH"), ("ko", "ko-KR"), ("lt", "lt-LT"), ("lv", "lv-LV"), ("mn", "mn-MN"),
159 ("nb", "nb-NO"), ("nl", "nl-NL"), ("nn", "nn-NO"), ("pl", "pl-PL"), ("pt", "pt-PT"), ("ro", "ro-RO"),
160 ("ru", "ru-RU"), ("sk", "sk-SK"), ("sl", "sl-SI"), ("sr", "sr-RS"), ("sv", "sv-SE"), ("th", "th-TH"),
161 ("tr", "tr-TR"), ("uk", "uk-UA"), ("vi", "vi-VN"), ("zh", "zh-CN"),
162];
163
164/// Locale files by code: the external half of the chain.
165#[derive(Clone, Debug, Default)]
166pub struct Locales {
167 pub files: BTreeMap<String, Locale>,
168}
169
170impl Locales {
171
172 pub fn new() -> Self {
173 Self::default()
174 }
175
176 /// Adds a parsed file under its own `xml:lang`.
177 pub fn insert(&mut self, loc: Locale) -> Outcome<()> {
178 let code = res!(loc.lang.clone().ok_or_else(|| err!(
179 "CSL locale file has no xml:lang on its <locale>."; Invalid, Input, Missing)));
180 self.files.insert(code, loc);
181 Ok(())
182 }
183
184 /// The locale files a style rendered in this code may consult, in order: what a host that
185 /// supplies files on demand must fetch.
186 pub fn wanted(style: &Style, code: Option<&str>) -> Vec<String> {
187 let code = chosen(style, code);
188 let mut out = vec![code.clone()];
189 if let Some(p) = primary_dialect(&code) {
190 out.push(p.to_string());
191 }
192 if !out.iter().any(|c| c == "en-US") {
193 out.push("en-US".to_string());
194 }
195 out
196 }
197
198 /// The chain a style rendered in this code consults. `None` means the style's default locale, or
199 /// `en-US` where it names none.
200 pub fn chain<'a>(&'a self, style: &'a Style, code: Option<&str>) -> Chain<'a> {
201 let code = chosen(style, code);
202 let mut locs: Vec<&'a Locale> = Vec::new();
203 let push = |l: &'a Locale, locs: &mut Vec<&'a Locale>| {
204 if !locs.iter().any(|x| std::ptr::eq(*x, l)) {
205 locs.push(l);
206 }
207 };
208 let base = res_base(&code).map(str::to_string);
209 // Only the first in-style locale of each code is consulted, as in hayagriva.
210 let in_style = |want: Option<&str>| style.locales.iter().find(|l| l.lang.as_deref() == want);
211 if let Some(l) = in_style(Some(&code)) { push(l, &mut locs) }
212 if let Some(b) = &base {
213 if let Some(l) = in_style(Some(b)) { push(l, &mut locs) }
214 }
215 if let Some(l) = in_style(None) { push(l, &mut locs) }
216 if let Some(l) = self.files.get(&code) { push(l, &mut locs) }
217 if let Some(p) = primary_dialect(&code) {
218 if let Some(l) = self.files.get(p) { push(l, &mut locs) }
219 }
220 if let Some(l) = self.files.get("en-US") { push(l, &mut locs) }
221 Chain { code, locs }
222 }
223}
224
225fn chosen(style: &Style, code: Option<&str>) -> String {
226 match code {
227 Some(c) => c.to_string(),
228 None => style.default_locale.clone().unwrap_or_else(|| "en-US".to_string()),
229 }
230}
231
232/// The locales one rendering consults, most specific first.
233#[derive(Clone, Debug)]
234pub struct Chain<'a> {
235 pub code: String,
236 pub locs: Vec<&'a Locale>,
237}
238
239impl<'a> Chain<'a> {
240
241 /// The first answer any locale in the chain gives.
242 pub fn first<R, F: FnMut(&'a Locale) -> Option<R>>(&self, mut f: F) -> Option<R> {
243 self.locs.iter().find_map(|l| f(l))
244 }
245
246 /// A term by name and form, falling back through the forms (symbol to short to long, verb-short
247 /// to verb to long) only once no locale has the asked form.
248 pub fn term(&self, name: &str, form: TermForm) -> Option<&'a Term> {
249 let mut form = Some(form);
250 while let Some(f) = form {
251 let hit = self.first(|l| l.terms.iter().find(|t| t.name == name && t.form == f));
252 if hit.is_some() {
253 return hit;
254 }
255 form = f.fallback();
256 }
257 None
258 }
259
260 /// The term's text, singular or plural.
261 pub fn term_text(&self, name: &str, form: TermForm, plural: bool) -> Option<&'a str> {
262 self.term(name, form).map(|t| t.text(plural))
263 }
264
265 /// A localised date format.
266 pub fn date(&self, form: crate::csl::style::DateForm) -> Option<&'a Date> {
267 self.first(|l| l.dates.iter().find(|d| d.form == Some(form)))
268 }
269
270 pub fn punctuation_in_quote(&self) -> bool {
271 self.first(|l| l.punctuation_in_quote).unwrap_or(false)
272 }
273
274 pub fn limit_ordinals_to_day_1(&self) -> bool {
275 self.first(|l| l.limit_ordinals_to_day_1).unwrap_or(false)
276 }
277}