oxedyne/fe2o3/fe2o3_sbj/src/export.rs
12.4 KiB, 7 runs
created by r1870400018:71296, 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 | //! Exporting a document tree back to the neutral tree of [`fe2o3_text::doc`](oxedyne_fe2o3_text::doc). |
| 2 | //! |
| 3 | //! The inverse of [`import::from_doc`](crate::import::from_doc): `to_doc(from_doc(d)) == d`. A node |
| 4 | //! that `from_doc` never writes has no block or inline to come back as, and is refused by name rather |
| 5 | //! than guessed at: a `section`, a `box`, a block `image`, a quote's `cite`, a `style`, and an address |
| 6 | //! or an inline image given by content hash, which a caller resolves to a source first. |
| 7 | |
| 8 | use crate::{ |
| 9 | canon, |
| 10 | kinds::{ |
| 11 | NodeKind, |
| 12 | ADDR_HASH, |
| 13 | ADDR_NAME, |
| 14 | KEY_CHILDREN, |
| 15 | KEY_CLASSES, |
| 16 | KEY_COLS, |
| 17 | KEY_HEAD, |
| 18 | KEY_HEADER, |
| 19 | KEY_ID, |
| 20 | KEY_PAIRS, |
| 21 | KEY_SRC, |
| 22 | KEY_STYLE, |
| 23 | KEY_STYLES, |
| 24 | KEY_TO, |
| 25 | }, |
| 26 | limit, |
| 27 | validate, |
| 28 | SCHEMA_DOC_1, |
| 29 | }; |
| 30 | |
| 31 | use oxedyne_fe2o3_jdat::prelude::*; |
| 32 | use oxedyne_fe2o3_text::doc::{ |
| 33 | Align, |
| 34 | Attrs, |
| 35 | Block, |
| 36 | Cell, |
| 37 | Doc, |
| 38 | Inline, |
| 39 | Row, |
| 40 | Term, |
| 41 | }; |
| 42 | |
| 43 | use oxedyne_fe2o3_core::prelude::*; |
| 44 | |
| 45 | /// Maps an `oxeweb/doc/1` tree to the document it holds. |
| 46 | /// |
| 47 | /// The tree is held to canon and to the validator first, so what is mapped is a tree the format |
| 48 | /// accepts; the walk then refuses any node deeper than [`limit::DEPTH`] whatever route the tree |
| 49 | /// arrived by, since it recurses. |
| 50 | pub fn to_doc(tree: &Dat) -> Outcome<Doc> { |
| 51 | res!(canon::check(tree)); |
| 52 | res!(validate::validate(tree, SCHEMA_DOC_1)); |
| 53 | let (kind, map) = res!(node_map(tree)); |
| 54 | if kind != NodeKind::Doc { |
| 55 | return Err(err!("The root is a {}, not a doc.", kind.label(); Invalid, Input)); |
| 56 | } |
| 57 | if map.get(&dat!(KEY_STYLES)).is_some() { |
| 58 | return Err(err!( |
| 59 | "The doc carries a '{}' table, which a document of fe2o3_text::doc has no place for.", |
| 60 | KEY_STYLES; |
| 61 | Invalid, Input, Unimplemented)); |
| 62 | } |
| 63 | Ok(Doc { blocks: res!(blocks(res!(kids(map)), 1)) }) |
| 64 | } |
| 65 | |
| 66 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 67 | // │ BLOCKS │ |
| 68 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 69 | |
| 70 | fn blocks( |
| 71 | nodes: &[Dat], |
| 72 | depth: usize, |
| 73 | ) |
| 74 | -> Outcome<Vec<Block>> |
| 75 | { |
| 76 | let mut out = Vec::with_capacity(nodes.len()); |
| 77 | for n in nodes { |
| 78 | out.push(res!(block(n, depth + 1))); |
| 79 | } |
| 80 | Ok(out) |
| 81 | } |
| 82 | |
| 83 | fn block( |
| 84 | n: &Dat, |
| 85 | depth: usize, |
| 86 | ) |
| 87 | -> Outcome<Block> |
| 88 | { |
| 89 | res!(check_depth(depth)); |
| 90 | let (kind, map) = res!(node_map(n)); |
| 91 | let kids = res!(kids(map)); |
| 92 | Ok(match kind { |
| 93 | NodeKind::Heading => Block::Heading { |
| 94 | level: match map.get(&dat!("level")) { |
| 95 | Some(Dat::U8(level)) => *level, |
| 96 | _ => return Err(err!("A heading carries no u8 level."; Invalid, Input, Missing)), |
| 97 | }, |
| 98 | content: res!(inlines(kids, depth)), |
| 99 | }, |
| 100 | NodeKind::Para => Block::Para(res!(inlines(kids, depth))), |
| 101 | NodeKind::List => { |
| 102 | let mut items = Vec::with_capacity(kids.len()); |
| 103 | for item in kids { |
| 104 | let (_, imap) = res!(node_map(item)); |
| 105 | items.push(res!(blocks(res!(self::kids(imap)), depth + 1))); |
| 106 | } |
| 107 | Block::List { ordered: res!(flag(map, "ordered")), items } |
| 108 | }, |
| 109 | NodeKind::Code => Block::Code { |
| 110 | lang: res!(opt_str(map, "lang")), |
| 111 | text: res!(req_str(map, "text")), |
| 112 | }, |
| 113 | NodeKind::Quote => { |
| 114 | if map.get(&dat!("cite")).is_some() { |
| 115 | return Err(err!( |
| 116 | "A quote carries a 'cite', which a document of fe2o3_text::doc has no place \ |
| 117 | for."; Invalid, Input, Unimplemented)); |
| 118 | } |
| 119 | Block::Quote(res!(blocks(kids, depth))) |
| 120 | }, |
| 121 | NodeKind::Rule => Block::Rule, |
| 122 | NodeKind::Table => { |
| 123 | let mut cols = Vec::new(); |
| 124 | if let Some(Dat::List(list)) = map.get(&dat!(KEY_COLS)) { |
| 125 | for c in list { |
| 126 | cols.push(match c { |
| 127 | Dat::Str(s) if s == "none" => Align::None, |
| 128 | Dat::Str(s) if s == "start" => Align::Start, |
| 129 | Dat::Str(s) if s == "center" => Align::Centre, |
| 130 | Dat::Str(s) if s == "end" => Align::End, |
| 131 | d => return Err(err!( |
| 132 | "A table column has the alignment {:?}.", d; Invalid, Input, Range)), |
| 133 | }); |
| 134 | } |
| 135 | } |
| 136 | let mut caption = Vec::new(); |
| 137 | let mut head = None; |
| 138 | let mut rows = Vec::with_capacity(kids.len()); |
| 139 | for kid in kids { |
| 140 | let (k, kmap) = res!(node_map(kid)); |
| 141 | match k { |
| 142 | NodeKind::Caption => caption = res!(inlines(res!(self::kids(kmap)), depth + 1)), |
| 143 | NodeKind::Row => { |
| 144 | let row = res!(table_row(res!(self::kids(kmap)), depth + 1)); |
| 145 | if res!(flag(kmap, KEY_HEAD)) { |
| 146 | head = Some(row); |
| 147 | } else { |
| 148 | rows.push(row); |
| 149 | } |
| 150 | }, |
| 151 | _ => return Err(err!( |
| 152 | "A table holds a {}, which is neither a caption nor a row.", k.label(); |
| 153 | Invalid, Input)), |
| 154 | } |
| 155 | } |
| 156 | Block::Table { head, rows, cols, caption } |
| 157 | }, |
| 158 | NodeKind::Div => Block::Div { |
| 159 | attrs: res!(attrs(map)), |
| 160 | content: res!(blocks(kids, depth)), |
| 161 | }, |
| 162 | NodeKind::Figure => { |
| 163 | let mut content = Vec::with_capacity(kids.len()); |
| 164 | let mut caption = Vec::new(); |
| 165 | for kid in kids { |
| 166 | let (k, kmap) = res!(node_map(kid)); |
| 167 | if k == NodeKind::Caption { |
| 168 | caption = res!(inlines(res!(self::kids(kmap)), depth + 1)); |
| 169 | } else { |
| 170 | content.push(res!(block(kid, depth + 1))); |
| 171 | } |
| 172 | } |
| 173 | Block::Figure { attrs: res!(attrs(map)), content, caption } |
| 174 | }, |
| 175 | NodeKind::Terms => { |
| 176 | let mut items = Vec::with_capacity(kids.len()); |
| 177 | for kid in kids { |
| 178 | items.push(res!(term(kid, depth + 1))); |
| 179 | } |
| 180 | Block::Terms { attrs: res!(attrs(map)), items } |
| 181 | }, |
| 182 | k => return Err(err!( |
| 183 | "A {} has no block of fe2o3_text::doc to become; only a tree import::from_doc could \ |
| 184 | have written maps back.", k.label(); |
| 185 | Invalid, Input, Unimplemented)), |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | fn table_row( |
| 190 | cells: &[Dat], |
| 191 | depth: usize, |
| 192 | ) |
| 193 | -> Outcome<Row> |
| 194 | { |
| 195 | res!(check_depth(depth)); |
| 196 | let mut out = Vec::with_capacity(cells.len()); |
| 197 | for c in cells { |
| 198 | res!(check_depth(depth + 1)); |
| 199 | let (_, cmap) = res!(node_map(c)); |
| 200 | out.push(Cell { |
| 201 | content: res!(inlines(res!(kids(cmap)), depth + 1)), |
| 202 | header: res!(flag(cmap, KEY_HEADER)), |
| 203 | }); |
| 204 | } |
| 205 | Ok(Row(out)) |
| 206 | } |
| 207 | |
| 208 | fn term( |
| 209 | n: &Dat, |
| 210 | depth: usize, |
| 211 | ) |
| 212 | -> Outcome<Term> |
| 213 | { |
| 214 | res!(check_depth(depth)); |
| 215 | let (_, map) = res!(node_map(n)); |
| 216 | let mut t = Term::default(); |
| 217 | for kid in res!(kids(map)) { |
| 218 | let (k, kmap) = res!(node_map(kid)); |
| 219 | let parts = res!(kids(kmap)); |
| 220 | match k { |
| 221 | NodeKind::Label => t.term = res!(inlines(parts, depth + 1)), |
| 222 | _ => t.defs.push(res!(blocks(parts, depth + 1))), |
| 223 | } |
| 224 | } |
| 225 | Ok(t) |
| 226 | } |
| 227 | |
| 228 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 229 | // │ INLINES │ |
| 230 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 231 | |
| 232 | fn inlines( |
| 233 | nodes: &[Dat], |
| 234 | depth: usize, |
| 235 | ) |
| 236 | -> Outcome<Vec<Inline>> |
| 237 | { |
| 238 | let mut out = Vec::with_capacity(nodes.len()); |
| 239 | for n in nodes { |
| 240 | out.push(res!(inline(n, depth + 1))); |
| 241 | } |
| 242 | Ok(out) |
| 243 | } |
| 244 | |
| 245 | fn inline( |
| 246 | n: &Dat, |
| 247 | depth: usize, |
| 248 | ) |
| 249 | -> Outcome<Inline> |
| 250 | { |
| 251 | res!(check_depth(depth)); |
| 252 | // A text run's payload is the string itself. |
| 253 | if let Dat::Usr(uid, Some(payload)) = n { |
| 254 | if uid.code() == NodeKind::Text.code() { |
| 255 | return match payload.as_ref() { |
| 256 | Dat::Str(s) => Ok(Inline::Text(s.clone())), |
| 257 | d => Err(err!("A text run carries a {:?}.", d.kind(); Invalid, Input)), |
| 258 | }; |
| 259 | } |
| 260 | } |
| 261 | let (kind, map) = res!(node_map(n)); |
| 262 | let kids = res!(kids(map)); |
| 263 | Ok(match kind { |
| 264 | NodeKind::Emph => Inline::Emph { |
| 265 | strong: res!(flag(map, "strong")), |
| 266 | content: res!(inlines(kids, depth)), |
| 267 | }, |
| 268 | NodeKind::Link => { |
| 269 | let to = match map.get(&dat!(KEY_TO)) { |
| 270 | Some(Dat::Map(addr)) => match addr.get(&dat!(ADDR_NAME)) { |
| 271 | Some(Dat::Str(s)) => s.clone(), |
| 272 | _ => return Err(err!( |
| 273 | "A link is addressed by content hash, which has no source string to \ |
| 274 | become; resolve it to a name first."; Invalid, Input, Unimplemented)), |
| 275 | }, |
| 276 | _ => return Err(err!("A link carries no address."; Invalid, Input, Missing)), |
| 277 | }; |
| 278 | Inline::Link { to, content: res!(inlines(kids, depth)) } |
| 279 | }, |
| 280 | NodeKind::Img => { |
| 281 | if map.get(&dat!(ADDR_HASH)).is_some() { |
| 282 | return Err(err!( |
| 283 | "An inline image is addressed by content hash, which has no source string to \ |
| 284 | become; resolve it to a source first."; Invalid, Input, Unimplemented)); |
| 285 | } |
| 286 | Inline::Image { |
| 287 | src: res!(req_str(map, KEY_SRC)), |
| 288 | alt: res!(req_str(map, "alt")), |
| 289 | attrs: res!(attrs(map)), |
| 290 | } |
| 291 | }, |
| 292 | NodeKind::Tt => Inline::Code(res!(req_str(map, "text"))), |
| 293 | NodeKind::Span => Inline::Span { |
| 294 | attrs: res!(attrs(map)), |
| 295 | content: res!(inlines(kids, depth)), |
| 296 | }, |
| 297 | NodeKind::Break => Inline::Break, |
| 298 | NodeKind::Sup => Inline::Sup(res!(inlines(kids, depth))), |
| 299 | NodeKind::Sub => Inline::Sub(res!(inlines(kids, depth))), |
| 300 | k => return Err(err!( |
| 301 | "A {} has no inline of fe2o3_text::doc to become.", k.label(); |
| 302 | Invalid, Input, Unimplemented)), |
| 303 | }) |
| 304 | } |
| 305 | |
| 306 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 307 | // │ NODES AND FIELDS │ |
| 308 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 309 | |
| 310 | fn check_depth(depth: usize) -> Outcome<()> { |
| 311 | if depth > limit::DEPTH { |
| 312 | return Err(err!( |
| 313 | "The tree is deeper than the limit of {} nodes.", limit::DEPTH; |
| 314 | Invalid, Input, TooBig, LimitReached)); |
| 315 | } |
| 316 | Ok(()) |
| 317 | } |
| 318 | |
| 319 | /// A node's kind and payload map, refusing a `style`, which no document of `fe2o3_text::doc` |
| 320 | /// carries. |
| 321 | fn node_map(n: &Dat) -> Outcome<(NodeKind, &DaticleMap)> { |
| 322 | match n { |
| 323 | Dat::Usr(uid, Some(payload)) => { |
| 324 | let kind = res!(NodeKind::from_code(uid.code())); |
| 325 | match payload.as_ref() { |
| 326 | Dat::Map(map) => { |
| 327 | if map.get(&dat!(KEY_STYLE)).is_some() { |
| 328 | return Err(err!( |
| 329 | "A {} carries a 'style', which a document of fe2o3_text::doc has no \ |
| 330 | place for.", kind.label(); |
| 331 | Invalid, Input, Unimplemented)); |
| 332 | } |
| 333 | Ok((kind, map)) |
| 334 | }, |
| 335 | d => Err(err!("A {} carries a {:?} payload.", kind.label(), d.kind(); Invalid, Input)), |
| 336 | } |
| 337 | }, |
| 338 | d => Err(err!("Expected a node, found a {:?}.", d.kind(); Invalid, Input)), |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | fn kids(map: &DaticleMap) -> Outcome<&[Dat]> { |
| 343 | match map.get(&dat!(KEY_CHILDREN)) { |
| 344 | None => Ok(&[]), |
| 345 | Some(Dat::List(v)) => Ok(&v[..]), |
| 346 | Some(d) => Err(err!("Node children are a {:?}, not a list.", d.kind(); Invalid, Input)), |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /// A flag field, absent meaning false. |
| 351 | fn flag( |
| 352 | map: &DaticleMap, |
| 353 | key: &str, |
| 354 | ) |
| 355 | -> Outcome<bool> |
| 356 | { |
| 357 | match map.get(&dat!(key)) { |
| 358 | None => Ok(false), |
| 359 | Some(Dat::Bool(b)) => Ok(*b), |
| 360 | Some(d) => Err(err!("Field '{}' is a {:?}, not a bool.", key, d.kind(); Invalid, Input)), |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | fn opt_str( |
| 365 | map: &DaticleMap, |
| 366 | key: &str, |
| 367 | ) |
| 368 | -> Outcome<Option<String>> |
| 369 | { |
| 370 | match map.get(&dat!(key)) { |
| 371 | None => Ok(None), |
| 372 | Some(Dat::Str(s)) => Ok(Some(s.clone())), |
| 373 | Some(d) => Err(err!("Field '{}' is a {:?}, not a str.", key, d.kind(); Invalid, Input)), |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | fn req_str( |
| 378 | map: &DaticleMap, |
| 379 | key: &str, |
| 380 | ) |
| 381 | -> Outcome<String> |
| 382 | { |
| 383 | match res!(opt_str(map, key)) { |
| 384 | Some(s) => Ok(s), |
| 385 | None => Err(err!("Field '{}' is missing.", key; Invalid, Input, Missing)), |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | fn strs( |
| 390 | map: &DaticleMap, |
| 391 | key: &str, |
| 392 | ) |
| 393 | -> Outcome<Vec<String>> |
| 394 | { |
| 395 | match map.get(&dat!(key)) { |
| 396 | None => Ok(Vec::new()), |
| 397 | Some(Dat::List(list)) => { |
| 398 | let mut out = Vec::with_capacity(list.len()); |
| 399 | for d in list { |
| 400 | match d { |
| 401 | Dat::Str(s) => out.push(s.clone()), |
| 402 | d => return Err(err!( |
| 403 | "Field '{}' holds a {:?}, not a str.", key, d.kind(); Invalid, Input)), |
| 404 | } |
| 405 | } |
| 406 | Ok(out) |
| 407 | }, |
| 408 | Some(d) => Err(err!("Field '{}' is a {:?}, not a list.", key, d.kind(); Invalid, Input)), |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | fn attrs(map: &DaticleMap) -> Outcome<Attrs> { |
| 413 | let flat = res!(strs(map, KEY_PAIRS)); |
| 414 | let mut pairs = Vec::with_capacity(flat.len() / 2); |
| 415 | for kv in flat.chunks(2) { |
| 416 | match kv { |
| 417 | [k, v] => pairs.push((k.clone(), v.clone())), |
| 418 | _ => return Err(err!( |
| 419 | "Field '{}' holds an odd number of strings.", KEY_PAIRS; Invalid, Input)), |
| 420 | } |
| 421 | } |
| 422 | Ok(Attrs { |
| 423 | id: res!(opt_str(map, KEY_ID)), |
| 424 | classes: res!(strs(map, KEY_CLASSES)), |
| 425 | pairs, |
| 426 | }) |
| 427 | } |