oxedyne/fe2o3/fe2o3_text/src/csl/node.rs
6.3 KiB, 10 runs
created by r1870400018:73177, 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 | //! Ported from hayagriva 0.10.1 `src/csl/elem.rs`, Copyright (c) Martin Haug and the hayagriva |
| 2 | //! contributors, MIT OR Apache-2.0. |
| 3 | //! |
| 4 | //! The shape is hayagriva's `ElemChildren`, which Typst realises: a host turns `Meta::Entry` into |
| 5 | //! a link to the bibliography entry, a `left-margin` element into the entry's prefix column, and |
| 6 | //! `Math` into an equation. |
| 7 | use crate::csl::person::Person; |
| 8 | use crate::csl::style::{ |
| 9 | Decoration, |
| 10 | Display, |
| 11 | Fmt, |
| 12 | FontStyle, |
| 13 | FontVariant, |
| 14 | FontWeight, |
| 15 | VAlign, |
| 16 | }; |
| 17 | |
| 18 | use std::fmt::Write; |
| 19 | |
| 20 | |
| 21 | /// Formatting as resolved for a run of text, every attribute set. |
| 22 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 23 | pub struct Formatting { |
| 24 | pub style: FontStyle, |
| 25 | pub variant: FontVariant, |
| 26 | pub weight: FontWeight, |
| 27 | pub deco: Decoration, |
| 28 | pub valign: VAlign, |
| 29 | } |
| 30 | |
| 31 | impl Default for Formatting { |
| 32 | fn default() -> Self { |
| 33 | Self { |
| 34 | style: FontStyle::Normal, |
| 35 | variant: FontVariant::Normal, |
| 36 | weight: FontWeight::Normal, |
| 37 | deco: Decoration::None, |
| 38 | valign: VAlign::Baseline, |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Formatting { |
| 44 | |
| 45 | /// Overlays an element's CSL formatting, leaving what it does not set. |
| 46 | pub fn apply(mut self, f: &Fmt) -> Self { |
| 47 | if let Some(v) = f.style { self.style = v; } |
| 48 | if let Some(v) = f.variant { self.variant = v; } |
| 49 | if let Some(v) = f.weight { self.weight = v; } |
| 50 | if let Some(v) = f.deco { self.deco = v; } |
| 51 | if let Some(v) = f.valign { self.valign = v; } |
| 52 | self |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// What an element stands for, so that a host can find or link it. |
| 57 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 58 | pub enum Meta { |
| 59 | Names(Vec<(Vec<Person>, String)>), // every variable's people, rendered or not |
| 60 | Date, |
| 61 | Text, |
| 62 | Number, |
| 63 | Label, |
| 64 | CitationNumber, |
| 65 | Name(String, usize), // variable and index of one name, for B2b |
| 66 | Entry(usize), // a citation's link to its bibliography entry |
| 67 | CitationLabel, |
| 68 | } |
| 69 | |
| 70 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 71 | pub struct Elem { |
| 72 | pub children: Vec<Node>, |
| 73 | pub display: Option<Display>, |
| 74 | pub meta: Option<Meta>, |
| 75 | } |
| 76 | |
| 77 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 78 | pub enum Node { |
| 79 | Text(String, Formatting), |
| 80 | Elem(Elem), |
| 81 | Math(String), // a math chunk's source, for the host to evaluate |
| 82 | Link { text: String, fmt: Formatting, url: String }, |
| 83 | Cite { idx: usize, fmt: Formatting }, // a locator the host renders, by citation index |
| 84 | } |
| 85 | |
| 86 | impl Node { |
| 87 | |
| 88 | pub fn str_len(&self) -> usize { |
| 89 | match self { |
| 90 | Self::Text(t, _) => t.len(), |
| 91 | Self::Elem(e) => e.children.iter().map(Node::str_len).sum(), |
| 92 | Self::Math(m) => m.len(), |
| 93 | Self::Link { text, .. } => text.len(), |
| 94 | Self::Cite { .. } => 0, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn has_content(&self) -> bool { |
| 99 | match self { |
| 100 | Self::Text(t, _) | Self::Math(t) => t.chars().any(|c| !c.is_whitespace()), |
| 101 | Self::Elem(e) => has_content(&e.children), |
| 102 | Self::Link { .. } | Self::Cite { .. } => true, |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub fn is_empty(&self) -> bool { |
| 107 | match self { |
| 108 | Self::Text(t, _) | Self::Math(t) => t.is_empty(), |
| 109 | Self::Elem(e) => is_empty(&e.children), |
| 110 | Self::Link { text, .. } => text.is_empty(), |
| 111 | Self::Cite { .. } => false, |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub fn has_content(nodes: &[Node]) -> bool { |
| 117 | nodes.iter().any(Node::has_content) |
| 118 | } |
| 119 | |
| 120 | pub fn is_empty(nodes: &[Node]) -> bool { |
| 121 | nodes.iter().all(Node::is_empty) |
| 122 | } |
| 123 | |
| 124 | /// The last text run, looking into the last element. |
| 125 | pub fn last_text_mut(nodes: &mut [Node]) -> Option<&mut String> { |
| 126 | match nodes.last_mut() { |
| 127 | Some(Node::Text(t, _)) => Some(t), |
| 128 | Some(Node::Elem(e)) => last_text_mut(&mut e.children), |
| 129 | _ => None, |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn last_text(nodes: &[Node]) -> Option<&str> { |
| 134 | match nodes.last() { |
| 135 | Some(Node::Text(t, _)) => Some(t), |
| 136 | Some(Node::Elem(e)) => last_text(&e.children), |
| 137 | _ => None, |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Takes out the first element, depth first, that carries a meta: second-field-align's first |
| 142 | /// field, as hayagriva's `remove_any_meta` finds it. |
| 143 | pub fn remove_any_meta(nodes: &mut Vec<Node>) -> Option<Node> { |
| 144 | for i in 0..nodes.len() { |
| 145 | if let Node::Elem(e) = &mut nodes[i] { |
| 146 | if e.meta.is_some() { |
| 147 | return Some(nodes.remove(i)); |
| 148 | } |
| 149 | if let Some(n) = remove_any_meta(&mut e.children) { |
| 150 | return Some(n); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | None |
| 155 | } |
| 156 | |
| 157 | /// The first element, depth first, that `f` accepts: hayagriva's `find_elem_by`. |
| 158 | pub fn find_elem<'a>(nodes: &'a [Node], f: &impl Fn(&Elem) -> bool) -> Option<&'a Elem> { |
| 159 | nodes.iter().find_map(|n| match n { |
| 160 | Node::Elem(e) if f(e) => Some(e), |
| 161 | Node::Elem(e) => find_elem(&e.children, f), |
| 162 | _ => None, |
| 163 | }) |
| 164 | } |
| 165 | |
| 166 | /// Merges neighbouring text of one formatting and inlines elements that carry neither display nor |
| 167 | /// meta. |
| 168 | pub fn simplify(nodes: Vec<Node>) -> Vec<Node> { |
| 169 | let mut acc: Vec<Node> = Vec::new(); |
| 170 | for n in nodes { |
| 171 | match n { |
| 172 | Node::Text(t, f) => match acc.last_mut() { |
| 173 | Some(Node::Text(last, lf)) if *lf == f => last.push_str(&t), |
| 174 | _ => acc.push(Node::Text(t, f)), |
| 175 | }, |
| 176 | // hayagriva extends without merging here, so inlined text stays a separate run. |
| 177 | Node::Elem(e) if e.display.is_none() && e.meta.is_none() => acc.extend(e.children), |
| 178 | Node::Elem(e) => acc.push(Node::Elem(Elem { children: simplify(e.children), ..e })), |
| 179 | other => acc.push(other), |
| 180 | } |
| 181 | } |
| 182 | acc |
| 183 | } |
| 184 | |
| 185 | /// The text with each run's formatting, as hayagriva's terminal writer sets it out: the key on |
| 186 | /// which disambiguation finds two cites that read the same. |
| 187 | pub fn styled(nodes: &[Node]) -> String { |
| 188 | fn walk(nodes: &[Node], s: &mut String) { |
| 189 | for n in nodes { |
| 190 | match n { |
| 191 | Node::Text(t, f) | Node::Link { text: t, fmt: f, .. } => { |
| 192 | let _ = write!(s, "{:?}{}\u{1b}", f, t); |
| 193 | } |
| 194 | Node::Math(m) => { let _ = write!(s, "${}$", m); } |
| 195 | Node::Cite { .. } => {} |
| 196 | Node::Elem(e) => { |
| 197 | let block = e.display == Some(Display::Block); |
| 198 | if block { s.push('\n'); } |
| 199 | walk(&e.children, s); |
| 200 | if block { s.push('\n'); } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | let mut s = String::new(); |
| 206 | walk(nodes, &mut s); |
| 207 | s |
| 208 | } |
| 209 | |
| 210 | /// The text without formatting. A block element sits on lines of its own, as hayagriva's plain |
| 211 | /// writer puts it. |
| 212 | pub fn plain(nodes: &[Node]) -> String { |
| 213 | let mut s = String::new(); |
| 214 | write_plain(nodes, &mut s); |
| 215 | s |
| 216 | } |
| 217 | |
| 218 | fn write_plain(nodes: &[Node], s: &mut String) { |
| 219 | for n in nodes { |
| 220 | match n { |
| 221 | Node::Text(t, _) => s.push_str(t), |
| 222 | Node::Math(m) => { let _ = write!(s, "${}$", m); } |
| 223 | Node::Link { text, .. } => s.push_str(text), |
| 224 | Node::Cite { .. } => {} |
| 225 | Node::Elem(e) => { |
| 226 | let block = e.display == Some(Display::Block); |
| 227 | if block { s.push('\n'); } |
| 228 | write_plain(&e.children, s); |
| 229 | if block { s.push('\n'); } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |