oxedyne/fe2o3/fe2o3_text/src/csl/archive.rs
4.2 KiB, 1 run
created by r1870400018:73154, 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 | //! The built-in styles and locales: the CSL XML of exactly the 80 styles and 63 locales hayagriva |
| 2 | //! 0.10.1 serves, at the revisions it archived (`data/csl/index.tsv` records each source commit). |
| 3 | //! |
| 4 | //! The index of names, ids and paths is always present under `csl`. The XML itself is embedded |
| 5 | //! only under `csl-archive`, about 4.0 MB; a host without it (the browser build) reads each file |
| 6 | //! as `<path>` inside the virtual package [`PACKAGE`] and supplies it on demand. |
| 7 | //! |
| 8 | //! The data is CC BY-SA 3.0 (the CSL project), except `alphanumeric`, which is MIT/Apache-2.0 |
| 9 | //! (the hayagriva authors); `data/csl/LICENSE.txt` holds the notices and travels with the crate. |
| 10 | #[cfg(feature = "csl-archive")] |
| 11 | use crate::csl::{ |
| 12 | Locale, |
| 13 | Locales, |
| 14 | Style, |
| 15 | }; |
| 16 | |
| 17 | #[cfg(feature = "csl-archive")] |
| 18 | use oxedyne_fe2o3_core::prelude::*; |
| 19 | |
| 20 | include!("archive_index.rs"); |
| 21 | |
| 22 | /// The virtual package a host that does not embed the archive serves its files from. |
| 23 | pub const PACKAGE: &str = "@austenite/csl:0.10.1"; |
| 24 | |
| 25 | /// One built-in style. |
| 26 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 27 | pub struct Archived { |
| 28 | pub stem: &'static str, // file stem under `styles/` |
| 29 | pub id: &'static str, // CSL `<id>` |
| 30 | pub names: &'static [&'static str], // what Typst's `style:` accepts |
| 31 | pub updated: &'static str, // `<updated>` as archived, empty for alphanumeric |
| 32 | } |
| 33 | |
| 34 | impl Archived { |
| 35 | |
| 36 | fn at(i: usize) -> Self { |
| 37 | let (stem, id, names, updated) = STYLES[i]; |
| 38 | Self { stem, id, names, updated } |
| 39 | } |
| 40 | |
| 41 | /// The style's path inside the archive, and inside [`PACKAGE`]. |
| 42 | pub fn path(&self) -> String { |
| 43 | format!("styles/{}.csl", self.stem) |
| 44 | } |
| 45 | |
| 46 | #[cfg(feature = "csl-archive")] |
| 47 | pub fn xml(&self) -> Outcome<&'static str> { |
| 48 | Ok(res!(style_xml(self.stem).ok_or_else(|| err!( |
| 49 | "CSL archive index names style '{}' but its file is not embedded.", self.stem; |
| 50 | Bug, Missing)))) |
| 51 | } |
| 52 | |
| 53 | #[cfg(feature = "csl-archive")] |
| 54 | pub fn style(&self) -> Outcome<Style> { |
| 55 | Style::parse(res!(self.xml())) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Every built-in style, in hayagriva's order. |
| 60 | pub fn styles() -> impl Iterator<Item = Archived> { |
| 61 | (0..STYLES.len()).map(Archived::at) |
| 62 | } |
| 63 | |
| 64 | /// A built-in style by a name Typst accepts for `style:`, such as `apa` or `ieee`. |
| 65 | pub fn by_name(name: &str) -> Option<Archived> { |
| 66 | STYLES.iter().position(|(_, _, names, _)| names.contains(&name)).map(Archived::at) |
| 67 | } |
| 68 | |
| 69 | /// A built-in style by its CSL id, such as `http://www.zotero.org/styles/apa`. |
| 70 | pub fn by_id(id: &str) -> Option<Archived> { |
| 71 | STYLES.iter().position(|(_, i, _, _)| *i == id).map(Archived::at) |
| 72 | } |
| 73 | |
| 74 | /// Every built-in locale code. |
| 75 | pub fn locale_codes() -> &'static [&'static str] { |
| 76 | LOCALES |
| 77 | } |
| 78 | |
| 79 | /// Is there a built-in locale file of this exact code? |
| 80 | pub fn has_locale(code: &str) -> bool { |
| 81 | LOCALES.contains(&code) |
| 82 | } |
| 83 | |
| 84 | /// A locale's path inside the archive, and inside [`PACKAGE`]. |
| 85 | pub fn locale_path(code: &str) -> String { |
| 86 | format!("locales/{}.xml", code) |
| 87 | } |
| 88 | |
| 89 | /// The built-in locale files a style rendered in this code consults, as paths: what a host |
| 90 | /// without the archive must supply. Codes the archive lacks are left out, as hayagriva's |
| 91 | /// lookup finds nothing for them either. |
| 92 | pub fn wanted_paths(style: &crate::csl::Style, code: Option<&str>) -> Vec<String> { |
| 93 | crate::csl::Locales::wanted(style, code).iter() |
| 94 | .filter(|c| has_locale(c)) |
| 95 | .map(|c| locale_path(c)) |
| 96 | .collect() |
| 97 | } |
| 98 | |
| 99 | #[cfg(feature = "csl-archive")] |
| 100 | pub fn locale_src(code: &str) -> Option<&'static str> { |
| 101 | locale_xml(code) |
| 102 | } |
| 103 | |
| 104 | /// `locales.json` as the CSL locales repository stood at the newest archived locale. |
| 105 | #[cfg(feature = "csl-archive")] |
| 106 | pub fn locales_json() -> &'static str { |
| 107 | LOCALES_JSON |
| 108 | } |
| 109 | |
| 110 | #[cfg(feature = "csl-archive")] |
| 111 | pub fn locale(code: &str) -> Outcome<Option<Locale>> { |
| 112 | match locale_xml(code) { |
| 113 | Some(src) => Ok(Some(res!(Locale::parse(src)))), |
| 114 | None => Ok(None), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// The built-in locale files a style rendered in this code consults, parsed, ready for |
| 119 | /// [`Locales::chain`]. |
| 120 | /// |
| 121 | /// The fallback is citationberg's primary-dialect table, the one Typst reaches through |
| 122 | /// hayagriva; `data/csl/locales.json` is carried for hosts but not read, since Typst ignores it. |
| 123 | #[cfg(feature = "csl-archive")] |
| 124 | pub fn locales_for(style: &Style, code: Option<&str>) -> Outcome<Locales> { |
| 125 | let mut out = Locales::new(); |
| 126 | for c in Locales::wanted(style, code) { |
| 127 | if let Some(loc) = res!(locale(&c)) { |
| 128 | res!(out.insert(loc)); |
| 129 | } |
| 130 | } |
| 131 | Ok(out) |
| 132 | } |