Skip to main content

docling_core/
document.rs

1//! The unified document representation.
2
3use crate::markdown::{to_markdown, to_markdown_images};
4use crate::ImageMode;
5
6/// The unified, format-agnostic document produced by every backend.
7///
8/// This is the heart of docling: backends parse their source format into a
9/// `DoclingDocument`, and serializers turn it back into Markdown, HTML, JSON,
10/// etc. Phase 0 uses a flat sequence of [`Node`]s; the production schema will
11/// match docling-core's body-tree-with-references layout.
12#[derive(Debug, Clone, PartialEq)]
13pub struct DoclingDocument {
14    /// Logical document name (usually the input file stem).
15    pub name: String,
16    /// Top-level content, in reading order.
17    pub nodes: Vec<Node>,
18    /// Default Markdown export mode for [`Self::export_to_markdown`]. `false`
19    /// (the default) reproduces docling's legacy output byte-for-byte; `true`
20    /// emits cleaner, more conformant Markdown. Set by `DocumentConverter`.
21    pub strict_markdown: bool,
22    /// Emit tables in the compact `| a | b |` / `| - | - |` form rather than
23    /// docling-core's width-padded GitHub serializer. The PDF backend sets this
24    /// (its committed groundtruth corpus predates the padded serializer); DOCX/HTML
25    /// leave it `false` to match current published docling.
26    pub compact_tables: bool,
27    /// Text the Markdown export inserts between two pages — docling-core's
28    /// `MarkdownParams.page_break_placeholder` (e.g. `"<!-- page break -->"`).
29    /// `None` (the default) omits page breaks from Markdown, as docling does.
30    /// Set by `DocumentConverter::page_break_placeholder`. See
31    /// [`crate::markdown`] for where a break lands (only between two rendered
32    /// blocks that sit on different pages, never leading or trailing).
33    pub page_break_placeholder: Option<String>,
34    /// Hyperlinks recovered from the source, as `(anchor_text, href)` pairs in
35    /// document order. docling's standard pipeline drops PDF link annotations, so
36    /// these are rendered as Markdown `[anchor](href)` **only in strict mode**
37    /// (legacy/docling output is left byte-for-byte unchanged). The PDF backend
38    /// populates this from pdfium link annotations; other backends leave it empty.
39    pub links: Vec<(String, String)>,
40    /// Conversion-confidence report (#183), populated by the PDF/image ML
41    /// pipeline; `None` for declarative conversions. Deliberately **not**
42    /// part of any document export (docling keeps it on the conversion
43    /// result, outside the document schema) — docling-serve surfaces it in
44    /// the HTTP response instead.
45    pub confidence: Option<crate::confidence::ConfidenceReport>,
46    /// docling's item tree, when the backend built one (the HTML backend
47    /// does): the JSON export serializes it instead of deriving a tree from
48    /// `nodes`, so the JSON carries upstream's exact parent/child structure,
49    /// item numbering, inline groups, formatting and content layers. Every
50    /// other serializer reads `nodes`. See [`crate::tree`].
51    pub tree: Option<crate::tree::ItemTree>,
52    /// Rendered page images by 1-based page number — docling's
53    /// `PageItem.image`, filled by the PDF/image pipeline only when page
54    /// images are requested (docling's `generate_page_images`, #520). The
55    /// JSON export writes each as the page's `image`, so docling-core's
56    /// `TableItem.get_image` / `FormulaItem.get_image` can crop from it.
57    /// Empty otherwise; no other export reads it.
58    pub page_images: std::collections::BTreeMap<usize, PictureImage>,
59}
60
61/// A single piece of document content.
62#[derive(Debug, Clone, PartialEq)]
63pub enum Node {
64    /// A heading. `level` is 1-6.
65    Heading { level: u8, text: String },
66    /// A run of body text.
67    Paragraph { text: String },
68    /// A form checkbox (docling's `checkbox_selected`/`checkbox_unselected`): its
69    /// clean label `text` with the checked state. DocLang emits a `<checkbox>`
70    /// element head; Markdown/JSON render the task-list form (`- [x] `/`- [ ] `).
71    CheckboxItem { checked: bool, text: String },
72    /// A single list item at the given nesting `level` (0 = top). For ordered
73    /// items, `number` is the display number (honoring the list's `start`); it
74    /// is unused for unordered items. `first_in_list` marks the first item of a
75    /// list so the serializer can blank-line-separate adjacent sibling lists.
76    ///
77    /// `marker` is the DocLang enumeration marker (`"1."`, `"1.1."`, …) when the
78    /// backend provides one — HTML and DOCX set it for enumerated items, so
79    /// DocLang emits `<ldiv><marker>…</marker></ldiv>`; Markdown and the other
80    /// declarative backends leave it `None`, giving a bare `<ldiv/>` (matching
81    /// docling, whose Markdown backend passes no marker).
82    ListItem {
83        ordered: bool,
84        number: u64,
85        first_in_list: bool,
86        text: String,
87        level: u8,
88        marker: Option<String>,
89        /// Optional layout provenance (`x0,y0,x1,y1`, normalized to 0–511): the
90        /// four DocLang `<location>` values emitted inside the `<list>` right
91        /// after the item's `<ldiv>`. Set only by backends with real geometry
92        /// (e.g. PPTX shapes); `None` for the declarative backends. Kept on the
93        /// item itself (rather than a [`Node::Located`] wrapper) so consecutive
94        /// items still group into one `<list>`.
95        location: Option<[u16; 4]>,
96        /// DocLang-only override for items whose DocLang form diverges from their
97        /// flat Markdown `text`. Markdown/JSON always render the fields above; the
98        /// DocLang serializer, when this is `Some`, takes the list kind, marker,
99        /// and content from here instead. Used for docx multilevel numbering
100        /// (Markdown shows `- 1.1. x`, DocLang an ordered `<marker>1.1.</marker>`
101        /// with clean text) and inline equations/formatting in list items.
102        dclx: Option<ListItemDclx>,
103        /// The item's hyperlink target, when its content is a link — docling's
104        /// HTML backend emits it as an `<href uri=…/>` in the item head, and the
105        /// anchor's Markdown link markup is stripped from the rendered content.
106        /// `None` for a plain item; ignored by Markdown/JSON.
107        href: Option<String>,
108        /// Non-body content layer (docling's HTML site chrome before the first
109        /// heading → `furniture`). DocLang emits a `<layer value=…/>` in the item
110        /// head; Markdown/JSON drop a non-body item entirely.
111        layer: Option<ContentLayer>,
112    },
113    /// A fenced code block.
114    Code {
115        language: Option<String>,
116        text: String,
117        /// The original (pre-enrichment) text when the CodeFormula model
118        /// rewrote `text`: docling keeps the raw extraction in the JSON `orig`
119        /// field while `text` carries the model output. `None` → `orig == text`.
120        orig: Option<String>,
121        /// A line-preserving rendering, when the backend can reconstruct one
122        /// but docling's own output for the format cannot. The PDF pipeline
123        /// sets it (docling-parse joins code lines with single spaces, so
124        /// `text` carries that flat docling-parity form): **strict** Markdown
125        /// prefers `pretty`, every byte-conformance surface (legacy Markdown,
126        /// JSON, DocLang, chunks) serializes `text`.
127        pretty: Option<String>,
128    },
129    /// A table. The first row is treated as the header.
130    Table(Table),
131    /// A picture/figure, with an optional caption and (when a backend extracts
132    /// it) the embedded image itself.
133    Picture {
134        caption: Option<String>,
135        /// Hyperlink annotation on the caption (docling's caption text item
136        /// `hyperlink`): the HTML backend sets it when an `<a href>` wraps the
137        /// image whose `alt` became the caption. DocLang emits the block-form
138        /// `<caption>` with an `<href uri=…/>` head; JSON puts `hyperlink` on
139        /// the caption item; Markdown and LaTeX print the plain caption text,
140        /// as docling does.
141        caption_href: Option<String>,
142        image: Option<PictureImage>,
143        /// DocumentPictureClassifier predictions (all classes, descending
144        /// confidence), when the picture-classification enrichment ran.
145        /// Serialized as docling's `classification` annotation + `meta` field
146        /// on the JSON picture item; Markdown/DocLang output is unaffected.
147        classification: Option<Vec<PictureClass>>,
148        /// Text read off the embedded image by the picture-OCR enrichment
149        /// (#645) — docling's `PictureDescriptionData` annotation /
150        /// `meta.description`, which is where upstream's picture-description
151        /// models (VLM captioning) put their text too. JSON and DCLX carry it
152        /// structurally; Markdown prints it between the caption and the
153        /// image placeholder, where docling's picture serializer renders
154        /// annotations. `None` when the enrichment did not run or read no
155        /// text, so every default export stays unchanged.
156        description: Option<PictureDescription>,
157        /// Where the caption item hangs in the JSON tree (#390); see
158        /// [`CaptionParent`]. Markdown, DocLang and LaTeX ignore it.
159        caption_parent: CaptionParent,
160        /// The caption's own box on the picture's page (0–511 grid, like
161        /// [`Node::Located`]) — the PDF pipeline's caption layout region. The
162        /// JSON export writes it as the caption item's `prov` (#609), as
163        /// docling's `ReadingOrderModel._add_caption_or_footnote` does; `None`
164        /// (every declarative backend) leaves the caption without one.
165        caption_location: Option<[u16; 4]>,
166    },
167    /// A display-math formula item decoded by the CodeFormula enrichment:
168    /// `latex` is the model's LaTeX (no `$$` wrapping), `orig` the raw glyph
169    /// text extracted from the PDF. Markdown renders `$$latex$$`; JSON emits a
170    /// `formula` text item (docling's un-enriched pipeline instead emits a
171    /// placeholder paragraph — see the PDF assembler).
172    Formula {
173        latex: String,
174        orig: String,
175        location: Option<[u16; 4]>,
176    },
177    /// A standalone caption item (docling's `DocItemLabel.CAPTION` text that
178    /// no picture or table claims): the HTML backend emits a `<figure>`'s
179    /// `<figcaption>` this way when the figure produced no picture and its
180    /// first item is not a table (docling#4050). `href` is the caption's
181    /// hyperlink annotation (the first link inside the figcaption); Markdown
182    /// renders it as `[text](href)`, JSON puts `hyperlink` on the caption
183    /// item, DocLang emits the block-form `<caption>` with an `<href>` head.
184    Caption { text: String, href: Option<String> },
185    /// A body text item with a docling label of its own and an optional
186    /// hyperlink — the PDF pipeline's `footnote` regions, whose item docling
187    /// keeps as a `footnote` text with the link annotation covering it as its
188    /// `hyperlink` (`PageAssembleModel._match_hyperlink`). The JSON writes
189    /// exactly that (label, raw text, `hyperlink`); every other serializer
190    /// renders it as the paragraph [`Self::labeled_markdown`] spells —
191    /// docling's Markdown wraps a linked item's whole text, `[text](uri)`.
192    LabeledText {
193        label: String,
194        text: String,
195        href: Option<String>,
196    },
197    /// A chart (docling's `PictureItem` classified as a chart, carrying a
198    /// `PictureTabularChartData` annotation). Markdown and JSON render it exactly
199    /// like a [`Node::Picture`] placeholder (an `<!-- image -->` / `picture`
200    /// item); the DocLang serializer emits `<picture class="chart">` with a
201    /// `<label value="{kind}"/>` and the data `table` as a `<tabular>`.
202    Chart {
203        /// docling's classification label, e.g. `bar_chart`, `line_chart`.
204        kind: String,
205        /// The chart's data grid (row 0 is the header band).
206        table: Table,
207        /// The chart title (docling's caption item on the picture).
208        caption: Option<String>,
209        /// DocLang `<location>` provenance for the picture element.
210        location: Option<[u16; 4]>,
211    },
212    /// A logical grouping of child nodes (e.g. a list, a section, a spreadsheet
213    /// sheet). `name` is docling's group name when it differs from the label —
214    /// an xlsx sheet group is `label: "sheet"`, `name: Some("Sheet1")`. `layer`
215    /// puts the group *and* everything it contains on a non-body content layer
216    /// (a hidden sheet is `invisible`), which is how the serializers that only
217    /// render body content know to skip it; DocLang stamps each child with the
218    /// layer token, exactly as a [`Node::Furniture`] wrapper on each would.
219    /// DocLang has no group element, so a group is transparent there.
220    Group {
221        label: String,
222        name: Option<String>,
223        layer: Option<ContentLayer>,
224        children: Vec<Node>,
225    },
226    /// A form key-value region (docling's `field_region`): a set of form fields,
227    /// each pairing an optional marker, key, and value. Backends detect these
228    /// from form structure (e.g. HTML's `keyN` / `keyN_valueM` / `keyN_marker`
229    /// `id`-convention); the serializers render each item's parts as separate
230    /// labelled texts (`marker` / `field_key` / `field_value`).
231    FieldRegion { items: Vec<FieldItem> },
232    /// docling's `KeyValueItem`: a graph of key and value cells and the links
233    /// between them (docling-core's `GraphData`). The XBRL backend builds one
234    /// from an instance's numeric facts and the presentation and calculation
235    /// hierarchies of its taxonomy. Only the JSON export carries the graph:
236    /// docling's Markdown serializer has no rendering for the item and writes
237    /// its `<!-- missing-key-value-item -->` placeholder, which the Markdown
238    /// export reproduces; the other serializers omit it.
239    KeyValueGraph {
240        cells: Vec<GraphCell>,
241        links: Vec<GraphLink>,
242    },
243    /// Rich inline content — docling's `InlineGroup`: a run of styled text
244    /// segments that a backend captured with formatting (`<bold>`, `<italic>`,
245    /// `<underline>`, `<strikethrough>`, sub/superscript, inline `<code>`) the
246    /// flat Markdown text cannot represent. Markdown/JSON render this exactly
247    /// like `Paragraph { text: md_text }` (so their output is unchanged); the
248    /// DocLang serializer uses the structured `runs`. `unwrapped` is set when the
249    /// group's docling parent is a heading/text (no enclosing `<text>` wrapper).
250    InlineGroup {
251        unwrapped: bool,
252        runs: Vec<InlineRun>,
253        md_text: String,
254    },
255    /// A node in a non-body content layer — `furniture` (page headers/footers,
256    /// the HTML `<title>`, site navigation/chrome) or `notes` (docx comments).
257    /// Markdown and JSON omit these layers by default; DocLang renders the wrapped
258    /// node with a `<layer value="{layer}"/>` head.
259    Furniture {
260        layer: ContentLayer,
261        inner: Box<Node>,
262    },
263    /// One reviewer comment: docling's notes-layer `comment_section` group
264    /// holding a single text item. `name` is docling's own — `comment-{id}` for
265    /// a docx `w:comment`, `comment-{sheet}-{cell}` for a spreadsheet cell note.
266    /// JSON emits the group plus its notes-layer text; DocLang emits the flat
267    /// `<text><layer value="notes"/>…</text>` upstream writes (its DocLang
268    /// carries no group for comments); Markdown and LaTeX omit the notes layer.
269    ///
270    /// `refs_note_text` picks what a [`Node::Commented`] annotation points at,
271    /// mirroring an upstream asymmetry: docling-core's `add_comment` appends the
272    /// **note text**'s ref to each target (which is what the xlsx backend gets),
273    /// while the docx backend overwrites that with the **group**'s ref so a
274    /// comment's replies group together.
275    ///
276    /// `grouped` is whether docling wraps the note in a `comment_section`
277    /// group at all: the docx and spreadsheet backends do, while backends that
278    /// call docling-core's `add_comment` directly (Pages, #383) get a bare
279    /// notes-layer text item under the body — JSON then emits no group and
280    /// `name` is unused.
281    CommentSection {
282        name: String,
283        text: String,
284        refs_note_text: bool,
285        grouped: bool,
286    },
287    /// A body item annotated by reviewer comments: `comments` are indices into
288    /// the document's [`Node::CommentSection`] nodes, in document order. JSON
289    /// emits docling's `comments: [{"$ref": …}]` on the item, each ref pointing
290    /// where the section says (see [`Node::CommentSection::refs_note_text`]);
291    /// every other serializer renders `inner` unchanged.
292    Commented {
293        comments: Vec<usize>,
294        inner: Box<Node>,
295    },
296    /// A text node taken from a time-based track — an ASR segment (#614):
297    /// docling's `TrackSource`. The JSON writes docling's ASR text item — `cue`
298    /// (the segment's words) as its text, `track` as its `source: [{"kind":
299    /// "track", …}]` — which the WebVTT export makes a cue of; every other
300    /// serializer renders `inner` (the `[time: start-end] words` paragraph)
301    /// unchanged.
302    Track {
303        track: crate::tree::TreeTrack,
304        cue: String,
305        inner: Box<Node>,
306    },
307    /// A node carrying layout provenance — the four DocLang `<location>` values
308    /// (`x0,y0,x1,y1`, normalized to 0–511) docling attaches to elements from
309    /// backends with real geometry (e.g. the slide shapes in PPTX). Markdown and
310    /// JSON render the wrapped node unchanged; DocLang emits the `<location>`
311    /// tokens as the element's first children.
312    Located {
313        location: [u16; 4],
314        inner: Box<Node>,
315    },
316    /// Exact page provenance for a backend whose geometry already *is* the
317    /// page's coordinate system — an XLSX item's cell-index box on its sheet,
318    /// which docling writes verbatim (`bbox` in a top-left origin, the
319    /// `charspan` the backend chose) and sizes the page from. The 0–511 grid
320    /// of a [`Node::Located`] cannot round-trip such integers exactly, so the
321    /// JSON export reads this wrapper; every other serializer renders `inner`
322    /// unchanged (DocLang keeps taking its `<location>` tokens from the grid).
323    Prov {
324        page_no: usize,
325        /// `[l, t, r, b]`, page units, top-left origin.
326        bbox: [f32; 4],
327        /// docling's `charspan` for the item (`[0, 0]` for an XLSX table).
328        charspan: [usize; 2],
329        /// The item's creation rank among its siblings, when that differs
330        /// from the node order: docling numbers `#/tables/N` / `#/texts/N` /
331        /// `#/pictures/N` in the order it *creates* items (a sheet's tables,
332        /// then its images, then its charts) and only afterwards sorts the
333        /// container's children by position. The JSON export adds siblings in
334        /// this order and lays their refs out in node order; `None` when the
335        /// two orders coincide.
336        seq: Option<usize>,
337        inner: Box<Node>,
338    },
339    /// A PDF page header or footer (docling's `page_header`/`page_footer`
340    /// furniture): DocLang emits `<page_header>`/`<page_footer>` with a
341    /// `<layer value="furniture"/>` head, the four `<location>` tokens, then the
342    /// text. The JSON writes it as a body-parented `page_header`/`page_footer`
343    /// text item on the furniture layer; Markdown omits it.
344    PageFurniture {
345        footer: bool,
346        location: [u16; 4],
347        text: String,
348    },
349    /// A furniture-layer text item with an explicit docling label — a page
350    /// header / footer paragraph (`page_header` / `page_footer`) or a note
351    /// body (`footnote`) from a backend that builds no item tree (#535: the
352    /// Word 97 `.doc` stories). The JSON writes a body-parented item of that
353    /// label on the furniture layer; DocLang `<{label}>` with a `<layer
354    /// value="furniture"/>` head (docling-core's own shape); Markdown, LaTeX
355    /// and the chunker leave it out like any furniture.
356    FurnitureText { label: String, text: String },
357    /// The text a PDF picture contains, kept the way docling keeps it: every
358    /// regular layout cluster > 80 % inside a picture cluster is that
359    /// `PictureItem`'s child (`LayoutPostprocessor._set_cluster_children`,
360    /// `ReadingOrderModel._add_child_elements`). Emitted right after its
361    /// picture node. The JSON export writes these nodes as items parented to
362    /// that picture, after its caption; docling's Markdown and LaTeX picture
363    /// serializers print only the caption and the image, so every other
364    /// serializer skips it — except the Markdown export asked to
365    /// `traverse_pictures` ([`MarkdownExportOptions`], #599), which renders
366    /// them after the picture like upstream's item walk does.
367    PictureChildren(Vec<Node>),
368    /// A page boundary — docling's implicit page break between pages. The PPTX
369    /// backend emits one between consecutive slides. DocLang renders it as
370    /// `<page_break/>`; Markdown and JSON omit it (matching docling's default
371    /// exports, which carry page breaks only in the document model).
372    PageBreak,
373    /// An invisible page marker — the first node of every page the PDF paths
374    /// assemble: the 1-based page number and the page size in PDF points. It
375    /// carries exactly what the JSON export needs to populate docling's
376    /// `pages` map and to denormalize the 0–511 `<location>` grid back into
377    /// BOTTOMLEFT point bboxes for per-item `prov` (#171). Every other
378    /// serializer skips it, so Markdown / DocLang / DocTags output is
379    /// byte-for-byte unchanged.
380    PageInfo {
381        /// 1-based page number (0 = "not yet numbered": the assembler emits
382        /// the marker, the document-level collector stamps the real number).
383        page_no: usize,
384        /// Page width in PDF points.
385        width: f32,
386        /// Page height in PDF points.
387        height: f32,
388    },
389    /// A node docling keeps in the document model (and DocLang) but leaves out
390    /// of the Markdown and JSON exports — e.g. an ODF *presentation*'s pictures
391    /// and charts, which appear in the `.dclx` body but not in its `.md`/`.json`.
392    /// DocLang renders the wrapped node in place; Markdown and JSON skip it.
393    DoclangOnly(Box<Node>),
394    /// A verbatim plain-text dump — docling's plain-text backend emits the whole
395    /// file as a single text item (used for legacy USPTO APS `.txt` grants, which
396    /// docling routes to plain text rather than its APS parser). The stored string
397    /// is the file body, one record per line. Markdown/JSON render it as one text
398    /// block; the DocLang serializer reproduces minidom's per-line layout, CDATA-
399    /// escaping only the lines that need it (see `emit_text_dump`).
400    TextDump(String),
401}
402
403/// Vertical text position of an [`InlineRun`] — docling's `Script`.
404#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
405pub enum Script {
406    #[default]
407    Baseline,
408    Sub,
409    Super,
410}
411
412/// One styled segment of a [`Node::InlineGroup`] — the docling.rs analogue of a
413/// `TextItem` inside an `InlineGroup`, carrying the ancestor formatting docling
414/// tracks. `text` is already whitespace-normalized/trimmed (one segment per
415/// source text node). A hyperlink is intentionally not stored: DocLang drops the
416/// target inside inline scope, keeping only the anchor text.
417#[derive(Debug, Clone, PartialEq, Eq, Default)]
418pub struct InlineRun {
419    pub text: String,
420    pub bold: bool,
421    pub italic: bool,
422    pub underline: bool,
423    pub strike: bool,
424    pub script: Script,
425    pub code: bool,
426    /// An inline equation (`text` holds LaTeX): DocLang renders `<formula>…`,
427    /// Markdown/JSON keep the `$…$` already baked into the group's `md_text`.
428    pub formula: bool,
429}
430
431/// A DocLang content layer other than the default `body` (see [`Node::Furniture`]).
432#[derive(Debug, Clone, Copy, PartialEq, Eq)]
433pub enum ContentLayer {
434    /// Page headers/footers, HTML `<title>`, site navigation/chrome.
435    Furniture,
436    /// Editorial notes (docx reviewer comments).
437    Notes,
438    /// Invisible content (hidden spreadsheet sheets).
439    Invisible,
440}
441
442impl ContentLayer {
443    /// The `<layer value="…"/>` token value.
444    pub fn value(self) -> &'static str {
445        match self {
446            ContentLayer::Furniture => "furniture",
447            ContentLayer::Notes => "notes",
448            ContentLayer::Invisible => "invisible",
449        }
450    }
451}
452
453/// A set of content layers, `body` included — docling-core's
454/// `set[ContentLayer]` (`HTMLParams.layers`,
455/// `export_to_html(included_content_layers=…)`), where `body` is a member
456/// like any other. [`Default`] is docling's `DEFAULT_CONTENT_LAYERS`: body
457/// only, so an export built with it is unchanged from before layers could
458/// be chosen (#499).
459#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
460pub struct ContentLayers {
461    /// The main content (items without an explicit layer).
462    pub body: bool,
463    /// [`ContentLayer::Furniture`]: page headers/footers, navigation chrome.
464    pub furniture: bool,
465    /// [`ContentLayer::Notes`]: reviewer comments and other editorial notes.
466    pub notes: bool,
467    /// [`ContentLayer::Invisible`]: hidden content (hidden sheets, …).
468    pub invisible: bool,
469}
470
471impl Default for ContentLayers {
472    fn default() -> Self {
473        Self::BODY
474    }
475}
476
477impl ContentLayers {
478    /// Body only — docling's `DEFAULT_CONTENT_LAYERS`.
479    pub const BODY: Self = Self {
480        body: true,
481        furniture: false,
482        notes: false,
483        invisible: false,
484    };
485    /// Every layer — Python's `set(ContentLayer)`.
486    pub const ALL: Self = Self {
487        body: true,
488        furniture: true,
489        notes: true,
490        invisible: true,
491    };
492    /// No layer at all (nothing renders); build a set from it with
493    /// [`with`](Self::with) / [`with_body`](Self::with_body).
494    pub const NONE: Self = Self {
495        body: false,
496        furniture: false,
497        notes: false,
498        invisible: false,
499    };
500
501    /// The set plus `layer`.
502    pub const fn with(mut self, layer: ContentLayer) -> Self {
503        match layer {
504            ContentLayer::Furniture => self.furniture = true,
505            ContentLayer::Notes => self.notes = true,
506            ContentLayer::Invisible => self.invisible = true,
507        }
508        self
509    }
510
511    /// The set plus the body layer.
512    pub const fn with_body(mut self) -> Self {
513        self.body = true;
514        self
515    }
516
517    /// Whether `layer` is in the set; `None` is the body layer.
518    pub fn contains(&self, layer: Option<ContentLayer>) -> bool {
519        match layer {
520            None => self.body,
521            Some(ContentLayer::Furniture) => self.furniture,
522            Some(ContentLayer::Notes) => self.notes,
523            Some(ContentLayer::Invisible) => self.invisible,
524        }
525    }
526
527    /// Whether the layer spelled `name` (`body`, `furniture`, `notes`,
528    /// `invisible` — the JSON `content_layer` values) is in the set; an
529    /// unknown name is not.
530    pub fn contains_name(&self, name: &str) -> bool {
531        match name {
532            "body" => self.body,
533            "furniture" => self.furniture,
534            "notes" => self.notes,
535            "invisible" => self.invisible,
536            _ => false,
537        }
538    }
539
540    /// Parse a comma-separated list of layer names (`body,furniture`;
541    /// whitespace around a name is ignored, `all` is every layer). An
542    /// unknown name is the error. docling-core 2.99's `background` layer
543    /// (watermarks) has no items in this model, so the name is accepted and
544    /// adds nothing — a set written for Python keeps parsing.
545    pub fn parse_list(list: &str) -> Result<Self, String> {
546        let mut set = Self::NONE;
547        for name in list.split(',').map(str::trim).filter(|n| !n.is_empty()) {
548            match name {
549                "body" => set.body = true,
550                "furniture" => set.furniture = true,
551                "notes" => set.notes = true,
552                "invisible" => set.invisible = true,
553                "background" => {}
554                "all" => set = Self::ALL,
555                other => {
556                    return Err(format!(
557                        "unknown content layer `{other}` (expected body, furniture, notes, invisible or all)"
558                    ))
559                }
560            }
561        }
562        Ok(set)
563    }
564}
565
566/// Options of the HTML export ([`DoclingDocument::export_to_html_with`]):
567/// docling-core's `HTMLParams` subset the port honours. [`Default`] is
568/// upstream's default export — placeholder images, `artifacts` as the
569/// referenced-image directory, the body layer only.
570#[derive(Debug, Clone, PartialEq, Eq)]
571pub struct HtmlExportOptions {
572    /// How pictures render (`HTMLParams.image_mode`): nothing but captions
573    /// and meta for [`ImageMode::Placeholder`], `data:` URIs when embedded,
574    /// `<img src>` paths under [`artifacts_dir`](Self::artifacts_dir) when
575    /// referenced.
576    pub image_mode: ImageMode,
577    /// The directory referenced images are named under, the Markdown
578    /// export's convention (`<artifacts_dir>/image_NNNNNN.<ext>`).
579    pub artifacts_dir: String,
580    /// The content layers rendered (`HTMLParams.layers`, #499): an item on a
581    /// layer outside the set is skipped, its children still walked — exactly
582    /// how docling-core's `get_excluded_refs` reads the set.
583    pub layers: ContentLayers,
584}
585
586impl Default for HtmlExportOptions {
587    fn default() -> Self {
588        Self {
589            image_mode: ImageMode::Placeholder,
590            artifacts_dir: "artifacts".to_string(),
591            layers: ContentLayers::BODY,
592        }
593    }
594}
595
596/// Options of the Markdown export
597/// ([`DoclingDocument::export_to_markdown_with_options`], #599): the
598/// docling-core `MarkdownParams` the port honours beyond the image mode.
599/// [`Default`] is upstream's default export — placeholder images,
600/// `artifacts` as the referenced-image directory, the body layer only, no
601/// picture traversal, HTML and underscore escaping on, `<!-- image -->` —
602/// so a document exported with it is byte-identical to
603/// [`DoclingDocument::export_to_markdown`].
604#[derive(Debug, Clone, PartialEq, Eq)]
605pub struct MarkdownExportOptions {
606    /// How pictures render (`MarkdownParams.image_mode`): the placeholder,
607    /// `data:` URIs when embedded, `![Image](<artifacts_dir>/image_NNNNNN.<ext>)`
608    /// when referenced (the bytes come back for the caller to write).
609    pub image_mode: ImageMode,
610    /// The directory referenced images are named under.
611    pub artifacts_dir: String,
612    /// The content layers rendered (`MarkdownParams.layers`,
613    /// `export_to_markdown(included_content_layers=…)`): an item on a layer
614    /// outside the set is skipped, its children still walked. The default
615    /// body-only set drops page headers/footers (`furniture`), reviewer
616    /// comments (`notes`) and hidden sheets (`invisible`), as upstream does; a
617    /// scanned form's running header reads out with
618    /// `ContentLayers::BODY.with(ContentLayer::Furniture)`. The extra items
619    /// render through the body's serializers — a page header is a paragraph.
620    pub layers: ContentLayers,
621    /// `CommonParams.traverse_pictures`: render the text items nested in a
622    /// picture (the PDF pipeline's picture children — a bordered form laid
623    /// out as one picture holds every field as a child) after the picture's
624    /// own caption and image, as upstream yields them. Off, a picture prints
625    /// only its caption and image.
626    pub traverse_pictures: bool,
627    /// `MarkdownParams.escape_html`: `&`, `<` and `>` in text as `&amp;`,
628    /// `&lt;`, `&gt;` (Python's `html.escape(quote=False)`). Off, `R&D` stays
629    /// `R&D`.
630    pub escape_html: bool,
631    /// `MarkdownParams.escape_underscores`: `_` in text as `\_`. Off, the
632    /// text keeps its underscores.
633    pub escape_underscores: bool,
634    /// `MarkdownParams.image_placeholder`: what a picture prints as when it
635    /// renders no image data (the placeholder mode, or a picture without a
636    /// payload); upstream's `<!-- image -->`.
637    pub image_placeholder: String,
638    /// docling-core's `PlainTextDocSerializer` (#613, `export_to_text`,
639    /// docling's `--to text`): the Markdown walk with the decoration turned
640    /// off — headings without `#`, bold/italic/strikethrough markers and
641    /// inline code backticks dropped, a hyperlink reduced to its label, code
642    /// blocks unfenced, no GFM hard line breaks. List bullets/numbers,
643    /// checkbox marks and table grids stay. [`Self::plain_text`] pairs it
644    /// with upstream's `PlainTextParams` (no escaping, an empty image
645    /// placeholder).
646    pub plain_text: bool,
647}
648
649impl MarkdownExportOptions {
650    /// docling-core's `PlainTextParams` defaults (#613): [`Self::plain_text`]
651    /// on, HTML and underscore escaping off, pictures print nothing (their
652    /// captions still do), body layer only, no picture traversal — what
653    /// `DoclingDocument.export_to_text()` and docling's `--to text` write.
654    pub fn plain_text() -> Self {
655        Self {
656            escape_html: false,
657            escape_underscores: false,
658            image_placeholder: String::new(),
659            plain_text: true,
660            ..Self::default()
661        }
662    }
663}
664
665impl Default for MarkdownExportOptions {
666    fn default() -> Self {
667        Self {
668            image_mode: ImageMode::Placeholder,
669            artifacts_dir: "artifacts".to_string(),
670            layers: ContentLayers::BODY,
671            traverse_pictures: false,
672            escape_html: true,
673            escape_underscores: true,
674            image_placeholder: "<!-- image -->".to_string(),
675            plain_text: false,
676        }
677    }
678}
679
680/// DocLang-only content for a [`Node::ListItem`] whose DocLang form differs from
681/// its flat Markdown `text` (see [`Node::ListItem::dclx`]). `ordered` picks the
682/// enclosing `<list>` kind, `marker` the `<ldiv><marker>`; content is `runs`
683/// (structured equations/formatting) when non-empty, else `text` re-parsed for
684/// inline markers.
685#[derive(Debug, Clone, PartialEq, Eq, Default)]
686pub struct ListItemDclx {
687    pub ordered: bool,
688    pub marker: Option<String>,
689    pub text: String,
690    pub runs: Vec<InlineRun>,
691}
692
693impl Node {
694    /// The paragraph a [`Node::LabeledText`] renders as outside the JSON:
695    /// its text, wrapped as `[text](uri)` when it carries a hyperlink. Any
696    /// other node is returned unchanged.
697    pub fn labeled_as_paragraph(&self) -> std::borrow::Cow<'_, Node> {
698        match self {
699            Node::LabeledText { text, href, .. } => std::borrow::Cow::Owned(Node::Paragraph {
700                text: match href {
701                    Some(uri) => format!("[{text}]({uri})"),
702                    None => text.clone(),
703                },
704            }),
705            other => std::borrow::Cow::Borrowed(other),
706        }
707    }
708}
709
710impl InlineRun {
711    /// A run with no active formatting (renders as bare inline text).
712    pub fn is_plain(&self) -> bool {
713        !self.bold
714            && !self.italic
715            && !self.underline
716            && !self.strike
717            && !self.code
718            && !self.formula
719            && self.script == Script::Baseline
720    }
721}
722
723/// Build the [`Node`] for a paragraph of inline content from its structured
724/// `runs` and Markdown text, applying docling's `InlineGroup` boundary:
725///
726/// * a single plain run (or none) → a plain [`Node::Paragraph`] (which the
727///   serializers render as `<text>…</text>`, and a lone hyperlink via `<href>`);
728/// * a single uniformly-formatted run, or two or more runs → a
729///   [`Node::InlineGroup`]. `unwrapped` (the group's docling parent is a
730///   heading, so no enclosing `<text>`) only applies to multi-run groups.
731///
732/// Markdown/JSON render the group's `md_text`, so their output is identical to
733/// emitting a `Paragraph` — the structured runs are DocLang-only.
734pub fn inline_paragraph_node(md_text: String, runs: Vec<InlineRun>, unwrapped: bool) -> Node {
735    let single_plain = runs.len() <= 1 && runs.first().is_none_or(|r| r.is_plain());
736    if single_plain {
737        Node::Paragraph { text: md_text }
738    } else {
739        Node::InlineGroup {
740            unwrapped: unwrapped && runs.len() >= 2,
741            runs,
742            md_text,
743        }
744    }
745}
746
747/// One entry of a [`Node::FieldRegion`]: a marker/key/value triple, any of which
748/// may be absent. Mirrors docling's `field_item` with its `marker` / `field_key`
749/// / `field_value` child texts.
750#[derive(Debug, Clone, PartialEq, Default)]
751pub struct FieldItem {
752    pub marker: Option<String>,
753    pub key: Option<String>,
754    pub value: Option<String>,
755    /// docling's `kind` on the `field_value` item — `read_only` or
756    /// `fillable` (the value is or holds a form control); JSON-only.
757    pub value_kind: Option<String>,
758}
759
760/// One node of a [`Node::KeyValueGraph`] — docling-core's `GraphCell`.
761#[derive(Debug, Clone, PartialEq)]
762pub struct GraphCell {
763    /// docling's `GraphCellLabel` value: `key` or `value` (it also knows
764    /// `unspecified` and `checkbox`).
765    pub label: String,
766    /// The cell's number within its graph, what the links refer to.
767    pub cell_id: usize,
768    pub text: String,
769    /// The text before any cleanup — for an XBRL fact key, the concept's
770    /// qualified name where `text` is its local name.
771    pub orig: String,
772}
773
774/// One edge of a [`Node::KeyValueGraph`] — docling-core's `GraphLink`.
775#[derive(Debug, Clone, PartialEq)]
776pub struct GraphLink {
777    /// docling's `GraphLinkLabel` value: `to_value`, `to_child`, `to_parent`
778    /// or `to_key`.
779    pub label: String,
780    pub source_cell_id: usize,
781    pub target_cell_id: usize,
782}
783
784/// One DocumentPictureClassifier prediction — docling-core's
785/// `PictureClassificationClass` (`class_name` + `confidence`).
786#[derive(Debug, Clone, PartialEq)]
787pub struct PictureClass {
788    /// e.g. `bar_chart`, `logo`, `signature` (the classifier's 26-label set).
789    pub class_name: String,
790    pub confidence: f32,
791}
792
793/// A picture's text annotation — docling-core's `PictureDescriptionData`
794/// (`text` + `provenance`), the one shape upstream uses for every model that
795/// writes prose about a picture. The picture-OCR enrichment (#645) fills it
796/// with the lines the OCR engine read off the image, `provenance` naming the
797/// engine (`ppocr` / `tesseract`) the way a VLM description names its model.
798#[derive(Debug, Clone, PartialEq, Eq)]
799pub struct PictureDescription {
800    pub text: String,
801    pub provenance: String,
802}
803
804/// An extracted picture's raw encoded bytes plus its mimetype and pixel size —
805/// the docling.rs analogue of docling-core's `ImageRef`.
806#[derive(Debug, Clone, PartialEq)]
807pub struct PictureImage {
808    /// e.g. `image/png`, `image/jpeg`.
809    pub mimetype: String,
810    pub width: u32,
811    pub height: u32,
812    /// The image file bytes, exactly as embedded (PNG/JPEG/…).
813    pub data: Vec<u8>,
814    /// Pixels per inch of the image relative to the page it came from —
815    /// docling-core's `ImageRef.dpi`, which consumers use to map pixels back
816    /// to points. A crop rendered from a PDF page at `s` px/pt is `72·s`
817    /// (docling: `int(72 * images_scale)`, #519); an image embedded in an
818    /// office file has no render scale and keeps docling's `72`
819    /// ([`Self::DEFAULT_DPI`]).
820    pub dpi: u32,
821}
822
823impl PictureImage {
824    /// docling's `ImageRef` default when no render scale applies.
825    pub const DEFAULT_DPI: u32 = 72;
826
827    /// The `dpi` of an image rendered at `scale` pixels per PDF point
828    /// (`72·scale`, rounded — `int()` would floor 143.99… to 143).
829    pub fn dpi_for_scale(scale: f32) -> u32 {
830        (72.0 * scale).round().max(1.0) as u32
831    }
832
833    /// A `data:` URI for the image (`data:<mimetype>;base64,<…>`).
834    pub fn data_uri(&self) -> String {
835        format!(
836            "data:{};base64,{}",
837            self.mimetype,
838            crate::base64::encode(&self.data)
839        )
840    }
841}
842
843/// One table cell as a first-class object (#240) — the Rust counterpart of
844/// docling's `TableCell`: its text, page geometry, grid rectangle and header
845/// roles. Produced by the PDF TableFormer paths from the predicted OTSL
846/// structure; `bbox` is `[l, t, r, b]` in page points with a top-left origin.
847#[derive(Debug, Clone, PartialEq)]
848pub struct TableCell {
849    pub text: String,
850    /// `[l, t, r, b]`, page points, top-left origin; `None` without geometry.
851    pub bbox: Option<[f32; 4]>,
852    /// Anchor grid position (0-based row/column offsets).
853    pub start_row: usize,
854    pub start_col: usize,
855    /// Span extents (≥ 1); the covered grid positions repeat the cell's text
856    /// in [`Table::rows`].
857    pub row_span: usize,
858    pub col_span: usize,
859    /// OTSL `ched` — a column-header cell.
860    pub column_header: bool,
861    /// OTSL `rhed` — a row-header cell.
862    pub row_header: bool,
863    /// OTSL `srow` — a section-row cell.
864    pub row_section: bool,
865}
866
867/// Where a picture's or table's caption text item hangs in the docling-JSON
868/// tree (#390). docling's backends do not agree, and the JSON structure is
869/// the only surface that shows it (Markdown, DocLang and LaTeX place a
870/// caption by its item, whatever its parent): the PDF pipeline parents a
871/// layout caption to the picture or table it belongs to, while every
872/// declarative backend creates the caption with `doc.add_text(label=CAPTION)`
873/// and no parent — the document body — even when the item itself sits in a
874/// group or under a section header (JATS, LaTeX, HTML, Markdown, EPUB,
875/// AsciiDoc all do). The office backends hang a chart's title caption off the
876/// chart's container (the sheet group, the slide, docx's current parent,
877/// docling#4190) — that is [`Node::Chart`]'s own path, not this choice. A
878/// backend that adds a new captioned item picks the variant matching
879/// upstream's `add_text` call for that format; the default is upstream's
880/// default.
881#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
882pub enum CaptionParent {
883    /// `#/body`, listed after the enclosing top-level item — docling's
884    /// `add_text` default, which every declarative backend leaves alone.
885    #[default]
886    Body,
887    /// The item's own container (its `parent`), listed ahead of the item:
888    /// the caption is created first, as docling's office backends do.
889    Container,
890    /// The item's own container, listed *after* the item: docling's HTML
891    /// backend adds a `<figure>`-wrapped table, then its `<figcaption>` under
892    /// `self.parents[self.level]` (docling#4050).
893    ContainerAfter,
894    /// The item itself — the caption is the picture's or table's first
895    /// child, as docling's PDF pipeline attaches a layout caption.
896    Item,
897}
898
899/// A simple row-major table. By default `rows[0]` is the header row; a
900/// [`TableStructure`] overlay overrides that and adds column spans.
901#[derive(Debug, Clone, PartialEq, Default)]
902pub struct Table {
903    pub rows: Vec<Vec<String>>,
904    /// Optional layout provenance: the four DocLang `<location>` values
905    /// (`x0,y0,x1,y1`, each already normalized to the 0–511 resolution) emitted
906    /// before the table's cells. Set only by backends with real geometry (e.g.
907    /// the spreadsheet backend, whose cell grid yields a bounding box); left
908    /// `None` by declarative backends, which have no coordinates.
909    pub location: Option<[u16; 4]>,
910    /// Optional OTSL structure overlay for backends that parse real table
911    /// geometry (USPTO CALS): explicit header-row count and horizontal-span
912    /// continuations. `None` → the default (row 0 is the header, no spans).
913    /// `rows` still carries the full text grid (span text replicated) for
914    /// Markdown/JSON; DocLang uses this overlay to emit `<ched/>`/`<lcel/>`.
915    pub structure: Option<TableStructure>,
916    /// Optional per-cell block content, parallel to `rows`. A *rich* cell (an
917    /// ODF cell holding a list, several paragraphs, or a nested table) carries
918    /// its DocLang blocks here; the DocLang serializer emits them after the
919    /// cell token instead of the flat `rows` text. Markdown/JSON ignore this
920    /// and render `rows`, so their output is unchanged. `None` (or an empty
921    /// `Vec` for a given cell) → the flat text is used everywhere.
922    pub cell_blocks: Option<Vec<Vec<Vec<Node>>>>,
923    /// Optional caption (docling's `TableItem.captions`): the JATS
924    /// `<table-wrap>` label+caption, an HTML `<caption>`, etc. Markdown renders
925    /// it as a text line *before* the grid; JSON emits a caption text item the
926    /// table references; DocLang emits a `<caption>` as the table's first child.
927    /// `None` → the table has no caption.
928    pub caption: Option<String>,
929    /// Where the caption item hangs in the JSON tree (#390); see
930    /// [`CaptionParent`]. Only the JSON export reads it.
931    pub caption_parent: CaptionParent,
932    /// The caption's own box on the table's page (0–511 grid) — the PDF
933    /// pipeline's caption layout region, written as the caption item's
934    /// `prov` (#609); see [`Node::Picture`]'s field of the same name.
935    pub caption_location: Option<[u16; 4]>,
936    /// Optional per-cell bounding boxes, same shape as [`Self::rows`]: `[l, t,
937    /// r, b]` in page points with a **top-left** origin (the PDF pipeline's
938    /// native space). Set by the ML pipeline's TableFormer paths — a spanned
939    /// cell repeats its anchor's box across the covered grid positions — and
940    /// First-class cells (#240): the authoritative per-cell records —
941    /// text, page geometry, spans and header roles — when the backend
942    /// produces them (the PDF TableFormer paths do; declarative backends
943    /// leave `None`). [`Self::rows`] stays the dense text grid every
944    /// serializer renders (a spanning cell's text is replicated across its
945    /// covered positions there); JSON serializes these cells verbatim when
946    /// present, and the DocLang structure overlay is derived from them.
947    pub cells: Option<Vec<TableCell>>,
948}
949
950impl Table {
951    /// A cell's text at a grid position, `None` outside the grid.
952    pub fn cell_text(&self, row: usize, col: usize) -> Option<&str> {
953        self.rows.get(row)?.get(col).map(String::as_str)
954    }
955
956    /// Replace the text at a grid position; `false` (and no change) outside
957    /// the grid. When a first-class cell covers the position, the whole
958    /// cell is updated: its record text and every grid position its span
959    /// covers, so the repair shows once in Markdown, not once per covered
960    /// column.
961    pub fn set_cell_text(&mut self, row: usize, col: usize, text: impl Into<String>) -> bool {
962        if self.rows.get(row).and_then(|r| r.get(col)).is_none() {
963            return false;
964        }
965        let text = text.into();
966        let covering = self.cells.as_mut().and_then(|cells| {
967            cells.iter_mut().find(|c| {
968                (c.start_row..c.start_row + c.row_span).contains(&row)
969                    && (c.start_col..c.start_col + c.col_span).contains(&col)
970            })
971        });
972        if let Some(cell) = covering {
973            cell.text = text.clone();
974            let (r0, r1) = (cell.start_row, cell.start_row + cell.row_span);
975            let (c0, c1) = (cell.start_col, cell.start_col + cell.col_span);
976            for r in self.rows.iter_mut().take(r1).skip(r0) {
977                for slot in r.iter_mut().take(c1).skip(c0) {
978                    *slot = text.clone();
979                }
980            }
981        } else {
982            self.rows[row][col] = text;
983        }
984        true
985    }
986
987    /// Derive first-class cells (#240) from the dense grid plus the
988    /// [`TableStructure`] overlay — how declarative tables (DOCX/XLSX merged
989    /// regions, HTML `th`/spans, ODF covered cells, USPTO CALS) get real
990    /// `TableCell` records without page geometry. Anchors are the positions
991    /// not marked as span continuations; extents scan the continuation grids
992    /// right/down (matching the DocLang `lcel`/`ucel` reading). Header roles
993    /// come from the per-cell `col_header`/`row_header` grids when present,
994    /// else the `header_row` band, else docling's declarative default (row 0
995    /// is the header). Without any overlay every position is a 1×1 cell.
996    pub fn derive_cells(&self) -> Vec<TableCell> {
997        let s = self.structure.as_ref();
998        let flag = |grid: Option<&Vec<Vec<bool>>>, r: usize, c: usize| {
999            grid.and_then(|g| g.get(r))
1000                .and_then(|row| row.get(c))
1001                .copied()
1002                .unwrap_or(false)
1003        };
1004        let col_cont = |r: usize, c: usize| flag(s.map(|s| &s.col_continuation), r, c);
1005        let row_cont = |r: usize, c: usize| flag(s.map(|s| &s.row_continuation), r, c);
1006        let is_col_header = |r: usize, c: usize| match s {
1007            Some(st) if !st.col_header.is_empty() => flag(Some(&st.col_header), r, c),
1008            Some(st) if !st.header_row.is_empty() => st.header_row.get(r).copied().unwrap_or(false),
1009            _ => r == 0,
1010        };
1011        let mut cells = Vec::new();
1012        for (r, row) in self.rows.iter().enumerate() {
1013            for (c, text) in row.iter().enumerate() {
1014                if col_cont(r, c) || row_cont(r, c) {
1015                    continue; // covered by a span anchor
1016                }
1017                let mut col_span = 1;
1018                while c + col_span < row.len() && col_cont(r, c + col_span) {
1019                    col_span += 1;
1020                }
1021                let mut row_span = 1;
1022                while r + row_span < self.rows.len() && row_cont(r + row_span, c) {
1023                    row_span += 1;
1024                }
1025                cells.push(TableCell {
1026                    text: text.clone(),
1027                    bbox: None,
1028                    start_row: r,
1029                    start_col: c,
1030                    row_span,
1031                    col_span,
1032                    column_header: is_col_header(r, c),
1033                    row_header: flag(s.map(|s| &s.row_header), r, c),
1034                    row_section: false,
1035                });
1036            }
1037        }
1038        cells
1039    }
1040
1041    /// The number of leading grid rows that form the column header —
1042    /// docling-core's `_count_header_rows` (docling-core#723, 2.96) shared by
1043    /// the Markdown serializer and the chunker's dataframe view: a row counts
1044    /// only when a `column_header` cell *starts* on it (a header spanning
1045    /// several rows is replicated into each row it covers, and counting those
1046    /// would pull the data rows beneath it into the header block). Two
1047    /// special cases: `1` when no cell carries the flag at all, so tables from
1048    /// backends that never set it keep row 0 as the header; `0` when flags
1049    /// exist but none starts on row 0 — then nothing is promotable and every
1050    /// row stays in the body. Uses the first-class [`Self::cells`] when
1051    /// present (the PDF pipeline's TableFormer flags), else the cells derived
1052    /// from the structure overlay.
1053    ///
1054    /// A pivot table's row headers no longer disturb this: docling#4216 flags
1055    /// a spanning `<th>` row as `row_header`, not `column_header`, so the
1056    /// first data row is no longer folded into the header block and the
1057    /// deviation this port carried for docling-core#765 is gone.
1058    ///
1059    /// A row also stops the block when it *carries body text* (#604,
1060    /// docling-core#766, 2.97): a cell starting on it that is not a column
1061    /// header, has non-blank text and does not start in the table's first
1062    /// column. A scanned form flags its first-column labels `column_header`
1063    /// on every row; without the check each such row folded its values into
1064    /// the header, and a table labelled on every row lost its whole body.
1065    /// When row 0 itself is stopped that way and no later row carries a
1066    /// flag, row 0 is still the header (`1`), as for an unflagged table.
1067    pub fn header_row_count(&self) -> usize {
1068        if self.rows.is_empty() {
1069            return 0;
1070        }
1071        let derived;
1072        let cells: &[TableCell] = match &self.cells {
1073            Some(c) if !c.is_empty() => c,
1074            _ => {
1075                derived = self.derive_cells();
1076                &derived
1077            }
1078        };
1079        let first_col = cells.iter().map(|c| c.start_col).min().unwrap_or(0);
1080        let is_header_row = |r: usize| {
1081            let mut flagged = false;
1082            for c in cells.iter().filter(|c| c.start_row == r) {
1083                if c.column_header {
1084                    flagged = true;
1085                } else if c.start_col != first_col && !c.text.trim().is_empty() {
1086                    return false;
1087                }
1088            }
1089            flagged
1090        };
1091        let count = (0..self.rows.len())
1092            .take_while(|&r| is_header_row(r))
1093            .count();
1094        // Upstream's row-0 fallback: no flag on any grid row below the first
1095        // (a spanning header cell counts on every row it covers) → row 0.
1096        if count == 0
1097            && !cells
1098                .iter()
1099                .any(|c| c.column_header && c.start_row + c.row_span.max(1) > 1)
1100        {
1101            return 1;
1102        }
1103        count
1104    }
1105
1106    /// The first-class cell covering a grid position, if any.
1107    pub fn cell_at(&self, row: usize, col: usize) -> Option<&TableCell> {
1108        self.cells.as_ref()?.iter().find(|c| {
1109            (c.start_row..c.start_row + c.row_span).contains(&row)
1110                && (c.start_col..c.start_col + c.col_span).contains(&col)
1111        })
1112    }
1113
1114    /// A cell's bounding box (`[l, t, r, b]`, page points, top-left origin);
1115    /// `None` when no cell with geometry covers the position.
1116    pub fn cell_bbox(&self, row: usize, col: usize) -> Option<[f32; 4]> {
1117        self.cell_at(row, col)?.bbox
1118    }
1119
1120    /// Set (or replace) the bounding box of the cell covering a grid
1121    /// position; `false` outside the text grid. A table without first-class
1122    /// cells materializes them first (one 1×1 cell per grid position, texts
1123    /// from the grid), so declarative tables can be annotated too.
1124    pub fn set_cell_bbox(&mut self, row: usize, col: usize, bbox: [f32; 4]) -> bool {
1125        if self.rows.get(row).and_then(|r| r.get(col)).is_none() {
1126            return false;
1127        }
1128        let rows = &self.rows;
1129        let cells = self.cells.get_or_insert_with(|| {
1130            rows.iter()
1131                .enumerate()
1132                .flat_map(|(r, cols)| {
1133                    cols.iter().enumerate().map(move |(c, text)| TableCell {
1134                        text: text.clone(),
1135                        bbox: None,
1136                        start_row: r,
1137                        start_col: c,
1138                        row_span: 1,
1139                        col_span: 1,
1140                        column_header: false,
1141                        row_header: false,
1142                        row_section: false,
1143                    })
1144                })
1145                .collect()
1146        });
1147        match cells.iter_mut().find(|c| {
1148            (c.start_row..c.start_row + c.row_span).contains(&row)
1149                && (c.start_col..c.start_col + c.col_span).contains(&col)
1150        }) {
1151            Some(cell) => {
1152                cell.bbox = Some(bbox);
1153                true
1154            }
1155            None => {
1156                cells.push(TableCell {
1157                    text: self.rows[row][col].clone(),
1158                    bbox: Some(bbox),
1159                    start_row: row,
1160                    start_col: col,
1161                    row_span: 1,
1162                    col_span: 1,
1163                    column_header: false,
1164                    row_header: false,
1165                    row_section: false,
1166                });
1167                true
1168            }
1169        }
1170    }
1171
1172    /// The anchor position of the cell whose box overlaps `bbox` best
1173    /// (largest intersection-over-union), ties resolved in cell order.
1174    /// `None` when nothing overlaps or the table carries no geometry. This
1175    /// is the lookup half of the repair workflow: find the cell an external
1176    /// OCR box refers to, then [`Self::set_cell_text`] it.
1177    pub fn find_cell_by_bbox(&self, bbox: [f32; 4]) -> Option<(usize, usize)> {
1178        let area = |b: &[f32; 4]| ((b[2] - b[0]) * (b[3] - b[1])).max(0.0);
1179        let mut best: Option<(f32, (usize, usize))> = None;
1180        for cell in self.cells.as_deref()?.iter() {
1181            let Some(cb) = cell.bbox else { continue };
1182            let iw = (bbox[2].min(cb[2]) - bbox[0].max(cb[0])).max(0.0);
1183            let ih = (bbox[3].min(cb[3]) - bbox[1].max(cb[1])).max(0.0);
1184            let inter = iw * ih;
1185            if inter <= 0.0 {
1186                continue;
1187            }
1188            let iou = inter / (area(&bbox) + area(&cb) - inter).max(f32::EPSILON);
1189            if best.is_none_or(|(b, _)| iou > b) {
1190                best = Some((iou, (cell.start_row, cell.start_col)));
1191            }
1192        }
1193        best.map(|(_, pos)| pos)
1194    }
1195
1196    /// Locate the cell overlapping `bbox` best and replace its text — the
1197    /// one-call form of the OCR-repair loop. Returns the updated anchor.
1198    pub fn update_cell_by_bbox(
1199        &mut self,
1200        bbox: [f32; 4],
1201        text: impl Into<String>,
1202    ) -> Option<(usize, usize)> {
1203        let (row, col) = self.find_cell_by_bbox(bbox)?;
1204        self.set_cell_text(row, col, text);
1205        Some((row, col))
1206    }
1207}
1208
1209/// OTSL structure overlay for a [`Table`], parallel to [`Table::rows`].
1210#[derive(Debug, Clone, PartialEq, Default)]
1211pub struct TableStructure {
1212    /// Per-row: `true` if the row's non-empty cells are column headers
1213    /// (emitted as `<ched/>` rather than `<fcel/>`).
1214    pub header_row: Vec<bool>,
1215    /// Same shape as [`Table::rows`]; `true` where a cell continues a
1216    /// horizontal span from its left neighbour (emitted as `<lcel/>`).
1217    pub col_continuation: Vec<Vec<bool>>,
1218    /// Same shape as [`Table::rows`]; `true` where a cell continues a
1219    /// vertical span from the cell above (emitted as `<ucel/>`). Empty or all
1220    /// `false` when the backend has no vertical spans (e.g. USPTO CALS).
1221    pub row_continuation: Vec<Vec<bool>>,
1222    /// Same shape as [`Table::rows`]; `true` where a non-empty cell is a row
1223    /// header (emitted as `<rhed/>`) — a chart's category column. Empty when
1224    /// the table has no row headers.
1225    pub row_header: Vec<Vec<bool>>,
1226    /// Same shape as [`Table::rows`]; `true` where a cell is a *column header*
1227    /// cell (an HTML `<th>`). When non-empty this per-cell grid supersedes the
1228    /// per-row [`Self::header_row`] for `<ched/>` emission, matching docling's
1229    /// cell-level `column_header` flag; the chunker derives its header-row
1230    /// count from it.
1231    pub col_header: Vec<Vec<bool>>,
1232}
1233
1234impl DoclingDocument {
1235    /// Create an empty document with the given name.
1236    pub fn new(name: impl Into<String>) -> Self {
1237        Self {
1238            name: name.into(),
1239            nodes: Vec::new(),
1240            strict_markdown: false,
1241            compact_tables: false,
1242            page_break_placeholder: None,
1243            links: Vec::new(),
1244            confidence: None,
1245            tree: None,
1246            page_images: std::collections::BTreeMap::new(),
1247        }
1248    }
1249
1250    /// Append a node.
1251    /// The document's top-level tables in reading order — the read half of
1252    /// the post-extraction table API (#238). [`Node::Located`] wrappers (the
1253    /// PDF pipeline attaches layout provenance that way) are looked through;
1254    /// tables nested inside rich table cells (`Table::cell_blocks`) are not
1255    /// traversed.
1256    pub fn tables(&self) -> impl Iterator<Item = &Table> {
1257        fn unwrap_table(n: &Node) -> Option<&Table> {
1258            match n {
1259                Node::Table(t) => Some(t),
1260                Node::Located { inner, .. }
1261                | Node::Prov { inner, .. }
1262                | Node::Track { inner, .. } => unwrap_table(inner),
1263                _ => None,
1264            }
1265        }
1266        self.nodes.iter().filter_map(unwrap_table)
1267    }
1268
1269    /// Mutable access to the document's top-level tables, for repair
1270    /// workflows (#238): locate a cell via [`Table::find_cell_by_bbox`], fix
1271    /// its text with [`Table::set_cell_text`], then re-export — every
1272    /// serializer reads the same grid.
1273    pub fn tables_mut(&mut self) -> impl Iterator<Item = &mut Table> {
1274        fn unwrap_table(n: &mut Node) -> Option<&mut Table> {
1275            match n {
1276                Node::Table(t) => Some(t),
1277                Node::Located { inner, .. }
1278                | Node::Prov { inner, .. }
1279                | Node::Track { inner, .. } => unwrap_table(inner),
1280                _ => None,
1281            }
1282        }
1283        self.nodes.iter_mut().filter_map(unwrap_table)
1284    }
1285
1286    pub fn push(&mut self, node: Node) {
1287        self.nodes.push(node);
1288    }
1289
1290    /// Visit every [`Node::Picture`] in the flat node stream, wherever it
1291    /// nests — group children, table cell blocks, a picture's own
1292    /// children, the located/prov/track/comment/furniture wrappers — so an
1293    /// enrichment that reads the embedded images (picture OCR, #645) reaches
1294    /// each one. The callback gets the picture node itself.
1295    pub fn for_each_picture_mut(&mut self, f: &mut dyn FnMut(&mut Node)) {
1296        fn walk(nodes: &mut [Node], f: &mut dyn FnMut(&mut Node)) {
1297            for node in nodes {
1298                match node {
1299                    Node::Picture { .. } => f(node),
1300                    Node::Group { children, .. } | Node::PictureChildren(children) => {
1301                        walk(children, f)
1302                    }
1303                    Node::Located { inner, .. }
1304                    | Node::Prov { inner, .. }
1305                    | Node::Track { inner, .. }
1306                    | Node::Furniture { inner, .. }
1307                    | Node::Commented { inner, .. }
1308                    | Node::DoclangOnly(inner) => walk(std::slice::from_mut(inner), f),
1309                    Node::Table(t) => {
1310                        if let Some(blocks) = t.cell_blocks.as_mut() {
1311                            for row in blocks {
1312                                for cell in row {
1313                                    walk(cell, f);
1314                                }
1315                            }
1316                        }
1317                    }
1318                    _ => {}
1319                }
1320            }
1321        }
1322        walk(&mut self.nodes, f);
1323    }
1324
1325    /// Convenience: append a heading.
1326    pub fn add_heading(&mut self, level: u8, text: impl Into<String>) {
1327        self.push(Node::Heading {
1328            level,
1329            text: text.into(),
1330        });
1331    }
1332
1333    /// Convenience: append a paragraph.
1334    pub fn add_paragraph(&mut self, text: impl Into<String>) {
1335        self.push(Node::Paragraph { text: text.into() });
1336    }
1337
1338    /// Serialize the document to Markdown.
1339    ///
1340    /// The Rust equivalent of docling-core's
1341    /// `DoclingDocument.export_to_markdown()`. Uses [`Self::strict_markdown`] to
1342    /// pick between docling-legacy output (default) and the cleaner, more
1343    /// conformant variant.
1344    pub fn export_to_markdown(&self) -> String {
1345        to_markdown(self, self.strict_markdown)
1346    }
1347
1348    /// Serialize to Markdown, explicitly choosing the mode regardless of
1349    /// [`Self::strict_markdown`]. `strict = true` produces cleaner, more
1350    /// conformant Markdown (code-fence languages preserved, no inline-run
1351    /// spacing artifacts); `strict = false` reproduces docling's legacy output.
1352    pub fn export_to_markdown_with(&self, strict: bool) -> String {
1353        to_markdown(self, strict)
1354    }
1355
1356    /// Markdown for this document as the *content of a rich table cell*
1357    /// (docling-core's `in_table_cell` serialization, docling-core#540):
1358    /// headings render as plain text since Markdown tables can't hold them.
1359    /// Backends build a sub-document per rich cell and flatten this into the
1360    /// cell text; no trailing newline.
1361    pub fn export_to_table_cell_markdown(&self) -> String {
1362        crate::markdown::to_markdown_table_cell(self, self.strict_markdown)
1363    }
1364
1365    /// Serialize to docling-core's native JSON wire format (`DoclingDocument`
1366    /// schema), pretty-printed — the Rust equivalent of
1367    /// `DoclingDocument.export_to_dict()` / `save_as_json()`. The output loads
1368    /// back into Python docling-core and round-trips to the same Markdown.
1369    pub fn export_to_json(&self) -> String {
1370        let mut out = Vec::new();
1371        self.write_json_pretty(&mut out)
1372            .expect("DoclingDocument JSON is always serializable");
1373        String::from_utf8(out).expect("serde_json writes UTF-8")
1374    }
1375
1376    /// The same JSON wire format as [`Self::export_to_json`], as a
1377    /// `serde_json::Value` — for callers that append response-level extras
1378    /// (docling-serve adds the confidence report, #183) before serializing.
1379    ///
1380    /// The `Value` is large — ~2.7 KB per text item — so on a long document
1381    /// prefer [`Self::write_json`], which writes the same JSON without it.
1382    pub fn export_to_json_value(&self) -> serde_json::Value {
1383        crate::json::to_json(self)
1384    }
1385
1386    /// Write the JSON of [`Self::export_to_json_value`] to `writer`, compact
1387    /// — byte for byte `serde_json::to_writer(writer, &doc.export_to_json_value())`
1388    /// — without building the `Value`: the export holds its text items in a
1389    /// compact form and serializes them straight to the writer. Wrap a file
1390    /// or socket in a `BufWriter`.
1391    pub fn write_json<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
1392        crate::json::write_json(self, writer, false).map_err(std::io::Error::from)
1393    }
1394
1395    /// [`Self::write_json`], indented as [`Self::export_to_json`] is.
1396    pub fn write_json_pretty<W: std::io::Write>(&self, writer: W) -> std::io::Result<()> {
1397        crate::json::write_json(self, writer, true).map_err(std::io::Error::from)
1398    }
1399
1400    /// Serialize to a complete LaTeX document — the Rust counterpart of
1401    /// docling-core's `LaTeXDocSerializer` with default parameters (docling
1402    /// 2.124's `--to latex`, #317). No trailing newline, like the upstream
1403    /// CLI's `<stem>.tex`.
1404    pub fn export_to_latex(&self) -> String {
1405        crate::latex::to_latex(self)
1406    }
1407
1408    /// Serialize to Pandoc's AST as JSON (`pandoc -f json`, #515) with the
1409    /// default options: pictures as captioned figures without image data,
1410    /// body layer only, the [`PANDOC_API_VERSION`](crate::pandoc::PANDOC_API_VERSION)
1411    /// API. One line, no trailing newline. See [`crate::pandoc`].
1412    pub fn export_to_pandoc_json(&self) -> String {
1413        crate::pandoc::to_pandoc(self, &crate::pandoc::PandocExportOptions::default())
1414            .expect("the default Pandoc API version is always supported")
1415            .0
1416    }
1417
1418    /// Pandoc JSON per `options` (image mode, artifacts directory, content
1419    /// layers, required API version). Returns the JSON and, for
1420    /// [`ImageMode::Referenced`], the `(path, bytes)` image files; an
1421    /// unsupported `api_version` is an error.
1422    pub fn export_to_pandoc_json_with(
1423        &self,
1424        options: &crate::pandoc::PandocExportOptions,
1425    ) -> Result<crate::pandoc::PandocOutput, crate::pandoc::PandocError> {
1426        crate::pandoc::to_pandoc(self, options)
1427    }
1428
1429    /// Serialize to DocLang XML (`<doclang version="0.7">…`), the markup that
1430    /// lives inside a `.dclx` archive — the Rust counterpart of docling-core's
1431    /// `export_to_doclang()` with default parameters. No trailing newline; the
1432    /// archive writer appends exactly one.
1433    pub fn export_to_doclang(&self) -> String {
1434        crate::doclang::export_to_doclang(&self.nodes)
1435    }
1436
1437    /// [`Self::export_to_doclang`] plus the picture assets its
1438    /// `<src uri="assets/image_….png"/>` references name, as `(path, bytes)`
1439    /// in document order — the parts docling's `save_as_doclang_archive`
1440    /// stores next to `document.xml`. PNG and JPEG pictures come back as PNG
1441    /// (a JPEG re-encoded from its libjpeg-decoded pixels); other encodings
1442    /// keep their own bytes for the archive writer to convert.
1443    pub fn export_to_doclang_with_assets(&self) -> (String, Vec<(String, Vec<u8>)>) {
1444        crate::doclang::export_to_doclang_with_assets(&self.nodes)
1445    }
1446
1447    /// Serialize to Markdown with an explicit picture [`ImageMode`] (mirrors
1448    /// docling's `image_mode`). Returns the Markdown and, for
1449    /// [`ImageMode::Referenced`], the `(relative-path, bytes)` of each image the
1450    /// caller should write next to the Markdown file. `artifacts_dir` is the
1451    /// directory name used in referenced links.
1452    pub fn export_to_markdown_with_images(
1453        &self,
1454        image_mode: ImageMode,
1455        artifacts_dir: &str,
1456    ) -> (String, Vec<(String, Vec<u8>)>) {
1457        to_markdown_images(self, self.strict_markdown, image_mode, artifacts_dir)
1458    }
1459
1460    /// Markdown per `options` (#599) — docling-core's
1461    /// `export_to_markdown(included_content_layers=…, image_mode=…,
1462    /// image_placeholder=…, escape_html=…, escape_underscores=…,
1463    /// traverse_pictures=…)`: the content layers rendered, whether a
1464    /// picture's nested text items print, HTML and underscore escaping, the
1465    /// image placeholder, and the image mode with its referenced-image
1466    /// directory. [`Self::strict_markdown`] still picks the Markdown flavour.
1467    /// Returns the Markdown and, for [`ImageMode::Referenced`], the
1468    /// `(path, bytes)` artifacts. With [`MarkdownExportOptions::default`]
1469    /// the Markdown is [`Self::export_to_markdown`]'s byte for byte.
1470    pub fn export_to_markdown_with_options(
1471        &self,
1472        options: &MarkdownExportOptions,
1473    ) -> (String, Vec<(String, Vec<u8>)>) {
1474        crate::markdown::to_markdown_with_options(self, self.strict_markdown, options)
1475    }
1476
1477    /// WebVTT (#614) — what docling's `--to vtt` writes
1478    /// (`DoclingDocument.save_as_vtt`): a cue per timed text item — a WebVTT
1479    /// input's cues, an audio/video transcript's segments — hours always
1480    /// written, the `</v>` of a lone voice span omitted. A document without
1481    /// timed text is the bare `WEBVTT` header. No trailing newline.
1482    pub fn export_to_vtt(&self) -> String {
1483        crate::vtt::to_vtt(self, &crate::vtt::VttExportOptions::default())
1484    }
1485
1486    /// [`Self::export_to_vtt`] with docling-core's `WebVTTParams`
1487    /// (`omit_hours_if_zero`, `omit_voice_end`).
1488    pub fn export_to_vtt_with_options(&self, options: &crate::vtt::VttExportOptions) -> String {
1489        crate::vtt::to_vtt(self, options)
1490    }
1491
1492    /// Plain text — docling-core's `DoclingDocument.export_to_text()` with
1493    /// its defaults (#613, docling's `--to text` / `<stem>.txt`): the
1494    /// Markdown export without decoration ([`MarkdownExportOptions::plain_text`]).
1495    /// Lists keep their bullets and numbers, tables their `|` grid. No
1496    /// trailing newline: upstream's `serialize().text`, which its CLI
1497    /// writes verbatim to `<stem>.txt`.
1498    pub fn export_to_text(&self) -> String {
1499        let (mut text, _) = crate::markdown::to_markdown_with_options(
1500            self,
1501            self.strict_markdown,
1502            &MarkdownExportOptions::plain_text(),
1503        );
1504        if text.ends_with('\n') {
1505            text.pop();
1506        }
1507        text
1508    }
1509
1510    /// A complete HTML document — docling-core's `HTMLDocSerializer` with its
1511    /// defaults (#492): pictures stay out (`ImageRefMode.PLACEHOLDER`, only
1512    /// their captions and meta render). See [`export_to_html_with_images`]
1513    /// for the embedded / referenced modes and [`crate::html`]'s module docs
1514    /// for what is reproduced.
1515    ///
1516    /// [`export_to_html_with_images`]: Self::export_to_html_with_images
1517    pub fn export_to_html(&self) -> String {
1518        crate::html::to_html(self, &HtmlExportOptions::default()).0
1519    }
1520
1521    /// HTML with pictures per `image_mode`: `data:` URIs when embedded, and
1522    /// when referenced `<img src>` paths under `artifacts_dir` whose bytes come
1523    /// back as `(path, bytes)` for the caller to write — the Markdown export's
1524    /// contract and file names.
1525    pub fn export_to_html_with_images(
1526        &self,
1527        image_mode: ImageMode,
1528        artifacts_dir: &str,
1529    ) -> (String, Vec<(String, Vec<u8>)>) {
1530        crate::html::to_html(
1531            self,
1532            &HtmlExportOptions {
1533                image_mode,
1534                artifacts_dir: artifacts_dir.to_string(),
1535                ..HtmlExportOptions::default()
1536            },
1537        )
1538    }
1539
1540    /// HTML rendering the content `layers` — docling-core's
1541    /// `export_to_html(included_content_layers=…)` (#499). The default export
1542    /// is body only; `ContentLayers::BODY.with(ContentLayer::Furniture)` adds
1543    /// page headers/footers, `.with(ContentLayer::Notes)` reviewer comments,
1544    /// [`ContentLayers::ALL`] is Python's `set(ContentLayer)`. An item on a
1545    /// layer outside the set is skipped while its children are still walked,
1546    /// and the items that do render go through the same serializers as the
1547    /// body (a page header is a `<p>`, a comment a `<p>`, a header table a
1548    /// `<table>`), exactly as upstream's `HTMLParams.layers` behaves. Pictures
1549    /// stay placeholders; see [`export_to_html_with`] for the full option set.
1550    ///
1551    /// [`export_to_html_with`]: Self::export_to_html_with
1552    pub fn export_to_html_with_layers(&self, layers: ContentLayers) -> String {
1553        crate::html::to_html(
1554            self,
1555            &HtmlExportOptions {
1556                layers,
1557                ..HtmlExportOptions::default()
1558            },
1559        )
1560        .0
1561    }
1562
1563    /// HTML per `options` — image mode, referenced-image directory and content
1564    /// layers in one call; the other `export_to_html*` methods are
1565    /// shorthands for it. Returns the HTML and, for
1566    /// [`ImageMode::Referenced`], the `(path, bytes)` artifacts.
1567    pub fn export_to_html_with(
1568        &self,
1569        options: &HtmlExportOptions,
1570    ) -> (String, Vec<(String, Vec<u8>)>) {
1571        crate::html::to_html(self, options)
1572    }
1573}
1574
1575#[cfg(test)]
1576mod table_api_tests {
1577    use super::*;
1578
1579    fn cell(
1580        text: &str,
1581        bbox: [f32; 4],
1582        (start_row, start_col): (usize, usize),
1583        (row_span, col_span): (usize, usize),
1584    ) -> TableCell {
1585        TableCell {
1586            text: text.into(),
1587            bbox: Some(bbox),
1588            start_row,
1589            start_col,
1590            row_span,
1591            col_span,
1592            column_header: false,
1593            row_header: false,
1594            row_section: false,
1595        }
1596    }
1597
1598    fn table() -> Table {
1599        Table {
1600            rows: vec![
1601                vec!["Year".into(), "Ducks".into()],
1602                vec!["2019".into(), "120".into()],
1603            ],
1604            cells: Some(vec![
1605                cell("Year", [0.0, 0.0, 50.0, 10.0], (0, 0), (1, 1)),
1606                cell("Ducks", [50.0, 0.0, 100.0, 10.0], (0, 1), (1, 1)),
1607                cell("2019", [0.0, 10.0, 50.0, 20.0], (1, 0), (1, 1)),
1608                cell("120", [50.0, 10.0, 100.0, 20.0], (1, 1), (1, 1)),
1609            ]),
1610            ..Default::default()
1611        }
1612    }
1613
1614    /// Declarative tables derive first-class cells from the structure
1615    /// overlay: continuation grids become span extents, `col_header` (or the
1616    /// row-0 fallback) becomes the header role — the XLSX/DOCX/HTML merge
1617    /// path into real `TableCell`s (#240).
1618    #[test]
1619    fn derive_cells_reads_spans_and_headers_from_structure() {
1620        let t = Table {
1621            rows: vec![
1622                vec!["Wide".into(), "Wide".into(), "C".into()],
1623                vec!["a".into(), "b".into(), "c".into()],
1624            ],
1625            structure: Some(TableStructure {
1626                header_row: vec![true, false],
1627                col_continuation: vec![vec![false, true, false], vec![false; 3]],
1628                row_continuation: vec![vec![false; 3], vec![false; 3]],
1629                row_header: Vec::new(),
1630                col_header: Vec::new(),
1631            }),
1632            ..Default::default()
1633        };
1634        let cells = t.derive_cells();
1635        assert_eq!(cells.len(), 5, "two anchors in row 0, three in row 1");
1636        let wide = &cells[0];
1637        assert_eq!((wide.col_span, wide.row_span), (2, 1));
1638        assert!(wide.column_header, "header_row band");
1639        assert!(cells.iter().skip(2).all(|c| !c.column_header));
1640
1641        // Without any overlay: every position 1x1, row 0 the header
1642        // (docling's declarative default — the old JSON synthesis).
1643        let plain = Table {
1644            rows: vec![vec!["h".into()], vec!["x".into()]],
1645            ..Default::default()
1646        };
1647        let cells = plain.derive_cells();
1648        assert_eq!(cells.len(), 2);
1649        assert!(cells[0].column_header && !cells[1].column_header);
1650    }
1651
1652    /// A spanning cell updates once: the record text and every covered grid
1653    /// position — a repair shows once in Markdown, not once per column.
1654    #[test]
1655    fn span_repair_updates_the_whole_cell() {
1656        let mut t = Table {
1657            rows: vec![
1658                vec!["Wide".into(), "Wide".into(), "C".into()],
1659                vec!["a".into(), "b".into(), "c".into()],
1660            ],
1661            cells: Some(vec![
1662                cell("Wide", [0.0, 0.0, 100.0, 10.0], (0, 0), (1, 2)),
1663                cell("C", [100.0, 0.0, 150.0, 10.0], (0, 2), (1, 1)),
1664            ]),
1665            ..Default::default()
1666        };
1667        // Update through the covered (non-anchor) position.
1668        assert!(t.set_cell_text(0, 1, "Fixed"));
1669        assert_eq!(
1670            t.rows[0],
1671            vec!["Fixed".to_string(), "Fixed".into(), "C".into()]
1672        );
1673        assert_eq!(t.cell_at(0, 1).unwrap().text, "Fixed");
1674        assert_eq!(t.cell_at(0, 1).unwrap().col_span, 2);
1675    }
1676
1677    /// The OCR-repair loop (#238): locate a cell by an external box (best
1678    /// IoU), replace its text, and see the fix in the export — the grid is
1679    /// the single source of truth for every serializer.
1680    #[test]
1681    fn bbox_lookup_and_repair_flow_into_exports() {
1682        let mut doc = DoclingDocument::new("t");
1683        doc.push(Node::Table(table()));
1684        assert_eq!(doc.tables().count(), 1);
1685
1686        let t = doc.tables_mut().next().unwrap();
1687        // A slightly-off OCR box still lands on the (1,1) cell.
1688        assert_eq!(t.find_cell_by_bbox([52.0, 11.0, 98.0, 19.0]), Some((1, 1)));
1689        assert_eq!(
1690            t.update_cell_by_bbox([52.0, 11.0, 98.0, 19.0], "125"),
1691            Some((1, 1))
1692        );
1693        assert_eq!(t.cell_text(1, 1), Some("125"));
1694        assert!(doc.export_to_markdown().contains("125"));
1695
1696        // No overlap → no match, nothing changed.
1697        let t = doc.tables_mut().next().unwrap();
1698        assert_eq!(t.find_cell_by_bbox([500.0, 500.0, 600.0, 600.0]), None);
1699    }
1700
1701    #[test]
1702    fn cell_accessors_bound_check_and_geometry_materializes() {
1703        let mut t = table();
1704        assert_eq!(t.cell_text(0, 0), Some("Year"));
1705        assert_eq!(t.cell_text(5, 0), None);
1706        assert!(!t.set_cell_text(0, 9, "x"), "outside the grid");
1707        assert_eq!(t.cell_bbox(1, 0), Some([0.0, 10.0, 50.0, 20.0]));
1708
1709        // A geometry-less table materializes its box grid on first set.
1710        let mut plain = Table {
1711            rows: vec![vec!["a".into(), "b".into()]],
1712            ..Default::default()
1713        };
1714        assert_eq!(plain.cell_bbox(0, 1), None);
1715        assert!(!plain.set_cell_bbox(0, 5, [0.0; 4]), "outside the grid");
1716        assert!(plain.set_cell_bbox(0, 1, [1.0, 2.0, 3.0, 4.0]));
1717        assert_eq!(plain.cell_bbox(0, 1), Some([1.0, 2.0, 3.0, 4.0]));
1718        assert_eq!(plain.find_cell_by_bbox([1.5, 2.5, 2.5, 3.5]), Some((0, 1)));
1719    }
1720}