Skip to main content

docling_core/
json.rs

1//! Export a [`DoclingDocument`] to docling-core's native JSON wire format
2//! (`DoclingDocument` schema v1.11.0) — the same shape `export_to_dict()` /
3//! `save_as_json()` produce in Python docling, and the inverse of the
4//! JSON-docling reader.
5//!
6//! The crate's [`Node`] model bakes Markdown escaping (and inline markers) into
7//! its text, whereas docling stores raw text and escapes at render time. We
8//! therefore *un-escape* on the way out so a docling-core round-trip
9//! (`load_from_json().export_to_markdown()`) reproduces the same Markdown.
10
11use serde_json::{json, Map, Value};
12
13use crate::document::{CaptionParent, ContentLayer, DoclingDocument, Node, Table};
14
15const SCHEMA_VERSION: &str = "1.11.0";
16
17/// docling-core's `CodeLanguageLabel` values (anything else serializes as
18/// `unknown`, which the model requires for code items).
19const CODE_LANGUAGES: &[&str] = &[
20    "Ada",
21    "Awk",
22    "Bash",
23    "bc",
24    "C",
25    "C#",
26    "C++",
27    "CMake",
28    "COBOL",
29    "CSS",
30    "Ceylon",
31    "Clojure",
32    "Crystal",
33    "Cuda",
34    "Cython",
35    "D",
36    "Dart",
37    "dc",
38    "Dockerfile",
39    "DocLang",
40    "Elixir",
41    "Erlang",
42    "FORTRAN",
43    "Forth",
44    "Go",
45    "HTML",
46    "Haskell",
47    "Haxe",
48    "Java",
49    "JavaScript",
50    "JSON",
51    "Julia",
52    "Kotlin",
53    "Latex",
54    "Lisp",
55    "Lua",
56    "Matlab",
57    "MoonScript",
58    "Nim",
59    "OCaml",
60    "ObjectiveC",
61    "Octave",
62    "PHP",
63    "Pascal",
64    "Perl",
65    "Prolog",
66    "Python",
67    "Racket",
68    "Ruby",
69    "Rust",
70    "SML",
71    "SQL",
72    "Scala",
73    "Scheme",
74    "Swift",
75    "Tikz",
76    "TypeScript",
77    "VisualBasic",
78    "XML",
79    "YAML",
80];
81
82/// Map a fence language to docling's `CodeLanguageLabel` (case-insensitive), else
83/// `unknown`.
84/// docling's `CodeLanguageLabel` for a language name (`unknown` when it is
85/// not one docling knows) — the mapping the JSON `code_language` field uses,
86/// for a backend that wants to test a hint before storing it.
87pub fn code_language_label(lang: &str) -> &'static str {
88    code_language(Some(lang))
89}
90
91pub(crate) fn code_language(lang: Option<&str>) -> &'static str {
92    match lang {
93        Some(l) => CODE_LANGUAGES
94            .iter()
95            .find(|c| c.eq_ignore_ascii_case(l))
96            .copied()
97            .unwrap_or("unknown"),
98        None => "unknown",
99    }
100}
101
102/// An item's exact provenance, written verbatim (see [`Builder::prov_json`]):
103/// a [`Node::Prov`] wrapper's top-left page box, or a tree item's
104/// [`TreeProv`](crate::tree::TreeProv).
105#[derive(Clone, Copy)]
106struct ExactProv {
107    page_no: usize,
108    bbox: [f64; 4],
109    bottom_left: bool,
110    charspan: [usize; 2],
111}
112
113impl From<&crate::tree::TreeProv> for ExactProv {
114    fn from(p: &crate::tree::TreeProv) -> Self {
115        ExactProv {
116            page_no: p.page_no,
117            bbox: p.bbox,
118            bottom_left: p.bottom_left,
119            charspan: p.charspan,
120        }
121    }
122}
123
124/// Page sizes by page number; the first entry per page wins.
125type PageSizes = std::collections::HashMap<usize, (f64, f64)>;
126
127fn page_sizes(pages: &[(usize, f64, f64)]) -> PageSizes {
128    let mut sizes = PageSizes::with_capacity(pages.len());
129    for &(p, w, h) in pages {
130        sizes.entry(p).or_insert((w, h));
131    }
132    sizes
133}
134
135/// docling-core's `_clamp_provenance_bboxes_to_pages`: each `prov` box of
136/// every item on a known page is clamped to `[0, width] × [0, height]`
137/// (whatever its `coord_origin` — docling clamps the stored numbers), and a
138/// table whose provenance sits on one page has its cell boxes clamped too.
139/// Runs over the finished `texts`, `pictures`, `tables` and
140/// `key_value_items` (the form-field buckets are left as built).
141fn clamp_boxes_to_pages(b: &mut Builder) {
142    if b.pages.is_empty() {
143        return;
144    }
145    // A map, not a scan per box: that was items × pages on long documents.
146    let sizes = page_sizes(&b.pages);
147    for item in &mut b.texts {
148        match item {
149            Item::Text(t) => t.prov.clamp(&sizes),
150            Item::Json(v) => {
151                clamp_item(v, &sizes);
152            }
153        }
154    }
155    for item in b.pictures.iter_mut().chain(b.key_value_items.iter_mut()) {
156        clamp_item(item, &sizes);
157    }
158    for t in &mut b.tables {
159        if let Some(size) = clamp_item(&mut t.item, &sizes) {
160            t.data.clamp(size);
161        }
162    }
163}
164
165/// Clamp `item`'s `prov` boxes; returns the size of the page when the item
166/// sits on exactly one known page — a table's cells are clamped to it.
167fn clamp_item(item: &mut Value, sizes: &PageSizes) -> Option<(f64, f64)> {
168    let size = |page_no: &Value| -> Option<(f64, f64)> {
169        let n = page_no.as_u64()? as usize;
170        sizes.get(&n).copied()
171    };
172    let clamp_bbox = |bbox: &mut Value, (w, h): (f64, f64)| {
173        for (key, hi) in [("l", w), ("r", w), ("t", h), ("b", h)] {
174            if let Some(v) = bbox.get(key).and_then(Value::as_f64) {
175                bbox[key] = json!(round2(v.clamp(0.0, hi.max(0.0))));
176            }
177        }
178    };
179    let mut table_page: Option<Option<(f64, f64)>> = None;
180    if let Some(provs) = item.get_mut("prov").and_then(Value::as_array_mut) {
181        let mut page_nos: Vec<u64> = Vec::new();
182        for prov in provs.iter_mut() {
183            if let Some(n) = prov.get("page_no").and_then(Value::as_u64) {
184                page_nos.push(n);
185            }
186            let Some(sz) = prov.get("page_no").and_then(size) else {
187                continue;
188            };
189            if let Some(bbox) = prov.get_mut("bbox") {
190                clamp_bbox(bbox, sz);
191            }
192        }
193        page_nos.sort_unstable();
194        page_nos.dedup();
195        if let [only] = page_nos[..] {
196            table_page = Some(size(&json!(only)));
197        }
198    }
199    table_page.flatten()
200}
201
202/// docling-core's `Formatting` model, every field written.
203fn formatting_json(f: &crate::tree::Formatting) -> Value {
204    json!({
205        "bold": f.bold,
206        "italic": f.italic,
207        "underline": f.underline,
208        "strikethrough": f.strikethrough,
209        "script": match f.script {
210            crate::Script::Baseline => "baseline",
211            crate::Script::Sub => "sub",
212            crate::Script::Super => "super",
213        },
214    })
215}
216
217/// docling's `TrackSource` entry: `kind`, the two offsets, then the optional
218/// `identifier` / `voice` (dropped when `None`, as `exclude_none` does).
219fn track_json(t: &crate::tree::TreeTrack) -> Value {
220    let mut m = serde_json::Map::new();
221    m.insert("kind".into(), json!("track"));
222    m.insert("start_time".into(), json!(t.start_time));
223    m.insert("end_time".into(), json!(t.end_time));
224    if let Some(id) = &t.identifier {
225        m.insert("identifier".into(), json!(id));
226    }
227    if let Some(v) = &t.voice {
228        m.insert("voice".into(), json!(v));
229    }
230    Value::Object(m)
231}
232
233/// docling-core's `ImageRef` (pydantic field order: mimetype, dpi, size —
234/// as floats — uri), with the bytes inlined as a `data:` URI — a PNG, like
235/// `ImageRef.from_pil`'s (`pixel_digest::docling_data_uri`).
236fn image_ref_json(img: &crate::PictureImage) -> Value {
237    let (mimetype, uri) = crate::pixel_digest::docling_data_uri(img);
238    json!({
239        "mimetype": mimetype,
240        "dpi": img.dpi,
241        "size": { "width": img.width as f64, "height": img.height as f64 },
242        "uri": uri,
243    })
244}
245
246/// Build the docling-core JSON object for `doc`.
247pub fn to_json(doc: &DoclingDocument) -> Value {
248    build_parts(doc, false).into_value(None)
249}
250
251/// The key a table's `data` is replaced by in [`to_json_typed_tables`]'s
252/// `Value`: the index of its [`TableData`] beside it.
253pub(crate) const TYPED_TABLE: &str = "_typed_table";
254
255/// [`to_json`] for the serializers that walk the `Value` (HTML, Pandoc,
256/// WebVTT), with each table's `data` left typed (#875): the item carries
257/// `{"_typed_table": i}` and the cells are `tables[i]`, read through
258/// [`table_view`]. As a `Value` a table cost two maps of eleven owned keys
259/// per cell (its `table_cells` record and its `grid` copy) — 2.6 GB of an
260/// HTML export's peak on a 640,000-cell CSV. `note_keys` adds the Pandoc
261/// writer's keys ([`to_json_with_notes`]).
262pub(crate) fn to_json_typed_tables(
263    doc: &DoclingDocument,
264    note_keys: bool,
265) -> (Value, Vec<TableData>) {
266    let mut tables = Vec::new();
267    let value = build_parts(doc, note_keys).into_value(Some(&mut tables));
268    (value, tables)
269}
270
271/// [`to_json`] plus the note calls docling's model cannot express: a text
272/// item calling footnotes carries `"_notes": [[offset, text], …]`, a
273/// footnote item that is such a note's body `"_note_body": true` (#538),
274/// a text item that is a link destination `"_anchors": ["id", …]` (#849),
275/// a heading whose text opens with generated numbering `"_number": "1.2"`
276/// (#850), a run kept as a tracked change `"_revision": {"kind":
277/// "insertion" | "deletion", "author", "date"}` (#851),
278/// a picture's image its displayed size `"_display": [width, height]` in
279/// inches (#834), and the document's `metadata` whether or not the JSON
280/// export was asked for it (#836). The Pandoc writer reads these; nothing
281/// is ever exported with them. The tables stay typed, as in
282/// [`to_json_typed_tables`].
283pub(crate) fn to_json_with_notes(doc: &DoclingDocument) -> (Value, Vec<TableData>) {
284    to_json_typed_tables(doc, true)
285}
286
287fn build_parts(doc: &DoclingDocument, note_keys: bool) -> Parts {
288    let mut b = Builder {
289        note_keys,
290        ..Builder::default()
291    };
292    // A backend that built docling's item tree ([`crate::tree`]) has already
293    // decided every parent, child and creation index; serialize that. The
294    // flat nodes are for the other serializers.
295    let body = match &doc.tree {
296        Some(tree) => {
297            // The page map still comes from the flat stream's markers (a
298            // PPTX slide's EMU size): the tree holds items, not pages.
299            for n in &doc.nodes {
300                if let Node::PageInfo {
301                    page_no,
302                    width,
303                    height,
304                } = n
305                {
306                    if *page_no > 0 {
307                        b.pages.push((*page_no, *width as f64, *height as f64));
308                    }
309                }
310            }
311            // docling-core's `validate_misplaced_list_items` runs on every
312            // document it serializes; apply it the same way (keeping the
313            // items' children, which docling-core drops — #586).
314            let mut tree = tree.clone();
315            // Schema 1.11.0's inline runs own their whitespace (#997); a
316            // backend still building trimmed runs gets the old join back in
317            // the runs, as docling-core migrates a pre-1.11 document on load.
318            if !tree.inline_whitespace {
319                tree.separate_legacy_inline_runs();
320            }
321            tree.wrap_misplaced_list_items();
322            b.write_tree(&tree).into_iter().map(Child::json).collect()
323        }
324        None => b.walk_into(&doc.nodes, "#/body"),
325    };
326    b.link_comments();
327    b.link_script_runs();
328
329    // docling-core's `validate_document` — a pydantic `model_validator` every
330    // `DoclingDocument` passes through when a `ConversionResult` (or a
331    // serializer) is built around it — clamps every provenance box, and a
332    // table's cell boxes, into its page's bounds in place
333    // (`_clamp_provenance_bboxes_to_pages`). The JSON docling writes therefore
334    // carries the clamped boxes: a spreadsheet region whose page was sized
335    // `right − left` × `bottom − top` loses its offset, a slide shape hanging
336    // off the slide edge is cut at it. Reproduce that on the finished items.
337    clamp_boxes_to_pages(&mut b);
338
339    let pages = b
340        .pages
341        .iter()
342        .map(|(n, w, h)| {
343            let r2 = |v: f64| (v * 100.0).round() / 100.0;
344            // docling-core's `PageItem` field order: size, image, page_no
345            // (#520 — the image only when page images were generated).
346            let mut page = serde_json::Map::new();
347            page.insert("size".into(), json!({ "width": r2(*w), "height": r2(*h) }));
348            if let Some(img) = doc.page_images.get(n) {
349                page.insert("image".into(), image_ref_json(img));
350            }
351            page.insert("page_no".into(), json!(n));
352            (n.to_string(), Value::Object(page))
353        })
354        .collect::<serde_json::Map<String, Value>>();
355    // docling only emits `field_regions` / `field_items` when a document has
356    // form fields, and places them just before `pages`.
357    let fields = (!b.field_regions.is_empty()).then_some((b.field_regions, b.field_items));
358    Parts {
359        name: doc.name.clone(),
360        // The Pandoc writer (`note_keys`) always gets the metadata, for
361        // its `meta` (#836); the JSON export only when asked.
362        metadata: (doc.metadata_output.in_json() || note_keys)
363            .then(|| doc.metadata.to_json())
364            .filter(|m| m.as_object().is_some_and(|o| !o.is_empty())),
365        origin: json!({
366            "mimetype": "text/plain",
367            "binary_hash": fnv1a(&doc.name),
368            "filename": doc.name,
369        }),
370        furniture: json!({
371            "self_ref": "#/furniture",
372            "children": [],
373            "content_layer": "furniture",
374            "name": "_root_",
375            "label": "unspecified",
376        }),
377        body,
378        groups: b.groups,
379        texts: b.texts,
380        pictures: b.pictures,
381        tables: b.tables,
382        key_value_items: b.key_value_items,
383        fields,
384        pages,
385    }
386}
387
388/// The finished document, before it is a `Value`: one representation
389/// behind both [`to_json`] (a `Value`) and [`write_json`] (bytes, with no
390/// `Value` ever built — the text items stay compact [`Item`]s throughout).
391/// Both produce the same keys in the same order.
392struct Parts {
393    name: String,
394    /// The document's metadata (#679), after `origin`, only when the
395    /// document's output mode asks for it and there is any.
396    metadata: Option<Value>,
397    origin: Value,
398    furniture: Value,
399    /// The body's `children`; the rest of the body object is fixed.
400    body: Vec<Child>,
401    groups: Vec<Value>,
402    texts: Vec<Item>,
403    pictures: Vec<Value>,
404    tables: Vec<TableItem>,
405    key_value_items: Vec<Value>,
406    /// `field_regions` and `field_items`, present only for a document with
407    /// form fields (non-form documents' JSON has neither key).
408    fields: Option<(Vec<Value>, Vec<Value>)>,
409    pages: serde_json::Map<String, Value>,
410}
411
412impl Parts {
413    /// The document `Value`; with `typed`, the tables' `data` goes there
414    /// instead (see [`to_json_typed_tables`]).
415    fn into_value(self, mut typed: Option<&mut Vec<TableData>>) -> Value {
416        // Moved in, never through `json!`, which would deep-copy each array.
417        let mut out = serde_json::Map::with_capacity(15);
418        out.insert("schema_name".into(), "DoclingDocument".into());
419        out.insert("version".into(), SCHEMA_VERSION.into());
420        out.insert("name".into(), Value::String(self.name));
421        out.insert("origin".into(), self.origin);
422        if let Some(m) = self.metadata {
423            out.insert("metadata".into(), m);
424        }
425        out.insert("furniture".into(), self.furniture);
426        out.insert(
427            "body".into(),
428            object([
429                ("self_ref", "#/body".into()),
430                (
431                    "children",
432                    Value::Array(self.body.into_iter().map(Child::into_value).collect()),
433                ),
434                ("content_layer", "body".into()),
435                ("name", "_root_".into()),
436                ("label", "unspecified".into()),
437            ]),
438        );
439        out.insert("groups".into(), Value::Array(self.groups));
440        out.insert(
441            "texts".into(),
442            Value::Array(
443                self.texts
444                    .into_iter()
445                    .enumerate()
446                    .map(|(i, item)| item.into_value(i))
447                    .collect(),
448            ),
449        );
450        out.insert("pictures".into(), Value::Array(self.pictures));
451        out.insert(
452            "tables".into(),
453            Value::Array(
454                self.tables
455                    .into_iter()
456                    .map(|t| match typed.as_deref_mut() {
457                        Some(store) => {
458                            let TableItem { mut item, data } = t;
459                            if let Some(slot) = item.get_mut("data") {
460                                *slot = object([(TYPED_TABLE, store.len().into())]);
461                            }
462                            store.push(data);
463                            item
464                        }
465                        None => t.into_value(),
466                    })
467                    .collect(),
468            ),
469        );
470        out.insert("key_value_items".into(), Value::Array(self.key_value_items));
471        out.insert("form_items".into(), Value::Array(Vec::new()));
472        if let Some((regions, items)) = self.fields {
473            out.insert("field_regions".into(), Value::Array(regions));
474            out.insert("field_items".into(), Value::Array(items));
475        }
476        out.insert("pages".into(), Value::Object(self.pages));
477        Value::Object(out)
478    }
479}
480
481/// [`Parts`] with `extras` — top-level keys a caller appends after
482/// `pages`, as inserting them into [`to_json`]'s `Value` would.
483struct PartsWith<'a>(&'a Parts, &'a [(&'a str, &'a Value)]);
484
485impl serde::Serialize for PartsWith<'_> {
486    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
487        use serde::ser::SerializeMap;
488        let PartsWith(parts, extras) = *self;
489        let mut m = s.serialize_map(Some(
490            13 + 2 * usize::from(parts.fields.is_some())
491                + usize::from(parts.metadata.is_some())
492                + extras.len(),
493        ))?;
494        m.serialize_entry("schema_name", "DoclingDocument")?;
495        m.serialize_entry("version", SCHEMA_VERSION)?;
496        m.serialize_entry("name", &parts.name)?;
497        m.serialize_entry("origin", &parts.origin)?;
498        if let Some(meta) = &parts.metadata {
499            m.serialize_entry("metadata", meta)?;
500        }
501        m.serialize_entry("furniture", &parts.furniture)?;
502        m.serialize_entry("body", &Body(&parts.body))?;
503        m.serialize_entry("groups", &parts.groups)?;
504        m.serialize_entry("texts", &Texts(&parts.texts))?;
505        m.serialize_entry("pictures", &parts.pictures)?;
506        m.serialize_entry("tables", &parts.tables)?;
507        m.serialize_entry("key_value_items", &parts.key_value_items)?;
508        m.serialize_entry("form_items", &[(); 0])?;
509        if let Some((regions, items)) = &parts.fields {
510            m.serialize_entry("field_regions", regions)?;
511            m.serialize_entry("field_items", items)?;
512        }
513        m.serialize_entry("pages", &parts.pages)?;
514        for (k, v) in extras {
515            m.serialize_entry(k, v)?;
516        }
517        m.end()
518    }
519}
520
521/// The body object: fixed keys around its `children`.
522struct Body<'a>(&'a [Child]);
523
524impl serde::Serialize for Body<'_> {
525    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
526        use serde::ser::SerializeMap;
527        let mut m = s.serialize_map(Some(5))?;
528        m.serialize_entry("self_ref", "#/body")?;
529        m.serialize_entry("children", self.0)?;
530        m.serialize_entry("content_layer", "body")?;
531        m.serialize_entry("name", "_root_")?;
532        m.serialize_entry("label", "unspecified")?;
533        m.end()
534    }
535}
536
537/// Write `doc`'s docling JSON to `w` — byte for byte what serializing
538/// [`to_json`]'s `Value` writes (compact, or `pretty` as
539/// `serde_json::to_writer_pretty` indents it), without building that
540/// `Value`. On a long PDF the `Value` is most of the export's memory: ~2.7 KB
541/// per text item (an owned-key map per JSON object) against a few hundred
542/// bytes here.
543pub fn write_json<W: std::io::Write>(
544    doc: &DoclingDocument,
545    w: W,
546    pretty: bool,
547) -> serde_json::Result<()> {
548    write_json_with(doc, w, pretty, &[])
549}
550
551/// [`write_json`] with `extras` appended after `pages` — byte for byte
552/// serializing [`to_json`]'s `Value` with each `value[k] = v` inserted (the
553/// keys must be new to it, as docling-serve's `confidence` is).
554pub fn write_json_with<W: std::io::Write>(
555    doc: &DoclingDocument,
556    w: W,
557    pretty: bool,
558    extras: &[(&str, &Value)],
559) -> serde_json::Result<()> {
560    use serde::Serialize;
561    let parts = build_parts(doc, false);
562    let parts = PartsWith(&parts, extras);
563    if pretty {
564        parts.serialize(&mut serde_json::Serializer::pretty(w))
565    } else {
566        parts.serialize(&mut serde_json::Serializer::new(w))
567    }
568}
569
570/// A DocumentPictureClassifier's predictions as the picture's `meta` — they
571/// land twice, exactly like docling 2.x writes them: the newer
572/// `meta.classification` field (pydantic field order: confidence, created_by,
573/// class_name) and the deprecated-but-still-emitted `classification`
574/// annotation, carried here under an `annotations` key that `add_picture`
575/// lifts onto the item.
576fn classification_meta(classes: &[crate::PictureClass]) -> Value {
577    json!({
578        "classification": {
579            "predictions": classes.iter().map(|c| json!({
580                "confidence": c.confidence as f64,
581                "created_by": "DocumentPictureClassifier",
582                "class_name": c.class_name,
583            })).collect::<Vec<_>>(),
584        },
585        "annotations": [{
586            "kind": "classification",
587            "provenance": "DocumentPictureClassifier",
588            "predicted_classes": classes.iter().map(|c| json!({
589                "class_name": c.class_name,
590                "confidence": c.confidence as f64,
591            })).collect::<Vec<_>>(),
592        }],
593    })
594}
595
596/// The picture-OCR enrichment's text (#645) as docling writes a picture
597/// description: `meta.description` — a `DescriptionMetaField`, pydantic
598/// field order `created_by` (the model, here the OCR engine) then `text` —
599/// plus the deprecated-but-still-emitted `PictureDescriptionData`
600/// annotation (`kind`, `text`, `provenance`), both exactly what
601/// `PictureDescriptionBaseModel` attaches. Carried like
602/// [`classification_meta`]'s: `annotations` is lifted onto the item.
603fn description_meta(desc: &crate::PictureDescription) -> Value {
604    json!({
605        "description": {
606            "created_by": desc.provenance,
607            "text": desc.text,
608        },
609        "annotations": [{
610            "kind": "description",
611            "text": desc.text,
612            "provenance": desc.provenance,
613        }],
614    })
615}
616
617/// A flat picture node's `meta`: the classifier's predictions and/or the
618/// OCR description, merged — `description` precedes `classification` in
619/// `PictureMeta`'s field order (it is inherited from the floating-item
620/// base), the annotations run in the order docling's enrichment pipeline
621/// appends them (classification, then description).
622fn picture_meta(
623    classes: Option<&[crate::PictureClass]>,
624    desc: Option<&crate::PictureDescription>,
625) -> Option<Value> {
626    let mut meta = Map::new();
627    let mut annotations = Vec::new();
628    if let Some(desc) = desc {
629        if desc.is_docling_meta() {
630            // docling's backend-written description: `text` alone, and no
631            // legacy annotation mirrors it (docling#5015).
632            meta.insert("description".into(), json!({ "text": desc.text }));
633        } else {
634            let mut m = description_meta(desc);
635            annotations.push(m["annotations"][0].take());
636            meta.insert("description".into(), m["description"].take());
637        }
638    }
639    if let Some(classes) = classes {
640        let mut m = classification_meta(classes);
641        annotations.insert(0, m["annotations"][0].take());
642        meta.insert("classification".into(), m["classification"].take());
643    }
644    if meta.is_empty() {
645        return None;
646    }
647    meta.insert("annotations".into(), Value::Array(annotations));
648    Some(Value::Object(meta))
649}
650
651/// One table cell of docling's `TableCell`, typed (#875): the table's cells
652/// stay these until the export writes them, rather than a `serde_json` map
653/// of eleven owned keys each — and a second copy of that map in every grid
654/// slot the cell covers. As maps a short numeric CSV cell cost ~7.5 KB of
655/// export peak; typed it is this struct plus its text.
656#[derive(Clone)]
657pub(crate) struct GridCell {
658    row_span: usize,
659    col_span: usize,
660    start_row: usize,
661    end_row: usize,
662    start_col: usize,
663    end_col: usize,
664    text: Box<str>,
665    column_header: bool,
666    row_header: bool,
667    row_section: bool,
668    /// `l t r b`, top-left origin — the backend's `f32` box widened to the
669    /// `f64` a `Value` would hold it as, so both paths print the same digits.
670    bbox: Option<[f64; 4]>,
671    /// A `RichTableCell`'s `ref` to the group holding its content — written
672    /// on `table_cells` only; the `grid` shows the plain cell.
673    cell_ref: Option<Box<Value>>,
674}
675
676impl GridCell {
677    /// docling's empty filler for a grid position no cell covers.
678    fn filler(r: usize, c: usize) -> Self {
679        GridCell {
680            row_span: 1,
681            col_span: 1,
682            start_row: r,
683            end_row: r + 1,
684            start_col: c,
685            end_col: c + 1,
686            text: "".into(),
687            column_header: false,
688            row_header: false,
689            row_section: false,
690            bbox: None,
691            cell_ref: None,
692        }
693    }
694
695    /// The cell as a `Value`: the eleven fields in pydantic order, `bbox`
696    /// appended when the cell carries one, then the rich cell's `ref`
697    /// (`with_ref`, the `table_cells` copy). Built straight into a `Map`
698    /// sized for its entries rather than through `json!`'s doubling map.
699    fn to_value(&self, with_ref: bool) -> Value {
700        let mut m = serde_json::Map::with_capacity(13);
701        m.insert("row_span".into(), self.row_span.into());
702        m.insert("col_span".into(), self.col_span.into());
703        m.insert("start_row_offset_idx".into(), self.start_row.into());
704        m.insert("end_row_offset_idx".into(), self.end_row.into());
705        m.insert("start_col_offset_idx".into(), self.start_col.into());
706        m.insert("end_col_offset_idx".into(), self.end_col.into());
707        m.insert("text".into(), Value::String(self.text.to_string()));
708        m.insert("column_header".into(), self.column_header.into());
709        m.insert("row_header".into(), self.row_header.into());
710        m.insert("row_section".into(), self.row_section.into());
711        m.insert("fillable".into(), false.into());
712        if let Some([l, t, r, b]) = self.bbox {
713            m.insert(
714                "bbox".into(),
715                object([
716                    ("l", l.into()),
717                    ("t", t.into()),
718                    ("r", r.into()),
719                    ("b", b.into()),
720                    ("coord_origin", "TOPLEFT".into()),
721                ]),
722            );
723        }
724        if let Some(r) = self.cell_ref.as_deref().filter(|_| with_ref) {
725            m.insert("ref".into(), r.clone());
726        }
727        Value::Object(m)
728    }
729}
730
731/// [`GridCell::to_value`] written in place: the same keys in the same order.
732struct CellOut<'a>(&'a GridCell, bool);
733
734impl serde::Serialize for CellOut<'_> {
735    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
736        use serde::ser::SerializeMap;
737        let CellOut(c, with_ref) = *self;
738        let cell_ref = c.cell_ref.as_deref().filter(|_| with_ref);
739        let mut m = s.serialize_map(Some(
740            11 + usize::from(c.bbox.is_some()) + usize::from(cell_ref.is_some()),
741        ))?;
742        m.serialize_entry("row_span", &c.row_span)?;
743        m.serialize_entry("col_span", &c.col_span)?;
744        m.serialize_entry("start_row_offset_idx", &c.start_row)?;
745        m.serialize_entry("end_row_offset_idx", &c.end_row)?;
746        m.serialize_entry("start_col_offset_idx", &c.start_col)?;
747        m.serialize_entry("end_col_offset_idx", &c.end_col)?;
748        m.serialize_entry("text", &c.text)?;
749        m.serialize_entry("column_header", &c.column_header)?;
750        m.serialize_entry("row_header", &c.row_header)?;
751        m.serialize_entry("row_section", &c.row_section)?;
752        m.serialize_entry("fillable", &false)?;
753        if let Some(b) = c.bbox {
754            struct Bbox([f64; 4]);
755            impl serde::Serialize for Bbox {
756                fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
757                    let mut m = s.serialize_map(Some(5))?;
758                    for (k, v) in ["l", "t", "r", "b"].iter().zip(self.0) {
759                        m.serialize_entry(k, &v)?;
760                    }
761                    m.serialize_entry("coord_origin", "TOPLEFT")?;
762                    m.end()
763                }
764            }
765            m.serialize_entry("bbox", &Bbox(b))?;
766        }
767        if let Some(r) = cell_ref {
768            m.serialize_entry("ref", r)?;
769        }
770        m.end()
771    }
772}
773
774/// What a `grid` position shows: the cell covering it (an index into
775/// [`TableData::cells`] — docling repeats the whole cell at every position
776/// it spans), docling's empty filler cell, or `null`.
777#[derive(Clone, Copy)]
778enum GridSlot {
779    Cell(usize),
780    Filler,
781    Null,
782}
783
784/// docling's `TableData` for a table: `table_cells`, `num_rows`/`num_cols`
785/// and the `grid` that repeats each cell at every position it covers —
786/// typed (#875), each cell stored once and the grid as slots pointing at
787/// it, so a table costs its cells rather than a map per cell and per slot.
788/// Shared by table items and a chart picture's `meta.tabular_chart.chart_data`.
789pub(crate) struct TableData {
790    cells: Vec<GridCell>,
791    num_rows: usize,
792    num_cols: usize,
793    /// Row-major, `num_rows × num_cols`.
794    grid: Vec<GridSlot>,
795}
796
797/// One position of docling's `TableData.grid`, as the DocLang tree
798/// serializer reads it (`doclang_tree.rs`): the covering cell's anchor,
799/// text and header roles — an empty filler where no cell covers the
800/// position, which is what docling's computed `grid` holds there.
801pub(crate) struct GridPos<'a> {
802    pub(crate) start_row: usize,
803    pub(crate) start_col: usize,
804    pub(crate) text: &'a str,
805    pub(crate) column_header: bool,
806    pub(crate) row_header: bool,
807    pub(crate) row_section: bool,
808}
809
810/// [`TableData`] of a backend-built tree's table (raw cell text).
811pub(crate) fn raw_table_data(t: &Table) -> TableData {
812    table_data_with(t, true)
813}
814
815impl TableData {
816    /// `(num_rows, num_cols)`.
817    pub(crate) fn dims(&self) -> (usize, usize) {
818        (self.num_rows, self.num_cols)
819    }
820
821    /// Whether `table_cells` holds any cell.
822    pub(crate) fn has_cells(&self) -> bool {
823        !self.cells.is_empty()
824    }
825
826    /// The grid position `(r, c)`.
827    pub(crate) fn at(&self, r: usize, c: usize) -> GridPos<'_> {
828        match self.grid[r * self.num_cols + c] {
829            GridSlot::Cell(i) => {
830                let cell = &self.cells[i];
831                GridPos {
832                    start_row: cell.start_row,
833                    start_col: cell.start_col,
834                    text: &cell.text,
835                    column_header: cell.column_header,
836                    row_header: cell.row_header,
837                    row_section: cell.row_section,
838                }
839            }
840            GridSlot::Filler | GridSlot::Null => GridPos {
841                start_row: r,
842                start_col: c,
843                text: "",
844                column_header: false,
845                row_header: false,
846                row_section: false,
847            },
848        }
849    }
850}
851
852impl TableData {
853    fn slot_cell(
854        &self,
855        slot: GridSlot,
856        r: usize,
857        c: usize,
858    ) -> Option<std::borrow::Cow<'_, GridCell>> {
859        match slot {
860            GridSlot::Cell(i) => Some(std::borrow::Cow::Borrowed(&self.cells[i])),
861            GridSlot::Filler => Some(std::borrow::Cow::Owned(GridCell::filler(r, c))),
862            GridSlot::Null => None,
863        }
864    }
865
866    fn into_value(self) -> Value {
867        let cells = Value::Array(self.cells.iter().map(|c| c.to_value(true)).collect());
868        let mut data = serde_json::Map::with_capacity(5);
869        data.insert("table_cells".into(), cells);
870        data.insert("num_rows".into(), self.num_rows.into());
871        data.insert("num_cols".into(), self.num_cols.into());
872        data.insert("orientation".into(), "rot_0".into());
873        let mut rows = Vec::with_capacity(self.num_rows);
874        for r in 0..self.num_rows {
875            let mut row = Vec::with_capacity(self.num_cols);
876            for c in 0..self.num_cols {
877                let slot = self.grid[r * self.num_cols + c];
878                row.push(match self.slot_cell(slot, r, c) {
879                    Some(cell) => cell.to_value(false),
880                    None => Value::Null,
881                });
882            }
883            rows.push(Value::Array(row));
884        }
885        data.insert("grid".into(), Value::Array(rows));
886        Value::Object(data)
887    }
888
889    /// docling's page clamp ([`clamp_boxes_to_pages`]) on every cell box —
890    /// once per cell: the grid's copies are the same cells.
891    fn clamp(&mut self, (w, h): (f64, f64)) {
892        for bbox in self.cells.iter_mut().filter_map(|c| c.bbox.as_mut()) {
893            for (v, hi) in bbox.iter_mut().zip([w, h, w, h]) {
894                // A non-finite coordinate is `null` in the JSON, which the
895                // clamp leaves alone.
896                if v.is_finite() {
897                    *v = round2(v.clamp(0.0, hi.max(0.0)));
898                }
899            }
900        }
901    }
902}
903
904impl serde::Serialize for TableData {
905    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
906        use serde::ser::{SerializeMap, SerializeSeq};
907        struct Cells<'a>(&'a [GridCell]);
908        impl serde::Serialize for Cells<'_> {
909            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
910                let mut seq = s.serialize_seq(Some(self.0.len()))?;
911                for c in self.0 {
912                    seq.serialize_element(&CellOut(c, true))?;
913                }
914                seq.end()
915            }
916        }
917        struct Row<'a>(&'a TableData, usize);
918        impl serde::Serialize for Row<'_> {
919            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
920                let Row(t, r) = *self;
921                let mut seq = s.serialize_seq(Some(t.num_cols))?;
922                for c in 0..t.num_cols {
923                    match t.slot_cell(t.grid[r * t.num_cols + c], r, c) {
924                        Some(cell) => seq.serialize_element(&CellOut(&cell, false))?,
925                        None => seq.serialize_element(&())?,
926                    }
927                }
928                seq.end()
929            }
930        }
931        struct Grid<'a>(&'a TableData);
932        impl serde::Serialize for Grid<'_> {
933            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
934                let mut seq = s.serialize_seq(Some(self.0.num_rows))?;
935                for r in 0..self.0.num_rows {
936                    seq.serialize_element(&Row(self.0, r))?;
937                }
938                seq.end()
939            }
940        }
941        let mut m = s.serialize_map(Some(5))?;
942        m.serialize_entry("table_cells", &Cells(&self.cells))?;
943        m.serialize_entry("num_rows", &self.num_rows)?;
944        m.serialize_entry("num_cols", &self.num_cols)?;
945        m.serialize_entry("orientation", "rot_0")?;
946        m.serialize_entry("grid", &Grid(self))?;
947        m.end()
948    }
949}
950
951/// A table cell as the `Value`-walking serializers read it — from a typed
952/// [`GridCell`] or from a `table_cells` entry of docling JSON (a document
953/// Python docling wrote, read by [`crate::pandoc::from_docling_json`]): a
954/// missing field reads as `0` / `""` / `false`, like the `Value` getters
955/// those serializers used.
956pub(crate) struct CellView<'a> {
957    pub start_row: usize,
958    pub end_row: usize,
959    pub start_col: usize,
960    pub end_col: usize,
961    pub row_span: usize,
962    pub col_span: usize,
963    pub text: &'a str,
964    pub column_header: bool,
965    pub row_header: bool,
966    pub row_section: bool,
967    /// A rich cell's `ref.$ref`.
968    pub cell_ref: Option<&'a str>,
969}
970
971/// A table's `data` as [`CellView`]s: `num_rows`, `num_cols` and the
972/// `table_cells` (the derived `grid` is not read).
973pub(crate) struct TableView<'a> {
974    pub num_rows: usize,
975    pub num_cols: usize,
976    pub cells: Vec<CellView<'a>>,
977}
978
979/// The [`TableView`] of a table item's `data`: the typed table it names
980/// ([`TYPED_TABLE`], an index into `typed`), or the JSON's own fields.
981pub(crate) fn table_view<'a>(data: &'a Value, typed: &'a [TableData]) -> TableView<'a> {
982    let ref_of = |r: Option<&'a Value>| r.and_then(|r| r.get("$ref")).and_then(Value::as_str);
983    if let Some(t) = data
984        .get(TYPED_TABLE)
985        .and_then(Value::as_u64)
986        .and_then(|i| typed.get(i as usize))
987    {
988        return TableView {
989            num_rows: t.num_rows,
990            num_cols: t.num_cols,
991            cells: t
992                .cells
993                .iter()
994                .map(|c| CellView {
995                    start_row: c.start_row,
996                    end_row: c.end_row,
997                    start_col: c.start_col,
998                    end_col: c.end_col,
999                    row_span: c.row_span,
1000                    col_span: c.col_span,
1001                    text: &c.text,
1002                    column_header: c.column_header,
1003                    row_header: c.row_header,
1004                    row_section: c.row_section,
1005                    cell_ref: ref_of(c.cell_ref.as_deref()),
1006                })
1007                .collect(),
1008        };
1009    }
1010    let num = |v: &Value, k: &str| v.get(k).and_then(Value::as_u64).unwrap_or(0) as usize;
1011    let flag = |v: &Value, k: &str| v.get(k).and_then(Value::as_bool).unwrap_or(false);
1012    TableView {
1013        num_rows: num(data, "num_rows"),
1014        num_cols: num(data, "num_cols"),
1015        cells: data
1016            .get("table_cells")
1017            .and_then(Value::as_array)
1018            .into_iter()
1019            .flatten()
1020            .map(|c| CellView {
1021                start_row: num(c, "start_row_offset_idx"),
1022                end_row: num(c, "end_row_offset_idx"),
1023                start_col: num(c, "start_col_offset_idx"),
1024                end_col: num(c, "end_col_offset_idx"),
1025                row_span: num(c, "row_span"),
1026                col_span: num(c, "col_span"),
1027                text: c.get("text").and_then(Value::as_str).unwrap_or(""),
1028                column_header: flag(c, "column_header"),
1029                row_header: flag(c, "row_header"),
1030                row_section: flag(c, "row_section"),
1031                cell_ref: ref_of(c.get("ref")),
1032            })
1033            .collect(),
1034    }
1035}
1036
1037/// A `tables` entry: the item's JSON with a `null` stand-in under `data`
1038/// — so every edit of the item (children, content layer, comment
1039/// back-refs) works on the `Value` as before and keeps its key order —
1040/// and the typed [`TableData`] that takes the stand-in's place when the
1041/// item is written or turned into a `Value`.
1042struct TableItem {
1043    item: Value,
1044    data: TableData,
1045}
1046
1047impl TableItem {
1048    fn into_value(self) -> Value {
1049        let TableItem { mut item, data } = self;
1050        if let Some(slot) = item.get_mut("data") {
1051            *slot = data.into_value();
1052        }
1053        item
1054    }
1055}
1056
1057impl serde::Serialize for TableItem {
1058    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1059        use serde::ser::SerializeMap;
1060        let Value::Object(obj) = &self.item else {
1061            return self.item.serialize(s);
1062        };
1063        let mut m = s.serialize_map(Some(obj.len()))?;
1064        for (k, v) in obj {
1065            if k == "data" {
1066                m.serialize_entry(k, &self.data)?;
1067            } else {
1068                m.serialize_entry(k, v)?;
1069            }
1070        }
1071        m.end()
1072    }
1073}
1074
1075/// `TableData` from a table whose cell text is Markdown-flavoured (the flat
1076/// nodes: escapes to undo, GFM hard-break markers to strip).
1077fn table_data(t: &Table) -> TableData {
1078    table_data_with(t, false)
1079}
1080
1081/// `TableData`; with `raw` the cell text is docling's own raw cell text (a
1082/// backend-built tree) and is written verbatim.
1083fn table_data_with(t: &Table, raw: bool) -> TableData {
1084    let cell_text = |s: &str| -> Box<str> {
1085        if raw {
1086            s.into()
1087        } else {
1088            unescape_text(&crate::markdown::strip_hard_breaks(s)).into()
1089        }
1090    };
1091    let widen = |b: [f32; 4]| b.map(f64::from);
1092    let mut num_rows = t.rows.len();
1093    let mut num_cols = t.rows.iter().map(Vec::len).max().unwrap_or(0);
1094    // Rectangular `rows` are the grid, and cells reaching past it are
1095    // clipped — docling's own `TableData.grid` on a USPTO table whose
1096    // replicated cells outrun `num_cols`, or an HTML rowspan past the last
1097    // row. Ragged rows are no grid at all: a backend that compacts them
1098    // (xlsx `skip_empty_cells`, #271) keeps the true offsets in first-class
1099    // cells, so there the grid spans every cell's extent and the omitted
1100    // positions come back as docling's empty filler cells.
1101    let ragged = t.rows.iter().any(|r| r.len() != num_cols);
1102    if let Some(first_class) = t.cells.as_ref().filter(|c| ragged && !c.is_empty()) {
1103        let (mut rows, mut cols) = (num_rows, num_cols);
1104        for c in first_class {
1105            rows = rows.max(c.start_row.saturating_add(c.row_span));
1106            cols = cols.max(c.start_col.saturating_add(c.col_span));
1107        }
1108        // A cell whose declared extent reaches past the cell budget (#861)
1109        // does not size the slot map; the grid stays the rows' own and the
1110        // cell is clipped to it, like any other cell reaching past the grid.
1111        if rows.saturating_mul(cols) <= crate::limits::max_table_cells() {
1112            num_rows = rows;
1113            num_cols = cols;
1114        }
1115    }
1116    let mut cells = Vec::new();
1117    // Grid slot → the cell covering it, row-major — no per-slot copy and
1118    // nothing hashed.
1119    let mut grid: Vec<GridSlot>;
1120    if let Some(first_class) = t.cells.as_ref().filter(|c| !c.is_empty()) {
1121        // A position no cell covers is docling's empty filler cell.
1122        grid = vec![GridSlot::Filler; num_rows * num_cols];
1123        cells.reserve(first_class.len());
1124        for c in first_class {
1125            let idx = cells.len();
1126            cells.push(GridCell {
1127                row_span: c.row_span,
1128                col_span: c.col_span,
1129                start_row: c.start_row,
1130                end_row: c.start_row + c.row_span,
1131                start_col: c.start_col,
1132                end_col: c.start_col + c.col_span,
1133                text: cell_text(&c.text),
1134                column_header: c.column_header,
1135                row_header: c.row_header,
1136                row_section: c.row_section,
1137                bbox: c.bbox.map(widen),
1138                cell_ref: None,
1139            });
1140            for r in c.start_row..(c.start_row + c.row_span).min(num_rows) {
1141                for k in c.start_col..(c.start_col + c.col_span).min(num_cols) {
1142                    grid[r * num_cols + k] = GridSlot::Cell(idx);
1143                }
1144            }
1145        }
1146    } else {
1147        grid = vec![GridSlot::Null; num_rows * num_cols];
1148        let s = t.structure.as_ref();
1149        let flag = |grid: Option<&Vec<Vec<bool>>>, r: usize, c: usize| -> bool {
1150            grid.and_then(|g| g.get(r))
1151                .and_then(|row| row.get(c))
1152                .copied()
1153                .unwrap_or(false)
1154        };
1155        let anchor_of = |r: usize, c: usize| -> (usize, usize) {
1156            let (mut r0, mut c0) = (r, c);
1157            while c0 > 0 && flag(s.map(|s| &s.col_continuation), r, c0) {
1158                c0 -= 1;
1159            }
1160            while r0 > 0 && flag(s.map(|s| &s.row_continuation), r0, c0) {
1161                r0 -= 1;
1162            }
1163            (r0, c0)
1164        };
1165        // Each slot's anchor, computed once; the anchor's extent is the
1166        // farthest slot that resolves to it.
1167        let anchors: Vec<(usize, usize)> = (0..num_rows)
1168            .flat_map(|r| (0..num_cols).map(move |c| (r, c)))
1169            .map(|(r, c)| anchor_of(r, c))
1170            .collect();
1171        let mut extent: Vec<(usize, usize)> = (0..num_rows)
1172            .flat_map(|r| (0..num_cols).map(move |c| (r, c)))
1173            .collect();
1174        for (i, &(ar, ac)) in anchors.iter().enumerate() {
1175            let (r, c) = (i / num_cols.max(1), i % num_cols.max(1));
1176            let e = &mut extent[ar * num_cols + ac];
1177            e.0 = e.0.max(r);
1178            e.1 = e.1.max(c);
1179        }
1180        // An anchor's cell, by slot, once it is created (anchors precede
1181        // the slots they cover in row-major order).
1182        let mut anchor_cell: Vec<Option<usize>> = vec![None; num_rows * num_cols];
1183        for (r, row) in t.rows.iter().enumerate() {
1184            for c in 0..num_cols {
1185                let (ar, ac) = anchors[r * num_cols + c];
1186                if (ar, ac) == (r, c) {
1187                    let (er, ec) = extent[r * num_cols + c];
1188                    let text = row.get(c).map(|s| cell_text(s)).unwrap_or_default();
1189                    let column_header = match s.filter(|s| !s.col_header.is_empty()) {
1190                        Some(s) => flag(Some(&s.col_header), r, c),
1191                        None => r == 0,
1192                    };
1193                    anchor_cell[r * num_cols + c] = Some(cells.len());
1194                    cells.push(GridCell {
1195                        row_span: er - r + 1,
1196                        col_span: ec - c + 1,
1197                        start_row: r,
1198                        end_row: er + 1,
1199                        start_col: c,
1200                        end_col: ec + 1,
1201                        text,
1202                        column_header,
1203                        row_header: flag(s.map(|s| &s.row_header), r, c),
1204                        row_section: false,
1205                        bbox: None,
1206                        cell_ref: None,
1207                    });
1208                }
1209                if let Some(i) = anchor_cell[ar * num_cols + ac] {
1210                    grid[r * num_cols + c] = GridSlot::Cell(i);
1211                }
1212            }
1213        }
1214    }
1215    TableData {
1216        cells,
1217        num_rows,
1218        num_cols,
1219        grid,
1220    }
1221}
1222
1223/// An item's `prov` array as the builder holds it: empty, or docling's one
1224/// `ProvenanceItem` — kept typed so a text item need not be a `Value` until
1225/// it is written (see [`Item`]).
1226#[derive(Clone, Copy)]
1227enum ProvSlot {
1228    None,
1229    One {
1230        page_no: usize,
1231        /// `l t r b`, already rounded to 2 decimals.
1232        bbox: [f64; 4],
1233        bottom_left: bool,
1234        charspan: [usize; 2],
1235    },
1236}
1237
1238impl ProvSlot {
1239    fn origin(bottom_left: bool) -> &'static str {
1240        if bottom_left {
1241            "BOTTOMLEFT"
1242        } else {
1243            "TOPLEFT"
1244        }
1245    }
1246
1247    /// The `prov` array: `page_no`, `bbox` (`l t r b` + `coord_origin`) and
1248    /// `charspan`, in pydantic field order.
1249    fn to_value(self) -> Value {
1250        let ProvSlot::One {
1251            page_no,
1252            bbox: [l, t, r, b],
1253            bottom_left,
1254            charspan,
1255        } = self
1256        else {
1257            return Value::Array(Vec::new());
1258        };
1259        let bbox = object([
1260            ("l", l.into()),
1261            ("t", t.into()),
1262            ("r", r.into()),
1263            ("b", b.into()),
1264            ("coord_origin", Self::origin(bottom_left).into()),
1265        ]);
1266        let span = Value::Array(vec![charspan[0].into(), charspan[1].into()]);
1267        Value::Array(vec![object([
1268            ("page_no", page_no.into()),
1269            ("bbox", bbox),
1270            ("charspan", span),
1271        ])])
1272    }
1273
1274    /// docling's page clamp ([`clamp_boxes_to_pages`]) on the typed box.
1275    fn clamp(&mut self, sizes: &PageSizes) {
1276        if let ProvSlot::One { page_no, bbox, .. } = self {
1277            if let Some(&(w, h)) = sizes.get(page_no) {
1278                for (v, hi) in bbox.iter_mut().zip([w, h, w, h]) {
1279                    // A non-finite coordinate is `null` in the JSON, which the
1280                    // clamp leaves alone.
1281                    if v.is_finite() {
1282                        *v = round2(v.clamp(0.0, hi.max(0.0)));
1283                    }
1284                }
1285            }
1286        }
1287    }
1288}
1289
1290impl serde::Serialize for ProvSlot {
1291    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1292        use serde::ser::{SerializeMap, SerializeSeq};
1293        let ProvSlot::One {
1294            page_no,
1295            bbox: [l, t, r, b],
1296            bottom_left,
1297            charspan,
1298        } = *self
1299        else {
1300            return s.serialize_seq(Some(0))?.end();
1301        };
1302        struct Bbox([f64; 4], &'static str);
1303        impl serde::Serialize for Bbox {
1304            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1305                let mut m = s.serialize_map(Some(5))?;
1306                for (k, v) in ["l", "t", "r", "b"].iter().zip(self.0) {
1307                    m.serialize_entry(k, &v)?;
1308                }
1309                m.serialize_entry("coord_origin", self.1)?;
1310                m.end()
1311            }
1312        }
1313        struct Entry(usize, Bbox, [usize; 2]);
1314        impl serde::Serialize for Entry {
1315            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1316                let mut m = s.serialize_map(Some(3))?;
1317                m.serialize_entry("page_no", &self.0)?;
1318                m.serialize_entry("bbox", &self.1)?;
1319                m.serialize_entry("charspan", &self.2)?;
1320                m.end()
1321            }
1322        }
1323        let mut seq = s.serialize_seq(Some(1))?;
1324        seq.serialize_element(&Entry(
1325            page_no,
1326            Bbox([l, t, r, b], Self::origin(bottom_left)),
1327            charspan,
1328        ))?;
1329        seq.end()
1330    }
1331}
1332
1333/// The plain text item — docling's `TextItem` with no children, on the body
1334/// layer, `orig` equal to `text` and no extra fields: what a PDF's text layer
1335/// is made of, millions of times over on a long document. As a `Value` each
1336/// cost ~2.7 KB (a map of owned keys per object); typed it is ~150 bytes, and
1337/// it serializes to exactly the same JSON. Its `self_ref` is its position
1338/// (`#/texts/{i}`), so it is not stored.
1339struct TextItem {
1340    parent: Box<str>,
1341    label: Box<str>,
1342    prov: ProvSlot,
1343    text: Box<str>,
1344}
1345
1346impl TextItem {
1347    fn into_value(self, index: usize) -> Value {
1348        let text = String::from(self.text);
1349        object([
1350            ("self_ref", Value::String(format!("#/texts/{index}"))),
1351            ("parent", ref_value(self.parent)),
1352            ("children", Value::Array(Vec::new())),
1353            ("content_layer", "body".into()),
1354            ("label", Value::String(self.label.into())),
1355            ("prov", self.prov.to_value()),
1356            ("orig", Value::String(text.clone())),
1357            ("text", Value::String(text)),
1358        ])
1359    }
1360}
1361
1362/// A `texts` entry: the compact [`TextItem`] until something needs to edit
1363/// it as JSON (a nested list's children, a content-layer move, a comment
1364/// back-ref), then the `Value` it stands for. Both boxed: a slot is 16 bytes.
1365enum Item {
1366    Text(Box<TextItem>),
1367    Json(Box<Value>),
1368}
1369
1370impl Item {
1371    fn json(v: Value) -> Self {
1372        Item::Json(Box::new(v))
1373    }
1374
1375    /// The item at `texts[index]` as an editable `Value`, converting a
1376    /// [`TextItem`] in place.
1377    fn value_mut(&mut self, index: usize) -> &mut Value {
1378        if let Item::Text(t) = self {
1379            let t = std::mem::replace(
1380                t,
1381                Box::new(TextItem {
1382                    parent: "".into(),
1383                    label: "".into(),
1384                    prov: ProvSlot::None,
1385                    text: "".into(),
1386                }),
1387            );
1388            *self = Item::json(t.into_value(index));
1389        }
1390        match self {
1391            Item::Json(v) => v,
1392            Item::Text(_) => unreachable!("converted above"),
1393        }
1394    }
1395
1396    fn into_value(self, index: usize) -> Value {
1397        match self {
1398            Item::Text(t) => t.into_value(index),
1399            Item::Json(v) => *v,
1400        }
1401    }
1402}
1403
1404/// The `texts` array, serialized in place.
1405struct Texts<'a>(&'a [Item]);
1406
1407impl serde::Serialize for Texts<'_> {
1408    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1409        use serde::ser::{SerializeMap, SerializeSeq};
1410        struct Text<'a>(usize, &'a TextItem);
1411        impl serde::Serialize for Text<'_> {
1412            fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1413                let Text(i, t) = *self;
1414                let mut m = s.serialize_map(Some(8))?;
1415                m.serialize_entry("self_ref", &format_args!("#/texts/{i}"))?;
1416                m.serialize_entry("parent", &Child::Ref(t.parent.clone()))?;
1417                m.serialize_entry("children", &[(); 0])?;
1418                m.serialize_entry("content_layer", "body")?;
1419                m.serialize_entry("label", &t.label)?;
1420                m.serialize_entry("prov", &t.prov)?;
1421                m.serialize_entry("orig", &t.text)?;
1422                m.serialize_entry("text", &t.text)?;
1423                m.end()
1424            }
1425        }
1426        let mut seq = s.serialize_seq(Some(self.0.len()))?;
1427        for (i, item) in self.0.iter().enumerate() {
1428            match item {
1429                Item::Text(t) => seq.serialize_element(&Text(i, t))?,
1430                Item::Json(v) => seq.serialize_element(v)?,
1431            }
1432        }
1433        seq.end()
1434    }
1435}
1436
1437/// An entry of a `children` list as the walk collects it: a `{"$ref": …}`
1438/// held as its target string (a body of millions of refs costs ~40 bytes
1439/// each, not a ~270-byte `Value` map), or any other JSON.
1440enum Child {
1441    Ref(Box<str>),
1442    Json(Box<Value>),
1443}
1444
1445impl Child {
1446    fn json(v: Value) -> Self {
1447        Child::Json(Box::new(v))
1448    }
1449
1450    fn into_value(self) -> Value {
1451        match self {
1452            Child::Ref(r) => ref_value(r),
1453            Child::Json(v) => *v,
1454        }
1455    }
1456}
1457
1458impl serde::Serialize for Child {
1459    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
1460        use serde::ser::SerializeMap;
1461        match self {
1462            Child::Ref(r) => {
1463                let mut m = s.serialize_map(Some(1))?;
1464                m.serialize_entry("$ref", r)?;
1465                m.end()
1466            }
1467            Child::Json(v) => v.serialize(s),
1468        }
1469    }
1470}
1471
1472fn round2(v: f64) -> f64 {
1473    (v * 100.0).round() / 100.0
1474}
1475
1476#[derive(Default)]
1477struct Builder {
1478    /// Write the internal `_notes` / `_note_body` keys of tree text items
1479    /// ([`crate::tree::TreeItem::notes`]) — for the Pandoc writer only
1480    /// ([`to_json_with_notes`]); docling's JSON has no such fields.
1481    note_keys: bool,
1482    texts: Vec<Item>,
1483    groups: Vec<Value>,
1484    tables: Vec<TableItem>,
1485    pictures: Vec<Value>,
1486    field_regions: Vec<Value>,
1487    field_items: Vec<Value>,
1488    key_value_items: Vec<Value>,
1489    /// Pages seen so far (`page_no`, width, height in points) — from the
1490    /// [`Node::PageInfo`] markers the PDF paths emit; empty for every other
1491    /// backend, which keeps their JSON byte-identical (`"pages": {}`, no prov).
1492    pages: Vec<(usize, f64, f64)>,
1493    /// The page the walk is currently on (0 before the first marker).
1494    cur_page: usize,
1495    cur_w: f64,
1496    cur_h: f64,
1497    /// The enclosing [`Node::Located`] wrapper's 0–511 grid box, waiting to be
1498    /// consumed as the next item's provenance.
1499    pending_loc: Option<[u16; 4]>,
1500    /// The enclosing [`Node::Prov`] wrapper's (or the tree item's) exact
1501    /// provenance, which takes precedence over the grid box.
1502    pending_exact: Option<ExactProv>,
1503    /// The enclosing [`Node::Track`]'s timing, for the text item it wraps
1504    /// (#614).
1505    pending_track: Option<(crate::tree::TreeTrack, String)>,
1506    /// [`Node::Anchor`]s met since the last text item (#852): that item's
1507    /// `_anchors` on the Pandoc path.
1508    pending_anchors: Vec<String>,
1509    /// `$ref`s an item wants placed in its parent's `children` *before* its
1510    /// own — a chart's caption item, which docling's office backends add to
1511    /// the container ahead of the picture that references it.
1512    pending_siblings: Vec<Value>,
1513    /// `$ref`s an item wants placed in its parent's `children` right *after*
1514    /// its own — an HTML `<figure>`-wrapped table's caption
1515    /// ([`CaptionParent::ContainerAfter`]).
1516    pending_after: Vec<Value>,
1517    /// Caption `$ref`s that hang off `#/body` while their item sits deeper
1518    /// ([`CaptionParent::Body`]): docling appends them to the body's children
1519    /// as they are created, so they follow the top-level item being walked.
1520    pending_body: Vec<Value>,
1521    /// What each [`Node::CommentSection`] is referenced by, in document order —
1522    /// its group `$ref`, or its note text's when the section says so. The index
1523    /// is what a [`Node::Commented`] annotation carries.
1524    comment_groups: Vec<String>,
1525    /// The last `comment_section` group emitted, as `(name, self_ref)`: a
1526    /// following section of the same name adds its note to that group rather
1527    /// than opening another — one `comment-{sheet}-{cell}` group holds every
1528    /// message of a threaded comment (docling#4353).
1529    last_comment_section: Option<(String, String)>,
1530    /// Annotated items awaiting their refs: comments are usually emitted
1531    /// *after* the body they annotate (docx appends them), so the link is
1532    /// patched in once the whole document has been walked.
1533    pending_comments: Vec<(String, Vec<usize>)>,
1534    /// The items the script-position stage marked (#1005), with their runs
1535    /// and the label and link the runs take: docling adds the inline groups
1536    /// once the document is assembled, so their groups and run items come
1537    /// after every other item.
1538    pending_scripts: Vec<(String, Vec<crate::ScriptRun>, String)>,
1539}
1540
1541impl Builder {
1542    /// Consume the pending location (if any) into a docling `prov` array: the
1543    /// 0-511 grid denormalized against the current page into BOTTOMLEFT
1544    /// points, rounded to 2 decimals like docling's own export. `char_len` is
1545    /// the item's text length in characters (0 for tables and pictures, whose
1546    /// charspan docling emits as `[0, 0]`).
1547    fn take_prov(&mut self, char_len: usize) -> Value {
1548        self.take_prov_slot(char_len).to_value()
1549    }
1550
1551    /// [`Self::take_prov`], typed.
1552    fn take_prov_slot(&mut self, char_len: usize) -> ProvSlot {
1553        let prov = self.prov_slot(char_len, false);
1554        self.pending_exact = None;
1555        self.pending_loc = None;
1556        prov
1557    }
1558
1559    /// The pending provenance without consuming it. An exact [`Node::Prov`]
1560    /// box wins over the grid; its own `charspan` is used unless
1561    /// `span_over_text` asks for `[0, char_len]` (a chart caption's span
1562    /// covers the caption text where the chart's is `[0, 0]`).
1563    fn prov_slot(&self, char_len: usize, span_over_text: bool) -> ProvSlot {
1564        let r2 = round2;
1565        if let Some(ExactProv {
1566            page_no,
1567            bbox: [l, t, r, b],
1568            bottom_left,
1569            charspan,
1570        }) = self.pending_exact
1571        {
1572            let charspan = if span_over_text {
1573                [0, char_len]
1574            } else {
1575                charspan
1576            };
1577            return ProvSlot::One {
1578                page_no,
1579                bbox: [r2(l), r2(t), r2(r), r2(b)],
1580                bottom_left,
1581                charspan,
1582            };
1583        }
1584        let Some([x0, y0, x1, y1]) = self.pending_loc else {
1585            return ProvSlot::None;
1586        };
1587        // An all-zero grid box is the sentinel for "this item has no geometry"
1588        // (a slide's speaker notes, say). docling writes a zero bbox for it,
1589        // not a box spanning the page, which is what denormalizing would give.
1590        if [x0, y0, x1, y1] == [0, 0, 0, 0] {
1591            return ProvSlot::One {
1592                page_no: self.cur_page,
1593                bbox: [0.0; 4],
1594                bottom_left: true,
1595                charspan: [0, char_len],
1596            };
1597        }
1598        ProvSlot::One {
1599            page_no: self.cur_page,
1600            bbox: [
1601                r2(x0 as f64 * self.cur_w / 512.0),
1602                r2(self.cur_h - y0 as f64 * self.cur_h / 512.0),
1603                r2(x1 as f64 * self.cur_w / 512.0),
1604                r2(self.cur_h - y1 as f64 * self.cur_h / 512.0),
1605            ],
1606            bottom_left: true,
1607            charspan: [0, char_len],
1608        }
1609    }
1610
1611    /// Adopt a node's own location field (tables, formulas, list items carry
1612    /// one instead of a [`Node::Located`] wrapper) when no wrapper is pending.
1613    fn adopt_loc(&mut self, loc: Option<[u16; 4]>) {
1614        if self.pending_loc.is_none() && self.cur_page > 0 {
1615            self.pending_loc = loc;
1616        }
1617    }
1618
1619    /// Give each item the script-position stage marked (#1005) its inline
1620    /// group, after the walk as docling's post-assembly stage does: the group
1621    /// is the item's first child (`insert_inline_group(…, after=False)` /
1622    /// `add_inline_group`), and each run is a text item with the item's label,
1623    /// its own `prov` (the box of its characters, per page) and
1624    /// `formatting.script` when it is raised or lowered. A linked footnote's
1625    /// link rides on its baseline runs only, so a marker stays out of it.
1626    fn link_script_runs(&mut self) {
1627        for (item, runs, label) in std::mem::take(&mut self.pending_scripts) {
1628            let group = format!("#/groups/{}", self.groups.len());
1629            let layer = self
1630                .item_mut(&item)
1631                .and_then(|v| v.get("content_layer").cloned())
1632                .unwrap_or_else(|| json!("body"));
1633            let mut children = Vec::with_capacity(runs.len());
1634            for run in &runs {
1635                let self_ref = format!("#/texts/{}", self.texts.len());
1636                children.push(ref_value(&self_ref));
1637                let prov: Vec<Value> = run
1638                    .prov
1639                    .iter()
1640                    .map(|p| {
1641                        let [l, t, r, b] = p.bbox.map(|v| round2(f64::from(v)));
1642                        json!({
1643                            "page_no": p.page_no,
1644                            "bbox": {"l": l, "t": t, "r": r, "b": b, "coord_origin": "BOTTOMLEFT"},
1645                            "charspan": p.charspan,
1646                        })
1647                    })
1648                    .collect();
1649                let mut m = serde_json::Map::new();
1650                m.insert("self_ref".into(), json!(self_ref));
1651                m.insert("parent".into(), ref_value(&group));
1652                m.insert("children".into(), json!([]));
1653                m.insert("content_layer".into(), layer.clone());
1654                m.insert("label".into(), json!(label));
1655                m.insert("prov".into(), Value::Array(prov));
1656                m.insert("orig".into(), json!(run.text));
1657                m.insert("text".into(), json!(run.text));
1658                if run.script != crate::Script::Baseline {
1659                    m.insert(
1660                        "formatting".into(),
1661                        formatting_json(&crate::tree::Formatting {
1662                            script: run.script,
1663                            ..Default::default()
1664                        }),
1665                    );
1666                }
1667                if let Some(url) = &run.hyperlink {
1668                    m.insert("hyperlink".into(), json!(url));
1669                }
1670                self.texts.push(Item::json(Value::Object(m)));
1671            }
1672            self.groups.push(json!({
1673                "self_ref": group,
1674                "parent": ref_value(&item),
1675                "children": children,
1676                "content_layer": layer,
1677                "name": "group",
1678                "label": "inline",
1679            }));
1680            if let Some(obj) = self.item_mut(&item).and_then(Value::as_object_mut) {
1681                let mut kids = vec![ref_value(&group)];
1682                if let Some(Value::Array(old)) = obj.get("children") {
1683                    kids.extend(old.iter().cloned());
1684                }
1685                obj.insert("children".into(), Value::Array(kids));
1686            }
1687        }
1688    }
1689
1690    /// Resolve the [`Node::Commented`] annotations collected during the walk
1691    /// into docling's `comments: [{"$ref": "#/groups/N"}]` key on the annotated
1692    /// item. It has to run after the whole walk: docx appends its comment
1693    /// bodies, so the groups they live in are usually allocated *later* than
1694    /// the paragraphs pointing at them. docling emits the key between `prov`
1695    /// and `orig`, so insert it in place rather than appending (serde_json runs
1696    /// with `preserve_order`, i.e. key order is output order).
1697    fn link_comments(&mut self) {
1698        let refs: Vec<(String, Vec<Value>)> = std::mem::take(&mut self.pending_comments)
1699            .into_iter()
1700            .map(|(item, comments)| {
1701                let refs = comments
1702                    .iter()
1703                    .filter_map(|i| self.comment_groups.get(*i))
1704                    .map(ref_value)
1705                    .collect();
1706                (item, refs)
1707            })
1708            .collect();
1709        for (item, comment_refs) in refs {
1710            if comment_refs.is_empty() {
1711                continue;
1712            }
1713            let Some(target) = self.item_mut(&item) else {
1714                continue;
1715            };
1716            let Some(obj) = target.as_object_mut() else {
1717                continue;
1718            };
1719            let tail: Vec<(String, Value)> = obj
1720                .iter()
1721                .skip_while(|(k, _)| k.as_str() != "prov")
1722                .skip(1)
1723                .map(|(k, v)| (k.clone(), v.clone()))
1724                .collect();
1725            for (k, _) in &tail {
1726                // Without `preserve-order` the map is sorted and the position
1727                // is moot (#970); `remove` is the only removal it has.
1728                #[cfg(feature = "preserve-order")]
1729                obj.shift_remove(k);
1730                #[cfg(not(feature = "preserve-order"))]
1731                obj.remove(k);
1732            }
1733            obj.insert("comments".into(), Value::Array(comment_refs));
1734            for (k, v) in tail {
1735                obj.insert(k, v);
1736            }
1737        }
1738    }
1739
1740    /// The stored JSON object a `#/texts/N`-style self-ref points at.
1741    fn item_mut(&mut self, self_ref: &str) -> Option<&mut Value> {
1742        let idx = ref_index(self_ref)?;
1743        if self_ref.starts_with("#/texts/") {
1744            return self.texts.get_mut(idx).map(|item| item.value_mut(idx));
1745        }
1746        if self_ref.starts_with("#/tables/") {
1747            return self.tables.get_mut(idx).map(|t| &mut t.item);
1748        }
1749        let bucket = if self_ref.starts_with("#/pictures/") {
1750            &mut self.pictures
1751        } else if self_ref.starts_with("#/groups/") {
1752            &mut self.groups
1753        } else {
1754            return None;
1755        };
1756        bucket.get_mut(idx)
1757    }
1758
1759    /// Serialize a backend-built [`ItemTree`](crate::tree::ItemTree): every
1760    /// item in creation order into its bucket, with the parent / children /
1761    /// layer the tree recorded. Returns the body's `children` refs.
1762    fn write_tree(&mut self, tree: &crate::tree::ItemTree) -> Vec<Value> {
1763        use crate::tree::TreeKind;
1764        // Every item's `self_ref` first: children may be listed before they
1765        // are written (a rich cell's group is created after its content).
1766        let mut refs: Vec<String> = Vec::with_capacity(tree.items.len());
1767        let (mut nt, mut ng, mut ntb, mut np, mut nf, mut nk) = (0, 0, 0, 0, 0, 0);
1768        for item in &tree.items {
1769            if item.deleted {
1770                refs.push(String::new());
1771                continue;
1772            }
1773            let r = match &item.kind {
1774                TreeKind::Text { .. } | TreeKind::Code { .. } => {
1775                    nt += 1;
1776                    format!("#/texts/{}", nt - 1)
1777                }
1778                TreeKind::Group { .. } => {
1779                    ng += 1;
1780                    format!("#/groups/{}", ng - 1)
1781                }
1782                TreeKind::Table { .. } => {
1783                    ntb += 1;
1784                    format!("#/tables/{}", ntb - 1)
1785                }
1786                TreeKind::Picture { .. } => {
1787                    np += 1;
1788                    format!("#/pictures/{}", np - 1)
1789                }
1790                TreeKind::FieldRegion { items } => {
1791                    // A region's marker / key / value parts are text items
1792                    // too, numbered where docling creates them.
1793                    nt += items
1794                        .iter()
1795                        .map(|i| {
1796                            [&i.marker, &i.key, &i.value]
1797                                .iter()
1798                                .filter(|p| p.is_some())
1799                                .count()
1800                        })
1801                        .sum::<usize>();
1802                    nf += 1;
1803                    format!("#/field_regions/{}", nf - 1)
1804                }
1805                TreeKind::KeyValueGraph { .. } => {
1806                    nk += 1;
1807                    format!("#/key_value_items/{}", nk - 1)
1808                }
1809            };
1810            refs.push(r);
1811        }
1812        let ref_of = |id: usize| ref_value(refs[id].as_str());
1813        for (id, item) in tree.items.iter().enumerate() {
1814            if item.deleted {
1815                continue;
1816            }
1817            let parent = item.parent.map_or("#/body", |p| refs[p].as_str());
1818            // A deleted item has no ref; one still listed as a child (a stale
1819            // backend link) is left out rather than written as `"$ref": ""`,
1820            // which docling-core rejects on load (#527).
1821            let children: Vec<Value> = item
1822                .children
1823                .iter()
1824                .filter(|&&c| !tree.items[c].deleted)
1825                .map(|&c| ref_of(c))
1826                .collect();
1827            let layer = item.layer.map_or("body", |l| l.value());
1828            // The item's own provenance, consumed by the writer below
1829            // (`take_prov`); an item without one writes `prov: []`.
1830            self.pending_exact = item.prov.as_ref().map(ExactProv::from);
1831            let self_ref = match &item.kind {
1832                TreeKind::Text {
1833                    label,
1834                    text,
1835                    orig,
1836                    formatting,
1837                    hyperlink,
1838                    level,
1839                    list,
1840                } => {
1841                    // docling's field order: …, text, formatting, hyperlink,
1842                    // then the subclass fields (`level`; `enumerated`, `marker`).
1843                    let mut tail = serde_json::Map::new();
1844                    if let Some(f) = formatting {
1845                        tail.insert("formatting".into(), formatting_json(f));
1846                    }
1847                    if let Some(h) = hyperlink {
1848                        tail.insert("hyperlink".into(), json!(h));
1849                    }
1850                    if let Some(l) = level {
1851                        tail.insert("level".into(), json!(l));
1852                    }
1853                    if let Some(l) = list {
1854                        tail.insert("enumerated".into(), json!(l.enumerated));
1855                        tail.insert("marker".into(), json!(l.marker));
1856                    }
1857                    let r = format!("#/texts/{}", self.texts.len());
1858                    let prov = self.take_prov(text.chars().count());
1859                    let mut item_json = json!({
1860                        "self_ref": r,
1861                        "parent": { "$ref": parent },
1862                        "children": children,
1863                        "content_layer": layer,
1864                        "label": label,
1865                        "prov": prov,
1866                    });
1867                    // docling's `comments` back-refs sit between `prov` and
1868                    // `orig`, and are written only when set.
1869                    if !item.comments.is_empty() {
1870                        item_json["comments"] =
1871                            Value::Array(item.comments.iter().map(|&c| ref_of(c)).collect());
1872                    }
1873                    // `DocItem.source`, likewise only when set: a WebVTT
1874                    // cue's `TrackSource`, whose `None` fields are omitted.
1875                    if let Some(track) = &item.source {
1876                        item_json["source"] = json!([track_json(track)]);
1877                    }
1878                    merge(
1879                        &mut item_json,
1880                        json!({
1881                            "orig": orig.as_deref().unwrap_or(text),
1882                            "text": text,
1883                        }),
1884                    );
1885                    merge(&mut item_json, Value::Object(tail));
1886                    if self.note_keys {
1887                        if !item.notes.is_empty() {
1888                            item_json["_notes"] = Value::Array(
1889                                item.notes
1890                                    .iter()
1891                                    .map(|n| json!([n.offset, n.text]))
1892                                    .collect(),
1893                            );
1894                        }
1895                        if item.note_body {
1896                            item_json["_note_body"] = json!(true);
1897                        }
1898                        if !item.anchors.is_empty() {
1899                            item_json["_anchors"] = json!(item.anchors);
1900                        }
1901                        if let Some(number) = &item.number {
1902                            item_json["_number"] = json!(number);
1903                        }
1904                        if let Some(rev) = &item.revision {
1905                            let mut r = serde_json::Map::new();
1906                            r.insert("kind".into(), json!(rev.kind.as_str()));
1907                            if let Some(a) = &rev.author {
1908                                r.insert("author".into(), json!(a));
1909                            }
1910                            if let Some(d) = &rev.date {
1911                                r.insert("date".into(), json!(d));
1912                            }
1913                            item_json["_revision"] = Value::Object(r);
1914                        }
1915                    }
1916                    self.texts.push(Item::json(item_json));
1917                    r
1918                }
1919                TreeKind::Code {
1920                    text,
1921                    orig,
1922                    language,
1923                    formatting,
1924                    hyperlink,
1925                    captions,
1926                } => {
1927                    let r = format!("#/texts/{}", self.texts.len());
1928                    let prov = self.take_prov(text.chars().count());
1929                    let mut item_json = json!({
1930                        "self_ref": r,
1931                        "parent": { "$ref": parent },
1932                        "children": children,
1933                        "content_layer": layer,
1934                        "label": "code",
1935                        "prov": prov,
1936                    });
1937                    if !item.comments.is_empty() {
1938                        item_json["comments"] =
1939                            Value::Array(item.comments.iter().map(|&c| ref_of(c)).collect());
1940                    }
1941                    merge(
1942                        &mut item_json,
1943                        json!({
1944                            "orig": orig.as_deref().unwrap_or(text),
1945                            "text": text,
1946                        }),
1947                    );
1948                    if let Some(f) = formatting {
1949                        item_json["formatting"] = formatting_json(f);
1950                    }
1951                    if let Some(h) = hyperlink {
1952                        item_json["hyperlink"] = json!(h);
1953                    }
1954                    merge(
1955                        &mut item_json,
1956                        json!({
1957                            "captions": captions.iter().map(|&c| ref_of(c)).collect::<Vec<_>>(),
1958                            "references": [],
1959                            "footnotes": [],
1960                            "code_language": code_language(language.as_deref()),
1961                        }),
1962                    );
1963                    self.texts.push(Item::json(item_json));
1964                    r
1965                }
1966                TreeKind::Group { label, name } => {
1967                    self.pending_exact = None;
1968                    let r = format!("#/groups/{}", self.groups.len());
1969                    self.groups.push(json!({
1970                        "self_ref": r,
1971                        "parent": { "$ref": parent },
1972                        "children": children,
1973                        "content_layer": layer,
1974                        "name": name,
1975                        "label": label,
1976                    }));
1977                    r
1978                }
1979                TreeKind::Table {
1980                    table,
1981                    rich_cells,
1982                    captions,
1983                    footnotes,
1984                } => {
1985                    // docling's raw cell text: nothing to unescape or strip.
1986                    let r = self.add_table_with(table, parent, true);
1987                    let idx = ref_index(&r).expect("table ref");
1988                    let t = &mut self.tables[idx];
1989                    t.item["children"] = Value::Array(children);
1990                    t.item["content_layer"] = json!(layer);
1991                    t.item["captions"] =
1992                        Value::Array(captions.iter().map(|&c| ref_of(c)).collect());
1993                    t.item["footnotes"] =
1994                        Value::Array(footnotes.iter().map(|&c| ref_of(c)).collect());
1995                    // A `RichTableCell` is the plain cell plus a `ref` to the
1996                    // group holding its content — on `table_cells` only; the
1997                    // derived `grid` shows plain cells.
1998                    for &(row, col, group) in rich_cells {
1999                        let cell_ref = ref_of(group);
2000                        for c in t.data.cells.iter_mut() {
2001                            if c.start_row == row && c.start_col == col {
2002                                c.cell_ref = Some(Box::new(cell_ref.clone()));
2003                            }
2004                        }
2005                    }
2006                    r
2007                }
2008                TreeKind::Picture {
2009                    captions,
2010                    image,
2011                    classification,
2012                    extra_classes,
2013                    description,
2014                    confidence,
2015                    chart,
2016                    dpi,
2017                } => {
2018                    // A chart's meta: the kind as the one classification
2019                    // prediction (pydantic's field order puts `confidence`
2020                    // first when present), then the reconstructed data grid
2021                    // (#405).
2022                    let mut meta = classification.as_ref().map(|c| {
2023                        let mut pred = serde_json::Map::new();
2024                        if let Some(conf) = confidence {
2025                            pred.insert("confidence".into(), json!(conf));
2026                        }
2027                        pred.insert("class_name".into(), json!(c));
2028                        let mut preds = vec![Value::Object(pred)];
2029                        preds.extend(extra_classes.iter().map(|c| json!({ "class_name": c })));
2030                        json!({ "classification": { "predictions": preds } })
2031                    });
2032                    if let (Some(m), Some(t)) = (meta.as_mut(), chart) {
2033                        if !t.rows.is_empty() {
2034                            m["tabular_chart"] =
2035                                object([("chart_data", table_data(t).into_value())]);
2036                        }
2037                    }
2038                    // The OCR description (#645) goes first in the meta —
2039                    // `PictureMeta` field order — and as the annotation the
2040                    // flat node writes (see `description_meta`).
2041                    if let Some(desc) = description {
2042                        let mut d = description_meta(desc);
2043                        let mut merged = Map::new();
2044                        merged.insert("description".into(), d["description"].take());
2045                        if let Some(Value::Object(rest)) = meta {
2046                            merged.extend(rest);
2047                        }
2048                        merged.insert("annotations".into(), d["annotations"].take());
2049                        meta = Some(Value::Object(merged));
2050                    }
2051                    // The source of a picture no engine rendered (a LaTeX
2052                    // `tikzpicture`): `PictureMeta.code`.
2053                    if let Some((text, language)) = &item.code_meta {
2054                        let code = json!({ "text": text, "language": language });
2055                        match meta.as_mut() {
2056                            Some(Value::Object(m)) => {
2057                                m.insert("code".into(), code);
2058                            }
2059                            _ => meta = Some(json!({ "code": code })),
2060                        }
2061                    }
2062                    let prov = self.take_prov(0);
2063                    let r = self.push_picture(
2064                        prov,
2065                        captions.iter().map(|&c| ref_of(c)).collect(),
2066                        children,
2067                        image.as_ref(),
2068                        meta,
2069                        parent,
2070                    );
2071                    if let Some(idx) = ref_index(&r) {
2072                        self.pictures[idx]["content_layer"] = json!(layer);
2073                        // The image's dpi is the file's when the backend read
2074                        // it (python-pptx does); the default 72 otherwise.
2075                        if let (Some(dpi), Some(img)) = (dpi, self.pictures[idx].get_mut("image")) {
2076                            img["dpi"] = json!(dpi);
2077                        }
2078                    }
2079                    r
2080                }
2081                TreeKind::FieldRegion { items } => {
2082                    self.pending_exact = None;
2083                    let r = self.add_field_region(items, parent);
2084                    if let Some(region) = self.field_regions.last_mut() {
2085                        region["content_layer"] = json!(layer);
2086                    }
2087                    r
2088                }
2089                TreeKind::KeyValueGraph { cells, links } => {
2090                    self.pending_exact = None;
2091                    let r = self.add_key_value_graph(cells, links, parent);
2092                    if let Some(item) = self.key_value_items.last_mut() {
2093                        item["content_layer"] = json!(layer);
2094                    }
2095                    r
2096                }
2097            };
2098            debug_assert_eq!(self_ref, refs[id], "tree item {id} numbered out of order");
2099            // A float's own footnotes (`FloatingItem.footnotes`).
2100            if !item.footnotes.is_empty() {
2101                let notes: Vec<Value> = item
2102                    .footnotes
2103                    .iter()
2104                    .filter(|&&f| !tree.items[f].deleted)
2105                    .map(|&f| ref_of(f))
2106                    .collect();
2107                if let Some(v) = self.item_mut(&self_ref) {
2108                    v["footnotes"] = Value::Array(notes);
2109                }
2110            }
2111        }
2112        tree.body
2113            .iter()
2114            .filter(|&&c| !tree.items[c].deleted)
2115            .map(|&c| ref_of(c))
2116            .collect()
2117    }
2118
2119    fn add_node(&mut self, node: &Node, parent: &str) -> Option<String> {
2120        match node {
2121            Node::Heading { level: 1, text } => {
2122                Some(self.add_text("title", text, parent, json!({})))
2123            }
2124            Node::Heading { level, text } => Some(self.add_text(
2125                "section_header",
2126                text,
2127                parent,
2128                json!({ "level": level.saturating_sub(1) }),
2129            )),
2130            Node::Caption { text, href } => {
2131                let extra = match href {
2132                    Some(url) => json!({ "hyperlink": url }),
2133                    None => json!({}),
2134                };
2135                Some(self.add_text("caption", text, parent, extra))
2136            }
2137            // A marked PDF item (#1005, docling's `ScriptPositionModel._apply`):
2138            // the item keeps its place and its `prov`, its text moves into an
2139            // inline group of the runs (see `link_script_runs`).
2140            Node::ScriptText { inner, runs } => {
2141                let label = match inner.as_ref() {
2142                    Node::LabeledText { label, .. } => label.clone(),
2143                    _ => "text".to_string(),
2144                };
2145                let r = self.add_node(inner, parent)?;
2146                let link = runs.iter().find_map(|run| run.hyperlink.clone());
2147                if let Some(obj) = self.item_mut(&r).and_then(Value::as_object_mut) {
2148                    obj.insert("orig".into(), json!(""));
2149                    obj.insert("text".into(), json!(""));
2150                    // The item keeps its own link, as docling's does.
2151                    if let Some(url) = link {
2152                        obj.insert("hyperlink".into(), json!(url));
2153                    }
2154                }
2155                self.pending_scripts.push((r.clone(), runs.clone(), label));
2156                Some(r)
2157            }
2158            // A PDF footnote: docling's own label, and its link as the item's
2159            // `hyperlink` rather than Markdown baked into the text.
2160            Node::LabeledText { label, text, href } => {
2161                let extra = match href {
2162                    Some(url) => json!({ "hyperlink": url }),
2163                    None => json!({}),
2164                };
2165                Some(self.add_text(label, text, parent, extra))
2166            }
2167            Node::Paragraph { text } | Node::ListText { text, .. } => {
2168                // A whole-paragraph display equation is a formula item (docling
2169                // wraps it in `$$…$$` and, unlike a text item, never escapes it).
2170                let t = text.trim();
2171                match t.strip_prefix("$$").and_then(|s| s.strip_suffix("$$")) {
2172                    Some(inner) if !inner.is_empty() => Some(self.add_formula(inner, parent)),
2173                    _ => Some(self.add_text("text", text, parent, json!({}))),
2174                }
2175            }
2176            Node::CheckboxItem { checked, text } => {
2177                // docling's checkbox item: the `checkbox_selected` /
2178                // `checkbox_unselected` label carries the state and the text
2179                // is the bare option label (right_to_left_03's `خير`,
2180                // docx_checkboxes' `Orange juice`). The `- [x]` task-list
2181                // marker is Markdown's rendering of it, not item text (#609).
2182                let label = if *checked {
2183                    "checkbox_selected"
2184                } else {
2185                    "checkbox_unselected"
2186                };
2187                Some(self.add_text(label, text, parent, json!({})))
2188            }
2189            Node::Code {
2190                language,
2191                text,
2192                orig,
2193                ..
2194            } => Some(self.add_code(text, language.as_deref(), orig.as_deref(), parent)),
2195            // A CodeFormula-decoded display formula: `text` carries the LaTeX,
2196            // `orig` the raw glyph extraction (docling's enriched shape).
2197            Node::Formula {
2198                latex,
2199                orig,
2200                location,
2201            } => {
2202                self.adopt_loc(*location);
2203                Some(self.add_formula_item(latex, orig, parent))
2204            }
2205            // docling's notes-layer `comment_section` group holding the
2206            // comment's text item. What the annotated items point at differs
2207            // upstream: the docx backend links the group (so a comment's
2208            // replies group together), everything going through
2209            // docling-core's `add_comment` links the note text itself.
2210            Node::CommentSection {
2211                name,
2212                text,
2213                refs_note_text,
2214                grouped,
2215            } => {
2216                if !*grouped {
2217                    // docling-core's bare `add_comment`: the note text sits
2218                    // directly under the parent and is what the back-refs
2219                    // point at.
2220                    let child =
2221                        self.add_text("text", text, parent, json!({ "content_layer": "notes" }));
2222                    self.comment_groups.push(child.clone());
2223                    return Some(child);
2224                }
2225                if let Some((_, self_ref)) = self
2226                    .last_comment_section
2227                    .as_ref()
2228                    .filter(|(n, _)| n == name)
2229                    .cloned()
2230                {
2231                    // Another message of the same thread: a further note in
2232                    // the group already open for this cell.
2233                    let child =
2234                        self.add_text("text", text, &self_ref, json!({ "content_layer": "notes" }));
2235                    if let Some(children) =
2236                        self.groups[group_index(&self_ref)]["children"].as_array_mut()
2237                    {
2238                        children.push(ref_value(child.as_str()));
2239                    }
2240                    self.comment_groups.push(if *refs_note_text {
2241                        child
2242                    } else {
2243                        self_ref.clone()
2244                    });
2245                    // The group is already its parent's child: listing it
2246                    // again put a thread's group in the body once per message.
2247                    return None;
2248                }
2249                let self_ref = format!("#/groups/{}", self.groups.len());
2250                self.groups.push(Value::Null);
2251                let child =
2252                    self.add_text("text", text, &self_ref, json!({ "content_layer": "notes" }));
2253                self.groups[group_index(&self_ref)] = json!({
2254                    "self_ref": self_ref,
2255                    "parent": { "$ref": parent },
2256                    "children": [{ "$ref": child }],
2257                    "content_layer": "notes",
2258                    "name": name,
2259                    "label": "comment_section",
2260                });
2261                self.last_comment_section = Some((name.clone(), self_ref.clone()));
2262                self.comment_groups.push(if *refs_note_text {
2263                    child
2264                } else {
2265                    self_ref.clone()
2266                });
2267                Some(self_ref)
2268            }
2269            // The annotation itself is a cross-reference: emit the item, then
2270            // remember it so the group refs can be filled in at the end.
2271            Node::Commented { comments, inner } => {
2272                let item = self.add_node(inner, parent)?;
2273                if !comments.is_empty() {
2274                    self.pending_comments.push((item.clone(), comments.clone()));
2275                }
2276                Some(item)
2277            }
2278            Node::Table(t) => Some(self.add_table(t, parent)),
2279            Node::Picture {
2280                caption,
2281                caption_href,
2282                image,
2283                classification,
2284                description,
2285                caption_parent,
2286                caption_location,
2287            } => Some(self.add_picture(
2288                caption.as_deref(),
2289                caption_href.as_deref(),
2290                image.as_ref(),
2291                picture_meta(classification.as_deref(), description.as_ref()),
2292                parent,
2293                (*caption_parent, *caption_location),
2294            )),
2295            // A chart is a picture item in the JSON with docling's chart
2296            // meta — `classification` (the chart kind, as the one prediction)
2297            // and `tabular_chart.chart_data`, the series reconstructed as a
2298            // `TableData` (#405) — and no image payload.
2299            Node::Chart {
2300                kind,
2301                table,
2302                caption,
2303                location,
2304            } => {
2305                self.adopt_loc(*location);
2306                let mut meta = json!({
2307                    "classification": { "predictions": [{ "class_name": kind }] },
2308                });
2309                if !table.rows.is_empty() {
2310                    meta["tabular_chart"] =
2311                        object([("chart_data", table_data(table).into_value())]);
2312                }
2313                // docling's office backends add the chart's title as a caption
2314                // item of the *container* (the sheet group, the slide), listed
2315                // before the picture that references it, with the chart's own
2316                // box and a charspan over the caption text — not as a child
2317                // of the picture, which is where a PDF caption lives.
2318                let mut captions = Vec::new();
2319                if let Some(cap) = caption.as_deref().filter(|c| !c.is_empty()) {
2320                    let prov = self.prov_slot(unescape_text(cap).chars().count(), true);
2321                    let cap_ref = self.add_text_with("caption", cap, parent, json!({}), prov);
2322                    self.pending_siblings.push(ref_value(cap_ref.as_str()));
2323                    captions.push(ref_value(cap_ref));
2324                }
2325                let prov = self.take_prov(0);
2326                Some(self.push_picture(prov, captions, Vec::new(), None, Some(meta), parent))
2327            }
2328            // A DocLang-only node is omitted from the JSON body.
2329            Node::DoclangOnly(_) => None,
2330            Node::Group {
2331                label,
2332                name,
2333                layer,
2334                children,
2335            } => Some(self.add_group(label, name.as_deref(), *layer, children, parent)),
2336            Node::FieldRegion { items } => Some(self.add_field_region(items, parent)),
2337            Node::KeyValueGraph { cells, links } => {
2338                Some(self.add_key_value_graph(cells, links, parent))
2339            }
2340            // A rich inline group is a text item over its Markdown text; the
2341            // structured runs are DocLang-only, so the JSON matches a paragraph.
2342            Node::InlineGroup { md_text, .. } => {
2343                Some(self.add_text("text", md_text, parent, json!({})))
2344            }
2345            // A plain-text backend dump is a single text item over the file body.
2346            Node::TextDump(text) => Some(self.add_text("text", text, parent, json!({}))),
2347            // Speaker notes are content a deck carries, and docling puts them
2348            // in the JSON on their own layer, so a consumer reading only JSON
2349            // can pick them (#402). Page furniture stays out of the flat
2350            // path; a backend that builds docling's item tree (HTML) puts its
2351            // furniture-layer items in the JSON through `write_tree`.
2352            Node::Furniture {
2353                layer: ContentLayer::Notes,
2354                inner,
2355            } => {
2356                let item = self.add_node(inner, parent)?;
2357                self.set_layer(&item, "notes");
2358                Some(item)
2359            }
2360            Node::Furniture { .. } => None,
2361            // PDF page headers and footers: docling writes them as body-parented
2362            // `page_header`/`page_footer` text items on the furniture layer, so a
2363            // JSON consumer can read running headers (printed page ids, dates).
2364            Node::PageFurniture {
2365                footer,
2366                location,
2367                text,
2368            } => {
2369                if self.cur_page > 0 {
2370                    self.pending_loc = Some(*location);
2371                }
2372                let label = if *footer {
2373                    "page_footer"
2374                } else {
2375                    "page_header"
2376                };
2377                let item = self.add_text(label, text, parent, json!({}));
2378                self.pending_loc = None;
2379                self.set_layer(&item, "furniture");
2380                Some(item)
2381            }
2382            // A labelled furniture paragraph (a `.doc` header / footer or
2383            // note body, #535): the item docling's office backends write.
2384            Node::FurnitureText { label, text } => {
2385                let item = self.add_text(label, text, parent, json!({}));
2386                self.set_layer(&item, "furniture");
2387                Some(item)
2388            }
2389            // A PDF picture's contained text (docling's `_add_child_elements`):
2390            // items parented to the picture just written, listed after its
2391            // caption in that picture's `children`.
2392            Node::PictureChildren(children) => {
2393                let pic = self.pictures.len().checked_sub(1)?;
2394                let pic_ref = format!("#/pictures/{pic}");
2395                let refs = self.walk_into(children, &pic_ref);
2396                if let Some(list) = self.pictures[pic]["children"].as_array_mut() {
2397                    list.extend(refs.into_iter().map(Child::into_value));
2398                }
2399                None
2400            }
2401            // A location wrapper turns into the wrapped item's `prov` entry —
2402            // but only on pages the PDF paths described with a PageInfo marker
2403            // (other geometry-bearing backends, e.g. PPTX shapes, keep their
2404            // pre-#171 provenance-less JSON until they emit markers too).
2405            Node::Located { location, inner } => {
2406                if self.cur_page > 0 {
2407                    self.pending_loc = Some(*location);
2408                }
2409                let r = self.add_node(inner, parent);
2410                self.pending_loc = None;
2411                r
2412            }
2413            Node::Anchor { id } => {
2414                self.pending_anchors.push(id.clone());
2415                None
2416            }
2417            Node::Track { track, cue, inner } => {
2418                self.pending_track = Some((track.clone(), cue.clone()));
2419                let r = self.add_node(inner, parent);
2420                self.pending_track = None;
2421                r
2422            }
2423            Node::Prov {
2424                page_no,
2425                bbox,
2426                charspan,
2427                bottom_left,
2428                inner,
2429                ..
2430            } => {
2431                self.pending_exact = Some(ExactProv {
2432                    page_no: *page_no,
2433                    bbox: bbox.map(f64::from),
2434                    bottom_left: *bottom_left,
2435                    charspan: *charspan,
2436                });
2437                let r = self.add_node(inner, parent);
2438                self.pending_exact = None;
2439                r
2440            }
2441            // Page breaks are DocLang-only; docling omits them from the JSON body.
2442            Node::PageBreak => None,
2443            // The page marker: record the page's number and size for the
2444            // `pages` map, and denormalize every following location against it.
2445            Node::PageInfo {
2446                page_no,
2447                width,
2448                height,
2449            } => {
2450                self.cur_page = *page_no;
2451                self.cur_w = *width as f64;
2452                self.cur_h = *height as f64;
2453                if *page_no > 0 {
2454                    self.pages.push((*page_no, self.cur_w, self.cur_h));
2455                }
2456                None
2457            }
2458            // Handled by `add_list` in `walk`.
2459            Node::ListItem { .. } => None,
2460        }
2461    }
2462
2463    /// A form key-value region: `field_regions/N` holds the region, each field is
2464    /// a `field_items/M` whose children are its `marker` / `field_key` /
2465    /// `field_value` texts (absent parts are simply omitted).
2466    fn add_field_region(&mut self, items: &[crate::FieldItem], parent: &str) -> String {
2467        let self_ref = format!("#/field_regions/{}", self.field_regions.len());
2468        self.field_regions.push(Value::Null);
2469        let region_index = self.field_regions.len() - 1;
2470        let mut item_refs = Vec::new();
2471        for item in items {
2472            item_refs.push(ref_value(self.add_field_item(item, &self_ref)));
2473        }
2474        self.field_regions[region_index] = json!({
2475            "self_ref": self_ref,
2476            "parent": { "$ref": parent },
2477            "children": item_refs,
2478            "content_layer": "body",
2479            "label": "field_region",
2480            "prov": [],
2481        });
2482        self_ref
2483    }
2484
2485    /// A `KeyValueItem`: docling's `GraphData` written cell for cell and link
2486    /// for link (`key_value_items/N`), the item itself childless and without
2487    /// provenance, the way the XBRL backend creates it.
2488    fn add_key_value_graph(
2489        &mut self,
2490        cells: &[crate::GraphCell],
2491        links: &[crate::GraphLink],
2492        parent: &str,
2493    ) -> String {
2494        let self_ref = format!("#/key_value_items/{}", self.key_value_items.len());
2495        let cells: Vec<Value> = cells
2496            .iter()
2497            .map(|c| {
2498                json!({
2499                    "label": c.label,
2500                    "cell_id": c.cell_id,
2501                    "text": c.text,
2502                    "orig": c.orig,
2503                })
2504            })
2505            .collect();
2506        let links: Vec<Value> = links
2507            .iter()
2508            .map(|l| {
2509                json!({
2510                    "label": l.label,
2511                    "source_cell_id": l.source_cell_id,
2512                    "target_cell_id": l.target_cell_id,
2513                })
2514            })
2515            .collect();
2516        self.key_value_items.push(json!({
2517            "self_ref": self_ref,
2518            "parent": { "$ref": parent },
2519            "children": [],
2520            "content_layer": "body",
2521            "label": "key_value_region",
2522            "prov": [],
2523            "captions": [],
2524            "references": [],
2525            "footnotes": [],
2526            "graph": { "cells": cells, "links": links },
2527        }));
2528        self_ref
2529    }
2530
2531    fn add_field_item(&mut self, item: &crate::FieldItem, parent: &str) -> String {
2532        let self_ref = format!("#/field_items/{}", self.field_items.len());
2533        self.field_items.push(Value::Null);
2534        let item_index = self.field_items.len() - 1;
2535        let mut child_refs = Vec::new();
2536        for (label, text) in [
2537            ("marker", &item.marker),
2538            ("field_key", &item.key),
2539            ("field_value", &item.value),
2540        ] {
2541            if let Some(text) = text {
2542                // A value's `kind` (docling's `read_only` / `fillable`)
2543                // follows its text.
2544                let extra = match (label, &item.value_kind) {
2545                    ("field_value", Some(kind)) => json!({ "kind": kind }),
2546                    _ => json!({}),
2547                };
2548                child_refs.push(ref_value(self.add_text(label, text, &self_ref, extra)));
2549            }
2550        }
2551        self.field_items[item_index] = json!({
2552            "self_ref": self_ref,
2553            "parent": { "$ref": parent },
2554            "children": child_refs,
2555            "content_layer": "body",
2556            "label": "field_item",
2557            "prov": [],
2558        });
2559        self_ref
2560    }
2561
2562    /// Move an already-emitted item onto a content layer. Notes are single
2563    /// text items today; a deeper notes subtree would need its children moved
2564    /// too, and no backend builds one.
2565    fn set_layer(&mut self, self_ref: &str, layer: &str) {
2566        if let Some(item) = self.item_mut(self_ref) {
2567            item["content_layer"] = json!(layer);
2568        }
2569    }
2570
2571    fn add_text(&mut self, label: &str, text: &str, parent: &str, extra: Value) -> String {
2572        let prov = self.take_prov_slot(unescape_text(text).chars().count());
2573        self.add_text_with(label, text, parent, extra, prov)
2574    }
2575
2576    /// [`Self::add_text`] with an explicit `prov` (a chart caption shares the
2577    /// chart's box without consuming it).
2578    fn add_text_with(
2579        &mut self,
2580        label: &str,
2581        text: &str,
2582        parent: &str,
2583        extra: Value,
2584        prov: ProvSlot,
2585    ) -> String {
2586        let index = self.texts.len();
2587        let self_ref = format!("#/texts/{index}");
2588        // A [`Node::Track`] item is docling's ASR text item (#614): the
2589        // segment's words as `text`, its timing as `source` — the `[time: …]`
2590        // prefix the wrapped paragraph shows is Markdown's alone. It carries a
2591        // field the compact [`TextItem`] doesn't, so it is built as JSON.
2592        let mut item = match self.pending_track.take() {
2593            None => Item::Text(Box::new(TextItem {
2594                parent: parent.into(),
2595                label: label.into(),
2596                prov,
2597                text: unescape_text(text).into(),
2598            })),
2599            Some((track, cue)) => {
2600                let mut m = serde_json::Map::with_capacity(9);
2601                m.insert("self_ref".into(), Value::String(self_ref.clone()));
2602                m.insert("parent".into(), ref_value(parent));
2603                m.insert("children".into(), Value::Array(Vec::new()));
2604                m.insert("content_layer".into(), "body".into());
2605                m.insert("label".into(), label.into());
2606                m.insert("prov".into(), prov.to_value());
2607                // `DocItem.source` follows `prov` (docling's field order).
2608                m.insert("source".into(), Value::Array(vec![track_json(&track)]));
2609                m.insert("orig".into(), Value::String(cue.clone()));
2610                m.insert("text".into(), Value::String(cue));
2611                Item::json(Value::Object(m))
2612            }
2613        };
2614        if extra.as_object().is_some_and(|e| !e.is_empty()) {
2615            merge(item.value_mut(index), extra);
2616        }
2617        // The link destinations met before this item (#852), on the Pandoc
2618        // path only — docling's model has no field for them.
2619        let anchors = std::mem::take(&mut self.pending_anchors);
2620        if self.note_keys && !anchors.is_empty() {
2621            merge(item.value_mut(index), json!({ "_anchors": anchors }));
2622        }
2623        self.texts.push(item);
2624        self_ref
2625    }
2626
2627    /// A display-math formula item. `latex` is the raw content (no `$$`); docling
2628    /// re-wraps it and never escapes it.
2629    fn add_formula(&mut self, latex: &str, parent: &str) -> String {
2630        let self_ref = format!("#/texts/{}", self.texts.len());
2631        let prov = self.take_prov(latex.chars().count());
2632        self.texts.push(Item::json(json!({
2633            "self_ref": self_ref,
2634            "parent": { "$ref": parent },
2635            "children": [],
2636            "content_layer": "body",
2637            "label": "formula",
2638            "prov": prov,
2639            "orig": latex,
2640            "text": latex,
2641        })));
2642        self_ref
2643    }
2644
2645    /// A CodeFormula-enriched display formula: `text` is the model's LaTeX
2646    /// while `orig` keeps the raw glyph extraction (docling's enriched shape;
2647    /// the plain [`Self::add_formula`] above sets both to the same string).
2648    fn add_formula_item(&mut self, latex: &str, orig: &str, parent: &str) -> String {
2649        let self_ref = format!("#/texts/{}", self.texts.len());
2650        let prov = self.take_prov(latex.chars().count());
2651        self.texts.push(Item::json(json!({
2652            "self_ref": self_ref,
2653            "parent": { "$ref": parent },
2654            "children": [],
2655            "content_layer": "body",
2656            "label": "formula",
2657            "prov": prov,
2658            "orig": orig,
2659            "text": latex,
2660        })));
2661        self_ref
2662    }
2663
2664    fn add_code(
2665        &mut self,
2666        text: &str,
2667        language: Option<&str>,
2668        orig: Option<&str>,
2669        parent: &str,
2670    ) -> String {
2671        let self_ref = format!("#/texts/{}", self.texts.len());
2672        let raw = unescape_text(text);
2673        let prov = self.take_prov(raw.chars().count());
2674        self.texts.push(Item::json(json!({
2675            "self_ref": self_ref,
2676            "parent": { "$ref": parent },
2677            "children": [],
2678            "content_layer": "body",
2679            "label": "code",
2680            "prov": prov,
2681            // With code enrichment, `text` is the model's rewrite while `orig`
2682            // keeps the raw extraction; otherwise both are the same string.
2683            "orig": orig.map(unescape_text).unwrap_or_else(|| raw.clone()),
2684            "text": raw,
2685            "captions": [],
2686            "references": [],
2687            "footnotes": [],
2688            "code_language": code_language(language),
2689        })));
2690        self_ref
2691    }
2692
2693    /// Build a list group from a run of (possibly multi-level) list items. A
2694    /// deeper level starts a nested list under the preceding item.
2695    fn add_list(&mut self, items: &[Node], parent: &str) -> String {
2696        let self_ref = format!("#/groups/{}", self.groups.len());
2697        // reserve the slot so nested groups get later indices
2698        self.groups.push(Value::Null);
2699        let base = level_of(&items[0]);
2700        let mut children = Vec::new();
2701        let mut i = 0;
2702        while i < items.len() {
2703            // Empty paragraphs absorbed into the run (blank lines between items)
2704            // are not list items — skip them.
2705            if !matches!(items[i], Node::ListItem { .. }) {
2706                i += 1;
2707                continue;
2708            }
2709            let lvl = level_of(&items[i]);
2710            if lvl > base {
2711                // shouldn't happen at the head; skip defensively
2712                i += 1;
2713                continue;
2714            }
2715            let item_ref = self.add_list_item(&items[i], &self_ref);
2716            // collect any deeper items that nest under this one
2717            let mut j = i + 1;
2718            while j < items.len() && level_of(&items[j]) > base {
2719                j += 1;
2720            }
2721            if j > i + 1 {
2722                let mut nested = Vec::new();
2723                self.add_sibling_lists(&items[i + 1..j], &item_ref, &mut nested);
2724                // the nested list group(s) are children of this item
2725                if let Some(idx) = ref_index(&item_ref) {
2726                    self.texts[idx].value_mut(idx)["children"]
2727                        .as_array_mut()
2728                        .unwrap()
2729                        .extend(nested);
2730                }
2731            }
2732            children.push(ref_value(item_ref));
2733            i = j;
2734        }
2735        self.groups[group_index(&self_ref)] = json!({
2736            "self_ref": self_ref,
2737            "parent": { "$ref": parent },
2738            "children": children,
2739            "content_layer": "body",
2740            "name": "list",
2741            "label": "list",
2742        });
2743        self_ref
2744    }
2745
2746    fn add_list_item(&mut self, node: &Node, parent: &str) -> String {
2747        let Node::ListItem {
2748            ordered,
2749            number,
2750            text,
2751            location,
2752            split,
2753            ..
2754        } = node
2755        else {
2756            unreachable!()
2757        };
2758        self.adopt_loc(*location);
2759        let self_ref = format!("#/texts/{}", self.texts.len());
2760        // A page pipeline's item (PDF) is written as docling's
2761        // `ListItemMarkerProcessor` leaves it: `orig` with the marker, `text`
2762        // without, the marker itself, `enumerated` for any numbered one.
2763        let (orig, raw, enumerated, marker) = match split.as_deref() {
2764            Some(s) => (
2765                s.orig.clone(),
2766                s.text.clone(),
2767                s.enumerated,
2768                s.marker.clone(),
2769            ),
2770            None => {
2771                let raw = unescape_text(text);
2772                let marker = if *ordered {
2773                    format!("{number}.")
2774                } else {
2775                    "-".to_string()
2776                };
2777                (raw.clone(), raw, *ordered, marker)
2778            }
2779        };
2780        let prov = self.take_prov(orig.chars().count());
2781        self.texts.push(Item::json(json!({
2782            "self_ref": self_ref,
2783            "parent": { "$ref": parent },
2784            "children": [],
2785            "content_layer": "body",
2786            "label": "list_item",
2787            "prov": prov,
2788            "orig": orig,
2789            "text": raw,
2790            "enumerated": enumerated,
2791            "marker": marker,
2792        })));
2793        self_ref
2794    }
2795
2796    fn add_table(&mut self, t: &Table, parent: &str) -> String {
2797        self.add_table_with(t, parent, false)
2798    }
2799
2800    /// [`Self::add_table`]; `raw` cell text is written verbatim (see
2801    /// [`table_data_with`]).
2802    fn add_table_with(&mut self, t: &Table, parent: &str, raw: bool) -> String {
2803        let self_ref = format!("#/tables/{}", self.tables.len());
2804        self.adopt_loc(t.location);
2805        let prov = self.take_prov(0);
2806        // The caption is a separate text item the table references (docling's
2807        // `TableItem.captions`), added before the grid so its box isn't
2808        // inherited by a later item.
2809        let (captions, children) = match t.caption.as_deref().filter(|c| !c.is_empty()) {
2810            Some(cap) => self.add_caption(
2811                cap,
2812                json!({}),
2813                &self_ref,
2814                parent,
2815                (t.caption_parent, t.caption_location),
2816            ),
2817            None => (Vec::new(), Vec::new()),
2818        };
2819        // docling-core#675 (#1009): each footnote is a `footnote` text item
2820        // under the table, added after it (docling's backends attach notes
2821        // once the table exists) and listed in its `footnotes`.
2822        // The item-tree path (`raw`) writes its own footnote items.
2823        let (mut children, mut footnotes) = (children, Vec::new());
2824        for note in t.footnotes.iter().filter(|n| !raw && !n.is_empty()) {
2825            let r = ref_value(self.add_text("footnote", note, &self_ref, json!({})));
2826            children.push(r.clone());
2827            footnotes.push(r);
2828        }
2829        let mut data = table_data_with(t, raw);
2830        if !raw {
2831            self.add_cell_pictures(t, &self_ref, &mut data, &mut children);
2832        }
2833        let item = json!({
2834            "self_ref": self_ref,
2835            "parent": { "$ref": parent },
2836            "children": children,
2837            "content_layer": "body",
2838            // docling's `DocItemLabel.DOCUMENT_INDEX` for a table of
2839            // contents (#1091), a plain `table` otherwise.
2840            "label": if t.document_index { "document_index" } else { "table" },
2841            "prov": prov,
2842            "captions": captions,
2843            "references": [],
2844            "footnotes": footnotes,
2845            // The typed `data` takes this stand-in's place on output.
2846            "data": null,
2847            "annotations": [],
2848        });
2849        self.tables.push(TableItem { item, data });
2850        self_ref
2851    }
2852
2853    /// The pictures placed in a table's cells (the XLSX "Place in Cell"
2854    /// image, docling#5015): a cell whose blocks are exactly one
2855    /// [`Node::Prov`]-wrapped picture is docling's `RichTableCell` over a
2856    /// picture item — the picture is the table's child, the cell keeps an
2857    /// empty text and a `ref` to it. Other rich cells (and the PDF
2858    /// pipeline's [`Node::Located`] in-cell pictures) keep their flat cell.
2859    /// The cells' blocks are parallel to `rows`; a ragged (compacted) row
2860    /// pairs its blocks with that row's first-class cells in column order.
2861    fn add_cell_pictures(
2862        &mut self,
2863        t: &Table,
2864        self_ref: &str,
2865        data: &mut TableData,
2866        children: &mut Vec<Value>,
2867    ) {
2868        let Some(blocks) = t.cell_blocks.as_ref() else {
2869            return;
2870        };
2871        for (r, row) in blocks.iter().enumerate() {
2872            let mut in_row: Vec<usize> = (0..data.cells.len())
2873                .filter(|&i| data.cells[i].start_row == r)
2874                .collect();
2875            in_row.sort_by_key(|&i| data.cells[i].start_col);
2876            let ragged = t.rows.get(r).map_or(0, Vec::len) != data.num_cols;
2877            for (c, cell) in row.iter().enumerate() {
2878                let [node @ Node::Prov { inner, .. }] = cell.as_slice() else {
2879                    continue;
2880                };
2881                if !matches!(**inner, Node::Picture { .. }) {
2882                    continue;
2883                }
2884                let target = if ragged {
2885                    in_row.get(c).copied()
2886                } else {
2887                    in_row
2888                        .iter()
2889                        .copied()
2890                        .find(|&i| data.cells[i].start_col == c)
2891                };
2892                let Some(target) = target else {
2893                    continue;
2894                };
2895                let Some(pic) = self.add_node(node, self_ref) else {
2896                    continue;
2897                };
2898                let pic = ref_value(pic);
2899                children.push(pic.clone());
2900                let cell = &mut data.cells[target];
2901                cell.text = "".into();
2902                cell.cell_ref = Some(Box::new(pic));
2903            }
2904        }
2905    }
2906
2907    /// Add a picture's or table's caption text item where `choice` says it
2908    /// hangs (#390), returning the `captions` entry for the item and the
2909    /// item's own `children` (the caption, when it is the item's child).
2910    /// The caption never consumes the item's pending provenance — the item
2911    /// takes its box first. Its own box, when the backend kept one (the PDF
2912    /// pipeline's caption region, #609), becomes the caption's `prov` on the
2913    /// item's page, as docling's reading-order model gives every caption the
2914    /// cluster it came from; without one the caption has no `prov`.
2915    fn add_caption(
2916        &mut self,
2917        text: &str,
2918        extra: Value,
2919        self_ref: &str,
2920        parent: &str,
2921        (choice, location): (CaptionParent, Option<[u16; 4]>),
2922    ) -> (Vec<Value>, Vec<Value>) {
2923        // docling's PDF pipeline parents the caption to the item; every
2924        // declarative backend leaves `add_text`'s default — the body — even
2925        // for an item inside a group; the office backends and HTML's
2926        // `<figure>` hang it off the item's container.
2927        let cap_parent = match choice {
2928            CaptionParent::Item => self_ref,
2929            CaptionParent::Container | CaptionParent::ContainerAfter => parent,
2930            CaptionParent::Body => "#/body",
2931        };
2932        if self.cur_page > 0 && location.is_some() {
2933            self.pending_loc = location;
2934        }
2935        let cap_ref = ref_value(self.add_text("caption", text, cap_parent, extra));
2936        match choice {
2937            CaptionParent::Item => return (vec![cap_ref.clone()], vec![cap_ref]),
2938            // Created ahead of the item, so it precedes the item in the
2939            // container's children — and, on the body, in the body's.
2940            CaptionParent::Container => self.pending_siblings.push(cap_ref.clone()),
2941            CaptionParent::Body if parent == "#/body" => {
2942                self.pending_siblings.push(cap_ref.clone())
2943            }
2944            CaptionParent::ContainerAfter => self.pending_after.push(cap_ref.clone()),
2945            // The item sits deeper: the body's children get the caption after
2946            // the top-level item under walk, where docling appended it.
2947            CaptionParent::Body => self.pending_body.push(cap_ref.clone()),
2948        }
2949        (vec![cap_ref], Vec::new())
2950    }
2951
2952    /// `meta` is the picture's docling `PictureMeta` (a classifier's
2953    /// predictions, a chart's kind and data), `None` for a plain picture.
2954    fn add_picture(
2955        &mut self,
2956        caption: Option<&str>,
2957        caption_href: Option<&str>,
2958        image: Option<&crate::PictureImage>,
2959        meta: Option<Value>,
2960        parent: &str,
2961        caption_at: (CaptionParent, Option<[u16; 4]>),
2962    ) -> String {
2963        let self_ref = format!("#/pictures/{}", self.pictures.len());
2964        // Take the picture's own provenance before the caption text is added —
2965        // the caption is a separate item and must not inherit the crop's box.
2966        let prov = self.take_prov(0);
2967        let (captions, children) = match caption.filter(|c| !c.is_empty()) {
2968            Some(cap) => {
2969                // Emit the caption as a text item that the picture references. A
2970                // wrapping `<a href>`'s link rides as docling's `hyperlink` field
2971                // on the caption item (#328).
2972                let extra = match caption_href {
2973                    Some(href) => json!({ "hyperlink": href }),
2974                    None => json!({}),
2975                };
2976                self.add_caption(cap, extra, &self_ref, parent, caption_at)
2977            }
2978            None => (Vec::new(), Vec::new()),
2979        };
2980        self.push_picture(prov, captions, children, image, meta, parent)
2981    }
2982
2983    /// Append the picture item itself — `prov`, `captions` and `children`
2984    /// (a PDF caption is the picture's child) already settled.
2985    fn push_picture(
2986        &mut self,
2987        prov: Value,
2988        captions: Vec<Value>,
2989        children: Vec<Value>,
2990        image: Option<&crate::PictureImage>,
2991        meta: Option<Value>,
2992        parent: &str,
2993    ) -> String {
2994        let self_ref = format!("#/pictures/{}", self.pictures.len());
2995        // The legacy `classification` annotation rides along with a
2996        // classifier's `meta` (see `classification_meta`); a chart's meta has
2997        // none, like docling's.
2998        let annotations = meta
2999            .as_ref()
3000            .and_then(|m| m.get("annotations").cloned())
3001            .unwrap_or_else(|| json!([]));
3002        let meta = meta.map(|mut m| {
3003            if let Some(obj) = m.as_object_mut() {
3004                obj.remove("annotations");
3005            }
3006            m
3007        });
3008        // `meta` sits between `content_layer` and `label` in docling's field
3009        // order (and `preserve_order` keeps ours byte-compatible), so the item
3010        // is built in one shot per shape rather than patched afterwards.
3011        let mut item = match meta {
3012            Some(meta) => json!({
3013                "self_ref": self_ref,
3014                "parent": { "$ref": parent },
3015                "children": children,
3016                "content_layer": "body",
3017                "meta": meta,
3018                "label": "picture",
3019                "prov": prov,
3020                "captions": captions,
3021                "references": [],
3022                "footnotes": [],
3023                "annotations": annotations,
3024            }),
3025            None => json!({
3026                "self_ref": self_ref,
3027                "parent": { "$ref": parent },
3028                "children": children,
3029                "content_layer": "body",
3030                "label": "picture",
3031                "prov": prov,
3032                "captions": captions,
3033                "references": [],
3034                "footnotes": [],
3035                "annotations": annotations,
3036            }),
3037        };
3038        // docling stores the extracted image as an `ImageRef` (data URI + size,
3039        // the size as floats) between `footnotes` and `annotations` — pydantic
3040        // field order, which `preserve_order` lets us reproduce by rebuilding
3041        // the tail.
3042        if let Some(img) = image {
3043            let mut image = image_ref_json(img);
3044            // The displayed size, for the Pandoc writer only (#834):
3045            // docling's `ImageRef` has no field for it.
3046            if self.note_keys {
3047                if let Some([w, h]) = img.display_size {
3048                    image["_display"] = json!([w, h]);
3049                }
3050            }
3051            if let Some(obj) = item.as_object_mut() {
3052                let annotations = obj.remove("annotations").unwrap_or_else(|| json!([]));
3053                obj.insert("image".into(), image);
3054                obj.insert("annotations".into(), annotations);
3055            }
3056        }
3057        self.pictures.push(item);
3058        self_ref
3059    }
3060
3061    fn add_group(
3062        &mut self,
3063        label: &str,
3064        name: Option<&str>,
3065        layer: Option<ContentLayer>,
3066        nodes: &[Node],
3067        parent: &str,
3068    ) -> String {
3069        let self_ref = format!("#/groups/{}", self.groups.len());
3070        self.groups.push(Value::Null);
3071        // Everything the walk creates belongs to this group, so a non-body
3072        // layer (a hidden sheet) is stamped on the whole subtree afterwards —
3073        // docling puts the layer on the group *and* on every item under it.
3074        let mark = (
3075            self.texts.len(),
3076            self.tables.len(),
3077            self.pictures.len(),
3078            self.groups.len(),
3079        );
3080        let children: Vec<Value> = self
3081            .walk_into(nodes, &self_ref)
3082            .into_iter()
3083            .map(Child::into_value)
3084            .collect();
3085        let name = name.unwrap_or(if label == "inline" { "group" } else { label });
3086        let content_layer = layer.map_or("body", |l| l.value());
3087        self.groups[group_index(&self_ref)] = json!({
3088            "self_ref": self_ref,
3089            "parent": { "$ref": parent },
3090            "children": children,
3091            "content_layer": content_layer,
3092            "name": name,
3093            "label": label,
3094        });
3095        if layer.is_some() {
3096            let (t, tb, p, g) = mark;
3097            for item in self.texts[t..]
3098                .iter_mut()
3099                .enumerate()
3100                .map(|(k, item)| item.value_mut(t + k))
3101                .chain(self.tables[tb..].iter_mut().map(|t| &mut t.item))
3102                .chain(self.pictures[p..].iter_mut())
3103                .chain(self.groups[g..].iter_mut())
3104            {
3105                if let Some(obj) = item.as_object_mut() {
3106                    obj.insert("content_layer".into(), json!(content_layer));
3107                }
3108            }
3109        }
3110        self_ref
3111    }
3112
3113    /// Walk a slice of sibling nodes, returning each child's `$ref`; runs of
3114    /// list items are folded into list groups (one per sibling list).
3115    fn walk_into(&mut self, nodes: &[Node], parent: &str) -> Vec<Child> {
3116        // Siblings that all carry a creation rank (an XLSX sheet's items) are
3117        // *added* in that order — so `#/tables/N` and friends are numbered as
3118        // docling numbers them — while their refs keep the node order, which
3119        // is docling's position-sorted `children`.
3120        let seqs: Option<Vec<usize>> = nodes
3121            .iter()
3122            .map(|n| match n {
3123                Node::Prov { seq: Some(s), .. } => Some(*s),
3124                _ => None,
3125            })
3126            .collect();
3127        if let Some(seqs) = seqs.filter(|s| !s.is_empty()) {
3128            let mut order: Vec<usize> = (0..nodes.len()).collect();
3129            order.sort_by_key(|&i| seqs[i]);
3130            let mut slots: Vec<Vec<Child>> = (0..nodes.len()).map(|_| Vec::new()).collect();
3131            for i in order {
3132                if let Some(r) = self.add_node(&nodes[i], parent) {
3133                    slots[i].extend(self.pending_siblings.drain(..).map(Child::json));
3134                    slots[i].push(Child::Ref(r.into()));
3135                    slots[i].extend(self.pending_after.drain(..).map(Child::json));
3136                }
3137                if parent == "#/body" {
3138                    slots[i].extend(self.pending_body.drain(..).map(Child::json));
3139                }
3140            }
3141            return slots.into_iter().flatten().collect();
3142        }
3143        let mut children = Vec::new();
3144        let mut i = 0;
3145        while i < nodes.len() {
3146            if matches!(nodes[i], Node::ListItem { .. }) {
3147                let start = i;
3148                i += 1;
3149                loop {
3150                    match nodes.get(i) {
3151                        Some(Node::ListItem { .. }) => i += 1,
3152                        // Absorb an empty paragraph sitting between two list
3153                        // items (docling keeps the ListGroup contiguous).
3154                        Some(Node::Paragraph { text })
3155                            if text.is_empty()
3156                                && matches!(nodes.get(i + 1), Some(Node::ListItem { .. })) =>
3157                        {
3158                            i += 1
3159                        }
3160                        _ => break,
3161                    }
3162                }
3163                let mut lists = Vec::new();
3164                self.add_sibling_lists(&nodes[start..i], parent, &mut lists);
3165                children.extend(lists.into_iter().map(Child::json));
3166            } else {
3167                if let Some(r) = self.add_node(&nodes[i], parent) {
3168                    children.extend(self.pending_siblings.drain(..).map(Child::json));
3169                    children.push(Child::Ref(r.into()));
3170                    children.extend(self.pending_after.drain(..).map(Child::json));
3171                }
3172                i += 1;
3173            }
3174            // Body-parented captions of items deeper in the tree follow the
3175            // top-level item they were created under (#390).
3176            if parent == "#/body" {
3177                children.extend(self.pending_body.drain(..).map(Child::json));
3178            }
3179        }
3180        children
3181    }
3182
3183    /// A run of list items may hold several *sibling* lists; emit one list group
3184    /// per sibling. The boundary is the backend's `first_in_list` flag on a
3185    /// base-level item — the same rule as the Markdown serializer's blank line
3186    /// (#385; the kind-flip and number-gap guesses are gone).
3187    fn add_sibling_lists(&mut self, run: &[Node], parent: &str, out: &mut Vec<Value>) {
3188        let base = level_of(&run[0]);
3189        let mut seg = 0;
3190        for k in 0..run.len() {
3191            let Node::ListItem {
3192                first_in_list,
3193                level,
3194                ..
3195            } = &run[k]
3196            else {
3197                continue;
3198            };
3199            if *level != base {
3200                continue; // nested item — handled inside add_list
3201            }
3202            if k > seg && *first_in_list {
3203                out.push(ref_value(self.add_list(&run[seg..k], parent)));
3204                seg = k;
3205            }
3206        }
3207        out.push(ref_value(self.add_list(&run[seg..], parent)));
3208    }
3209}
3210
3211fn level_of(node: &Node) -> u8 {
3212    match node {
3213        Node::ListItem { level, .. } => *level,
3214        _ => 0,
3215    }
3216}
3217
3218fn group_index(self_ref: &str) -> usize {
3219    self_ref.rsplit('/').next().unwrap().parse().unwrap()
3220}
3221
3222fn ref_index(self_ref: &str) -> Option<usize> {
3223    self_ref.rsplit('/').next()?.parse().ok()
3224}
3225
3226/// Merge the key/values of `extra` (an object) into `target` (an object).
3227fn merge(target: &mut Value, extra: Value) {
3228    if let (Some(t), Value::Object(e)) = (target.as_object_mut(), extra) {
3229        t.extend(e);
3230    }
3231}
3232
3233/// A JSON object of exactly these entries, in this order. The hot per-item
3234/// constructors use this instead of `json!`: the macro grows its map by
3235/// doubling (an 8-key item kept 14 slots) and deep-copies every interpolated
3236/// `Value` through `to_value`. On a long text-heavy PDF those item objects
3237/// *are* the export's footprint (the same reasoning as [`cell_value`]).
3238fn object<const N: usize>(entries: [(&str, Value); N]) -> Value {
3239    let mut m = serde_json::Map::with_capacity(N);
3240    for (k, v) in entries {
3241        m.insert(k.to_owned(), v);
3242    }
3243    Value::Object(m)
3244}
3245
3246/// A `{"$ref": r}` reference object.
3247fn ref_value(r: impl Into<String>) -> Value {
3248    object([("$ref", Value::String(r.into()))])
3249}
3250
3251/// Reverse [`crate`]'s Markdown text escaping (HTML entities + `\_`).
3252fn unescape_text(s: &str) -> String {
3253    s.replace("&lt;", "<")
3254        .replace("&gt;", ">")
3255        .replace("&amp;", "&")
3256        .replace("\\_", "_")
3257}
3258
3259/// 64-bit FNV-1a, a stand-in for docling's `binary_hash` (we lack the source bytes
3260/// at export time; the value only needs to be a stable u64).
3261fn fnv1a(s: &str) -> u64 {
3262    let mut h: u64 = 0xcbf29ce484222325;
3263    for b in s.bytes() {
3264        h ^= b as u64;
3265        h = h.wrapping_mul(0x100000001b3);
3266    }
3267    h
3268}
3269
3270#[cfg(test)]
3271mod tests {
3272    #[cfg(feature = "preserve-order")]
3273    use crate::ImageMode;
3274    #[cfg(feature = "preserve-order")]
3275    use crate::PictureImage;
3276    use crate::{CaptionParent, ContentLayer, DoclingDocument, Node, Table};
3277    use serde_json::Value;
3278
3279    fn cell_at(text: &str, row: usize, col: usize, col_span: usize) -> crate::TableCell {
3280        crate::TableCell {
3281            text: text.into(),
3282            bbox: None,
3283            start_row: row,
3284            start_col: col,
3285            row_span: 1,
3286            col_span,
3287            column_header: row == 0,
3288            row_header: false,
3289            row_section: false,
3290        }
3291    }
3292
3293    /// First-class cells past the rows: rectangular rows are the grid and
3294    /// clip them (docling's `TableData.grid` on USPTO's overrun replicas);
3295    /// ragged rows — an xlsx table compacted by `skip_empty_cells` (#271) —
3296    /// are no grid, so the cells' extent sizes it and the omitted positions
3297    /// come back as empty filler cells in `grid` only.
3298    #[test]
3299    fn ragged_rows_take_their_grid_from_first_class_cells() {
3300        let cells = vec![
3301            cell_at("h0", 0, 0, 1),
3302            cell_at("h2", 0, 2, 1),
3303            cell_at("wide", 1, 0, 3),
3304        ];
3305        let export = |rows: Vec<Vec<String>>| -> Value {
3306            let mut doc = DoclingDocument::new("t");
3307            doc.push(Node::Table(Table {
3308                rows,
3309                cells: Some(cells.clone()),
3310                ..Table::default()
3311            }));
3312            let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3313            v["tables"][0]["data"].clone()
3314        };
3315        // Compacted rows: 2 and 3 wide, the cells say the grid is 3 wide.
3316        let data = export(vec![
3317            vec!["h0".into(), "h2".into()],
3318            vec!["wide".into(), "wide".into(), "wide".into()],
3319        ]);
3320        assert_eq!(data["num_rows"], 2);
3321        assert_eq!(data["num_cols"], 3);
3322        assert_eq!(data["table_cells"].as_array().unwrap().len(), 3);
3323        let row0: Vec<&str> = data["grid"][0]
3324            .as_array()
3325            .unwrap()
3326            .iter()
3327            .map(|c| c["text"].as_str().unwrap())
3328            .collect();
3329        assert_eq!(row0, ["h0", "", "h2"]);
3330        assert_eq!(data["table_cells"][2]["col_span"], 3);
3331        // Rectangular rows narrower than the cells: the rows win, the cells
3332        // are clipped to them exactly as before.
3333        let data = export(vec![
3334            vec!["h0".into(), "h2".into()],
3335            vec!["wide".into(), "wide".into()],
3336        ]);
3337        assert_eq!(data["num_cols"], 2);
3338        assert_eq!(data["grid"][0].as_array().unwrap().len(), 2);
3339    }
3340
3341    #[cfg(feature = "preserve-order")]
3342    fn doc_with_image() -> DoclingDocument {
3343        let mut doc = DoclingDocument::new("t");
3344        doc.push(Node::Picture {
3345            caption: Some("Fig 1".into()),
3346            caption_href: None,
3347            image: Some(PictureImage {
3348                dpi: crate::PictureImage::DEFAULT_DPI,
3349                mimetype: "image/png".into(),
3350                width: 4,
3351                height: 2,
3352                data: b"foobar".to_vec(),
3353                display_size: None,
3354            }),
3355            classification: None,
3356            description: None,
3357            caption_parent: Default::default(),
3358            caption_location: None,
3359        });
3360        doc
3361    }
3362
3363    /// #402: a deck's speaker notes are content, and docling puts them in the
3364    /// JSON on the `notes` layer so a consumer reading only JSON can pick them
3365    /// out. Markdown still serializes the body layer alone, and page furniture
3366    /// stays out of the JSON, where docling does keep it.
3367    #[test]
3368    fn notes_layer_items_reach_the_json_but_furniture_does_not() {
3369        let mut doc = DoclingDocument::new("t");
3370        doc.push(Node::Heading {
3371            level: 1,
3372            text: "Slide One".into(),
3373        });
3374        doc.push(Node::Furniture {
3375            layer: ContentLayer::Notes,
3376            inner: Box::new(Node::Located {
3377                location: [0, 0, 0, 0],
3378                inner: Box::new(Node::Paragraph {
3379                    text: "Speaker note for slide 1.".into(),
3380                }),
3381            }),
3382        });
3383        doc.push(Node::Furniture {
3384            layer: ContentLayer::Furniture,
3385            inner: Box::new(Node::Paragraph {
3386                text: "page header".into(),
3387            }),
3388        });
3389
3390        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3391        let texts = v["texts"].as_array().unwrap();
3392        assert_eq!(
3393            texts
3394                .iter()
3395                .map(|t| (
3396                    t["label"].as_str().unwrap(),
3397                    t["content_layer"].as_str().unwrap(),
3398                    t["text"].as_str().unwrap()
3399                ))
3400                .collect::<Vec<_>>(),
3401            vec![
3402                ("title", "body", "Slide One"),
3403                ("text", "notes", "Speaker note for slide 1."),
3404            ],
3405            "the note is carried on its own layer; the furniture is not carried"
3406        );
3407        // The body layer is what Markdown serializes, so it does not change.
3408        assert_eq!(doc.export_to_markdown(), "# Slide One\n");
3409    }
3410
3411    /// #410: a backend that describes merged ranges only as continuation
3412    /// flags (xlsx `<mergeCell>`, docx `gridSpan`/`vMerge`) gets docling's
3413    /// one-`TableCell`-per-range JSON: the anchor's offsets and spans, the
3414    /// entry repeated across the grid positions it covers — not a 1×1 cell
3415    /// per position with the text copied into each.
3416    #[test]
3417    fn continuation_flags_become_spanning_cells() {
3418        let mut doc = DoclingDocument::new("t");
3419        // A1:C2 merged ("merged"), then a plain row underneath.
3420        let rows = vec![
3421            vec!["merged".to_string(), "merged".into(), "merged".into()],
3422            vec!["merged".to_string(), "merged".into(), "merged".into()],
3423            vec!["a".to_string(), "b".into(), "c".into()],
3424        ];
3425        doc.push(Node::Table(crate::Table {
3426            footnotes: Vec::new(),
3427            rows,
3428            location: None,
3429            structure: Some(crate::TableStructure {
3430                header_row: vec![true, false, false],
3431                col_continuation: vec![
3432                    vec![false, true, true],
3433                    vec![false, true, true],
3434                    vec![false, false, false],
3435                ],
3436                row_continuation: vec![
3437                    vec![false, false, false],
3438                    vec![true, true, true],
3439                    vec![false, false, false],
3440                ],
3441                row_header: Vec::new(),
3442                col_header: Vec::new(),
3443            }),
3444            cell_blocks: None,
3445            cells: None,
3446            caption: None,
3447            caption_parent: Default::default(),
3448            caption_location: None,
3449            document_index: false,
3450        }));
3451        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3452        let data = &v["tables"][0]["data"];
3453        assert_eq!(data["num_rows"], 3);
3454        assert_eq!(data["num_cols"], 3);
3455        let cells = data["table_cells"].as_array().unwrap();
3456        assert_eq!(
3457            cells.len(),
3458            4,
3459            "one cell for the range, three for the plain row"
3460        );
3461        assert_eq!(
3462            cells[0],
3463            serde_json::json!({
3464                "row_span": 2, "col_span": 3,
3465                "start_row_offset_idx": 0, "end_row_offset_idx": 2,
3466                "start_col_offset_idx": 0, "end_col_offset_idx": 3,
3467                "text": "merged", "column_header": true, "row_header": false,
3468                "row_section": false, "fillable": false,
3469            })
3470        );
3471        assert_eq!(cells[1]["text"], "a");
3472        assert_eq!(cells[1]["row_span"], 1);
3473        assert_eq!(cells[1]["column_header"], false);
3474        // The grid repeats the range's entry at every position it covers.
3475        let grid = data["grid"].as_array().unwrap();
3476        assert_eq!(grid.len(), 3);
3477        for (r, row) in grid.iter().take(2).enumerate() {
3478            for (c, cell) in row.as_array().unwrap().iter().enumerate() {
3479                assert_eq!(*cell, cells[0], "grid[{r}][{c}]");
3480            }
3481        }
3482        assert_eq!(grid[2][2]["text"], "c");
3483    }
3484
3485    /// A [`Node::Prov`] wrapper is docling's provenance verbatim — the exact
3486    /// box in a top-left origin, the backend's charspan — and the page marker
3487    /// before it sizes the page; a chart's caption becomes a sibling of the
3488    /// picture in the container, listed first, sharing the chart's box with
3489    /// a charspan over its text. That is the JSON shape of an XLSX sheet.
3490    #[test]
3491    fn exact_provenance_pages_and_chart_captions_follow_docling() {
3492        let mut doc = DoclingDocument::new("t");
3493        doc.push(Node::PageInfo {
3494            page_no: 1,
3495            width: 3.0,
3496            height: 4.0,
3497        });
3498        let table = crate::Table {
3499            rows: vec![vec!["a".to_string(), "b".into()]],
3500            ..Default::default()
3501        };
3502        doc.push(Node::Group {
3503            label: "sheet".into(),
3504            name: Some("Data".into()),
3505            layer: None,
3506            children: vec![
3507                // Node order is the position-sorted one; creation order (the
3508                // `seq`) had the chart first — so the chart is `#/pictures/0`
3509                // *and* its caption `#/texts/0`, while the table stays the
3510                // group's first child.
3511                Node::Prov {
3512                    page_no: 1,
3513                    bbox: [0.0, 0.0, 3.0, 4.0],
3514                    charspan: [0, 0],
3515                    bottom_left: false,
3516                    seq: Some(1),
3517                    inner: Box::new(Node::Table(table.clone())),
3518                },
3519                Node::Prov {
3520                    page_no: 1,
3521                    bbox: [0.0, 1.0, 1.0, 1.0],
3522                    charspan: [0, 0],
3523                    bottom_left: false,
3524                    seq: Some(0),
3525                    inner: Box::new(Node::Chart {
3526                        kind: "bar_chart".into(),
3527                        table,
3528                        caption: Some("Sales".into()),
3529                        location: Some([0, 128, 170, 128]),
3530                    }),
3531                },
3532            ],
3533        });
3534        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3535        assert_eq!(
3536            v["pages"],
3537            serde_json::json!({"1": {"size": {"width": 3.0, "height": 4.0}, "page_no": 1}})
3538        );
3539        assert_eq!(
3540            v["tables"][0]["prov"],
3541            serde_json::json!([{
3542                "page_no": 1,
3543                "bbox": {"l": 0.0, "t": 0.0, "r": 3.0, "b": 4.0, "coord_origin": "TOPLEFT"},
3544                "charspan": [0, 0],
3545            }])
3546        );
3547        assert_eq!(v["tables"][0]["data"]["orientation"], "rot_0");
3548        // The caption is the group's child *before* the picture, parented to
3549        // the group, and referenced by the picture.
3550        let sheet = &v["groups"][0];
3551        assert_eq!(
3552            sheet["children"],
3553            serde_json::json!([
3554                {"$ref": "#/tables/0"}, {"$ref": "#/texts/0"}, {"$ref": "#/pictures/0"}
3555            ])
3556        );
3557        let cap = &v["texts"][0];
3558        assert_eq!(cap["label"], "caption");
3559        assert_eq!(cap["parent"], serde_json::json!({"$ref": "#/groups/0"}));
3560        assert_eq!(cap["prov"][0]["charspan"], serde_json::json!([0, 5]));
3561        assert_eq!(cap["prov"][0]["bbox"]["b"], 1.0);
3562        let pic = &v["pictures"][0];
3563        assert_eq!(pic["captions"], serde_json::json!([{"$ref": "#/texts/0"}]));
3564        assert_eq!(pic["prov"][0]["charspan"], serde_json::json!([0, 0]));
3565        assert_eq!(pic["prov"][0]["bbox"]["coord_origin"], "TOPLEFT");
3566        assert_eq!(
3567            pic["meta"]["classification"]["predictions"][0]["class_name"],
3568            "bar_chart"
3569        );
3570        assert_eq!(pic["meta"]["tabular_chart"]["chart_data"]["num_cols"], 2);
3571    }
3572
3573    /// An all-zero location is the "no geometry" sentinel — a slide's speaker
3574    /// notes carry one — and docling writes it as a zero bbox, not as a box
3575    /// spanning the whole page, which is what denormalizing the grid gives.
3576    #[test]
3577    fn a_zero_location_is_a_zero_bbox_not_the_whole_page() {
3578        let mut doc = DoclingDocument::new("t");
3579        doc.push(Node::PageInfo {
3580            page_no: 1,
3581            width: 12192000.0,
3582            height: 6858000.0,
3583        });
3584        doc.push(Node::Furniture {
3585            layer: ContentLayer::Notes,
3586            inner: Box::new(Node::Located {
3587                location: [0, 0, 0, 0],
3588                inner: Box::new(Node::Paragraph {
3589                    text: "a note".into(),
3590                }),
3591            }),
3592        });
3593        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3594        let prov = &v["texts"][0]["prov"][0];
3595        assert_eq!(prov["page_no"], 1);
3596        assert_eq!(prov["charspan"], serde_json::json!([0, 6]));
3597        assert_eq!(
3598            prov["bbox"],
3599            serde_json::json!({"l": 0.0, "t": 0.0, "r": 0.0, "b": 0.0, "coord_origin": "BOTTOMLEFT"})
3600        );
3601        // The page itself is recorded at its true size.
3602        assert_eq!(
3603            v["pages"]["1"]["size"],
3604            serde_json::json!({"width": 12192000.0, "height": 6858000.0})
3605        );
3606    }
3607
3608    /// #171: PageInfo markers become the `pages` map, and `Located` wrappers /
3609    /// node-level locations become per-item `prov` — the 0–511 grid
3610    /// denormalized against the page into BOTTOMLEFT points. Without markers
3611    /// (every declarative backend) the JSON stays exactly as before: empty
3612    /// `pages`, `prov: []` even for located nodes.
3613    /// #609: a PDF caption and a checkbox item carry their own page and box.
3614    /// docling's reading-order model gives every caption the cluster it came
3615    /// from (`_add_caption_or_footnote`), not its picture's or table's, and
3616    /// a checkbox is a text item like any other.
3617    #[test]
3618    fn captions_and_checkboxes_carry_their_own_prov() {
3619        let mut doc = DoclingDocument::new("t");
3620        doc.push(Node::PageInfo {
3621            page_no: 1,
3622            width: 512.0,
3623            height: 512.0,
3624        });
3625        doc.push(Node::Located {
3626            location: [64, 64, 448, 256],
3627            inner: Box::new(Node::Picture {
3628                caption: Some("Figure 1".into()),
3629                caption_href: None,
3630                image: None,
3631                classification: None,
3632                description: None,
3633                caption_parent: CaptionParent::Item,
3634                caption_location: Some([64, 264, 448, 280]),
3635            }),
3636        });
3637        doc.push(Node::PageInfo {
3638            page_no: 2,
3639            width: 512.0,
3640            height: 512.0,
3641        });
3642        doc.push(Node::Located {
3643            location: [64, 120, 448, 300],
3644            inner: Box::new(Node::Table(Table {
3645                rows: vec![vec!["a".into()]],
3646                caption: Some("Table 1".into()),
3647                caption_parent: CaptionParent::Item,
3648                caption_location: Some([64, 100, 300, 112]),
3649                ..Table::default()
3650            })),
3651        });
3652        doc.push(Node::Located {
3653            location: [64, 320, 200, 332],
3654            inner: Box::new(Node::CheckboxItem {
3655                checked: false,
3656                text: "First option".into(),
3657            }),
3658        });
3659        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3660        let texts = v["texts"].as_array().unwrap();
3661        let by_text = |t: &str| texts.iter().find(|x| x["text"] == t).expect(t).clone();
3662        let bbox = |p: &Value| {
3663            let b = &p["prov"][0]["bbox"];
3664            [&b["l"], &b["t"], &b["r"], &b["b"]].map(|x| x.as_f64().unwrap())
3665        };
3666        // Figure 1: its own box (grid == points on a 512 page, flipped to
3667        // BOTTOMLEFT), not the picture's.
3668        let fig = by_text("Figure 1");
3669        assert_eq!(fig["prov"][0]["page_no"], 1);
3670        assert_eq!(bbox(&fig), [64.0, 248.0, 448.0, 232.0]);
3671        assert_eq!(fig["prov"][0]["charspan"], serde_json::json!([0, 8]));
3672        assert_eq!(fig["parent"]["$ref"], "#/pictures/0");
3673        assert_eq!(v["pictures"][0]["prov"][0]["bbox"]["t"], 448.0);
3674        // Table 1 on page 2, its own box; the table keeps its own.
3675        let tab = by_text("Table 1");
3676        assert_eq!(tab["prov"][0]["page_no"], 2);
3677        assert_eq!(bbox(&tab), [64.0, 412.0, 300.0, 400.0]);
3678        assert_eq!(v["tables"][0]["prov"][0]["bbox"]["t"], 392.0);
3679        // The checkbox item.
3680        let cb = by_text("First option");
3681        assert_eq!(cb["label"], "checkbox_unselected");
3682        assert_eq!(cb["prov"][0]["page_no"], 2);
3683        assert_eq!(bbox(&cb), [64.0, 192.0, 200.0, 180.0]);
3684        assert!(texts
3685            .iter()
3686            .all(|t| !t["prov"].as_array().unwrap().is_empty()));
3687
3688        // Without a caption box (every declarative backend) the caption
3689        // still has no prov, and nothing else moves.
3690        let mut plain = DoclingDocument::new("t");
3691        plain.push(Node::PageInfo {
3692            page_no: 1,
3693            width: 512.0,
3694            height: 512.0,
3695        });
3696        plain.push(Node::Located {
3697            location: [64, 64, 448, 256],
3698            inner: Box::new(Node::Picture {
3699                caption: Some("Figure 1".into()),
3700                caption_href: None,
3701                image: None,
3702                classification: None,
3703                description: None,
3704                caption_parent: CaptionParent::Item,
3705                caption_location: None,
3706            }),
3707        });
3708        let v: Value = serde_json::from_str(&plain.export_to_json()).unwrap();
3709        assert_eq!(v["texts"][0]["prov"], serde_json::json!([]));
3710        assert_eq!(v["pictures"][0]["prov"][0]["bbox"]["t"], 448.0);
3711    }
3712
3713    #[test]
3714    fn page_markers_produce_pages_and_prov() {
3715        let mut doc = DoclingDocument::new("t");
3716        doc.push(Node::PageInfo {
3717            page_no: 1,
3718            width: 512.0,
3719            height: 1024.0,
3720        });
3721        doc.push(Node::Located {
3722            location: [128, 64, 256, 128], // quarter/eighth points of the grid
3723            inner: Box::new(Node::Paragraph {
3724                text: "hello".into(),
3725            }),
3726        });
3727        doc.push(Node::Table(Table {
3728            rows: vec![vec!["a".into()]],
3729            location: Some([0, 0, 512, 512]),
3730            ..Table::default()
3731        }));
3732        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3733        assert_eq!(v["pages"]["1"]["page_no"], 1);
3734        assert_eq!(v["pages"]["1"]["size"]["width"], 512.0);
3735        assert_eq!(v["pages"]["1"]["size"]["height"], 1024.0);
3736        // 512-wide page: grid x scales 1:1; 1024-high: grid y doubles, then
3737        // flips to the BOTTOMLEFT origin (t from grid-top 64 → 1024-128=896).
3738        let prov = &v["texts"][0]["prov"][0];
3739        assert_eq!(prov["page_no"], 1);
3740        assert_eq!(prov["bbox"]["l"], 128.0);
3741        assert_eq!(prov["bbox"]["t"], 896.0);
3742        assert_eq!(prov["bbox"]["r"], 256.0);
3743        assert_eq!(prov["bbox"]["b"], 768.0);
3744        assert_eq!(prov["bbox"]["coord_origin"], "BOTTOMLEFT");
3745        assert_eq!(prov["charspan"][1], 5);
3746        // The table adopts its own location field; charspan is [0, 0].
3747        let tprov = &v["tables"][0]["prov"][0];
3748        assert_eq!(tprov["bbox"]["t"], 1024.0);
3749        assert_eq!(tprov["bbox"]["b"], 0.0);
3750        assert_eq!(tprov["charspan"][1], 0);
3751
3752        // No markers → the pre-#171 shape, byte for byte.
3753        let mut plain = DoclingDocument::new("t");
3754        plain.push(Node::Located {
3755            location: [1, 2, 3, 4],
3756            inner: Box::new(Node::Paragraph { text: "x".into() }),
3757        });
3758        let v: Value = serde_json::from_str(&plain.export_to_json()).unwrap();
3759        assert_eq!(v["pages"], serde_json::json!({}));
3760        assert_eq!(v["texts"][0]["prov"], serde_json::json!([]));
3761    }
3762
3763    /// PDF page headers/footers reach the JSON as docling writes them:
3764    /// body-parented text items on the furniture layer, with their box.
3765    #[test]
3766    fn page_furniture_becomes_furniture_layer_text() {
3767        let mut doc = DoclingDocument::new("t");
3768        doc.push(Node::PageInfo {
3769            page_no: 1,
3770            width: 512.0,
3771            height: 512.0,
3772        });
3773        doc.push(Node::PageFurniture {
3774            footer: false,
3775            location: [10, 0, 100, 20],
3776            text: "Chapter 1".into(),
3777        });
3778        doc.push(Node::Paragraph {
3779            text: "body".into(),
3780        });
3781        doc.push(Node::PageFurniture {
3782            footer: true,
3783            location: [400, 490, 500, 512],
3784            text: "1.10.2".into(),
3785        });
3786        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3787        let texts = v["texts"].as_array().unwrap();
3788        assert_eq!(texts.len(), 3);
3789        assert_eq!(texts[0]["label"], "page_header");
3790        assert_eq!(texts[0]["content_layer"], "furniture");
3791        assert_eq!(texts[0]["parent"]["$ref"], "#/body");
3792        assert_eq!(texts[0]["prov"][0]["bbox"]["l"], 10.0);
3793        assert_eq!(texts[1]["content_layer"], "body");
3794        assert_eq!(texts[1]["prov"], serde_json::json!([]));
3795        assert_eq!(texts[2]["label"], "page_footer");
3796        assert_eq!(texts[2]["text"], "1.10.2");
3797        assert_eq!(texts[2]["content_layer"], "furniture");
3798    }
3799
3800    #[test]
3801    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
3802    fn picture_image_in_markdown_modes_and_json() {
3803        let doc = doc_with_image();
3804        // placeholder (default) ignores the image
3805        assert!(doc.export_to_markdown().contains("<!-- image -->"));
3806        // embedded → base64 data URI (b"foobar" → "Zm9vYmFy")
3807        let (md, files) = doc.export_to_markdown_with_images(ImageMode::Embedded, "artifacts");
3808        assert!(
3809            md.contains("![Image](data:image/png;base64,Zm9vYmFy)"),
3810            "got:\n{md}"
3811        );
3812        assert!(files.is_empty());
3813        // referenced → file link + collected bytes
3814        let (md, files) = doc.export_to_markdown_with_images(ImageMode::Referenced, "artifacts");
3815        assert!(
3816            md.contains("![Image](artifacts/image_000000.png)"),
3817            "got:\n{md}"
3818        );
3819        assert_eq!(
3820            files,
3821            vec![("artifacts/image_000000.png".to_string(), b"foobar".to_vec())]
3822        );
3823        // JSON carries the ImageRef (data URI + size — floats, as docling's
3824        // `Size` is — placed before `annotations`).
3825        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3826        assert_eq!(v["pictures"][0]["image"]["mimetype"], "image/png");
3827        assert_eq!(v["pictures"][0]["image"]["size"]["width"], 4.0);
3828        let keys: Vec<&str> = v["pictures"][0]
3829            .as_object()
3830            .unwrap()
3831            .keys()
3832            .map(String::as_str)
3833            .collect();
3834        assert_eq!(&keys[keys.len() - 2..], ["image", "annotations"]);
3835        assert_eq!(
3836            v["pictures"][0]["image"]["uri"],
3837            "data:image/png;base64,Zm9vYmFy"
3838        );
3839    }
3840
3841    #[test]
3842    fn exports_docling_schema() {
3843        let mut doc = DoclingDocument::new("t");
3844        doc.push(Node::Heading {
3845            level: 1,
3846            text: "Title".into(),
3847        });
3848        doc.push(Node::Heading {
3849            level: 2,
3850            text: "Sec".into(),
3851        });
3852        doc.push(Node::Paragraph {
3853            text: "Body &amp; more".into(),
3854        }); // markdown-escaped
3855        doc.push(Node::ListItem {
3856            ordered: false,
3857            number: 0,
3858            first_in_list: true,
3859            text: "one".into(),
3860            level: 0,
3861            marker: None,
3862            location: None,
3863            split: None,
3864            dclx: None,
3865            href: None,
3866            layer: None,
3867        });
3868        doc.push(Node::ListItem {
3869            ordered: false,
3870            number: 0,
3871            first_in_list: false,
3872            text: "two".into(),
3873            level: 0,
3874            marker: None,
3875            location: None,
3876            split: None,
3877            dclx: None,
3878            href: None,
3879            layer: None,
3880        });
3881        doc.push(Node::Table(Table {
3882            footnotes: Vec::new(),
3883            rows: vec![vec!["A".into(), "B".into()]],
3884            location: None,
3885            structure: None,
3886            cell_blocks: None,
3887            cells: None,
3888            caption: None,
3889            caption_parent: Default::default(),
3890            caption_location: None,
3891            document_index: false,
3892        }));
3893
3894        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3895        assert_eq!(v["schema_name"], "DoclingDocument");
3896        assert_eq!(v["version"], "1.11.0");
3897        assert_eq!(v["texts"][0]["label"], "title");
3898        assert_eq!(v["texts"][1]["label"], "section_header");
3899        assert_eq!(v["texts"][1]["level"], 1); // heading level 2 → docling level 1
3900        assert_eq!(v["texts"][2]["text"], "Body & more"); // un-escaped for the wire format
3901                                                          // consecutive list items fold into one list group, parented to it
3902        assert_eq!(v["groups"][0]["label"], "list");
3903        assert_eq!(v["groups"][0]["children"].as_array().unwrap().len(), 2);
3904        assert_eq!(v["texts"][3]["parent"]["$ref"], "#/groups/0");
3905        assert_eq!(v["texts"][3]["marker"], "-");
3906        // table grid + header flag
3907        assert_eq!(v["tables"][0]["data"]["num_cols"], 2);
3908        assert_eq!(v["tables"][0]["data"]["grid"][0][0]["column_header"], true);
3909    }
3910    /// A named group on a non-body layer — docling's hidden spreadsheet sheet:
3911    /// the group carries the sheet's name and the `invisible` layer, and every
3912    /// item inside it carries the layer too.
3913    #[test]
3914    fn a_layered_group_stamps_its_whole_subtree() {
3915        let doc = DoclingDocument {
3916            name: "s".into(),
3917            nodes: vec![
3918                Node::Group {
3919                    label: "sheet".into(),
3920                    name: Some("Sheet1".into()),
3921                    layer: None,
3922                    children: vec![Node::Paragraph {
3923                        text: "visible".into(),
3924                    }],
3925                },
3926                Node::Group {
3927                    label: "sheet".into(),
3928                    name: Some("Sheet2".into()),
3929                    layer: Some(ContentLayer::Invisible),
3930                    children: vec![Node::Paragraph {
3931                        text: "hidden".into(),
3932                    }],
3933                },
3934            ],
3935            ..DoclingDocument::new("s")
3936        };
3937        let v = crate::json::to_json(&doc);
3938        assert_eq!(v["groups"][0]["label"], "sheet");
3939        assert_eq!(v["groups"][0]["name"], "Sheet1");
3940        assert_eq!(v["groups"][0]["content_layer"], "body");
3941        assert_eq!(v["texts"][0]["content_layer"], "body");
3942        assert_eq!(v["groups"][1]["name"], "Sheet2");
3943        assert_eq!(v["groups"][1]["content_layer"], "invisible");
3944        assert_eq!(v["texts"][1]["content_layer"], "invisible");
3945        // The group's children are the items, and the body holds the groups.
3946        assert_eq!(v["groups"][1]["children"][0]["$ref"], "#/texts/1");
3947        assert_eq!(v["body"]["children"][1]["$ref"], "#/groups/1");
3948    }
3949
3950    /// A backend-built item tree is serialized as it is: items numbered in
3951    /// creation order per bucket (a field region's part texts included), the
3952    /// tree's parents / children / layers, docling's field order for
3953    /// `formatting`, `hyperlink`, `level`, `enumerated`/`marker`, a rich
3954    /// cell's `ref` on `table_cells` only, raw cell text.
3955    /// The DOCX tree's extras: an item `delete`d (docling's `delete_items`,
3956    /// the spacer between two items of a resumed list) is neither written nor
3957    /// numbered, `comments` back-refs sit between `prov` and `orig`, and a
3958    /// chart picture carries `classification` plus `tabular_chart`.
3959    #[test]
3960    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
3961    fn deleted_items_comment_refs_and_chart_meta_in_the_tree() {
3962        use crate::tree::{ItemTree, TreeKind};
3963        let mut t = ItemTree::default();
3964        let text = |txt: &str| TreeKind::Text {
3965            label: "text".into(),
3966            text: txt.into(),
3967            orig: None,
3968            formatting: None,
3969            hyperlink: None,
3970            level: None,
3971            list: None,
3972        };
3973        let a = t.add(None, None, text("a"));
3974        let blank = t.add(None, None, text(""));
3975        let b = t.add(None, None, text("b"));
3976        t.delete(blank);
3977        let group = t.add(
3978            None,
3979            Some(ContentLayer::Notes),
3980            TreeKind::Group {
3981                label: "comment_section".into(),
3982                name: "comment-0".into(),
3983            },
3984        );
3985        t.add(Some(group), Some(ContentLayer::Notes), text("note"));
3986        t.items[a].comments.push(group);
3987        t.add(
3988            None,
3989            None,
3990            TreeKind::Picture {
3991                captions: Vec::new(),
3992                image: None,
3993                extra_classes: Vec::new(),
3994                classification: Some("bar_chart".into()),
3995                description: None,
3996                confidence: None,
3997                chart: Some(Table {
3998                    rows: vec![vec!["".into(), "s".into()], vec!["c".into(), "1".into()]],
3999                    ..Table::default()
4000                }),
4001                dpi: None,
4002            },
4003        );
4004        assert_eq!(t.last_text(), Some(4), "the note; the blank is skipped");
4005        assert_eq!(t.bucket_index(b), 1, "numbered past the deleted item");
4006        let mut doc = DoclingDocument::new("t");
4007        doc.tree = Some(t);
4008        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4009        let texts = v["texts"].as_array().unwrap();
4010        assert_eq!(texts.len(), 3);
4011        assert_eq!(texts[1]["text"], "b");
4012        assert_eq!(texts[1]["self_ref"], "#/texts/1");
4013        assert_eq!(
4014            v["body"]["children"],
4015            serde_json::json!([{"$ref": "#/texts/0"}, {"$ref": "#/texts/1"}, {"$ref": "#/groups/0"}, {"$ref": "#/pictures/0"}])
4016        );
4017        let keys: Vec<&str> = texts[0]
4018            .as_object()
4019            .unwrap()
4020            .keys()
4021            .map(String::as_str)
4022            .collect();
4023        assert_eq!(
4024            keys,
4025            vec![
4026                "self_ref",
4027                "parent",
4028                "children",
4029                "content_layer",
4030                "label",
4031                "prov",
4032                "comments",
4033                "orig",
4034                "text"
4035            ]
4036        );
4037        assert_eq!(
4038            texts[0]["comments"],
4039            serde_json::json!([{"$ref": "#/groups/0"}])
4040        );
4041        assert!(texts[1].get("comments").is_none());
4042        let meta = &v["pictures"][0]["meta"];
4043        assert_eq!(
4044            meta["classification"]["predictions"][0]["class_name"],
4045            "bar_chart"
4046        );
4047        assert_eq!(meta["tabular_chart"]["chart_data"]["num_rows"], 2);
4048    }
4049
4050    /// A tree item's `TreeProv` is written verbatim — a raw EMU box with
4051    /// whatever origin tag the backend set (`BOTTOMLEFT` here) and a per-item
4052    /// charspan, a note's zero `TOPLEFT` box — a picture's `image.dpi` is the file's when the backend
4053    /// read one, an item without provenance writes `prov: []`, and the page
4054    /// map still comes from the flat stream's markers.
4055    #[test]
4056    fn tree_items_carry_exact_provenance_and_dpi() {
4057        use crate::tree::{ItemTree, TreeKind, TreeProv};
4058        let text = |label: &str, t: &str| TreeKind::Text {
4059            label: label.into(),
4060            text: t.into(),
4061            orig: None,
4062            formatting: None,
4063            hyperlink: None,
4064            level: None,
4065            list: None,
4066        };
4067        let mut t = ItemTree::default();
4068        let slide = t.add(
4069            None,
4070            None,
4071            TreeKind::Group {
4072                label: "chapter".into(),
4073                name: "slide-0".into(),
4074            },
4075        );
4076        t.add_with_prov(
4077            Some(slide),
4078            None,
4079            text("paragraph", "héllo"),
4080            TreeProv {
4081                page_no: 1,
4082                bbox: [914400.0, 1828800.0, 2743200.0, 457200.0],
4083                bottom_left: true,
4084                charspan: [0, 5],
4085            },
4086        );
4087        t.add_with_prov(
4088            Some(slide),
4089            None,
4090            TreeKind::Picture {
4091                captions: Vec::new(),
4092                image: Some(crate::PictureImage {
4093                    dpi: crate::PictureImage::DEFAULT_DPI,
4094                    mimetype: "image/png".into(),
4095                    width: 2,
4096                    height: 2,
4097                    data: vec![0],
4098                    display_size: None,
4099                }),
4100                extra_classes: Vec::new(),
4101                classification: None,
4102                description: None,
4103                confidence: None,
4104                chart: None,
4105                dpi: Some(300),
4106            },
4107            TreeProv {
4108                page_no: 1,
4109                bbox: [0.0; 4],
4110                bottom_left: false,
4111                charspan: [0, 0],
4112            },
4113        );
4114        t.add(
4115            Some(slide),
4116            Some(ContentLayer::Notes),
4117            text("text", "no geometry"),
4118        );
4119        let mut doc = DoclingDocument::new("t");
4120        doc.push(Node::PageInfo {
4121            page_no: 1,
4122            width: 9144000.0,
4123            height: 6858000.0,
4124        });
4125        doc.tree = Some(t);
4126        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4127        assert_eq!(
4128            v["texts"][0]["prov"],
4129            serde_json::json!([{
4130                "page_no": 1,
4131                "bbox": { "l": 914400.0, "t": 1828800.0, "r": 2743200.0, "b": 457200.0, "coord_origin": "BOTTOMLEFT" },
4132                "charspan": [0, 5],
4133            }])
4134        );
4135        assert_eq!(v["texts"][0]["label"], "paragraph");
4136        assert_eq!(
4137            v["pictures"][0]["prov"][0]["bbox"]["coord_origin"],
4138            "TOPLEFT"
4139        );
4140        assert_eq!(v["pictures"][0]["image"]["dpi"], 300);
4141        assert_eq!(v["texts"][1]["prov"], serde_json::json!([]));
4142        assert_eq!(v["texts"][1]["content_layer"], "notes");
4143        assert_eq!(v["pages"]["1"]["size"]["width"], 9144000.0);
4144        assert_eq!(v["pages"]["1"]["page_no"], 1);
4145    }
4146
4147    /// docling-core's `validate_document` clamps every provenance box (and a
4148    /// one-page table's cell boxes) into its page — the state every
4149    /// `ConversionResult` leaves a document in, so the state docling's JSON
4150    /// shows. A box on a page the document does not describe is left alone.
4151    #[test]
4152    fn provenance_boxes_are_clamped_to_their_page() {
4153        let mut doc = DoclingDocument::new("t");
4154        doc.push(Node::PageInfo {
4155            page_no: 1,
4156            width: 10.0,
4157            height: 8.0,
4158        });
4159        doc.push(Node::Prov {
4160            page_no: 1,
4161            bbox: [-1.0, 2.0, 12.0, 9.5],
4162            charspan: [0, 1],
4163            bottom_left: false,
4164            seq: None,
4165            inner: Box::new(Node::Paragraph { text: "x".into() }),
4166        });
4167        let mut table = Table {
4168            rows: vec![vec!["a".into()]],
4169            ..Table::default()
4170        };
4171        table.cells = Some(vec![crate::TableCell {
4172            text: "a".into(),
4173            bbox: Some([1.0, 1.0, 11.0, 9.0]),
4174            start_row: 0,
4175            start_col: 0,
4176            row_span: 1,
4177            col_span: 1,
4178            column_header: false,
4179            row_header: false,
4180            row_section: false,
4181        }]);
4182        doc.push(Node::Prov {
4183            page_no: 1,
4184            bbox: [0.0, 0.0, 10.0, 8.0],
4185            charspan: [0, 0],
4186            bottom_left: false,
4187            seq: None,
4188            inner: Box::new(Node::Table(table)),
4189        });
4190        doc.push(Node::Prov {
4191            page_no: 7,
4192            bbox: [-5.0, 0.0, 50.0, 50.0],
4193            charspan: [0, 1],
4194            bottom_left: false,
4195            seq: None,
4196            inner: Box::new(Node::Paragraph { text: "y".into() }),
4197        });
4198        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4199        assert_eq!(
4200            v["texts"][0]["prov"][0]["bbox"],
4201            serde_json::json!({ "l": 0.0, "t": 2.0, "r": 10.0, "b": 8.0, "coord_origin": "TOPLEFT" })
4202        );
4203        let cell = &v["tables"][0]["data"]["table_cells"][0]["bbox"];
4204        assert_eq!(
4205            (cell["l"].as_f64(), cell["r"].as_f64(), cell["b"].as_f64()),
4206            (Some(1.0), Some(10.0), Some(8.0))
4207        );
4208        assert_eq!(v["tables"][0]["data"]["grid"][0][0]["bbox"]["r"], 10.0);
4209        assert_eq!(
4210            v["texts"][1]["prov"][0]["bbox"]["r"], 50.0,
4211            "page 7 is not described"
4212        );
4213    }
4214
4215    /// docling-core's `validate_misplaced_list_items`, as it re-homes list
4216    /// items outside a `list` group (#527) — the refs below are what
4217    /// docling-core 2.99 writes for the same document: the two consecutive
4218    /// items on the body share a group, each item in another group gets its
4219    /// own, the runs are handled last-first (new groups and re-added texts
4220    /// numbered in that order), and a well-placed item is left alone.
4221    #[test]
4222    fn misplaced_list_items_are_wrapped_like_docling_core() {
4223        use crate::tree::{ItemTree, ListMeta, TreeKind};
4224        let mut t = ItemTree::default();
4225        let li = |txt: &str| TreeKind::Text {
4226            label: "list_item".into(),
4227            text: txt.into(),
4228            orig: None,
4229            formatting: None,
4230            hyperlink: None,
4231            level: None,
4232            list: Some(ListMeta {
4233                enumerated: false,
4234                marker: "-".into(),
4235            }),
4236        };
4237        let group = |label: &str, name: &str| TreeKind::Group {
4238            label: label.into(),
4239            name: name.into(),
4240        };
4241        t.add(None, None, li("A"));
4242        t.add(None, None, li("B"));
4243        t.add(
4244            None,
4245            None,
4246            TreeKind::Text {
4247                label: "text".into(),
4248                text: "plain".into(),
4249                orig: None,
4250                formatting: None,
4251                hyperlink: None,
4252                level: None,
4253                list: None,
4254            },
4255        );
4256        let cell = t.add(None, None, group("unspecified", "cell"));
4257        t.add(Some(cell), None, li("C"));
4258        t.add(Some(cell), None, li("D"));
4259        let list = t.add(None, None, group("list", "list"));
4260        t.add(Some(list), None, li("E"));
4261        let doc = DoclingDocument {
4262            tree: Some(t),
4263            ..DoclingDocument::new("t")
4264        };
4265        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4266        let refs = |x: &Value| -> Vec<String> {
4267            x.as_array()
4268                .unwrap()
4269                .iter()
4270                .map(|r| r["$ref"].as_str().unwrap().to_string())
4271                .collect()
4272        };
4273        assert_eq!(
4274            refs(&v["body"]["children"]),
4275            ["#/groups/4", "#/texts/0", "#/groups/0", "#/groups/1"]
4276        );
4277        assert_eq!(
4278            refs(&v["groups"][0]["children"]),
4279            ["#/groups/3", "#/groups/2"]
4280        );
4281        for (g, name, kids) in [
4282            (2, "group", vec!["#/texts/2"]),
4283            (3, "group", vec!["#/texts/3"]),
4284            (4, "group", vec!["#/texts/4", "#/texts/5"]),
4285            (1, "list", vec!["#/texts/1"]),
4286        ] {
4287            assert_eq!(v["groups"][g]["label"], "list");
4288            assert_eq!(v["groups"][g]["name"], name);
4289            assert_eq!(refs(&v["groups"][g]["children"]), kids);
4290        }
4291        let texts: Vec<&str> = v["texts"]
4292            .as_array()
4293            .unwrap()
4294            .iter()
4295            .map(|t| t["text"].as_str().unwrap())
4296            .collect();
4297        assert_eq!(texts, ["plain", "E", "D", "C", "A", "B"]);
4298        assert_eq!(v["texts"][2]["parent"]["$ref"], "#/groups/2");
4299    }
4300
4301    /// A child the tree still lists after deleting it is left out instead
4302    /// of written as `"$ref": ""`, which docling-core rejects (#527).
4303    #[test]
4304    fn a_deleted_child_is_not_written_as_an_empty_ref() {
4305        use crate::tree::{ItemTree, TreeKind};
4306        let mut t = ItemTree::default();
4307        let text = |txt: &str| TreeKind::Text {
4308            label: "text".into(),
4309            text: txt.into(),
4310            orig: None,
4311            formatting: None,
4312            hyperlink: None,
4313            level: None,
4314            list: None,
4315        };
4316        let g = t.add(
4317            None,
4318            None,
4319            TreeKind::Group {
4320                label: "unspecified".into(),
4321                name: "g".into(),
4322            },
4323        );
4324        let gone = t.add(Some(g), None, text(""));
4325        t.add(Some(g), None, text("kept"));
4326        t.items[gone].deleted = true; // still in `g`'s children
4327        let doc = DoclingDocument {
4328            tree: Some(t),
4329            ..DoclingDocument::new("t")
4330        };
4331        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4332        assert_eq!(
4333            v["groups"][0]["children"],
4334            serde_json::json!([{"$ref": "#/texts/0"}])
4335        );
4336    }
4337
4338    #[test]
4339    fn a_trimmed_run_tree_gets_the_legacy_join_in_its_runs() {
4340        use crate::tree::{ItemTree, TreeKind, TreeProv};
4341        let text = |label: &str, txt: &str| TreeKind::Text {
4342            label: label.into(),
4343            text: txt.into(),
4344            orig: None,
4345            formatting: None,
4346            hyperlink: None,
4347            level: None,
4348            list: None,
4349        };
4350        let mut t = ItemTree::default();
4351        let g = t.add(
4352            None,
4353            None,
4354            TreeKind::Group {
4355                label: "inline".into(),
4356                name: "group".into(),
4357            },
4358        );
4359        t.add(Some(g), None, text("text", "left"));
4360        let prov = TreeProv {
4361            page_no: 1,
4362            bbox: [0.0, 0.0, 1.0, 1.0],
4363            bottom_left: false,
4364            charspan: [0, 1],
4365        };
4366        t.add_with_prov(Some(g), None, text("formula", "a"), prov.clone());
4367        t.add_with_prov(Some(g), None, text("formula", "b"), prov);
4368        t.add(Some(g), None, text("text", "right"));
4369        let doc = DoclingDocument {
4370            tree: Some(t),
4371            ..DoclingDocument::new("t")
4372        };
4373        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4374        let texts: Vec<&str> = v["texts"]
4375            .as_array()
4376            .unwrap()
4377            .iter()
4378            .map(|t| t["text"].as_str().unwrap())
4379            .collect();
4380        // `left ` · `a` · ` ` (standalone, appended) · `b` · ` right`.
4381        assert_eq!(texts, ["left ", "a", "b", " right", " "]);
4382        assert_eq!(v["texts"][0]["orig"], "left");
4383        assert_eq!(v["groups"][0]["children"][2]["$ref"], "#/texts/4");
4384        assert_eq!(
4385            v["texts"][4]["prov"][0]["charspan"],
4386            serde_json::json!([0, 1])
4387        );
4388    }
4389
4390    #[test]
4391    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
4392    fn a_backend_item_tree_is_written_verbatim() {
4393        use crate::tree::{Formatting, ItemTree, ListMeta, TreeKind};
4394        let mut t = ItemTree {
4395            // Runs as written: no legacy separator injection (#997).
4396            inline_whitespace: true,
4397            ..ItemTree::default()
4398        };
4399        let text = |label: &str, txt: &str| TreeKind::Text {
4400            label: label.into(),
4401            text: txt.into(),
4402            orig: None,
4403            formatting: None,
4404            hyperlink: None,
4405            level: None,
4406            list: None,
4407        };
4408        let title = t.add(None, Some(ContentLayer::Furniture), text("title", "Page"));
4409        let h = t.add(None, None, text("title", "Heading"));
4410        let group = t.add(
4411            Some(h),
4412            None,
4413            TreeKind::Group {
4414                label: "inline".into(),
4415                name: "group".into(),
4416            },
4417        );
4418        t.add(
4419            Some(group),
4420            None,
4421            TreeKind::Text {
4422                label: "text".into(),
4423                text: "bold".into(),
4424                orig: None,
4425                formatting: Some(Formatting {
4426                    bold: true,
4427                    ..Formatting::default()
4428                }),
4429                hyperlink: Some("https://example.com/".into()),
4430                level: None,
4431                list: None,
4432            },
4433        );
4434        t.add(
4435            Some(group),
4436            None,
4437            TreeKind::Code {
4438                text: "x = 1".into(),
4439                orig: None,
4440                language: Some("python".into()),
4441                formatting: None,
4442                hyperlink: None,
4443                captions: Vec::new(),
4444            },
4445        );
4446        let sub = t.add(
4447            Some(h),
4448            None,
4449            TreeKind::Text {
4450                label: "section_header".into(),
4451                text: "Sub".into(),
4452                orig: Some("Sub\u{2019}".into()),
4453                formatting: None,
4454                hyperlink: None,
4455                level: Some(1),
4456                list: None,
4457            },
4458        );
4459        let item = t.add(
4460            Some(sub),
4461            None,
4462            TreeKind::Text {
4463                label: "list_item".into(),
4464                text: "item".into(),
4465                orig: None,
4466                formatting: None,
4467                hyperlink: None,
4468                level: None,
4469                list: Some(ListMeta {
4470                    enumerated: true,
4471                    marker: "3.".into(),
4472                }),
4473            },
4474        );
4475        let _region = t.add(
4476            Some(sub),
4477            None,
4478            TreeKind::FieldRegion {
4479                items: vec![crate::FieldItem {
4480                    marker: None,
4481                    key: Some("Name".into()),
4482                    value: Some("Duck".into()),
4483                    value_kind: Some("read_only".into()),
4484                }],
4485            },
4486        );
4487        let table = t.add(
4488            Some(sub),
4489            None,
4490            TreeKind::Table {
4491                footnotes: Vec::new(),
4492                table: Table {
4493                    rows: vec![vec!["a  \n&lt;".into(), "b".into()]],
4494                    cells: Some(vec![
4495                        crate::TableCell {
4496                            text: "a  \n&lt;".into(),
4497                            bbox: None,
4498                            start_row: 0,
4499                            start_col: 0,
4500                            row_span: 3,
4501                            col_span: 1,
4502                            column_header: false,
4503                            row_header: true,
4504                            row_section: false,
4505                        },
4506                        crate::TableCell {
4507                            text: "b".into(),
4508                            bbox: None,
4509                            start_row: 0,
4510                            start_col: 1,
4511                            row_span: 1,
4512                            col_span: 1,
4513                            column_header: false,
4514                            row_header: false,
4515                            row_section: false,
4516                        },
4517                    ]),
4518                    ..Table::default()
4519                },
4520                rich_cells: vec![(0, 1, 0)], // patched below
4521                captions: Vec::new(),
4522            },
4523        );
4524        let cell_group = t.add(
4525            Some(table),
4526            None,
4527            TreeKind::Group {
4528                label: "unspecified".into(),
4529                name: "rich_cell_group_1_0_0".into(),
4530            },
4531        );
4532        if let TreeKind::Table { rich_cells, .. } = &mut t.items[table].kind {
4533            *rich_cells = vec![(0, 1, cell_group)];
4534        }
4535        let after = t.add(Some(sub), None, text("text", "after the region"));
4536        let _ = (title, after);
4537        // A list item belongs in a `list` group — one outside it would be
4538        // re-homed by `wrap_misplaced_list_items` (created last, so the
4539        // other groups keep their numbers).
4540        let list = t.add(
4541            Some(sub),
4542            None,
4543            TreeKind::Group {
4544                label: "list".into(),
4545                name: "list".into(),
4546            },
4547        );
4548        t.reparent(item, Some(list));
4549
4550        let doc = DoclingDocument {
4551            tree: Some(t),
4552            ..DoclingDocument::new("t")
4553        };
4554        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4555        // Creation order: Page, Heading, bold, x = 1, Sub, item, [Name, Duck], after.
4556        let texts: Vec<&str> = v["texts"]
4557            .as_array()
4558            .unwrap()
4559            .iter()
4560            .map(|t| t["text"].as_str().unwrap())
4561            .collect();
4562        assert_eq!(
4563            texts,
4564            [
4565                "Page",
4566                "Heading",
4567                "bold",
4568                "x = 1",
4569                "Sub",
4570                "item",
4571                "Name",
4572                "Duck",
4573                "after the region"
4574            ]
4575        );
4576        assert_eq!(
4577            v["body"]["children"],
4578            serde_json::json!([{"$ref": "#/texts/0"}, {"$ref": "#/texts/1"}])
4579        );
4580        assert_eq!(v["texts"][0]["content_layer"], "furniture");
4581        assert_eq!(
4582            v["texts"][1]["children"],
4583            serde_json::json!([{"$ref": "#/groups/0"}, {"$ref": "#/texts/4"}])
4584        );
4585        let bold = &v["texts"][2];
4586        assert_eq!(bold["parent"]["$ref"], "#/groups/0");
4587        let keys: Vec<&str> = bold
4588            .as_object()
4589            .unwrap()
4590            .keys()
4591            .map(String::as_str)
4592            .collect();
4593        assert_eq!(
4594            keys,
4595            [
4596                "self_ref",
4597                "parent",
4598                "children",
4599                "content_layer",
4600                "label",
4601                "prov",
4602                "orig",
4603                "text",
4604                "formatting",
4605                "hyperlink"
4606            ]
4607        );
4608        assert_eq!(
4609            bold["formatting"],
4610            serde_json::json!({"bold": true, "italic": false, "underline": false, "strikethrough": false, "script": "baseline"})
4611        );
4612        let code = &v["texts"][3];
4613        assert_eq!(code["label"], "code");
4614        assert_eq!(code["code_language"], "Python");
4615        let sub = &v["texts"][4];
4616        assert_eq!(sub["orig"], "Sub\u{2019}");
4617        assert_eq!(sub["level"], 1);
4618        let item = &v["texts"][5];
4619        let keys: Vec<&str> = item
4620            .as_object()
4621            .unwrap()
4622            .keys()
4623            .map(String::as_str)
4624            .collect();
4625        assert_eq!(
4626            keys,
4627            [
4628                "self_ref",
4629                "parent",
4630                "children",
4631                "content_layer",
4632                "label",
4633                "prov",
4634                "orig",
4635                "text",
4636                "enumerated",
4637                "marker"
4638            ]
4639        );
4640        assert_eq!(item["marker"], "3.");
4641        assert_eq!(v["texts"][7]["kind"], "read_only");
4642        assert_eq!(v["field_regions"][0]["parent"]["$ref"], "#/texts/4");
4643        let table = &v["tables"][0];
4644        assert_eq!(
4645            table["children"],
4646            serde_json::json!([{"$ref": "#/groups/1"}])
4647        );
4648        let cells = table["data"]["table_cells"].as_array().unwrap();
4649        assert_eq!(
4650            cells[0]["text"], "a  \n&lt;",
4651            "raw cell text is written verbatim"
4652        );
4653        assert_eq!(
4654            cells[0]["end_row_offset_idx"], 3,
4655            "declared spans are not clamped"
4656        );
4657        assert_eq!(cells[1]["ref"], serde_json::json!({"$ref": "#/groups/1"}));
4658        assert!(cells[0].get("ref").is_none());
4659        assert!(
4660            table["data"]["grid"][0][1].get("ref").is_none(),
4661            "the grid shows plain cells"
4662        );
4663        assert_eq!(v["groups"][1]["name"], "rich_cell_group_1_0_0");
4664    }
4665
4666    /// A comment section that links its note text rather than its group — the
4667    /// spreadsheet shape, where docling-core's `add_comment` appends the text
4668    /// item's ref to each target.
4669    #[test]
4670    fn a_comment_section_can_be_referenced_by_its_note_text() {
4671        let doc = DoclingDocument {
4672            name: "c".into(),
4673            nodes: vec![
4674                Node::Commented {
4675                    comments: vec![0],
4676                    inner: Box::new(Node::Paragraph {
4677                        text: "annotated".into(),
4678                    }),
4679                },
4680                Node::CommentSection {
4681                    name: "comment-Sheet1-A1".into(),
4682                    text: "[author: A]: note".into(),
4683                    refs_note_text: true,
4684                    grouped: true,
4685                },
4686            ],
4687            ..DoclingDocument::new("c")
4688        };
4689        let v = crate::json::to_json(&doc);
4690        assert_eq!(v["groups"][0]["name"], "comment-Sheet1-A1");
4691        assert_eq!(v["texts"][0]["comments"][0]["$ref"], "#/texts/1");
4692    }
4693
4694    /// A thread's messages are notes of one `comment_section` group, which is
4695    /// listed once among its parent's children however many notes it holds.
4696    #[test]
4697    fn a_comment_thread_is_one_child_of_its_parent() {
4698        let note = |text: &str| Node::CommentSection {
4699            name: "comment-Sheet1-F7".into(),
4700            text: text.into(),
4701            refs_note_text: true,
4702            grouped: true,
4703        };
4704        let doc = DoclingDocument {
4705            name: "c".into(),
4706            nodes: vec![note("[author: A]: first"), note("[author: B]: reply")],
4707            ..DoclingDocument::new("c")
4708        };
4709        let v = crate::json::to_json(&doc);
4710        assert_eq!(v["body"]["children"].as_array().unwrap().len(), 1);
4711        assert_eq!(v["groups"][0]["children"].as_array().unwrap().len(), 2);
4712    }
4713
4714    /// docx reviewer comments: a `comment_section` group on the notes layer
4715    /// holding the note text, and a `comments` back-ref on the annotated item —
4716    /// keyed between `prov` and `orig`, the slot docling emits it in.
4717    #[test]
4718    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
4719    fn comment_sections_link_back_to_their_items() {
4720        let doc = DoclingDocument {
4721            name: "c".into(),
4722            nodes: vec![
4723                Node::Commented {
4724                    comments: vec![0],
4725                    inner: Box::new(Node::Paragraph {
4726                        text: "annotated".into(),
4727                    }),
4728                },
4729                Node::Paragraph {
4730                    text: "plain".into(),
4731                },
4732                Node::CommentSection {
4733                    name: "comment-7".into(),
4734                    text: "[time: t]: note".into(),
4735                    refs_note_text: false,
4736                    grouped: true,
4737                },
4738            ],
4739            ..DoclingDocument::new("c")
4740        };
4741        let v = crate::json::to_json(&doc);
4742        // The group is the comment section; its only child is the notes text.
4743        assert_eq!(v["groups"][0]["label"], "comment_section");
4744        assert_eq!(v["groups"][0]["name"], "comment-7");
4745        assert_eq!(v["groups"][0]["content_layer"], "notes");
4746        assert_eq!(v["groups"][0]["children"][0]["$ref"], "#/texts/2");
4747        assert_eq!(v["texts"][2]["content_layer"], "notes");
4748        // The annotated item points back at the group; the plain one has no key.
4749        assert_eq!(v["texts"][0]["comments"][0]["$ref"], "#/groups/0");
4750        assert!(v["texts"][1].get("comments").is_none());
4751        // docling's key order: … prov, comments, orig, text.
4752        let keys: Vec<&str> = v["texts"][0]
4753            .as_object()
4754            .unwrap()
4755            .keys()
4756            .map(String::as_str)
4757            .collect();
4758        assert_eq!(
4759            &keys[keys.len() - 4..],
4760            &["prov", "comments", "orig", "text"]
4761        );
4762    }
4763
4764    fn picture(caption: &str, caption_parent: CaptionParent) -> Node {
4765        Node::Picture {
4766            caption: Some(caption.into()),
4767            caption_href: None,
4768            image: None,
4769            classification: None,
4770            description: None,
4771            caption_parent,
4772            caption_location: None,
4773        }
4774    }
4775
4776    fn group(children: Vec<Node>) -> Node {
4777        Node::Group {
4778            label: "section".into(),
4779            name: None,
4780            layer: None,
4781            children,
4782        }
4783    }
4784
4785    fn refs(v: &Value) -> Vec<&str> {
4786        v.as_array()
4787            .unwrap()
4788            .iter()
4789            .map(|r| r["$ref"].as_str().unwrap())
4790            .collect()
4791    }
4792
4793    /// #390: a declarative backend's caption is docling's `add_text` default —
4794    /// a body child, appended as it is created — wherever the picture sits:
4795    /// ahead of a top-level picture, behind the top-level item enclosing a
4796    /// nested one. The picture references it either way and has no children.
4797    #[test]
4798    fn a_body_caption_follows_the_enclosing_top_level_item() {
4799        let mut doc = DoclingDocument::new("t");
4800        doc.push(picture("top", CaptionParent::Body));
4801        doc.push(group(vec![
4802            Node::Paragraph { text: "p".into() },
4803            picture("nested", CaptionParent::Body),
4804        ]));
4805        doc.push(Node::Paragraph {
4806            text: "after".into(),
4807        });
4808        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4809        assert_eq!(
4810            refs(&v["body"]["children"]),
4811            [
4812                "#/texts/0",
4813                "#/pictures/0",
4814                "#/groups/0",
4815                "#/texts/2",
4816                "#/texts/3"
4817            ]
4818        );
4819        assert_eq!(
4820            refs(&v["groups"][0]["children"]),
4821            ["#/texts/1", "#/pictures/1"]
4822        );
4823        for (cap, pic) in [(0, 0), (2, 1)] {
4824            assert_eq!(v["texts"][cap]["label"], "caption");
4825            assert_eq!(v["texts"][cap]["parent"]["$ref"], "#/body");
4826            assert_eq!(
4827                refs(&v["pictures"][pic]["captions"]),
4828                [format!("#/texts/{cap}")]
4829            );
4830            assert_eq!(v["pictures"][pic]["children"], serde_json::json!([]));
4831        }
4832    }
4833
4834    /// The PDF pipeline's caption is the picture's (or table's) own child,
4835    /// as docling attaches a layout caption.
4836    #[test]
4837    fn an_item_caption_is_the_items_first_child() {
4838        let mut doc = DoclingDocument::new("t");
4839        doc.push(picture("fig", CaptionParent::Item));
4840        doc.push(Node::Table(Table {
4841            rows: vec![vec!["a".into()]],
4842            caption: Some("tab".into()),
4843            caption_parent: CaptionParent::Item,
4844            ..Table::default()
4845        }));
4846        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4847        assert_eq!(refs(&v["body"]["children"]), ["#/pictures/0", "#/tables/0"]);
4848        assert_eq!(v["texts"][0]["parent"]["$ref"], "#/pictures/0");
4849        assert_eq!(refs(&v["pictures"][0]["children"]), ["#/texts/0"]);
4850        assert_eq!(refs(&v["pictures"][0]["captions"]), ["#/texts/0"]);
4851        assert_eq!(v["texts"][1]["parent"]["$ref"], "#/tables/0");
4852        assert_eq!(refs(&v["tables"][0]["children"]), ["#/texts/1"]);
4853        assert_eq!(refs(&v["tables"][0]["captions"]), ["#/texts/1"]);
4854    }
4855
4856    /// A PDF picture's contained text (`Node::PictureChildren`) is written as
4857    /// docling's `_add_child_elements` writes it: items parented to the
4858    /// picture, after its caption in the picture's `children`, never in the
4859    /// body; a list item opens its own list group under the picture. The
4860    /// Markdown and the chunker's refs are unaffected.
4861    #[test]
4862    fn picture_children_hang_off_the_picture_after_its_caption() {
4863        let mut doc = DoclingDocument::new("t");
4864        doc.push(picture("fig", CaptionParent::Item));
4865        doc.push(Node::PictureChildren(vec![
4866            Node::Heading {
4867                level: 2,
4868                text: "in-figure title".into(),
4869            },
4870            Node::Paragraph {
4871                text: "axis label".into(),
4872            },
4873            Node::ListItem {
4874                ordered: false,
4875                number: 0,
4876                first_in_list: true,
4877                text: "callout".into(),
4878                level: 0,
4879                marker: None,
4880                location: None,
4881                split: None,
4882                dclx: None,
4883                href: None,
4884                layer: None,
4885            },
4886        ]));
4887        doc.push(Node::Paragraph {
4888            text: "after".into(),
4889        });
4890        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4891        assert_eq!(refs(&v["body"]["children"]), ["#/pictures/0", "#/texts/4"]);
4892        assert_eq!(
4893            refs(&v["pictures"][0]["children"]),
4894            ["#/texts/0", "#/texts/1", "#/texts/2", "#/groups/0"]
4895        );
4896        assert_eq!(refs(&v["pictures"][0]["captions"]), ["#/texts/0"]);
4897        assert_eq!(v["texts"][1]["label"], "section_header");
4898        assert_eq!(v["texts"][2]["label"], "text");
4899        for t in 1..=2 {
4900            assert_eq!(v["texts"][t]["parent"]["$ref"], "#/pictures/0");
4901            assert_eq!(v["texts"][t]["content_layer"], "body");
4902        }
4903        assert_eq!(v["groups"][0]["parent"]["$ref"], "#/pictures/0");
4904        assert_eq!(v["texts"][3]["parent"]["$ref"], "#/groups/0");
4905        let md = doc.export_to_markdown();
4906        assert!(
4907            !md.contains("axis label") && !md.contains("callout"),
4908            "{md}"
4909        );
4910        assert!(md.contains("after"), "{md}");
4911    }
4912
4913    /// A container caption sits beside its item under the item's parent —
4914    /// ahead of it (an office chart's title) or behind it (an HTML
4915    /// `<figure>`'s table, whose figcaption docling adds after the table).
4916    #[test]
4917    fn a_container_caption_is_the_items_sibling() {
4918        let mut doc = DoclingDocument::new("t");
4919        doc.push(group(vec![
4920            picture("chart", CaptionParent::Container),
4921            Node::Table(Table {
4922                rows: vec![vec!["a".into()]],
4923                caption: Some("figcaption".into()),
4924                caption_parent: CaptionParent::ContainerAfter,
4925                ..Table::default()
4926            }),
4927        ]));
4928        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4929        assert_eq!(refs(&v["body"]["children"]), ["#/groups/0"]);
4930        assert_eq!(
4931            refs(&v["groups"][0]["children"]),
4932            ["#/texts/0", "#/pictures/0", "#/tables/0", "#/texts/1"]
4933        );
4934        assert_eq!(v["texts"][0]["parent"]["$ref"], "#/groups/0");
4935        assert_eq!(v["texts"][1]["parent"]["$ref"], "#/groups/0");
4936        assert_eq!(v["pictures"][0]["children"], serde_json::json!([]));
4937        assert_eq!(v["tables"][0]["children"], serde_json::json!([]));
4938    }
4939
4940    /// A PDF footnote (#620 follow-up): the JSON keeps docling's
4941    /// `footnote` label, the raw text and the link as `hyperlink`; the
4942    /// Markdown still wraps the whole item as `[text](uri)`, and the plain
4943    /// text drops the link like docling-core's `export_to_text`.
4944    #[test]
4945    fn a_labeled_text_keeps_its_label_and_hyperlink_out_of_the_text() {
4946        let mut doc = DoclingDocument::new("t");
4947        doc.push(Node::LabeledText {
4948            label: "footnote".into(),
4949            text: "1 https://example.com/a\\_b".into(),
4950            href: Some("https://example.com/a_b".into()),
4951        });
4952        doc.push(Node::LabeledText {
4953            label: "footnote".into(),
4954            text: "© 2022 the authors".into(),
4955            href: None,
4956        });
4957        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4958        let t = &v["texts"][0];
4959        assert_eq!(t["label"], "footnote");
4960        assert_eq!(t["text"], "1 https://example.com/a_b");
4961        assert_eq!(t["orig"], "1 https://example.com/a_b");
4962        assert_eq!(t["hyperlink"], "https://example.com/a_b");
4963        assert_eq!(v["texts"][1]["label"], "footnote");
4964        assert!(v["texts"][1].get("hyperlink").is_none());
4965        assert_eq!(
4966            doc.export_to_markdown(),
4967            "[1 https://example.com/a\\_b](https://example.com/a_b)\n\n© 2022 the authors\n"
4968        );
4969        assert_eq!(
4970            doc.export_to_text(),
4971            "1 https://example.com/a_b\n\n© 2022 the authors"
4972        );
4973    }
4974
4975    /// A marked PDF item (#1005, docling's `ScriptPositionModel._apply`):
4976    /// the item keeps its place with an empty text and holds an inline group
4977    /// of its runs, added after every other item; two such paragraphs stay
4978    /// separate HTML blocks; a linked footnote's link rides on its baseline
4979    /// runs only, so the marker stays out of it.
4980    #[test]
4981    fn script_runs_follow_docling() {
4982        use crate::{Script, ScriptRun, ScriptRunProv};
4983        let run = |text: &str, script, hyperlink: Option<&str>| ScriptRun {
4984            text: text.into(),
4985            script,
4986            prov: vec![ScriptRunProv {
4987                page_no: 1,
4988                bbox: [10.0, 700.0, 20.0, 690.0],
4989                charspan: [0, text.chars().count()],
4990            }],
4991            hyperlink: hyperlink.map(String::from),
4992        };
4993        let mut doc = DoclingDocument::new("t");
4994        doc.push(Node::PageInfo {
4995            page_no: 1,
4996            width: 612.0,
4997            height: 792.0,
4998        });
4999        let located = |inner: Node| Node::Located {
5000            location: [40, 80, 200, 90],
5001            inner: Box::new(inner),
5002        };
5003        doc.push(located(Node::ScriptText {
5004            inner: Box::new(Node::Paragraph {
5005                text: "E = mc2".into(),
5006            }),
5007            runs: vec![
5008                run("E = mc", Script::Baseline, None),
5009                run("2", Script::Super, None),
5010            ],
5011        }));
5012        doc.push(located(Node::ScriptText {
5013            inner: Box::new(Node::Paragraph { text: "H2O".into() }),
5014            runs: vec![
5015                run("H", Script::Baseline, None),
5016                run("2", Script::Sub, None),
5017                run("O", Script::Baseline, None),
5018            ],
5019        }));
5020        let url = "https://example.com/x";
5021        doc.push(located(Node::ScriptText {
5022            inner: Box::new(Node::LabeledText {
5023                label: "footnote".into(),
5024                text: format!("1[Note text]({url})"),
5025                href: None,
5026            }),
5027            runs: vec![
5028                run("1", Script::Super, None),
5029                run("Note text", Script::Baseline, Some(url)),
5030            ],
5031        }));
5032        doc.push(located(Node::Paragraph {
5033            text: "Plain line".into(),
5034        }));
5035
5036        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
5037        let texts = v["texts"].as_array().unwrap();
5038        // The items first, in place; the runs after them.
5039        assert_eq!(texts[0]["text"], "");
5040        assert_eq!(
5041            texts[0]["children"],
5042            serde_json::json!([{"$ref": "#/groups/0"}])
5043        );
5044        assert_eq!(texts[2]["label"], "footnote");
5045        assert_eq!(texts[2]["hyperlink"], url);
5046        assert_eq!(texts[3]["text"], "Plain line");
5047        assert_eq!(texts[4]["text"], "E = mc");
5048        assert_eq!(texts[5]["formatting"]["script"], "super");
5049        assert_eq!(texts[5]["prov"][0]["bbox"]["coord_origin"], "BOTTOMLEFT");
5050        assert_eq!(texts[7]["formatting"]["script"], "sub");
5051        assert_eq!(texts[9]["label"], "footnote");
5052        assert!(texts[9].get("hyperlink").is_none());
5053        assert_eq!(texts[10]["hyperlink"], url);
5054        assert_eq!(v["groups"][0]["label"], "inline");
5055        assert_eq!(v["groups"][0]["parent"]["$ref"], "#/texts/0");
5056
5057        assert_eq!(
5058            doc.export_to_markdown(),
5059            format!("E = mc2\n\nH2O\n\n1[Note text]({url})\n\nPlain line\n")
5060        );
5061        let html = doc.export_to_html();
5062        // Each marked item is a block of its own (the footnote too, as
5063        // docling-core renders it).
5064        assert_eq!(
5065            html.matches("<p><span class='inline-group'>").count(),
5066            3,
5067            "{html}"
5068        );
5069        assert!(html.contains("E = mc<sup>2</sup>"), "{html}");
5070        assert!(html.contains("H<sub>2</sub>O"), "{html}");
5071        assert!(
5072            html.contains(&format!("<sup>1</sup><a href=\"{url}\">Note text</a>")),
5073            "{html}"
5074        );
5075        let dclx = doc.export_to_doclang();
5076        assert!(
5077            dclx.contains("E = mc<superscript>2</superscript>"),
5078            "{dclx}"
5079        );
5080    }
5081}