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            ..
2753        } = node
2754        else {
2755            unreachable!()
2756        };
2757        self.adopt_loc(*location);
2758        let self_ref = format!("#/texts/{}", self.texts.len());
2759        let raw = unescape_text(text);
2760        let prov = self.take_prov(raw.chars().count());
2761        let marker = if *ordered {
2762            format!("{number}.")
2763        } else {
2764            "-".to_string()
2765        };
2766        self.texts.push(Item::json(json!({
2767            "self_ref": self_ref,
2768            "parent": { "$ref": parent },
2769            "children": [],
2770            "content_layer": "body",
2771            "label": "list_item",
2772            "prov": prov,
2773            "orig": raw,
2774            "text": raw,
2775            "enumerated": ordered,
2776            "marker": marker,
2777        })));
2778        self_ref
2779    }
2780
2781    fn add_table(&mut self, t: &Table, parent: &str) -> String {
2782        self.add_table_with(t, parent, false)
2783    }
2784
2785    /// [`Self::add_table`]; `raw` cell text is written verbatim (see
2786    /// [`table_data_with`]).
2787    fn add_table_with(&mut self, t: &Table, parent: &str, raw: bool) -> String {
2788        let self_ref = format!("#/tables/{}", self.tables.len());
2789        self.adopt_loc(t.location);
2790        let prov = self.take_prov(0);
2791        // The caption is a separate text item the table references (docling's
2792        // `TableItem.captions`), added before the grid so its box isn't
2793        // inherited by a later item.
2794        let (captions, children) = match t.caption.as_deref().filter(|c| !c.is_empty()) {
2795            Some(cap) => self.add_caption(
2796                cap,
2797                json!({}),
2798                &self_ref,
2799                parent,
2800                (t.caption_parent, t.caption_location),
2801            ),
2802            None => (Vec::new(), Vec::new()),
2803        };
2804        // docling-core#675 (#1009): each footnote is a `footnote` text item
2805        // under the table, added after it (docling's backends attach notes
2806        // once the table exists) and listed in its `footnotes`.
2807        // The item-tree path (`raw`) writes its own footnote items.
2808        let (mut children, mut footnotes) = (children, Vec::new());
2809        for note in t.footnotes.iter().filter(|n| !raw && !n.is_empty()) {
2810            let r = ref_value(self.add_text("footnote", note, &self_ref, json!({})));
2811            children.push(r.clone());
2812            footnotes.push(r);
2813        }
2814        let mut data = table_data_with(t, raw);
2815        if !raw {
2816            self.add_cell_pictures(t, &self_ref, &mut data, &mut children);
2817        }
2818        let item = json!({
2819            "self_ref": self_ref,
2820            "parent": { "$ref": parent },
2821            "children": children,
2822            "content_layer": "body",
2823            "label": "table",
2824            "prov": prov,
2825            "captions": captions,
2826            "references": [],
2827            "footnotes": footnotes,
2828            // The typed `data` takes this stand-in's place on output.
2829            "data": null,
2830            "annotations": [],
2831        });
2832        self.tables.push(TableItem { item, data });
2833        self_ref
2834    }
2835
2836    /// The pictures placed in a table's cells (the XLSX "Place in Cell"
2837    /// image, docling#5015): a cell whose blocks are exactly one
2838    /// [`Node::Prov`]-wrapped picture is docling's `RichTableCell` over a
2839    /// picture item — the picture is the table's child, the cell keeps an
2840    /// empty text and a `ref` to it. Other rich cells (and the PDF
2841    /// pipeline's [`Node::Located`] in-cell pictures) keep their flat cell.
2842    /// The cells' blocks are parallel to `rows`; a ragged (compacted) row
2843    /// pairs its blocks with that row's first-class cells in column order.
2844    fn add_cell_pictures(
2845        &mut self,
2846        t: &Table,
2847        self_ref: &str,
2848        data: &mut TableData,
2849        children: &mut Vec<Value>,
2850    ) {
2851        let Some(blocks) = t.cell_blocks.as_ref() else {
2852            return;
2853        };
2854        for (r, row) in blocks.iter().enumerate() {
2855            let mut in_row: Vec<usize> = (0..data.cells.len())
2856                .filter(|&i| data.cells[i].start_row == r)
2857                .collect();
2858            in_row.sort_by_key(|&i| data.cells[i].start_col);
2859            let ragged = t.rows.get(r).map_or(0, Vec::len) != data.num_cols;
2860            for (c, cell) in row.iter().enumerate() {
2861                let [node @ Node::Prov { inner, .. }] = cell.as_slice() else {
2862                    continue;
2863                };
2864                if !matches!(**inner, Node::Picture { .. }) {
2865                    continue;
2866                }
2867                let target = if ragged {
2868                    in_row.get(c).copied()
2869                } else {
2870                    in_row
2871                        .iter()
2872                        .copied()
2873                        .find(|&i| data.cells[i].start_col == c)
2874                };
2875                let Some(target) = target else {
2876                    continue;
2877                };
2878                let Some(pic) = self.add_node(node, self_ref) else {
2879                    continue;
2880                };
2881                let pic = ref_value(pic);
2882                children.push(pic.clone());
2883                let cell = &mut data.cells[target];
2884                cell.text = "".into();
2885                cell.cell_ref = Some(Box::new(pic));
2886            }
2887        }
2888    }
2889
2890    /// Add a picture's or table's caption text item where `choice` says it
2891    /// hangs (#390), returning the `captions` entry for the item and the
2892    /// item's own `children` (the caption, when it is the item's child).
2893    /// The caption never consumes the item's pending provenance — the item
2894    /// takes its box first. Its own box, when the backend kept one (the PDF
2895    /// pipeline's caption region, #609), becomes the caption's `prov` on the
2896    /// item's page, as docling's reading-order model gives every caption the
2897    /// cluster it came from; without one the caption has no `prov`.
2898    fn add_caption(
2899        &mut self,
2900        text: &str,
2901        extra: Value,
2902        self_ref: &str,
2903        parent: &str,
2904        (choice, location): (CaptionParent, Option<[u16; 4]>),
2905    ) -> (Vec<Value>, Vec<Value>) {
2906        // docling's PDF pipeline parents the caption to the item; every
2907        // declarative backend leaves `add_text`'s default — the body — even
2908        // for an item inside a group; the office backends and HTML's
2909        // `<figure>` hang it off the item's container.
2910        let cap_parent = match choice {
2911            CaptionParent::Item => self_ref,
2912            CaptionParent::Container | CaptionParent::ContainerAfter => parent,
2913            CaptionParent::Body => "#/body",
2914        };
2915        if self.cur_page > 0 && location.is_some() {
2916            self.pending_loc = location;
2917        }
2918        let cap_ref = ref_value(self.add_text("caption", text, cap_parent, extra));
2919        match choice {
2920            CaptionParent::Item => return (vec![cap_ref.clone()], vec![cap_ref]),
2921            // Created ahead of the item, so it precedes the item in the
2922            // container's children — and, on the body, in the body's.
2923            CaptionParent::Container => self.pending_siblings.push(cap_ref.clone()),
2924            CaptionParent::Body if parent == "#/body" => {
2925                self.pending_siblings.push(cap_ref.clone())
2926            }
2927            CaptionParent::ContainerAfter => self.pending_after.push(cap_ref.clone()),
2928            // The item sits deeper: the body's children get the caption after
2929            // the top-level item under walk, where docling appended it.
2930            CaptionParent::Body => self.pending_body.push(cap_ref.clone()),
2931        }
2932        (vec![cap_ref], Vec::new())
2933    }
2934
2935    /// `meta` is the picture's docling `PictureMeta` (a classifier's
2936    /// predictions, a chart's kind and data), `None` for a plain picture.
2937    fn add_picture(
2938        &mut self,
2939        caption: Option<&str>,
2940        caption_href: Option<&str>,
2941        image: Option<&crate::PictureImage>,
2942        meta: Option<Value>,
2943        parent: &str,
2944        caption_at: (CaptionParent, Option<[u16; 4]>),
2945    ) -> String {
2946        let self_ref = format!("#/pictures/{}", self.pictures.len());
2947        // Take the picture's own provenance before the caption text is added —
2948        // the caption is a separate item and must not inherit the crop's box.
2949        let prov = self.take_prov(0);
2950        let (captions, children) = match caption.filter(|c| !c.is_empty()) {
2951            Some(cap) => {
2952                // Emit the caption as a text item that the picture references. A
2953                // wrapping `<a href>`'s link rides as docling's `hyperlink` field
2954                // on the caption item (#328).
2955                let extra = match caption_href {
2956                    Some(href) => json!({ "hyperlink": href }),
2957                    None => json!({}),
2958                };
2959                self.add_caption(cap, extra, &self_ref, parent, caption_at)
2960            }
2961            None => (Vec::new(), Vec::new()),
2962        };
2963        self.push_picture(prov, captions, children, image, meta, parent)
2964    }
2965
2966    /// Append the picture item itself — `prov`, `captions` and `children`
2967    /// (a PDF caption is the picture's child) already settled.
2968    fn push_picture(
2969        &mut self,
2970        prov: Value,
2971        captions: Vec<Value>,
2972        children: Vec<Value>,
2973        image: Option<&crate::PictureImage>,
2974        meta: Option<Value>,
2975        parent: &str,
2976    ) -> String {
2977        let self_ref = format!("#/pictures/{}", self.pictures.len());
2978        // The legacy `classification` annotation rides along with a
2979        // classifier's `meta` (see `classification_meta`); a chart's meta has
2980        // none, like docling's.
2981        let annotations = meta
2982            .as_ref()
2983            .and_then(|m| m.get("annotations").cloned())
2984            .unwrap_or_else(|| json!([]));
2985        let meta = meta.map(|mut m| {
2986            if let Some(obj) = m.as_object_mut() {
2987                obj.remove("annotations");
2988            }
2989            m
2990        });
2991        // `meta` sits between `content_layer` and `label` in docling's field
2992        // order (and `preserve_order` keeps ours byte-compatible), so the item
2993        // is built in one shot per shape rather than patched afterwards.
2994        let mut item = match meta {
2995            Some(meta) => json!({
2996                "self_ref": self_ref,
2997                "parent": { "$ref": parent },
2998                "children": children,
2999                "content_layer": "body",
3000                "meta": meta,
3001                "label": "picture",
3002                "prov": prov,
3003                "captions": captions,
3004                "references": [],
3005                "footnotes": [],
3006                "annotations": annotations,
3007            }),
3008            None => json!({
3009                "self_ref": self_ref,
3010                "parent": { "$ref": parent },
3011                "children": children,
3012                "content_layer": "body",
3013                "label": "picture",
3014                "prov": prov,
3015                "captions": captions,
3016                "references": [],
3017                "footnotes": [],
3018                "annotations": annotations,
3019            }),
3020        };
3021        // docling stores the extracted image as an `ImageRef` (data URI + size,
3022        // the size as floats) between `footnotes` and `annotations` — pydantic
3023        // field order, which `preserve_order` lets us reproduce by rebuilding
3024        // the tail.
3025        if let Some(img) = image {
3026            let mut image = image_ref_json(img);
3027            // The displayed size, for the Pandoc writer only (#834):
3028            // docling's `ImageRef` has no field for it.
3029            if self.note_keys {
3030                if let Some([w, h]) = img.display_size {
3031                    image["_display"] = json!([w, h]);
3032                }
3033            }
3034            if let Some(obj) = item.as_object_mut() {
3035                let annotations = obj.remove("annotations").unwrap_or_else(|| json!([]));
3036                obj.insert("image".into(), image);
3037                obj.insert("annotations".into(), annotations);
3038            }
3039        }
3040        self.pictures.push(item);
3041        self_ref
3042    }
3043
3044    fn add_group(
3045        &mut self,
3046        label: &str,
3047        name: Option<&str>,
3048        layer: Option<ContentLayer>,
3049        nodes: &[Node],
3050        parent: &str,
3051    ) -> String {
3052        let self_ref = format!("#/groups/{}", self.groups.len());
3053        self.groups.push(Value::Null);
3054        // Everything the walk creates belongs to this group, so a non-body
3055        // layer (a hidden sheet) is stamped on the whole subtree afterwards —
3056        // docling puts the layer on the group *and* on every item under it.
3057        let mark = (
3058            self.texts.len(),
3059            self.tables.len(),
3060            self.pictures.len(),
3061            self.groups.len(),
3062        );
3063        let children: Vec<Value> = self
3064            .walk_into(nodes, &self_ref)
3065            .into_iter()
3066            .map(Child::into_value)
3067            .collect();
3068        let name = name.unwrap_or(if label == "inline" { "group" } else { label });
3069        let content_layer = layer.map_or("body", |l| l.value());
3070        self.groups[group_index(&self_ref)] = json!({
3071            "self_ref": self_ref,
3072            "parent": { "$ref": parent },
3073            "children": children,
3074            "content_layer": content_layer,
3075            "name": name,
3076            "label": label,
3077        });
3078        if layer.is_some() {
3079            let (t, tb, p, g) = mark;
3080            for item in self.texts[t..]
3081                .iter_mut()
3082                .enumerate()
3083                .map(|(k, item)| item.value_mut(t + k))
3084                .chain(self.tables[tb..].iter_mut().map(|t| &mut t.item))
3085                .chain(self.pictures[p..].iter_mut())
3086                .chain(self.groups[g..].iter_mut())
3087            {
3088                if let Some(obj) = item.as_object_mut() {
3089                    obj.insert("content_layer".into(), json!(content_layer));
3090                }
3091            }
3092        }
3093        self_ref
3094    }
3095
3096    /// Walk a slice of sibling nodes, returning each child's `$ref`; runs of
3097    /// list items are folded into list groups (one per sibling list).
3098    fn walk_into(&mut self, nodes: &[Node], parent: &str) -> Vec<Child> {
3099        // Siblings that all carry a creation rank (an XLSX sheet's items) are
3100        // *added* in that order — so `#/tables/N` and friends are numbered as
3101        // docling numbers them — while their refs keep the node order, which
3102        // is docling's position-sorted `children`.
3103        let seqs: Option<Vec<usize>> = nodes
3104            .iter()
3105            .map(|n| match n {
3106                Node::Prov { seq: Some(s), .. } => Some(*s),
3107                _ => None,
3108            })
3109            .collect();
3110        if let Some(seqs) = seqs.filter(|s| !s.is_empty()) {
3111            let mut order: Vec<usize> = (0..nodes.len()).collect();
3112            order.sort_by_key(|&i| seqs[i]);
3113            let mut slots: Vec<Vec<Child>> = (0..nodes.len()).map(|_| Vec::new()).collect();
3114            for i in order {
3115                if let Some(r) = self.add_node(&nodes[i], parent) {
3116                    slots[i].extend(self.pending_siblings.drain(..).map(Child::json));
3117                    slots[i].push(Child::Ref(r.into()));
3118                    slots[i].extend(self.pending_after.drain(..).map(Child::json));
3119                }
3120                if parent == "#/body" {
3121                    slots[i].extend(self.pending_body.drain(..).map(Child::json));
3122                }
3123            }
3124            return slots.into_iter().flatten().collect();
3125        }
3126        let mut children = Vec::new();
3127        let mut i = 0;
3128        while i < nodes.len() {
3129            if matches!(nodes[i], Node::ListItem { .. }) {
3130                let start = i;
3131                i += 1;
3132                loop {
3133                    match nodes.get(i) {
3134                        Some(Node::ListItem { .. }) => i += 1,
3135                        // Absorb an empty paragraph sitting between two list
3136                        // items (docling keeps the ListGroup contiguous).
3137                        Some(Node::Paragraph { text })
3138                            if text.is_empty()
3139                                && matches!(nodes.get(i + 1), Some(Node::ListItem { .. })) =>
3140                        {
3141                            i += 1
3142                        }
3143                        _ => break,
3144                    }
3145                }
3146                let mut lists = Vec::new();
3147                self.add_sibling_lists(&nodes[start..i], parent, &mut lists);
3148                children.extend(lists.into_iter().map(Child::json));
3149            } else {
3150                if let Some(r) = self.add_node(&nodes[i], parent) {
3151                    children.extend(self.pending_siblings.drain(..).map(Child::json));
3152                    children.push(Child::Ref(r.into()));
3153                    children.extend(self.pending_after.drain(..).map(Child::json));
3154                }
3155                i += 1;
3156            }
3157            // Body-parented captions of items deeper in the tree follow the
3158            // top-level item they were created under (#390).
3159            if parent == "#/body" {
3160                children.extend(self.pending_body.drain(..).map(Child::json));
3161            }
3162        }
3163        children
3164    }
3165
3166    /// A run of list items may hold several *sibling* lists; emit one list group
3167    /// per sibling. The boundary is the backend's `first_in_list` flag on a
3168    /// base-level item — the same rule as the Markdown serializer's blank line
3169    /// (#385; the kind-flip and number-gap guesses are gone).
3170    fn add_sibling_lists(&mut self, run: &[Node], parent: &str, out: &mut Vec<Value>) {
3171        let base = level_of(&run[0]);
3172        let mut seg = 0;
3173        for k in 0..run.len() {
3174            let Node::ListItem {
3175                first_in_list,
3176                level,
3177                ..
3178            } = &run[k]
3179            else {
3180                continue;
3181            };
3182            if *level != base {
3183                continue; // nested item — handled inside add_list
3184            }
3185            if k > seg && *first_in_list {
3186                out.push(ref_value(self.add_list(&run[seg..k], parent)));
3187                seg = k;
3188            }
3189        }
3190        out.push(ref_value(self.add_list(&run[seg..], parent)));
3191    }
3192}
3193
3194fn level_of(node: &Node) -> u8 {
3195    match node {
3196        Node::ListItem { level, .. } => *level,
3197        _ => 0,
3198    }
3199}
3200
3201fn group_index(self_ref: &str) -> usize {
3202    self_ref.rsplit('/').next().unwrap().parse().unwrap()
3203}
3204
3205fn ref_index(self_ref: &str) -> Option<usize> {
3206    self_ref.rsplit('/').next()?.parse().ok()
3207}
3208
3209/// Merge the key/values of `extra` (an object) into `target` (an object).
3210fn merge(target: &mut Value, extra: Value) {
3211    if let (Some(t), Value::Object(e)) = (target.as_object_mut(), extra) {
3212        t.extend(e);
3213    }
3214}
3215
3216/// A JSON object of exactly these entries, in this order. The hot per-item
3217/// constructors use this instead of `json!`: the macro grows its map by
3218/// doubling (an 8-key item kept 14 slots) and deep-copies every interpolated
3219/// `Value` through `to_value`. On a long text-heavy PDF those item objects
3220/// *are* the export's footprint (the same reasoning as [`cell_value`]).
3221fn object<const N: usize>(entries: [(&str, Value); N]) -> Value {
3222    let mut m = serde_json::Map::with_capacity(N);
3223    for (k, v) in entries {
3224        m.insert(k.to_owned(), v);
3225    }
3226    Value::Object(m)
3227}
3228
3229/// A `{"$ref": r}` reference object.
3230fn ref_value(r: impl Into<String>) -> Value {
3231    object([("$ref", Value::String(r.into()))])
3232}
3233
3234/// Reverse [`crate`]'s Markdown text escaping (HTML entities + `\_`).
3235fn unescape_text(s: &str) -> String {
3236    s.replace("&lt;", "<")
3237        .replace("&gt;", ">")
3238        .replace("&amp;", "&")
3239        .replace("\\_", "_")
3240}
3241
3242/// 64-bit FNV-1a, a stand-in for docling's `binary_hash` (we lack the source bytes
3243/// at export time; the value only needs to be a stable u64).
3244fn fnv1a(s: &str) -> u64 {
3245    let mut h: u64 = 0xcbf29ce484222325;
3246    for b in s.bytes() {
3247        h ^= b as u64;
3248        h = h.wrapping_mul(0x100000001b3);
3249    }
3250    h
3251}
3252
3253#[cfg(test)]
3254mod tests {
3255    #[cfg(feature = "preserve-order")]
3256    use crate::ImageMode;
3257    #[cfg(feature = "preserve-order")]
3258    use crate::PictureImage;
3259    use crate::{CaptionParent, ContentLayer, DoclingDocument, Node, Table};
3260    use serde_json::Value;
3261
3262    fn cell_at(text: &str, row: usize, col: usize, col_span: usize) -> crate::TableCell {
3263        crate::TableCell {
3264            text: text.into(),
3265            bbox: None,
3266            start_row: row,
3267            start_col: col,
3268            row_span: 1,
3269            col_span,
3270            column_header: row == 0,
3271            row_header: false,
3272            row_section: false,
3273        }
3274    }
3275
3276    /// First-class cells past the rows: rectangular rows are the grid and
3277    /// clip them (docling's `TableData.grid` on USPTO's overrun replicas);
3278    /// ragged rows — an xlsx table compacted by `skip_empty_cells` (#271) —
3279    /// are no grid, so the cells' extent sizes it and the omitted positions
3280    /// come back as empty filler cells in `grid` only.
3281    #[test]
3282    fn ragged_rows_take_their_grid_from_first_class_cells() {
3283        let cells = vec![
3284            cell_at("h0", 0, 0, 1),
3285            cell_at("h2", 0, 2, 1),
3286            cell_at("wide", 1, 0, 3),
3287        ];
3288        let export = |rows: Vec<Vec<String>>| -> Value {
3289            let mut doc = DoclingDocument::new("t");
3290            doc.push(Node::Table(Table {
3291                rows,
3292                cells: Some(cells.clone()),
3293                ..Table::default()
3294            }));
3295            let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3296            v["tables"][0]["data"].clone()
3297        };
3298        // Compacted rows: 2 and 3 wide, the cells say the grid is 3 wide.
3299        let data = export(vec![
3300            vec!["h0".into(), "h2".into()],
3301            vec!["wide".into(), "wide".into(), "wide".into()],
3302        ]);
3303        assert_eq!(data["num_rows"], 2);
3304        assert_eq!(data["num_cols"], 3);
3305        assert_eq!(data["table_cells"].as_array().unwrap().len(), 3);
3306        let row0: Vec<&str> = data["grid"][0]
3307            .as_array()
3308            .unwrap()
3309            .iter()
3310            .map(|c| c["text"].as_str().unwrap())
3311            .collect();
3312        assert_eq!(row0, ["h0", "", "h2"]);
3313        assert_eq!(data["table_cells"][2]["col_span"], 3);
3314        // Rectangular rows narrower than the cells: the rows win, the cells
3315        // are clipped to them exactly as before.
3316        let data = export(vec![
3317            vec!["h0".into(), "h2".into()],
3318            vec!["wide".into(), "wide".into()],
3319        ]);
3320        assert_eq!(data["num_cols"], 2);
3321        assert_eq!(data["grid"][0].as_array().unwrap().len(), 2);
3322    }
3323
3324    #[cfg(feature = "preserve-order")]
3325    fn doc_with_image() -> DoclingDocument {
3326        let mut doc = DoclingDocument::new("t");
3327        doc.push(Node::Picture {
3328            caption: Some("Fig 1".into()),
3329            caption_href: None,
3330            image: Some(PictureImage {
3331                dpi: crate::PictureImage::DEFAULT_DPI,
3332                mimetype: "image/png".into(),
3333                width: 4,
3334                height: 2,
3335                data: b"foobar".to_vec(),
3336                display_size: None,
3337            }),
3338            classification: None,
3339            description: None,
3340            caption_parent: Default::default(),
3341            caption_location: None,
3342        });
3343        doc
3344    }
3345
3346    /// #402: a deck's speaker notes are content, and docling puts them in the
3347    /// JSON on the `notes` layer so a consumer reading only JSON can pick them
3348    /// out. Markdown still serializes the body layer alone, and page furniture
3349    /// stays out of the JSON, where docling does keep it.
3350    #[test]
3351    fn notes_layer_items_reach_the_json_but_furniture_does_not() {
3352        let mut doc = DoclingDocument::new("t");
3353        doc.push(Node::Heading {
3354            level: 1,
3355            text: "Slide One".into(),
3356        });
3357        doc.push(Node::Furniture {
3358            layer: ContentLayer::Notes,
3359            inner: Box::new(Node::Located {
3360                location: [0, 0, 0, 0],
3361                inner: Box::new(Node::Paragraph {
3362                    text: "Speaker note for slide 1.".into(),
3363                }),
3364            }),
3365        });
3366        doc.push(Node::Furniture {
3367            layer: ContentLayer::Furniture,
3368            inner: Box::new(Node::Paragraph {
3369                text: "page header".into(),
3370            }),
3371        });
3372
3373        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3374        let texts = v["texts"].as_array().unwrap();
3375        assert_eq!(
3376            texts
3377                .iter()
3378                .map(|t| (
3379                    t["label"].as_str().unwrap(),
3380                    t["content_layer"].as_str().unwrap(),
3381                    t["text"].as_str().unwrap()
3382                ))
3383                .collect::<Vec<_>>(),
3384            vec![
3385                ("title", "body", "Slide One"),
3386                ("text", "notes", "Speaker note for slide 1."),
3387            ],
3388            "the note is carried on its own layer; the furniture is not carried"
3389        );
3390        // The body layer is what Markdown serializes, so it does not change.
3391        assert_eq!(doc.export_to_markdown(), "# Slide One\n");
3392    }
3393
3394    /// #410: a backend that describes merged ranges only as continuation
3395    /// flags (xlsx `<mergeCell>`, docx `gridSpan`/`vMerge`) gets docling's
3396    /// one-`TableCell`-per-range JSON: the anchor's offsets and spans, the
3397    /// entry repeated across the grid positions it covers — not a 1×1 cell
3398    /// per position with the text copied into each.
3399    #[test]
3400    fn continuation_flags_become_spanning_cells() {
3401        let mut doc = DoclingDocument::new("t");
3402        // A1:C2 merged ("merged"), then a plain row underneath.
3403        let rows = vec![
3404            vec!["merged".to_string(), "merged".into(), "merged".into()],
3405            vec!["merged".to_string(), "merged".into(), "merged".into()],
3406            vec!["a".to_string(), "b".into(), "c".into()],
3407        ];
3408        doc.push(Node::Table(crate::Table {
3409            footnotes: Vec::new(),
3410            rows,
3411            location: None,
3412            structure: Some(crate::TableStructure {
3413                header_row: vec![true, false, false],
3414                col_continuation: vec![
3415                    vec![false, true, true],
3416                    vec![false, true, true],
3417                    vec![false, false, false],
3418                ],
3419                row_continuation: vec![
3420                    vec![false, false, false],
3421                    vec![true, true, true],
3422                    vec![false, false, false],
3423                ],
3424                row_header: Vec::new(),
3425                col_header: Vec::new(),
3426            }),
3427            cell_blocks: None,
3428            cells: None,
3429            caption: None,
3430            caption_parent: Default::default(),
3431            caption_location: None,
3432        }));
3433        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3434        let data = &v["tables"][0]["data"];
3435        assert_eq!(data["num_rows"], 3);
3436        assert_eq!(data["num_cols"], 3);
3437        let cells = data["table_cells"].as_array().unwrap();
3438        assert_eq!(
3439            cells.len(),
3440            4,
3441            "one cell for the range, three for the plain row"
3442        );
3443        assert_eq!(
3444            cells[0],
3445            serde_json::json!({
3446                "row_span": 2, "col_span": 3,
3447                "start_row_offset_idx": 0, "end_row_offset_idx": 2,
3448                "start_col_offset_idx": 0, "end_col_offset_idx": 3,
3449                "text": "merged", "column_header": true, "row_header": false,
3450                "row_section": false, "fillable": false,
3451            })
3452        );
3453        assert_eq!(cells[1]["text"], "a");
3454        assert_eq!(cells[1]["row_span"], 1);
3455        assert_eq!(cells[1]["column_header"], false);
3456        // The grid repeats the range's entry at every position it covers.
3457        let grid = data["grid"].as_array().unwrap();
3458        assert_eq!(grid.len(), 3);
3459        for (r, row) in grid.iter().take(2).enumerate() {
3460            for (c, cell) in row.as_array().unwrap().iter().enumerate() {
3461                assert_eq!(*cell, cells[0], "grid[{r}][{c}]");
3462            }
3463        }
3464        assert_eq!(grid[2][2]["text"], "c");
3465    }
3466
3467    /// A [`Node::Prov`] wrapper is docling's provenance verbatim — the exact
3468    /// box in a top-left origin, the backend's charspan — and the page marker
3469    /// before it sizes the page; a chart's caption becomes a sibling of the
3470    /// picture in the container, listed first, sharing the chart's box with
3471    /// a charspan over its text. That is the JSON shape of an XLSX sheet.
3472    #[test]
3473    fn exact_provenance_pages_and_chart_captions_follow_docling() {
3474        let mut doc = DoclingDocument::new("t");
3475        doc.push(Node::PageInfo {
3476            page_no: 1,
3477            width: 3.0,
3478            height: 4.0,
3479        });
3480        let table = crate::Table {
3481            rows: vec![vec!["a".to_string(), "b".into()]],
3482            ..Default::default()
3483        };
3484        doc.push(Node::Group {
3485            label: "sheet".into(),
3486            name: Some("Data".into()),
3487            layer: None,
3488            children: vec![
3489                // Node order is the position-sorted one; creation order (the
3490                // `seq`) had the chart first — so the chart is `#/pictures/0`
3491                // *and* its caption `#/texts/0`, while the table stays the
3492                // group's first child.
3493                Node::Prov {
3494                    page_no: 1,
3495                    bbox: [0.0, 0.0, 3.0, 4.0],
3496                    charspan: [0, 0],
3497                    bottom_left: false,
3498                    seq: Some(1),
3499                    inner: Box::new(Node::Table(table.clone())),
3500                },
3501                Node::Prov {
3502                    page_no: 1,
3503                    bbox: [0.0, 1.0, 1.0, 1.0],
3504                    charspan: [0, 0],
3505                    bottom_left: false,
3506                    seq: Some(0),
3507                    inner: Box::new(Node::Chart {
3508                        kind: "bar_chart".into(),
3509                        table,
3510                        caption: Some("Sales".into()),
3511                        location: Some([0, 128, 170, 128]),
3512                    }),
3513                },
3514            ],
3515        });
3516        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3517        assert_eq!(
3518            v["pages"],
3519            serde_json::json!({"1": {"size": {"width": 3.0, "height": 4.0}, "page_no": 1}})
3520        );
3521        assert_eq!(
3522            v["tables"][0]["prov"],
3523            serde_json::json!([{
3524                "page_no": 1,
3525                "bbox": {"l": 0.0, "t": 0.0, "r": 3.0, "b": 4.0, "coord_origin": "TOPLEFT"},
3526                "charspan": [0, 0],
3527            }])
3528        );
3529        assert_eq!(v["tables"][0]["data"]["orientation"], "rot_0");
3530        // The caption is the group's child *before* the picture, parented to
3531        // the group, and referenced by the picture.
3532        let sheet = &v["groups"][0];
3533        assert_eq!(
3534            sheet["children"],
3535            serde_json::json!([
3536                {"$ref": "#/tables/0"}, {"$ref": "#/texts/0"}, {"$ref": "#/pictures/0"}
3537            ])
3538        );
3539        let cap = &v["texts"][0];
3540        assert_eq!(cap["label"], "caption");
3541        assert_eq!(cap["parent"], serde_json::json!({"$ref": "#/groups/0"}));
3542        assert_eq!(cap["prov"][0]["charspan"], serde_json::json!([0, 5]));
3543        assert_eq!(cap["prov"][0]["bbox"]["b"], 1.0);
3544        let pic = &v["pictures"][0];
3545        assert_eq!(pic["captions"], serde_json::json!([{"$ref": "#/texts/0"}]));
3546        assert_eq!(pic["prov"][0]["charspan"], serde_json::json!([0, 0]));
3547        assert_eq!(pic["prov"][0]["bbox"]["coord_origin"], "TOPLEFT");
3548        assert_eq!(
3549            pic["meta"]["classification"]["predictions"][0]["class_name"],
3550            "bar_chart"
3551        );
3552        assert_eq!(pic["meta"]["tabular_chart"]["chart_data"]["num_cols"], 2);
3553    }
3554
3555    /// An all-zero location is the "no geometry" sentinel — a slide's speaker
3556    /// notes carry one — and docling writes it as a zero bbox, not as a box
3557    /// spanning the whole page, which is what denormalizing the grid gives.
3558    #[test]
3559    fn a_zero_location_is_a_zero_bbox_not_the_whole_page() {
3560        let mut doc = DoclingDocument::new("t");
3561        doc.push(Node::PageInfo {
3562            page_no: 1,
3563            width: 12192000.0,
3564            height: 6858000.0,
3565        });
3566        doc.push(Node::Furniture {
3567            layer: ContentLayer::Notes,
3568            inner: Box::new(Node::Located {
3569                location: [0, 0, 0, 0],
3570                inner: Box::new(Node::Paragraph {
3571                    text: "a note".into(),
3572                }),
3573            }),
3574        });
3575        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3576        let prov = &v["texts"][0]["prov"][0];
3577        assert_eq!(prov["page_no"], 1);
3578        assert_eq!(prov["charspan"], serde_json::json!([0, 6]));
3579        assert_eq!(
3580            prov["bbox"],
3581            serde_json::json!({"l": 0.0, "t": 0.0, "r": 0.0, "b": 0.0, "coord_origin": "BOTTOMLEFT"})
3582        );
3583        // The page itself is recorded at its true size.
3584        assert_eq!(
3585            v["pages"]["1"]["size"],
3586            serde_json::json!({"width": 12192000.0, "height": 6858000.0})
3587        );
3588    }
3589
3590    /// #171: PageInfo markers become the `pages` map, and `Located` wrappers /
3591    /// node-level locations become per-item `prov` — the 0–511 grid
3592    /// denormalized against the page into BOTTOMLEFT points. Without markers
3593    /// (every declarative backend) the JSON stays exactly as before: empty
3594    /// `pages`, `prov: []` even for located nodes.
3595    /// #609: a PDF caption and a checkbox item carry their own page and box.
3596    /// docling's reading-order model gives every caption the cluster it came
3597    /// from (`_add_caption_or_footnote`), not its picture's or table's, and
3598    /// a checkbox is a text item like any other.
3599    #[test]
3600    fn captions_and_checkboxes_carry_their_own_prov() {
3601        let mut doc = DoclingDocument::new("t");
3602        doc.push(Node::PageInfo {
3603            page_no: 1,
3604            width: 512.0,
3605            height: 512.0,
3606        });
3607        doc.push(Node::Located {
3608            location: [64, 64, 448, 256],
3609            inner: Box::new(Node::Picture {
3610                caption: Some("Figure 1".into()),
3611                caption_href: None,
3612                image: None,
3613                classification: None,
3614                description: None,
3615                caption_parent: CaptionParent::Item,
3616                caption_location: Some([64, 264, 448, 280]),
3617            }),
3618        });
3619        doc.push(Node::PageInfo {
3620            page_no: 2,
3621            width: 512.0,
3622            height: 512.0,
3623        });
3624        doc.push(Node::Located {
3625            location: [64, 120, 448, 300],
3626            inner: Box::new(Node::Table(Table {
3627                rows: vec![vec!["a".into()]],
3628                caption: Some("Table 1".into()),
3629                caption_parent: CaptionParent::Item,
3630                caption_location: Some([64, 100, 300, 112]),
3631                ..Table::default()
3632            })),
3633        });
3634        doc.push(Node::Located {
3635            location: [64, 320, 200, 332],
3636            inner: Box::new(Node::CheckboxItem {
3637                checked: false,
3638                text: "First option".into(),
3639            }),
3640        });
3641        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3642        let texts = v["texts"].as_array().unwrap();
3643        let by_text = |t: &str| texts.iter().find(|x| x["text"] == t).expect(t).clone();
3644        let bbox = |p: &Value| {
3645            let b = &p["prov"][0]["bbox"];
3646            [&b["l"], &b["t"], &b["r"], &b["b"]].map(|x| x.as_f64().unwrap())
3647        };
3648        // Figure 1: its own box (grid == points on a 512 page, flipped to
3649        // BOTTOMLEFT), not the picture's.
3650        let fig = by_text("Figure 1");
3651        assert_eq!(fig["prov"][0]["page_no"], 1);
3652        assert_eq!(bbox(&fig), [64.0, 248.0, 448.0, 232.0]);
3653        assert_eq!(fig["prov"][0]["charspan"], serde_json::json!([0, 8]));
3654        assert_eq!(fig["parent"]["$ref"], "#/pictures/0");
3655        assert_eq!(v["pictures"][0]["prov"][0]["bbox"]["t"], 448.0);
3656        // Table 1 on page 2, its own box; the table keeps its own.
3657        let tab = by_text("Table 1");
3658        assert_eq!(tab["prov"][0]["page_no"], 2);
3659        assert_eq!(bbox(&tab), [64.0, 412.0, 300.0, 400.0]);
3660        assert_eq!(v["tables"][0]["prov"][0]["bbox"]["t"], 392.0);
3661        // The checkbox item.
3662        let cb = by_text("First option");
3663        assert_eq!(cb["label"], "checkbox_unselected");
3664        assert_eq!(cb["prov"][0]["page_no"], 2);
3665        assert_eq!(bbox(&cb), [64.0, 192.0, 200.0, 180.0]);
3666        assert!(texts
3667            .iter()
3668            .all(|t| !t["prov"].as_array().unwrap().is_empty()));
3669
3670        // Without a caption box (every declarative backend) the caption
3671        // still has no prov, and nothing else moves.
3672        let mut plain = DoclingDocument::new("t");
3673        plain.push(Node::PageInfo {
3674            page_no: 1,
3675            width: 512.0,
3676            height: 512.0,
3677        });
3678        plain.push(Node::Located {
3679            location: [64, 64, 448, 256],
3680            inner: Box::new(Node::Picture {
3681                caption: Some("Figure 1".into()),
3682                caption_href: None,
3683                image: None,
3684                classification: None,
3685                description: None,
3686                caption_parent: CaptionParent::Item,
3687                caption_location: None,
3688            }),
3689        });
3690        let v: Value = serde_json::from_str(&plain.export_to_json()).unwrap();
3691        assert_eq!(v["texts"][0]["prov"], serde_json::json!([]));
3692        assert_eq!(v["pictures"][0]["prov"][0]["bbox"]["t"], 448.0);
3693    }
3694
3695    #[test]
3696    fn page_markers_produce_pages_and_prov() {
3697        let mut doc = DoclingDocument::new("t");
3698        doc.push(Node::PageInfo {
3699            page_no: 1,
3700            width: 512.0,
3701            height: 1024.0,
3702        });
3703        doc.push(Node::Located {
3704            location: [128, 64, 256, 128], // quarter/eighth points of the grid
3705            inner: Box::new(Node::Paragraph {
3706                text: "hello".into(),
3707            }),
3708        });
3709        doc.push(Node::Table(Table {
3710            rows: vec![vec!["a".into()]],
3711            location: Some([0, 0, 512, 512]),
3712            ..Table::default()
3713        }));
3714        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3715        assert_eq!(v["pages"]["1"]["page_no"], 1);
3716        assert_eq!(v["pages"]["1"]["size"]["width"], 512.0);
3717        assert_eq!(v["pages"]["1"]["size"]["height"], 1024.0);
3718        // 512-wide page: grid x scales 1:1; 1024-high: grid y doubles, then
3719        // flips to the BOTTOMLEFT origin (t from grid-top 64 → 1024-128=896).
3720        let prov = &v["texts"][0]["prov"][0];
3721        assert_eq!(prov["page_no"], 1);
3722        assert_eq!(prov["bbox"]["l"], 128.0);
3723        assert_eq!(prov["bbox"]["t"], 896.0);
3724        assert_eq!(prov["bbox"]["r"], 256.0);
3725        assert_eq!(prov["bbox"]["b"], 768.0);
3726        assert_eq!(prov["bbox"]["coord_origin"], "BOTTOMLEFT");
3727        assert_eq!(prov["charspan"][1], 5);
3728        // The table adopts its own location field; charspan is [0, 0].
3729        let tprov = &v["tables"][0]["prov"][0];
3730        assert_eq!(tprov["bbox"]["t"], 1024.0);
3731        assert_eq!(tprov["bbox"]["b"], 0.0);
3732        assert_eq!(tprov["charspan"][1], 0);
3733
3734        // No markers → the pre-#171 shape, byte for byte.
3735        let mut plain = DoclingDocument::new("t");
3736        plain.push(Node::Located {
3737            location: [1, 2, 3, 4],
3738            inner: Box::new(Node::Paragraph { text: "x".into() }),
3739        });
3740        let v: Value = serde_json::from_str(&plain.export_to_json()).unwrap();
3741        assert_eq!(v["pages"], serde_json::json!({}));
3742        assert_eq!(v["texts"][0]["prov"], serde_json::json!([]));
3743    }
3744
3745    /// PDF page headers/footers reach the JSON as docling writes them:
3746    /// body-parented text items on the furniture layer, with their box.
3747    #[test]
3748    fn page_furniture_becomes_furniture_layer_text() {
3749        let mut doc = DoclingDocument::new("t");
3750        doc.push(Node::PageInfo {
3751            page_no: 1,
3752            width: 512.0,
3753            height: 512.0,
3754        });
3755        doc.push(Node::PageFurniture {
3756            footer: false,
3757            location: [10, 0, 100, 20],
3758            text: "Chapter 1".into(),
3759        });
3760        doc.push(Node::Paragraph {
3761            text: "body".into(),
3762        });
3763        doc.push(Node::PageFurniture {
3764            footer: true,
3765            location: [400, 490, 500, 512],
3766            text: "1.10.2".into(),
3767        });
3768        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3769        let texts = v["texts"].as_array().unwrap();
3770        assert_eq!(texts.len(), 3);
3771        assert_eq!(texts[0]["label"], "page_header");
3772        assert_eq!(texts[0]["content_layer"], "furniture");
3773        assert_eq!(texts[0]["parent"]["$ref"], "#/body");
3774        assert_eq!(texts[0]["prov"][0]["bbox"]["l"], 10.0);
3775        assert_eq!(texts[1]["content_layer"], "body");
3776        assert_eq!(texts[1]["prov"], serde_json::json!([]));
3777        assert_eq!(texts[2]["label"], "page_footer");
3778        assert_eq!(texts[2]["text"], "1.10.2");
3779        assert_eq!(texts[2]["content_layer"], "furniture");
3780    }
3781
3782    #[test]
3783    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
3784    fn picture_image_in_markdown_modes_and_json() {
3785        let doc = doc_with_image();
3786        // placeholder (default) ignores the image
3787        assert!(doc.export_to_markdown().contains("<!-- image -->"));
3788        // embedded → base64 data URI (b"foobar" → "Zm9vYmFy")
3789        let (md, files) = doc.export_to_markdown_with_images(ImageMode::Embedded, "artifacts");
3790        assert!(
3791            md.contains("![Image](data:image/png;base64,Zm9vYmFy)"),
3792            "got:\n{md}"
3793        );
3794        assert!(files.is_empty());
3795        // referenced → file link + collected bytes
3796        let (md, files) = doc.export_to_markdown_with_images(ImageMode::Referenced, "artifacts");
3797        assert!(
3798            md.contains("![Image](artifacts/image_000000.png)"),
3799            "got:\n{md}"
3800        );
3801        assert_eq!(
3802            files,
3803            vec![("artifacts/image_000000.png".to_string(), b"foobar".to_vec())]
3804        );
3805        // JSON carries the ImageRef (data URI + size — floats, as docling's
3806        // `Size` is — placed before `annotations`).
3807        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3808        assert_eq!(v["pictures"][0]["image"]["mimetype"], "image/png");
3809        assert_eq!(v["pictures"][0]["image"]["size"]["width"], 4.0);
3810        let keys: Vec<&str> = v["pictures"][0]
3811            .as_object()
3812            .unwrap()
3813            .keys()
3814            .map(String::as_str)
3815            .collect();
3816        assert_eq!(&keys[keys.len() - 2..], ["image", "annotations"]);
3817        assert_eq!(
3818            v["pictures"][0]["image"]["uri"],
3819            "data:image/png;base64,Zm9vYmFy"
3820        );
3821    }
3822
3823    #[test]
3824    fn exports_docling_schema() {
3825        let mut doc = DoclingDocument::new("t");
3826        doc.push(Node::Heading {
3827            level: 1,
3828            text: "Title".into(),
3829        });
3830        doc.push(Node::Heading {
3831            level: 2,
3832            text: "Sec".into(),
3833        });
3834        doc.push(Node::Paragraph {
3835            text: "Body &amp; more".into(),
3836        }); // markdown-escaped
3837        doc.push(Node::ListItem {
3838            ordered: false,
3839            number: 0,
3840            first_in_list: true,
3841            text: "one".into(),
3842            level: 0,
3843            marker: None,
3844            location: None,
3845            dclx: None,
3846            href: None,
3847            layer: None,
3848        });
3849        doc.push(Node::ListItem {
3850            ordered: false,
3851            number: 0,
3852            first_in_list: false,
3853            text: "two".into(),
3854            level: 0,
3855            marker: None,
3856            location: None,
3857            dclx: None,
3858            href: None,
3859            layer: None,
3860        });
3861        doc.push(Node::Table(Table {
3862            footnotes: Vec::new(),
3863            rows: vec![vec!["A".into(), "B".into()]],
3864            location: None,
3865            structure: None,
3866            cell_blocks: None,
3867            cells: None,
3868            caption: None,
3869            caption_parent: Default::default(),
3870            caption_location: None,
3871        }));
3872
3873        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3874        assert_eq!(v["schema_name"], "DoclingDocument");
3875        assert_eq!(v["version"], "1.11.0");
3876        assert_eq!(v["texts"][0]["label"], "title");
3877        assert_eq!(v["texts"][1]["label"], "section_header");
3878        assert_eq!(v["texts"][1]["level"], 1); // heading level 2 → docling level 1
3879        assert_eq!(v["texts"][2]["text"], "Body & more"); // un-escaped for the wire format
3880                                                          // consecutive list items fold into one list group, parented to it
3881        assert_eq!(v["groups"][0]["label"], "list");
3882        assert_eq!(v["groups"][0]["children"].as_array().unwrap().len(), 2);
3883        assert_eq!(v["texts"][3]["parent"]["$ref"], "#/groups/0");
3884        assert_eq!(v["texts"][3]["marker"], "-");
3885        // table grid + header flag
3886        assert_eq!(v["tables"][0]["data"]["num_cols"], 2);
3887        assert_eq!(v["tables"][0]["data"]["grid"][0][0]["column_header"], true);
3888    }
3889    /// A named group on a non-body layer — docling's hidden spreadsheet sheet:
3890    /// the group carries the sheet's name and the `invisible` layer, and every
3891    /// item inside it carries the layer too.
3892    #[test]
3893    fn a_layered_group_stamps_its_whole_subtree() {
3894        let doc = DoclingDocument {
3895            name: "s".into(),
3896            nodes: vec![
3897                Node::Group {
3898                    label: "sheet".into(),
3899                    name: Some("Sheet1".into()),
3900                    layer: None,
3901                    children: vec![Node::Paragraph {
3902                        text: "visible".into(),
3903                    }],
3904                },
3905                Node::Group {
3906                    label: "sheet".into(),
3907                    name: Some("Sheet2".into()),
3908                    layer: Some(ContentLayer::Invisible),
3909                    children: vec![Node::Paragraph {
3910                        text: "hidden".into(),
3911                    }],
3912                },
3913            ],
3914            ..DoclingDocument::new("s")
3915        };
3916        let v = crate::json::to_json(&doc);
3917        assert_eq!(v["groups"][0]["label"], "sheet");
3918        assert_eq!(v["groups"][0]["name"], "Sheet1");
3919        assert_eq!(v["groups"][0]["content_layer"], "body");
3920        assert_eq!(v["texts"][0]["content_layer"], "body");
3921        assert_eq!(v["groups"][1]["name"], "Sheet2");
3922        assert_eq!(v["groups"][1]["content_layer"], "invisible");
3923        assert_eq!(v["texts"][1]["content_layer"], "invisible");
3924        // The group's children are the items, and the body holds the groups.
3925        assert_eq!(v["groups"][1]["children"][0]["$ref"], "#/texts/1");
3926        assert_eq!(v["body"]["children"][1]["$ref"], "#/groups/1");
3927    }
3928
3929    /// A backend-built item tree is serialized as it is: items numbered in
3930    /// creation order per bucket (a field region's part texts included), the
3931    /// tree's parents / children / layers, docling's field order for
3932    /// `formatting`, `hyperlink`, `level`, `enumerated`/`marker`, a rich
3933    /// cell's `ref` on `table_cells` only, raw cell text.
3934    /// The DOCX tree's extras: an item `delete`d (docling's `delete_items`,
3935    /// the spacer between two items of a resumed list) is neither written nor
3936    /// numbered, `comments` back-refs sit between `prov` and `orig`, and a
3937    /// chart picture carries `classification` plus `tabular_chart`.
3938    #[test]
3939    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
3940    fn deleted_items_comment_refs_and_chart_meta_in_the_tree() {
3941        use crate::tree::{ItemTree, TreeKind};
3942        let mut t = ItemTree::default();
3943        let text = |txt: &str| TreeKind::Text {
3944            label: "text".into(),
3945            text: txt.into(),
3946            orig: None,
3947            formatting: None,
3948            hyperlink: None,
3949            level: None,
3950            list: None,
3951        };
3952        let a = t.add(None, None, text("a"));
3953        let blank = t.add(None, None, text(""));
3954        let b = t.add(None, None, text("b"));
3955        t.delete(blank);
3956        let group = t.add(
3957            None,
3958            Some(ContentLayer::Notes),
3959            TreeKind::Group {
3960                label: "comment_section".into(),
3961                name: "comment-0".into(),
3962            },
3963        );
3964        t.add(Some(group), Some(ContentLayer::Notes), text("note"));
3965        t.items[a].comments.push(group);
3966        t.add(
3967            None,
3968            None,
3969            TreeKind::Picture {
3970                captions: Vec::new(),
3971                image: None,
3972                extra_classes: Vec::new(),
3973                classification: Some("bar_chart".into()),
3974                description: None,
3975                confidence: None,
3976                chart: Some(Table {
3977                    rows: vec![vec!["".into(), "s".into()], vec!["c".into(), "1".into()]],
3978                    ..Table::default()
3979                }),
3980                dpi: None,
3981            },
3982        );
3983        assert_eq!(t.last_text(), Some(4), "the note; the blank is skipped");
3984        assert_eq!(t.bucket_index(b), 1, "numbered past the deleted item");
3985        let mut doc = DoclingDocument::new("t");
3986        doc.tree = Some(t);
3987        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
3988        let texts = v["texts"].as_array().unwrap();
3989        assert_eq!(texts.len(), 3);
3990        assert_eq!(texts[1]["text"], "b");
3991        assert_eq!(texts[1]["self_ref"], "#/texts/1");
3992        assert_eq!(
3993            v["body"]["children"],
3994            serde_json::json!([{"$ref": "#/texts/0"}, {"$ref": "#/texts/1"}, {"$ref": "#/groups/0"}, {"$ref": "#/pictures/0"}])
3995        );
3996        let keys: Vec<&str> = texts[0]
3997            .as_object()
3998            .unwrap()
3999            .keys()
4000            .map(String::as_str)
4001            .collect();
4002        assert_eq!(
4003            keys,
4004            vec![
4005                "self_ref",
4006                "parent",
4007                "children",
4008                "content_layer",
4009                "label",
4010                "prov",
4011                "comments",
4012                "orig",
4013                "text"
4014            ]
4015        );
4016        assert_eq!(
4017            texts[0]["comments"],
4018            serde_json::json!([{"$ref": "#/groups/0"}])
4019        );
4020        assert!(texts[1].get("comments").is_none());
4021        let meta = &v["pictures"][0]["meta"];
4022        assert_eq!(
4023            meta["classification"]["predictions"][0]["class_name"],
4024            "bar_chart"
4025        );
4026        assert_eq!(meta["tabular_chart"]["chart_data"]["num_rows"], 2);
4027    }
4028
4029    /// A tree item's `TreeProv` is written verbatim — a raw EMU box with
4030    /// whatever origin tag the backend set (`BOTTOMLEFT` here) and a per-item
4031    /// charspan, a note's zero `TOPLEFT` box — a picture's `image.dpi` is the file's when the backend
4032    /// read one, an item without provenance writes `prov: []`, and the page
4033    /// map still comes from the flat stream's markers.
4034    #[test]
4035    fn tree_items_carry_exact_provenance_and_dpi() {
4036        use crate::tree::{ItemTree, TreeKind, TreeProv};
4037        let text = |label: &str, t: &str| TreeKind::Text {
4038            label: label.into(),
4039            text: t.into(),
4040            orig: None,
4041            formatting: None,
4042            hyperlink: None,
4043            level: None,
4044            list: None,
4045        };
4046        let mut t = ItemTree::default();
4047        let slide = t.add(
4048            None,
4049            None,
4050            TreeKind::Group {
4051                label: "chapter".into(),
4052                name: "slide-0".into(),
4053            },
4054        );
4055        t.add_with_prov(
4056            Some(slide),
4057            None,
4058            text("paragraph", "héllo"),
4059            TreeProv {
4060                page_no: 1,
4061                bbox: [914400.0, 1828800.0, 2743200.0, 457200.0],
4062                bottom_left: true,
4063                charspan: [0, 5],
4064            },
4065        );
4066        t.add_with_prov(
4067            Some(slide),
4068            None,
4069            TreeKind::Picture {
4070                captions: Vec::new(),
4071                image: Some(crate::PictureImage {
4072                    dpi: crate::PictureImage::DEFAULT_DPI,
4073                    mimetype: "image/png".into(),
4074                    width: 2,
4075                    height: 2,
4076                    data: vec![0],
4077                    display_size: None,
4078                }),
4079                extra_classes: Vec::new(),
4080                classification: None,
4081                description: None,
4082                confidence: None,
4083                chart: None,
4084                dpi: Some(300),
4085            },
4086            TreeProv {
4087                page_no: 1,
4088                bbox: [0.0; 4],
4089                bottom_left: false,
4090                charspan: [0, 0],
4091            },
4092        );
4093        t.add(
4094            Some(slide),
4095            Some(ContentLayer::Notes),
4096            text("text", "no geometry"),
4097        );
4098        let mut doc = DoclingDocument::new("t");
4099        doc.push(Node::PageInfo {
4100            page_no: 1,
4101            width: 9144000.0,
4102            height: 6858000.0,
4103        });
4104        doc.tree = Some(t);
4105        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4106        assert_eq!(
4107            v["texts"][0]["prov"],
4108            serde_json::json!([{
4109                "page_no": 1,
4110                "bbox": { "l": 914400.0, "t": 1828800.0, "r": 2743200.0, "b": 457200.0, "coord_origin": "BOTTOMLEFT" },
4111                "charspan": [0, 5],
4112            }])
4113        );
4114        assert_eq!(v["texts"][0]["label"], "paragraph");
4115        assert_eq!(
4116            v["pictures"][0]["prov"][0]["bbox"]["coord_origin"],
4117            "TOPLEFT"
4118        );
4119        assert_eq!(v["pictures"][0]["image"]["dpi"], 300);
4120        assert_eq!(v["texts"][1]["prov"], serde_json::json!([]));
4121        assert_eq!(v["texts"][1]["content_layer"], "notes");
4122        assert_eq!(v["pages"]["1"]["size"]["width"], 9144000.0);
4123        assert_eq!(v["pages"]["1"]["page_no"], 1);
4124    }
4125
4126    /// docling-core's `validate_document` clamps every provenance box (and a
4127    /// one-page table's cell boxes) into its page — the state every
4128    /// `ConversionResult` leaves a document in, so the state docling's JSON
4129    /// shows. A box on a page the document does not describe is left alone.
4130    #[test]
4131    fn provenance_boxes_are_clamped_to_their_page() {
4132        let mut doc = DoclingDocument::new("t");
4133        doc.push(Node::PageInfo {
4134            page_no: 1,
4135            width: 10.0,
4136            height: 8.0,
4137        });
4138        doc.push(Node::Prov {
4139            page_no: 1,
4140            bbox: [-1.0, 2.0, 12.0, 9.5],
4141            charspan: [0, 1],
4142            bottom_left: false,
4143            seq: None,
4144            inner: Box::new(Node::Paragraph { text: "x".into() }),
4145        });
4146        let mut table = Table {
4147            rows: vec![vec!["a".into()]],
4148            ..Table::default()
4149        };
4150        table.cells = Some(vec![crate::TableCell {
4151            text: "a".into(),
4152            bbox: Some([1.0, 1.0, 11.0, 9.0]),
4153            start_row: 0,
4154            start_col: 0,
4155            row_span: 1,
4156            col_span: 1,
4157            column_header: false,
4158            row_header: false,
4159            row_section: false,
4160        }]);
4161        doc.push(Node::Prov {
4162            page_no: 1,
4163            bbox: [0.0, 0.0, 10.0, 8.0],
4164            charspan: [0, 0],
4165            bottom_left: false,
4166            seq: None,
4167            inner: Box::new(Node::Table(table)),
4168        });
4169        doc.push(Node::Prov {
4170            page_no: 7,
4171            bbox: [-5.0, 0.0, 50.0, 50.0],
4172            charspan: [0, 1],
4173            bottom_left: false,
4174            seq: None,
4175            inner: Box::new(Node::Paragraph { text: "y".into() }),
4176        });
4177        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4178        assert_eq!(
4179            v["texts"][0]["prov"][0]["bbox"],
4180            serde_json::json!({ "l": 0.0, "t": 2.0, "r": 10.0, "b": 8.0, "coord_origin": "TOPLEFT" })
4181        );
4182        let cell = &v["tables"][0]["data"]["table_cells"][0]["bbox"];
4183        assert_eq!(
4184            (cell["l"].as_f64(), cell["r"].as_f64(), cell["b"].as_f64()),
4185            (Some(1.0), Some(10.0), Some(8.0))
4186        );
4187        assert_eq!(v["tables"][0]["data"]["grid"][0][0]["bbox"]["r"], 10.0);
4188        assert_eq!(
4189            v["texts"][1]["prov"][0]["bbox"]["r"], 50.0,
4190            "page 7 is not described"
4191        );
4192    }
4193
4194    /// docling-core's `validate_misplaced_list_items`, as it re-homes list
4195    /// items outside a `list` group (#527) — the refs below are what
4196    /// docling-core 2.99 writes for the same document: the two consecutive
4197    /// items on the body share a group, each item in another group gets its
4198    /// own, the runs are handled last-first (new groups and re-added texts
4199    /// numbered in that order), and a well-placed item is left alone.
4200    #[test]
4201    fn misplaced_list_items_are_wrapped_like_docling_core() {
4202        use crate::tree::{ItemTree, ListMeta, TreeKind};
4203        let mut t = ItemTree::default();
4204        let li = |txt: &str| TreeKind::Text {
4205            label: "list_item".into(),
4206            text: txt.into(),
4207            orig: None,
4208            formatting: None,
4209            hyperlink: None,
4210            level: None,
4211            list: Some(ListMeta {
4212                enumerated: false,
4213                marker: "-".into(),
4214            }),
4215        };
4216        let group = |label: &str, name: &str| TreeKind::Group {
4217            label: label.into(),
4218            name: name.into(),
4219        };
4220        t.add(None, None, li("A"));
4221        t.add(None, None, li("B"));
4222        t.add(
4223            None,
4224            None,
4225            TreeKind::Text {
4226                label: "text".into(),
4227                text: "plain".into(),
4228                orig: None,
4229                formatting: None,
4230                hyperlink: None,
4231                level: None,
4232                list: None,
4233            },
4234        );
4235        let cell = t.add(None, None, group("unspecified", "cell"));
4236        t.add(Some(cell), None, li("C"));
4237        t.add(Some(cell), None, li("D"));
4238        let list = t.add(None, None, group("list", "list"));
4239        t.add(Some(list), None, li("E"));
4240        let doc = DoclingDocument {
4241            tree: Some(t),
4242            ..DoclingDocument::new("t")
4243        };
4244        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4245        let refs = |x: &Value| -> Vec<String> {
4246            x.as_array()
4247                .unwrap()
4248                .iter()
4249                .map(|r| r["$ref"].as_str().unwrap().to_string())
4250                .collect()
4251        };
4252        assert_eq!(
4253            refs(&v["body"]["children"]),
4254            ["#/groups/4", "#/texts/0", "#/groups/0", "#/groups/1"]
4255        );
4256        assert_eq!(
4257            refs(&v["groups"][0]["children"]),
4258            ["#/groups/3", "#/groups/2"]
4259        );
4260        for (g, name, kids) in [
4261            (2, "group", vec!["#/texts/2"]),
4262            (3, "group", vec!["#/texts/3"]),
4263            (4, "group", vec!["#/texts/4", "#/texts/5"]),
4264            (1, "list", vec!["#/texts/1"]),
4265        ] {
4266            assert_eq!(v["groups"][g]["label"], "list");
4267            assert_eq!(v["groups"][g]["name"], name);
4268            assert_eq!(refs(&v["groups"][g]["children"]), kids);
4269        }
4270        let texts: Vec<&str> = v["texts"]
4271            .as_array()
4272            .unwrap()
4273            .iter()
4274            .map(|t| t["text"].as_str().unwrap())
4275            .collect();
4276        assert_eq!(texts, ["plain", "E", "D", "C", "A", "B"]);
4277        assert_eq!(v["texts"][2]["parent"]["$ref"], "#/groups/2");
4278    }
4279
4280    /// A child the tree still lists after deleting it is left out instead
4281    /// of written as `"$ref": ""`, which docling-core rejects (#527).
4282    #[test]
4283    fn a_deleted_child_is_not_written_as_an_empty_ref() {
4284        use crate::tree::{ItemTree, TreeKind};
4285        let mut t = ItemTree::default();
4286        let text = |txt: &str| TreeKind::Text {
4287            label: "text".into(),
4288            text: txt.into(),
4289            orig: None,
4290            formatting: None,
4291            hyperlink: None,
4292            level: None,
4293            list: None,
4294        };
4295        let g = t.add(
4296            None,
4297            None,
4298            TreeKind::Group {
4299                label: "unspecified".into(),
4300                name: "g".into(),
4301            },
4302        );
4303        let gone = t.add(Some(g), None, text(""));
4304        t.add(Some(g), None, text("kept"));
4305        t.items[gone].deleted = true; // still in `g`'s children
4306        let doc = DoclingDocument {
4307            tree: Some(t),
4308            ..DoclingDocument::new("t")
4309        };
4310        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4311        assert_eq!(
4312            v["groups"][0]["children"],
4313            serde_json::json!([{"$ref": "#/texts/0"}])
4314        );
4315    }
4316
4317    #[test]
4318    fn a_trimmed_run_tree_gets_the_legacy_join_in_its_runs() {
4319        use crate::tree::{ItemTree, TreeKind, TreeProv};
4320        let text = |label: &str, txt: &str| TreeKind::Text {
4321            label: label.into(),
4322            text: txt.into(),
4323            orig: None,
4324            formatting: None,
4325            hyperlink: None,
4326            level: None,
4327            list: None,
4328        };
4329        let mut t = ItemTree::default();
4330        let g = t.add(
4331            None,
4332            None,
4333            TreeKind::Group {
4334                label: "inline".into(),
4335                name: "group".into(),
4336            },
4337        );
4338        t.add(Some(g), None, text("text", "left"));
4339        let prov = TreeProv {
4340            page_no: 1,
4341            bbox: [0.0, 0.0, 1.0, 1.0],
4342            bottom_left: false,
4343            charspan: [0, 1],
4344        };
4345        t.add_with_prov(Some(g), None, text("formula", "a"), prov.clone());
4346        t.add_with_prov(Some(g), None, text("formula", "b"), prov);
4347        t.add(Some(g), None, text("text", "right"));
4348        let doc = DoclingDocument {
4349            tree: Some(t),
4350            ..DoclingDocument::new("t")
4351        };
4352        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4353        let texts: Vec<&str> = v["texts"]
4354            .as_array()
4355            .unwrap()
4356            .iter()
4357            .map(|t| t["text"].as_str().unwrap())
4358            .collect();
4359        // `left ` · `a` · ` ` (standalone, appended) · `b` · ` right`.
4360        assert_eq!(texts, ["left ", "a", "b", " right", " "]);
4361        assert_eq!(v["texts"][0]["orig"], "left");
4362        assert_eq!(v["groups"][0]["children"][2]["$ref"], "#/texts/4");
4363        assert_eq!(
4364            v["texts"][4]["prov"][0]["charspan"],
4365            serde_json::json!([0, 1])
4366        );
4367    }
4368
4369    #[test]
4370    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
4371    fn a_backend_item_tree_is_written_verbatim() {
4372        use crate::tree::{Formatting, ItemTree, ListMeta, TreeKind};
4373        let mut t = ItemTree {
4374            // Runs as written: no legacy separator injection (#997).
4375            inline_whitespace: true,
4376            ..ItemTree::default()
4377        };
4378        let text = |label: &str, txt: &str| TreeKind::Text {
4379            label: label.into(),
4380            text: txt.into(),
4381            orig: None,
4382            formatting: None,
4383            hyperlink: None,
4384            level: None,
4385            list: None,
4386        };
4387        let title = t.add(None, Some(ContentLayer::Furniture), text("title", "Page"));
4388        let h = t.add(None, None, text("title", "Heading"));
4389        let group = t.add(
4390            Some(h),
4391            None,
4392            TreeKind::Group {
4393                label: "inline".into(),
4394                name: "group".into(),
4395            },
4396        );
4397        t.add(
4398            Some(group),
4399            None,
4400            TreeKind::Text {
4401                label: "text".into(),
4402                text: "bold".into(),
4403                orig: None,
4404                formatting: Some(Formatting {
4405                    bold: true,
4406                    ..Formatting::default()
4407                }),
4408                hyperlink: Some("https://example.com/".into()),
4409                level: None,
4410                list: None,
4411            },
4412        );
4413        t.add(
4414            Some(group),
4415            None,
4416            TreeKind::Code {
4417                text: "x = 1".into(),
4418                orig: None,
4419                language: Some("python".into()),
4420                formatting: None,
4421                hyperlink: None,
4422                captions: Vec::new(),
4423            },
4424        );
4425        let sub = t.add(
4426            Some(h),
4427            None,
4428            TreeKind::Text {
4429                label: "section_header".into(),
4430                text: "Sub".into(),
4431                orig: Some("Sub\u{2019}".into()),
4432                formatting: None,
4433                hyperlink: None,
4434                level: Some(1),
4435                list: None,
4436            },
4437        );
4438        let item = t.add(
4439            Some(sub),
4440            None,
4441            TreeKind::Text {
4442                label: "list_item".into(),
4443                text: "item".into(),
4444                orig: None,
4445                formatting: None,
4446                hyperlink: None,
4447                level: None,
4448                list: Some(ListMeta {
4449                    enumerated: true,
4450                    marker: "3.".into(),
4451                }),
4452            },
4453        );
4454        let _region = t.add(
4455            Some(sub),
4456            None,
4457            TreeKind::FieldRegion {
4458                items: vec![crate::FieldItem {
4459                    marker: None,
4460                    key: Some("Name".into()),
4461                    value: Some("Duck".into()),
4462                    value_kind: Some("read_only".into()),
4463                }],
4464            },
4465        );
4466        let table = t.add(
4467            Some(sub),
4468            None,
4469            TreeKind::Table {
4470                footnotes: Vec::new(),
4471                table: Table {
4472                    rows: vec![vec!["a  \n&lt;".into(), "b".into()]],
4473                    cells: Some(vec![
4474                        crate::TableCell {
4475                            text: "a  \n&lt;".into(),
4476                            bbox: None,
4477                            start_row: 0,
4478                            start_col: 0,
4479                            row_span: 3,
4480                            col_span: 1,
4481                            column_header: false,
4482                            row_header: true,
4483                            row_section: false,
4484                        },
4485                        crate::TableCell {
4486                            text: "b".into(),
4487                            bbox: None,
4488                            start_row: 0,
4489                            start_col: 1,
4490                            row_span: 1,
4491                            col_span: 1,
4492                            column_header: false,
4493                            row_header: false,
4494                            row_section: false,
4495                        },
4496                    ]),
4497                    ..Table::default()
4498                },
4499                rich_cells: vec![(0, 1, 0)], // patched below
4500                captions: Vec::new(),
4501            },
4502        );
4503        let cell_group = t.add(
4504            Some(table),
4505            None,
4506            TreeKind::Group {
4507                label: "unspecified".into(),
4508                name: "rich_cell_group_1_0_0".into(),
4509            },
4510        );
4511        if let TreeKind::Table { rich_cells, .. } = &mut t.items[table].kind {
4512            *rich_cells = vec![(0, 1, cell_group)];
4513        }
4514        let after = t.add(Some(sub), None, text("text", "after the region"));
4515        let _ = (title, after);
4516        // A list item belongs in a `list` group — one outside it would be
4517        // re-homed by `wrap_misplaced_list_items` (created last, so the
4518        // other groups keep their numbers).
4519        let list = t.add(
4520            Some(sub),
4521            None,
4522            TreeKind::Group {
4523                label: "list".into(),
4524                name: "list".into(),
4525            },
4526        );
4527        t.reparent(item, Some(list));
4528
4529        let doc = DoclingDocument {
4530            tree: Some(t),
4531            ..DoclingDocument::new("t")
4532        };
4533        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4534        // Creation order: Page, Heading, bold, x = 1, Sub, item, [Name, Duck], after.
4535        let texts: Vec<&str> = v["texts"]
4536            .as_array()
4537            .unwrap()
4538            .iter()
4539            .map(|t| t["text"].as_str().unwrap())
4540            .collect();
4541        assert_eq!(
4542            texts,
4543            [
4544                "Page",
4545                "Heading",
4546                "bold",
4547                "x = 1",
4548                "Sub",
4549                "item",
4550                "Name",
4551                "Duck",
4552                "after the region"
4553            ]
4554        );
4555        assert_eq!(
4556            v["body"]["children"],
4557            serde_json::json!([{"$ref": "#/texts/0"}, {"$ref": "#/texts/1"}])
4558        );
4559        assert_eq!(v["texts"][0]["content_layer"], "furniture");
4560        assert_eq!(
4561            v["texts"][1]["children"],
4562            serde_json::json!([{"$ref": "#/groups/0"}, {"$ref": "#/texts/4"}])
4563        );
4564        let bold = &v["texts"][2];
4565        assert_eq!(bold["parent"]["$ref"], "#/groups/0");
4566        let keys: Vec<&str> = bold
4567            .as_object()
4568            .unwrap()
4569            .keys()
4570            .map(String::as_str)
4571            .collect();
4572        assert_eq!(
4573            keys,
4574            [
4575                "self_ref",
4576                "parent",
4577                "children",
4578                "content_layer",
4579                "label",
4580                "prov",
4581                "orig",
4582                "text",
4583                "formatting",
4584                "hyperlink"
4585            ]
4586        );
4587        assert_eq!(
4588            bold["formatting"],
4589            serde_json::json!({"bold": true, "italic": false, "underline": false, "strikethrough": false, "script": "baseline"})
4590        );
4591        let code = &v["texts"][3];
4592        assert_eq!(code["label"], "code");
4593        assert_eq!(code["code_language"], "Python");
4594        let sub = &v["texts"][4];
4595        assert_eq!(sub["orig"], "Sub\u{2019}");
4596        assert_eq!(sub["level"], 1);
4597        let item = &v["texts"][5];
4598        let keys: Vec<&str> = item
4599            .as_object()
4600            .unwrap()
4601            .keys()
4602            .map(String::as_str)
4603            .collect();
4604        assert_eq!(
4605            keys,
4606            [
4607                "self_ref",
4608                "parent",
4609                "children",
4610                "content_layer",
4611                "label",
4612                "prov",
4613                "orig",
4614                "text",
4615                "enumerated",
4616                "marker"
4617            ]
4618        );
4619        assert_eq!(item["marker"], "3.");
4620        assert_eq!(v["texts"][7]["kind"], "read_only");
4621        assert_eq!(v["field_regions"][0]["parent"]["$ref"], "#/texts/4");
4622        let table = &v["tables"][0];
4623        assert_eq!(
4624            table["children"],
4625            serde_json::json!([{"$ref": "#/groups/1"}])
4626        );
4627        let cells = table["data"]["table_cells"].as_array().unwrap();
4628        assert_eq!(
4629            cells[0]["text"], "a  \n&lt;",
4630            "raw cell text is written verbatim"
4631        );
4632        assert_eq!(
4633            cells[0]["end_row_offset_idx"], 3,
4634            "declared spans are not clamped"
4635        );
4636        assert_eq!(cells[1]["ref"], serde_json::json!({"$ref": "#/groups/1"}));
4637        assert!(cells[0].get("ref").is_none());
4638        assert!(
4639            table["data"]["grid"][0][1].get("ref").is_none(),
4640            "the grid shows plain cells"
4641        );
4642        assert_eq!(v["groups"][1]["name"], "rich_cell_group_1_0_0");
4643    }
4644
4645    /// A comment section that links its note text rather than its group — the
4646    /// spreadsheet shape, where docling-core's `add_comment` appends the text
4647    /// item's ref to each target.
4648    #[test]
4649    fn a_comment_section_can_be_referenced_by_its_note_text() {
4650        let doc = DoclingDocument {
4651            name: "c".into(),
4652            nodes: vec![
4653                Node::Commented {
4654                    comments: vec![0],
4655                    inner: Box::new(Node::Paragraph {
4656                        text: "annotated".into(),
4657                    }),
4658                },
4659                Node::CommentSection {
4660                    name: "comment-Sheet1-A1".into(),
4661                    text: "[author: A]: note".into(),
4662                    refs_note_text: true,
4663                    grouped: true,
4664                },
4665            ],
4666            ..DoclingDocument::new("c")
4667        };
4668        let v = crate::json::to_json(&doc);
4669        assert_eq!(v["groups"][0]["name"], "comment-Sheet1-A1");
4670        assert_eq!(v["texts"][0]["comments"][0]["$ref"], "#/texts/1");
4671    }
4672
4673    /// A thread's messages are notes of one `comment_section` group, which is
4674    /// listed once among its parent's children however many notes it holds.
4675    #[test]
4676    fn a_comment_thread_is_one_child_of_its_parent() {
4677        let note = |text: &str| Node::CommentSection {
4678            name: "comment-Sheet1-F7".into(),
4679            text: text.into(),
4680            refs_note_text: true,
4681            grouped: true,
4682        };
4683        let doc = DoclingDocument {
4684            name: "c".into(),
4685            nodes: vec![note("[author: A]: first"), note("[author: B]: reply")],
4686            ..DoclingDocument::new("c")
4687        };
4688        let v = crate::json::to_json(&doc);
4689        assert_eq!(v["body"]["children"].as_array().unwrap().len(), 1);
4690        assert_eq!(v["groups"][0]["children"].as_array().unwrap().len(), 2);
4691    }
4692
4693    /// docx reviewer comments: a `comment_section` group on the notes layer
4694    /// holding the note text, and a `comments` back-ref on the annotated item —
4695    /// keyed between `prov` and `orig`, the slot docling emits it in.
4696    #[test]
4697    #[cfg(feature = "preserve-order")] // field-order assertions (#970)
4698    fn comment_sections_link_back_to_their_items() {
4699        let doc = DoclingDocument {
4700            name: "c".into(),
4701            nodes: vec![
4702                Node::Commented {
4703                    comments: vec![0],
4704                    inner: Box::new(Node::Paragraph {
4705                        text: "annotated".into(),
4706                    }),
4707                },
4708                Node::Paragraph {
4709                    text: "plain".into(),
4710                },
4711                Node::CommentSection {
4712                    name: "comment-7".into(),
4713                    text: "[time: t]: note".into(),
4714                    refs_note_text: false,
4715                    grouped: true,
4716                },
4717            ],
4718            ..DoclingDocument::new("c")
4719        };
4720        let v = crate::json::to_json(&doc);
4721        // The group is the comment section; its only child is the notes text.
4722        assert_eq!(v["groups"][0]["label"], "comment_section");
4723        assert_eq!(v["groups"][0]["name"], "comment-7");
4724        assert_eq!(v["groups"][0]["content_layer"], "notes");
4725        assert_eq!(v["groups"][0]["children"][0]["$ref"], "#/texts/2");
4726        assert_eq!(v["texts"][2]["content_layer"], "notes");
4727        // The annotated item points back at the group; the plain one has no key.
4728        assert_eq!(v["texts"][0]["comments"][0]["$ref"], "#/groups/0");
4729        assert!(v["texts"][1].get("comments").is_none());
4730        // docling's key order: … prov, comments, orig, text.
4731        let keys: Vec<&str> = v["texts"][0]
4732            .as_object()
4733            .unwrap()
4734            .keys()
4735            .map(String::as_str)
4736            .collect();
4737        assert_eq!(
4738            &keys[keys.len() - 4..],
4739            &["prov", "comments", "orig", "text"]
4740        );
4741    }
4742
4743    fn picture(caption: &str, caption_parent: CaptionParent) -> Node {
4744        Node::Picture {
4745            caption: Some(caption.into()),
4746            caption_href: None,
4747            image: None,
4748            classification: None,
4749            description: None,
4750            caption_parent,
4751            caption_location: None,
4752        }
4753    }
4754
4755    fn group(children: Vec<Node>) -> Node {
4756        Node::Group {
4757            label: "section".into(),
4758            name: None,
4759            layer: None,
4760            children,
4761        }
4762    }
4763
4764    fn refs(v: &Value) -> Vec<&str> {
4765        v.as_array()
4766            .unwrap()
4767            .iter()
4768            .map(|r| r["$ref"].as_str().unwrap())
4769            .collect()
4770    }
4771
4772    /// #390: a declarative backend's caption is docling's `add_text` default —
4773    /// a body child, appended as it is created — wherever the picture sits:
4774    /// ahead of a top-level picture, behind the top-level item enclosing a
4775    /// nested one. The picture references it either way and has no children.
4776    #[test]
4777    fn a_body_caption_follows_the_enclosing_top_level_item() {
4778        let mut doc = DoclingDocument::new("t");
4779        doc.push(picture("top", CaptionParent::Body));
4780        doc.push(group(vec![
4781            Node::Paragraph { text: "p".into() },
4782            picture("nested", CaptionParent::Body),
4783        ]));
4784        doc.push(Node::Paragraph {
4785            text: "after".into(),
4786        });
4787        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4788        assert_eq!(
4789            refs(&v["body"]["children"]),
4790            [
4791                "#/texts/0",
4792                "#/pictures/0",
4793                "#/groups/0",
4794                "#/texts/2",
4795                "#/texts/3"
4796            ]
4797        );
4798        assert_eq!(
4799            refs(&v["groups"][0]["children"]),
4800            ["#/texts/1", "#/pictures/1"]
4801        );
4802        for (cap, pic) in [(0, 0), (2, 1)] {
4803            assert_eq!(v["texts"][cap]["label"], "caption");
4804            assert_eq!(v["texts"][cap]["parent"]["$ref"], "#/body");
4805            assert_eq!(
4806                refs(&v["pictures"][pic]["captions"]),
4807                [format!("#/texts/{cap}")]
4808            );
4809            assert_eq!(v["pictures"][pic]["children"], serde_json::json!([]));
4810        }
4811    }
4812
4813    /// The PDF pipeline's caption is the picture's (or table's) own child,
4814    /// as docling attaches a layout caption.
4815    #[test]
4816    fn an_item_caption_is_the_items_first_child() {
4817        let mut doc = DoclingDocument::new("t");
4818        doc.push(picture("fig", CaptionParent::Item));
4819        doc.push(Node::Table(Table {
4820            rows: vec![vec!["a".into()]],
4821            caption: Some("tab".into()),
4822            caption_parent: CaptionParent::Item,
4823            ..Table::default()
4824        }));
4825        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4826        assert_eq!(refs(&v["body"]["children"]), ["#/pictures/0", "#/tables/0"]);
4827        assert_eq!(v["texts"][0]["parent"]["$ref"], "#/pictures/0");
4828        assert_eq!(refs(&v["pictures"][0]["children"]), ["#/texts/0"]);
4829        assert_eq!(refs(&v["pictures"][0]["captions"]), ["#/texts/0"]);
4830        assert_eq!(v["texts"][1]["parent"]["$ref"], "#/tables/0");
4831        assert_eq!(refs(&v["tables"][0]["children"]), ["#/texts/1"]);
4832        assert_eq!(refs(&v["tables"][0]["captions"]), ["#/texts/1"]);
4833    }
4834
4835    /// A PDF picture's contained text (`Node::PictureChildren`) is written as
4836    /// docling's `_add_child_elements` writes it: items parented to the
4837    /// picture, after its caption in the picture's `children`, never in the
4838    /// body; a list item opens its own list group under the picture. The
4839    /// Markdown and the chunker's refs are unaffected.
4840    #[test]
4841    fn picture_children_hang_off_the_picture_after_its_caption() {
4842        let mut doc = DoclingDocument::new("t");
4843        doc.push(picture("fig", CaptionParent::Item));
4844        doc.push(Node::PictureChildren(vec![
4845            Node::Heading {
4846                level: 2,
4847                text: "in-figure title".into(),
4848            },
4849            Node::Paragraph {
4850                text: "axis label".into(),
4851            },
4852            Node::ListItem {
4853                ordered: false,
4854                number: 0,
4855                first_in_list: true,
4856                text: "callout".into(),
4857                level: 0,
4858                marker: None,
4859                location: None,
4860                dclx: None,
4861                href: None,
4862                layer: None,
4863            },
4864        ]));
4865        doc.push(Node::Paragraph {
4866            text: "after".into(),
4867        });
4868        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4869        assert_eq!(refs(&v["body"]["children"]), ["#/pictures/0", "#/texts/4"]);
4870        assert_eq!(
4871            refs(&v["pictures"][0]["children"]),
4872            ["#/texts/0", "#/texts/1", "#/texts/2", "#/groups/0"]
4873        );
4874        assert_eq!(refs(&v["pictures"][0]["captions"]), ["#/texts/0"]);
4875        assert_eq!(v["texts"][1]["label"], "section_header");
4876        assert_eq!(v["texts"][2]["label"], "text");
4877        for t in 1..=2 {
4878            assert_eq!(v["texts"][t]["parent"]["$ref"], "#/pictures/0");
4879            assert_eq!(v["texts"][t]["content_layer"], "body");
4880        }
4881        assert_eq!(v["groups"][0]["parent"]["$ref"], "#/pictures/0");
4882        assert_eq!(v["texts"][3]["parent"]["$ref"], "#/groups/0");
4883        let md = doc.export_to_markdown();
4884        assert!(
4885            !md.contains("axis label") && !md.contains("callout"),
4886            "{md}"
4887        );
4888        assert!(md.contains("after"), "{md}");
4889    }
4890
4891    /// A container caption sits beside its item under the item's parent —
4892    /// ahead of it (an office chart's title) or behind it (an HTML
4893    /// `<figure>`'s table, whose figcaption docling adds after the table).
4894    #[test]
4895    fn a_container_caption_is_the_items_sibling() {
4896        let mut doc = DoclingDocument::new("t");
4897        doc.push(group(vec![
4898            picture("chart", CaptionParent::Container),
4899            Node::Table(Table {
4900                rows: vec![vec!["a".into()]],
4901                caption: Some("figcaption".into()),
4902                caption_parent: CaptionParent::ContainerAfter,
4903                ..Table::default()
4904            }),
4905        ]));
4906        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4907        assert_eq!(refs(&v["body"]["children"]), ["#/groups/0"]);
4908        assert_eq!(
4909            refs(&v["groups"][0]["children"]),
4910            ["#/texts/0", "#/pictures/0", "#/tables/0", "#/texts/1"]
4911        );
4912        assert_eq!(v["texts"][0]["parent"]["$ref"], "#/groups/0");
4913        assert_eq!(v["texts"][1]["parent"]["$ref"], "#/groups/0");
4914        assert_eq!(v["pictures"][0]["children"], serde_json::json!([]));
4915        assert_eq!(v["tables"][0]["children"], serde_json::json!([]));
4916    }
4917
4918    /// A PDF footnote (#620 follow-up): the JSON keeps docling's
4919    /// `footnote` label, the raw text and the link as `hyperlink`; the
4920    /// Markdown still wraps the whole item as `[text](uri)`, and the plain
4921    /// text drops the link like docling-core's `export_to_text`.
4922    #[test]
4923    fn a_labeled_text_keeps_its_label_and_hyperlink_out_of_the_text() {
4924        let mut doc = DoclingDocument::new("t");
4925        doc.push(Node::LabeledText {
4926            label: "footnote".into(),
4927            text: "1 https://example.com/a\\_b".into(),
4928            href: Some("https://example.com/a_b".into()),
4929        });
4930        doc.push(Node::LabeledText {
4931            label: "footnote".into(),
4932            text: "© 2022 the authors".into(),
4933            href: None,
4934        });
4935        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
4936        let t = &v["texts"][0];
4937        assert_eq!(t["label"], "footnote");
4938        assert_eq!(t["text"], "1 https://example.com/a_b");
4939        assert_eq!(t["orig"], "1 https://example.com/a_b");
4940        assert_eq!(t["hyperlink"], "https://example.com/a_b");
4941        assert_eq!(v["texts"][1]["label"], "footnote");
4942        assert!(v["texts"][1].get("hyperlink").is_none());
4943        assert_eq!(
4944            doc.export_to_markdown(),
4945            "[1 https://example.com/a\\_b](https://example.com/a_b)\n\n© 2022 the authors\n"
4946        );
4947        assert_eq!(
4948            doc.export_to_text(),
4949            "1 https://example.com/a_b\n\n© 2022 the authors"
4950        );
4951    }
4952
4953    /// A marked PDF item (#1005, docling's `ScriptPositionModel._apply`):
4954    /// the item keeps its place with an empty text and holds an inline group
4955    /// of its runs, added after every other item; two such paragraphs stay
4956    /// separate HTML blocks; a linked footnote's link rides on its baseline
4957    /// runs only, so the marker stays out of it.
4958    #[test]
4959    fn script_runs_follow_docling() {
4960        use crate::{Script, ScriptRun, ScriptRunProv};
4961        let run = |text: &str, script, hyperlink: Option<&str>| ScriptRun {
4962            text: text.into(),
4963            script,
4964            prov: vec![ScriptRunProv {
4965                page_no: 1,
4966                bbox: [10.0, 700.0, 20.0, 690.0],
4967                charspan: [0, text.chars().count()],
4968            }],
4969            hyperlink: hyperlink.map(String::from),
4970        };
4971        let mut doc = DoclingDocument::new("t");
4972        doc.push(Node::PageInfo {
4973            page_no: 1,
4974            width: 612.0,
4975            height: 792.0,
4976        });
4977        let located = |inner: Node| Node::Located {
4978            location: [40, 80, 200, 90],
4979            inner: Box::new(inner),
4980        };
4981        doc.push(located(Node::ScriptText {
4982            inner: Box::new(Node::Paragraph {
4983                text: "E = mc2".into(),
4984            }),
4985            runs: vec![
4986                run("E = mc", Script::Baseline, None),
4987                run("2", Script::Super, None),
4988            ],
4989        }));
4990        doc.push(located(Node::ScriptText {
4991            inner: Box::new(Node::Paragraph { text: "H2O".into() }),
4992            runs: vec![
4993                run("H", Script::Baseline, None),
4994                run("2", Script::Sub, None),
4995                run("O", Script::Baseline, None),
4996            ],
4997        }));
4998        let url = "https://example.com/x";
4999        doc.push(located(Node::ScriptText {
5000            inner: Box::new(Node::LabeledText {
5001                label: "footnote".into(),
5002                text: format!("1[Note text]({url})"),
5003                href: None,
5004            }),
5005            runs: vec![
5006                run("1", Script::Super, None),
5007                run("Note text", Script::Baseline, Some(url)),
5008            ],
5009        }));
5010        doc.push(located(Node::Paragraph {
5011            text: "Plain line".into(),
5012        }));
5013
5014        let v: Value = serde_json::from_str(&doc.export_to_json()).unwrap();
5015        let texts = v["texts"].as_array().unwrap();
5016        // The items first, in place; the runs after them.
5017        assert_eq!(texts[0]["text"], "");
5018        assert_eq!(
5019            texts[0]["children"],
5020            serde_json::json!([{"$ref": "#/groups/0"}])
5021        );
5022        assert_eq!(texts[2]["label"], "footnote");
5023        assert_eq!(texts[2]["hyperlink"], url);
5024        assert_eq!(texts[3]["text"], "Plain line");
5025        assert_eq!(texts[4]["text"], "E = mc");
5026        assert_eq!(texts[5]["formatting"]["script"], "super");
5027        assert_eq!(texts[5]["prov"][0]["bbox"]["coord_origin"], "BOTTOMLEFT");
5028        assert_eq!(texts[7]["formatting"]["script"], "sub");
5029        assert_eq!(texts[9]["label"], "footnote");
5030        assert!(texts[9].get("hyperlink").is_none());
5031        assert_eq!(texts[10]["hyperlink"], url);
5032        assert_eq!(v["groups"][0]["label"], "inline");
5033        assert_eq!(v["groups"][0]["parent"]["$ref"], "#/texts/0");
5034
5035        assert_eq!(
5036            doc.export_to_markdown(),
5037            format!("E = mc2\n\nH2O\n\n1[Note text]({url})\n\nPlain line\n")
5038        );
5039        let html = doc.export_to_html();
5040        // Each marked item is a block of its own (the footnote too, as
5041        // docling-core renders it).
5042        assert_eq!(
5043            html.matches("<p><span class='inline-group'>").count(),
5044            3,
5045            "{html}"
5046        );
5047        assert!(html.contains("E = mc<sup>2</sup>"), "{html}");
5048        assert!(html.contains("H<sub>2</sub>O"), "{html}");
5049        assert!(
5050            html.contains(&format!("<sup>1</sup><a href=\"{url}\">Note text</a>")),
5051            "{html}"
5052        );
5053        let dclx = doc.export_to_doclang();
5054        assert!(
5055            dclx.contains("E = mc<superscript>2</superscript>"),
5056            "{dclx}"
5057        );
5058    }
5059}