oxedyne/fe2o3/fe2o3_austenite/src/eval/lib/model/quote.rs
7.4 KiB, 1 run
created by r1870400018:63278, 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 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Original work copyright (c) the Typst project authors (typst-library `model/quote.rs`, version 0.15.1). |
| 3 | // Modified for Austenite: the element's fields, defaults and default show, ported to Hematite's types, IR and error handling. |
| 4 | // U5 owns this file: quote. An inline quote is wrapped in the language's quotation marks, alternating |
| 5 | // double and single with nesting (`quote.depth`, a style-only field); a block quote is padded, with its |
| 6 | // attribution set below it after an em dash. The quotation marks by language and region are Typst |
| 7 | // 0.15.1's `SmartQuotes::get` table; a custom `smartquote(quotes: ..)` set is not yet consulted. |
| 8 | |
| 9 | use crate::eval::content::{ |
| 10 | Content, |
| 11 | ElemKind, |
| 12 | FieldDefault, |
| 13 | FieldSpec, |
| 14 | FieldType, |
| 15 | }; |
| 16 | use crate::eval::lib::model::common::{ |
| 17 | self, |
| 18 | expect, |
| 19 | to_content, |
| 20 | CastErr, |
| 21 | K, |
| 22 | }; |
| 23 | use crate::eval::styles::{ |
| 24 | StyleChain, |
| 25 | Styles, |
| 26 | }; |
| 27 | use crate::eval::value::{ |
| 28 | Alignment, |
| 29 | HAlign, |
| 30 | Type, |
| 31 | Value, |
| 32 | }; |
| 33 | use crate::eval::Engine; |
| 34 | use crate::syntax::Span; |
| 35 | |
| 36 | use oxedyne_fe2o3_core::prelude::*; |
| 37 | |
| 38 | const ANY: FieldType = FieldType::Any; |
| 39 | |
| 40 | static QUOTE: [FieldSpec; 5] = [ |
| 41 | FieldSpec::named("block", FieldType::Of(Type::Bool), FieldDefault::Bool(false)), |
| 42 | FieldSpec::named("quotes", FieldType::OneOf(&[Type::Bool, Type::Auto]), FieldDefault::Auto), |
| 43 | FieldSpec::named("attribution", ANY, FieldDefault::None), |
| 44 | FieldSpec::required("body", ANY), |
| 45 | FieldSpec::named("depth", ANY, FieldDefault::Int(0)).internal(), |
| 46 | ]; |
| 47 | |
| 48 | pub fn fields(kind: ElemKind) -> &'static [FieldSpec] { |
| 49 | match kind { |
| 50 | ElemKind::Quote => "E, |
| 51 | _ => &[], |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn cast(_kind: ElemKind, name: &str, v: Value) -> Result<Value, CastErr> { |
| 56 | match name { |
| 57 | "block" => expect(&v, &[K::Bool]).map(|_| v), |
| 58 | "quotes" => expect(&v, &[K::Bool, K::Auto]).map(|_| v), |
| 59 | "attribution" => match v { |
| 60 | Value::None | Value::Label(_) => Ok(v), |
| 61 | other => expect(&other, &[K::Content, K::Label, K::None]).map(|_| to_content(other)), |
| 62 | }, |
| 63 | "body" => expect(&v, &[K::Content]).map(|_| to_content(v)), |
| 64 | _ => Ok(v), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// A block quote is padded by 1em each side and set apart from its neighbours. |
| 69 | pub fn show_set(elem: &Content, styles: &StyleChain) -> Outcome<Styles> { |
| 70 | if !matches!(res!(common::get(elem, styles, "block")), Value::Bool(true)) { |
| 71 | return Ok(Styles::new()); |
| 72 | } |
| 73 | common::props(vec![ |
| 74 | (ElemKind::Pad, "left", common::em(1.0)), |
| 75 | (ElemKind::Pad, "right", common::em(1.0)), |
| 76 | (ElemKind::Block, "above", common::em(2.4)), |
| 77 | (ElemKind::Block, "below", common::em(1.8)), |
| 78 | ]) |
| 79 | } |
| 80 | |
| 81 | /// The quotation marks in force: single open, single close, double open, double close. |
| 82 | pub fn smart_quotes(styles: &StyleChain) -> [&'static str; 4] { |
| 83 | let (lang, region) = common::lang(styles); |
| 84 | let alternative = matches!( |
| 85 | common::style_or(styles, ElemKind::SmartQuote, "alternative", Value::Bool(false)), Value::Bool(true)); |
| 86 | let region = region.as_deref(); |
| 87 | let default = ["\u{2018}", "\u{2019}", "\u{201C}", "\u{201D}"]; |
| 88 | let low_high = ["\u{201A}", "\u{2018}", "\u{201E}", "\u{201C}"]; |
| 89 | match lang.as_str() { |
| 90 | "de" if matches!(region, Some("CH") | Some("LI")) => if alternative { |
| 91 | low_high |
| 92 | } else { |
| 93 | ["\u{2039}", "\u{203A}", "\u{AB}", "\u{BB}"] |
| 94 | }, |
| 95 | "fr" if region == Some("CH") => if alternative { |
| 96 | default |
| 97 | } else { |
| 98 | ["\u{2039}\u{202F}", "\u{202F}\u{203A}", "\u{AB}\u{202F}", "\u{202F}\u{BB}"] |
| 99 | }, |
| 100 | "cs" | "da" | "de" | "sk" | "sl" if alternative => ["\u{203A}", "\u{2039}", "\u{BB}", "\u{AB}"], |
| 101 | "cs" | "de" | "et" | "is" | "lt" | "lv" | "sk" | "sl" => low_high, |
| 102 | "da" => default, |
| 103 | "fr" if alternative => default, |
| 104 | "fr" => ["\u{201C}", "\u{201D}", "\u{AB}\u{202F}", "\u{202F}\u{BB}"], |
| 105 | "fi" | "sv" if alternative => ["\u{2019}", "\u{2019}", "\u{BB}", "\u{BB}"], |
| 106 | "gl" => ["\u{201C}", "\u{201D}", "\u{AB}", "\u{BB}"], |
| 107 | "bs" | "fi" | "sv" => ["\u{2019}", "\u{2019}", "\u{201D}", "\u{201D}"], |
| 108 | "it" if alternative => default, |
| 109 | "la" if alternative => ["\u{201C}", "\u{201D}", "\u{AB}\u{202F}", "\u{202F}\u{BB}"], |
| 110 | "it" | "la" => ["\u{201C}", "\u{201D}", "\u{AB}", "\u{BB}"], |
| 111 | "es" if matches!(region, Some("ES") | None) => ["\u{201C}", "\u{201D}", "\u{AB}", "\u{BB}"], |
| 112 | "hu" | "pl" | "ro" => ["\u{2019}", "\u{2019}", "\u{201E}", "\u{201D}"], |
| 113 | "no" | "nb" | "nn" if alternative => low_high, |
| 114 | "no" | "nb" | "nn" => ["\u{2019}", "\u{2019}", "\u{AB}", "\u{BB}"], |
| 115 | "ru" => ["\u{201E}", "\u{201C}", "\u{AB}", "\u{BB}"], |
| 116 | "uk" => ["\u{201C}", "\u{201D}", "\u{AB}", "\u{BB}"], |
| 117 | "el" => ["\u{2018}", "\u{2019}", "\u{AB}", "\u{BB}"], |
| 118 | "he" => ["\u{2019}", "\u{2019}", "\u{201D}", "\u{201D}"], |
| 119 | "hr" => ["\u{2018}", "\u{2019}", "\u{201E}", "\u{201D}"], |
| 120 | "bg" => ["\u{2019}", "\u{2019}", "\u{201E}", "\u{201C}"], |
| 121 | "ar" if !alternative => ["\u{2019}", "\u{2018}", "\u{AB}", "\u{BB}"], |
| 122 | _ if common::is_rtl(styles) => ["\u{2019}", "\u{2018}", "\u{201D}", "\u{201C}"], |
| 123 | _ => default, |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | pub fn show(engine: &mut Engine, elem: &Content, styles: &StyleChain) -> Outcome<Option<Content>> { |
| 128 | let span = elem.span(); |
| 129 | let block = matches!(res!(common::get(elem, styles, "block")), Value::Bool(true)); |
| 130 | let mut realised = common::body(elem, "body"); |
| 131 | let quoted = match res!(common::get(elem, styles, "quotes")) { |
| 132 | Value::Bool(b) => b, |
| 133 | _ => !block, |
| 134 | }; |
| 135 | if quoted { |
| 136 | // Zero-width weak spacing either side keeps the marks against the text. |
| 137 | let hole = res!(common::h(engine, common::pt(0.0), true)); |
| 138 | let depth = match common::style_or(styles, ElemKind::Quote, "depth", Value::Int(0)) { |
| 139 | Value::Int(d) => d, |
| 140 | _ => 0, |
| 141 | }; |
| 142 | let q = smart_quotes(styles); |
| 143 | let (open, close) = if depth % 2 == 0 { (q[2], q[3]) } else { (q[0], q[1]) }; |
| 144 | let inner = common::seq(vec![hole.clone(), realised, hole]); |
| 145 | let marked = common::seq(vec![common::text(open), inner, common::text(close)]); |
| 146 | realised = res!(common::set(marked, ElemKind::Quote, "depth", Value::Int(depth + 1))); |
| 147 | } |
| 148 | let attribution = res!(common::get(elem, styles, "attribution")); |
| 149 | if block { |
| 150 | realised = res!(common::block(engine, realised, span, Vec::new())); |
| 151 | if !attribution.is_none() { |
| 152 | let gap = res!(common::build(engine, ElemKind::V, span, vec![common::em(0.9)], vec![ |
| 153 | ("weak", Value::Bool(true)), |
| 154 | ])); |
| 155 | let attr = res!(attribution_content(&attribution, span)); |
| 156 | // The block carries the alignment as a style, as Typst's `BlockElem::aligned` does, so its lines |
| 157 | // are set against the end whatever width the block takes. |
| 158 | let end = Value::Alignment(Alignment { x: Some(HAlign::End), y: None }); |
| 159 | let attr_block = res!(common::block(engine, attr, span, Vec::new())); |
| 160 | let attr_block = res!(common::set(attr_block, ElemKind::Align, "alignment", end)); |
| 161 | realised = common::seq(vec![realised, gap, attr_block]); |
| 162 | } |
| 163 | realised = res!(common::build(engine, ElemKind::Pad, span, vec![Value::Content(realised)], Vec::new())); |
| 164 | } else if let Value::Label(l) = &attribution { |
| 165 | let cite = res!(common::raw_elem(ElemKind::Cite, span, vec![("key", Value::Label(l.clone()))])); |
| 166 | realised = common::seq(vec![realised, Content::marker(ElemKind::Space, Span::detached()), cite]); |
| 167 | } |
| 168 | Ok(Some(realised)) |
| 169 | } |
| 170 | |
| 171 | /// An em dash, a space, then the attribution: content as given, a label as a prose citation. |
| 172 | fn attribution_content(v: &Value, span: Span) -> Outcome<Content> { |
| 173 | let who = match v { |
| 174 | Value::Label(l) => { |
| 175 | res!(common::raw_elem(ElemKind::Cite, span, vec![ |
| 176 | ("key", Value::Label(l.clone())), |
| 177 | ("form", Value::str("prose")), |
| 178 | ])) |
| 179 | } |
| 180 | other => common::display(other.clone()), |
| 181 | }; |
| 182 | Ok(common::seq(vec![common::text("\u{2014}"), Content::marker(ElemKind::Space, Span::detached()), who])) |
| 183 | } |