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    /// Hyperlinks recovered from the source, as `(anchor_text, href)` pairs in
28    /// document order. docling's standard pipeline drops PDF link annotations, so
29    /// these are rendered as Markdown `[anchor](href)` **only in strict mode**
30    /// (legacy/docling output is left byte-for-byte unchanged). The PDF backend
31    /// populates this from pdfium link annotations; other backends leave it empty.
32    pub links: Vec<(String, String)>,
33    /// Conversion-confidence report (#183), populated by the PDF/image ML
34    /// pipeline; `None` for declarative conversions. Deliberately **not**
35    /// part of any document export (docling keeps it on the conversion
36    /// result, outside the document schema) — docling-serve surfaces it in
37    /// the HTTP response instead.
38    pub confidence: Option<crate::confidence::ConfidenceReport>,
39}
40
41/// A single piece of document content.
42#[derive(Debug, Clone, PartialEq)]
43pub enum Node {
44    /// A heading. `level` is 1-6.
45    Heading { level: u8, text: String },
46    /// A run of body text.
47    Paragraph { text: String },
48    /// A form checkbox (docling's `checkbox_selected`/`checkbox_unselected`): its
49    /// clean label `text` with the checked state. DocLang emits a `<checkbox>`
50    /// element head; Markdown/JSON render the task-list form (`- [x] `/`- [ ] `).
51    CheckboxItem { checked: bool, text: String },
52    /// A single list item at the given nesting `level` (0 = top). For ordered
53    /// items, `number` is the display number (honoring the list's `start`); it
54    /// is unused for unordered items. `first_in_list` marks the first item of a
55    /// list so the serializer can blank-line-separate adjacent sibling lists.
56    ///
57    /// `marker` is the DocLang enumeration marker (`"1."`, `"1.1."`, …) when the
58    /// backend provides one — HTML and DOCX set it for enumerated items, so
59    /// DocLang emits `<ldiv><marker>…</marker></ldiv>`; Markdown and the other
60    /// declarative backends leave it `None`, giving a bare `<ldiv/>` (matching
61    /// docling, whose Markdown backend passes no marker).
62    ListItem {
63        ordered: bool,
64        number: u64,
65        first_in_list: bool,
66        text: String,
67        level: u8,
68        marker: Option<String>,
69        /// Optional layout provenance (`x0,y0,x1,y1`, normalized to 0–511): the
70        /// four DocLang `<location>` values emitted inside the `<list>` right
71        /// after the item's `<ldiv>`. Set only by backends with real geometry
72        /// (e.g. PPTX shapes); `None` for the declarative backends. Kept on the
73        /// item itself (rather than a [`Node::Located`] wrapper) so consecutive
74        /// items still group into one `<list>`.
75        location: Option<[u16; 4]>,
76        /// DocLang-only override for items whose DocLang form diverges from their
77        /// flat Markdown `text`. Markdown/JSON always render the fields above; the
78        /// DocLang serializer, when this is `Some`, takes the list kind, marker,
79        /// and content from here instead. Used for docx multilevel numbering
80        /// (Markdown shows `- 1.1. x`, DocLang an ordered `<marker>1.1.</marker>`
81        /// with clean text) and inline equations/formatting in list items.
82        dclx: Option<ListItemDclx>,
83        /// The item's hyperlink target, when its content is a link — docling's
84        /// HTML backend emits it as an `<href uri=…/>` in the item head, and the
85        /// anchor's Markdown link markup is stripped from the rendered content.
86        /// `None` for a plain item; ignored by Markdown/JSON.
87        href: Option<String>,
88        /// Non-body content layer (docling's HTML site chrome before the first
89        /// heading → `furniture`). DocLang emits a `<layer value=…/>` in the item
90        /// head; Markdown/JSON drop a non-body item entirely.
91        layer: Option<ContentLayer>,
92    },
93    /// A fenced code block.
94    Code {
95        language: Option<String>,
96        text: String,
97        /// The original (pre-enrichment) text when the CodeFormula model
98        /// rewrote `text`: docling keeps the raw extraction in the JSON `orig`
99        /// field while `text` carries the model output. `None` → `orig == text`.
100        orig: Option<String>,
101        /// A line-preserving rendering, when the backend can reconstruct one
102        /// but docling's own output for the format cannot. The PDF pipeline
103        /// sets it (docling-parse joins code lines with single spaces, so
104        /// `text` carries that flat docling-parity form): **strict** Markdown
105        /// prefers `pretty`, every byte-conformance surface (legacy Markdown,
106        /// JSON, DocLang, chunks) serializes `text`.
107        pretty: Option<String>,
108    },
109    /// A table. The first row is treated as the header.
110    Table(Table),
111    /// A picture/figure, with an optional caption and (when a backend extracts
112    /// it) the embedded image itself.
113    Picture {
114        caption: Option<String>,
115        image: Option<PictureImage>,
116        /// DocumentPictureClassifier predictions (all classes, descending
117        /// confidence), when the picture-classification enrichment ran.
118        /// Serialized as docling's `classification` annotation + `meta` field
119        /// on the JSON picture item; Markdown/DocLang output is unaffected.
120        classification: Option<Vec<PictureClass>>,
121    },
122    /// A display-math formula item decoded by the CodeFormula enrichment:
123    /// `latex` is the model's LaTeX (no `$$` wrapping), `orig` the raw glyph
124    /// text extracted from the PDF. Markdown renders `$$latex$$`; JSON emits a
125    /// `formula` text item (docling's un-enriched pipeline instead emits a
126    /// placeholder paragraph — see the PDF assembler).
127    Formula {
128        latex: String,
129        orig: String,
130        location: Option<[u16; 4]>,
131    },
132    /// A chart (docling's `PictureItem` classified as a chart, carrying a
133    /// `PictureTabularChartData` annotation). Markdown and JSON render it exactly
134    /// like a [`Node::Picture`] placeholder (an `<!-- image -->` / `picture`
135    /// item); the DocLang serializer emits `<picture class="chart">` with a
136    /// `<label value="{kind}"/>` and the data `table` as a `<tabular>`.
137    Chart {
138        /// docling's classification label, e.g. `bar_chart`, `line_chart`.
139        kind: String,
140        /// The chart's data grid (row 0 is the header band).
141        table: Table,
142        /// The chart title (docling's caption item on the picture).
143        caption: Option<String>,
144        /// DocLang `<location>` provenance for the picture element.
145        location: Option<[u16; 4]>,
146    },
147    /// A logical grouping of child nodes (e.g. a list, a section).
148    Group { label: String, children: Vec<Node> },
149    /// A form key-value region (docling's `field_region`): a set of form fields,
150    /// each pairing an optional marker, key, and value. Backends detect these
151    /// from form structure (e.g. HTML's `keyN` / `keyN_valueM` / `keyN_marker`
152    /// `id`-convention); the serializers render each item's parts as separate
153    /// labelled texts (`marker` / `field_key` / `field_value`).
154    FieldRegion { items: Vec<FieldItem> },
155    /// Rich inline content — docling's `InlineGroup`: a run of styled text
156    /// segments that a backend captured with formatting (`<bold>`, `<italic>`,
157    /// `<underline>`, `<strikethrough>`, sub/superscript, inline `<code>`) the
158    /// flat Markdown text cannot represent. Markdown/JSON render this exactly
159    /// like `Paragraph { text: md_text }` (so their output is unchanged); the
160    /// DocLang serializer uses the structured `runs`. `unwrapped` is set when the
161    /// group's docling parent is a heading/text (no enclosing `<text>` wrapper).
162    InlineGroup {
163        unwrapped: bool,
164        runs: Vec<InlineRun>,
165        md_text: String,
166    },
167    /// A node in a non-body content layer — `furniture` (page headers/footers,
168    /// the HTML `<title>`, site navigation/chrome) or `notes` (docx comments).
169    /// Markdown and JSON omit these layers by default; DocLang renders the wrapped
170    /// node with a `<layer value="{layer}"/>` head.
171    Furniture {
172        layer: ContentLayer,
173        inner: Box<Node>,
174    },
175    /// A node carrying layout provenance — the four DocLang `<location>` values
176    /// (`x0,y0,x1,y1`, normalized to 0–511) docling attaches to elements from
177    /// backends with real geometry (e.g. the slide shapes in PPTX). Markdown and
178    /// JSON render the wrapped node unchanged; DocLang emits the `<location>`
179    /// tokens as the element's first children.
180    Located {
181        location: [u16; 4],
182        inner: Box<Node>,
183    },
184    /// A PDF page header or footer (docling's `page_header`/`page_footer`
185    /// furniture): DocLang emits `<page_header>`/`<page_footer>` with a
186    /// `<layer value="furniture"/>` head, the four `<location>` tokens, then the
187    /// text. Markdown and JSON omit it like other furniture.
188    PageFurniture {
189        footer: bool,
190        location: [u16; 4],
191        text: String,
192    },
193    /// A page boundary — docling's implicit page break between pages. The PPTX
194    /// backend emits one between consecutive slides. DocLang renders it as
195    /// `<page_break/>`; Markdown and JSON omit it (matching docling's default
196    /// exports, which carry page breaks only in the document model).
197    PageBreak,
198    /// An invisible page marker — the first node of every page the PDF paths
199    /// assemble: the 1-based page number and the page size in PDF points. It
200    /// carries exactly what the JSON export needs to populate docling's
201    /// `pages` map and to denormalize the 0–511 `<location>` grid back into
202    /// BOTTOMLEFT point bboxes for per-item `prov` (#171). Every other
203    /// serializer skips it, so Markdown / DocLang / DocTags output is
204    /// byte-for-byte unchanged.
205    PageInfo {
206        /// 1-based page number (0 = "not yet numbered": the assembler emits
207        /// the marker, the document-level collector stamps the real number).
208        page_no: usize,
209        /// Page width in PDF points.
210        width: f32,
211        /// Page height in PDF points.
212        height: f32,
213    },
214    /// A node docling keeps in the document model (and DocLang) but leaves out
215    /// of the Markdown and JSON exports — e.g. an ODF *presentation*'s pictures
216    /// and charts, which appear in the `.dclx` body but not in its `.md`/`.json`.
217    /// DocLang renders the wrapped node in place; Markdown and JSON skip it.
218    DoclangOnly(Box<Node>),
219    /// A verbatim plain-text dump — docling's plain-text backend emits the whole
220    /// file as a single text item (used for legacy USPTO APS `.txt` grants, which
221    /// docling routes to plain text rather than its APS parser). The stored string
222    /// is the file body, one record per line. Markdown/JSON render it as one text
223    /// block; the DocLang serializer reproduces minidom's per-line layout, CDATA-
224    /// escaping only the lines that need it (see `emit_text_dump`).
225    TextDump(String),
226}
227
228/// Vertical text position of an [`InlineRun`] — docling's `Script`.
229#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
230pub enum Script {
231    #[default]
232    Baseline,
233    Sub,
234    Super,
235}
236
237/// One styled segment of a [`Node::InlineGroup`] — the docling.rs analogue of a
238/// `TextItem` inside an `InlineGroup`, carrying the ancestor formatting docling
239/// tracks. `text` is already whitespace-normalized/trimmed (one segment per
240/// source text node). A hyperlink is intentionally not stored: DocLang drops the
241/// target inside inline scope, keeping only the anchor text.
242#[derive(Debug, Clone, PartialEq, Eq, Default)]
243pub struct InlineRun {
244    pub text: String,
245    pub bold: bool,
246    pub italic: bool,
247    pub underline: bool,
248    pub strike: bool,
249    pub script: Script,
250    pub code: bool,
251    /// An inline equation (`text` holds LaTeX): DocLang renders `<formula>…`,
252    /// Markdown/JSON keep the `$…$` already baked into the group's `md_text`.
253    pub formula: bool,
254}
255
256/// A DocLang content layer other than the default `body` (see [`Node::Furniture`]).
257#[derive(Debug, Clone, Copy, PartialEq, Eq)]
258pub enum ContentLayer {
259    /// Page headers/footers, HTML `<title>`, site navigation/chrome.
260    Furniture,
261    /// Editorial notes (docx reviewer comments).
262    Notes,
263    /// Invisible content (hidden spreadsheet sheets).
264    Invisible,
265}
266
267impl ContentLayer {
268    /// The `<layer value="…"/>` token value.
269    pub fn value(self) -> &'static str {
270        match self {
271            ContentLayer::Furniture => "furniture",
272            ContentLayer::Notes => "notes",
273            ContentLayer::Invisible => "invisible",
274        }
275    }
276}
277
278/// DocLang-only content for a [`Node::ListItem`] whose DocLang form differs from
279/// its flat Markdown `text` (see [`Node::ListItem::dclx`]). `ordered` picks the
280/// enclosing `<list>` kind, `marker` the `<ldiv><marker>`; content is `runs`
281/// (structured equations/formatting) when non-empty, else `text` re-parsed for
282/// inline markers.
283#[derive(Debug, Clone, PartialEq, Eq, Default)]
284pub struct ListItemDclx {
285    pub ordered: bool,
286    pub marker: Option<String>,
287    pub text: String,
288    pub runs: Vec<InlineRun>,
289}
290
291impl InlineRun {
292    /// A run with no active formatting (renders as bare inline text).
293    pub fn is_plain(&self) -> bool {
294        !self.bold
295            && !self.italic
296            && !self.underline
297            && !self.strike
298            && !self.code
299            && !self.formula
300            && self.script == Script::Baseline
301    }
302}
303
304/// Build the [`Node`] for a paragraph of inline content from its structured
305/// `runs` and Markdown text, applying docling's `InlineGroup` boundary:
306///
307/// * a single plain run (or none) → a plain [`Node::Paragraph`] (which the
308///   serializers render as `<text>…</text>`, and a lone hyperlink via `<href>`);
309/// * a single uniformly-formatted run, or two or more runs → a
310///   [`Node::InlineGroup`]. `unwrapped` (the group's docling parent is a
311///   heading, so no enclosing `<text>`) only applies to multi-run groups.
312///
313/// Markdown/JSON render the group's `md_text`, so their output is identical to
314/// emitting a `Paragraph` — the structured runs are DocLang-only.
315pub fn inline_paragraph_node(md_text: String, runs: Vec<InlineRun>, unwrapped: bool) -> Node {
316    let single_plain = runs.len() <= 1 && runs.first().is_none_or(|r| r.is_plain());
317    if single_plain {
318        Node::Paragraph { text: md_text }
319    } else {
320        Node::InlineGroup {
321            unwrapped: unwrapped && runs.len() >= 2,
322            runs,
323            md_text,
324        }
325    }
326}
327
328/// One entry of a [`Node::FieldRegion`]: a marker/key/value triple, any of which
329/// may be absent. Mirrors docling's `field_item` with its `marker` / `field_key`
330/// / `field_value` child texts.
331#[derive(Debug, Clone, PartialEq, Default)]
332pub struct FieldItem {
333    pub marker: Option<String>,
334    pub key: Option<String>,
335    pub value: Option<String>,
336}
337
338/// One DocumentPictureClassifier prediction — docling-core's
339/// `PictureClassificationClass` (`class_name` + `confidence`).
340#[derive(Debug, Clone, PartialEq)]
341pub struct PictureClass {
342    /// e.g. `bar_chart`, `logo`, `signature` (the classifier's 26-label set).
343    pub class_name: String,
344    pub confidence: f32,
345}
346
347/// An extracted picture's raw encoded bytes plus its mimetype and pixel size —
348/// the docling.rs analogue of docling-core's `ImageRef`.
349#[derive(Debug, Clone, PartialEq)]
350pub struct PictureImage {
351    /// e.g. `image/png`, `image/jpeg`.
352    pub mimetype: String,
353    pub width: u32,
354    pub height: u32,
355    /// The image file bytes, exactly as embedded (PNG/JPEG/…).
356    pub data: Vec<u8>,
357}
358
359impl PictureImage {
360    /// A `data:` URI for the image (`data:<mimetype>;base64,<…>`).
361    pub fn data_uri(&self) -> String {
362        format!(
363            "data:{};base64,{}",
364            self.mimetype,
365            crate::base64::encode(&self.data)
366        )
367    }
368}
369
370/// A simple row-major table. By default `rows[0]` is the header row; a
371/// [`TableStructure`] overlay overrides that and adds column spans.
372#[derive(Debug, Clone, PartialEq, Default)]
373pub struct Table {
374    pub rows: Vec<Vec<String>>,
375    /// Optional layout provenance: the four DocLang `<location>` values
376    /// (`x0,y0,x1,y1`, each already normalized to the 0–511 resolution) emitted
377    /// before the table's cells. Set only by backends with real geometry (e.g.
378    /// the spreadsheet backend, whose cell grid yields a bounding box); left
379    /// `None` by declarative backends, which have no coordinates.
380    pub location: Option<[u16; 4]>,
381    /// Optional OTSL structure overlay for backends that parse real table
382    /// geometry (USPTO CALS): explicit header-row count and horizontal-span
383    /// continuations. `None` → the default (row 0 is the header, no spans).
384    /// `rows` still carries the full text grid (span text replicated) for
385    /// Markdown/JSON; DocLang uses this overlay to emit `<ched/>`/`<lcel/>`.
386    pub structure: Option<TableStructure>,
387    /// Optional per-cell block content, parallel to `rows`. A *rich* cell (an
388    /// ODF cell holding a list, several paragraphs, or a nested table) carries
389    /// its DocLang blocks here; the DocLang serializer emits them after the
390    /// cell token instead of the flat `rows` text. Markdown/JSON ignore this
391    /// and render `rows`, so their output is unchanged. `None` (or an empty
392    /// `Vec` for a given cell) → the flat text is used everywhere.
393    pub cell_blocks: Option<Vec<Vec<Vec<Node>>>>,
394    /// Optional caption (docling's `TableItem.captions`): the JATS
395    /// `<table-wrap>` label+caption, an HTML `<caption>`, etc. Markdown renders
396    /// it as a text line *before* the grid; JSON emits a caption text item the
397    /// table references; DocLang emits a `<caption>` as the table's first child.
398    /// `None` → the table has no caption.
399    pub caption: Option<String>,
400    /// Optional per-cell bounding boxes, same shape as [`Self::rows`]: `[l, t,
401    /// r, b]` in page points with a **top-left** origin (the PDF pipeline's
402    /// native space). Set by the ML pipeline's TableFormer paths — a spanned
403    /// cell repeats its anchor's box across the covered grid positions — and
404    /// `None` for backends without page geometry (declarative formats, the
405    /// geometric table fallback). This is API-level provenance for
406    /// post-extraction repair workflows (#238: locate a cell by box, fix its
407    /// OCR text, re-export); no serializer reads it, so wire outputs are
408    /// unchanged by its presence.
409    pub cell_boxes: Option<Vec<Vec<Option<[f32; 4]>>>>,
410}
411
412impl Table {
413    /// A cell's text, `None` outside the grid.
414    pub fn cell_text(&self, row: usize, col: usize) -> Option<&str> {
415        self.rows.get(row)?.get(col).map(String::as_str)
416    }
417
418    /// Replace a cell's text; `false` (and no change) outside the grid. The
419    /// grid is the single source of truth for every serializer, so the new
420    /// text flows into Markdown/JSON/DocLang exports as-is. Note that a
421    /// spanned cell's text is *replicated* across its covered positions —
422    /// repairing a span means updating each covered position.
423    pub fn set_cell_text(&mut self, row: usize, col: usize, text: impl Into<String>) -> bool {
424        match self.rows.get_mut(row).and_then(|r| r.get_mut(col)) {
425            Some(cell) => {
426                *cell = text.into();
427                true
428            }
429            None => false,
430        }
431    }
432
433    /// A cell's bounding box (`[l, t, r, b]`, page points, top-left origin);
434    /// `None` when the backend recorded no geometry or the position is
435    /// outside the grid.
436    pub fn cell_bbox(&self, row: usize, col: usize) -> Option<[f32; 4]> {
437        *self.cell_boxes.as_ref()?.get(row)?.get(col)?
438    }
439
440    /// Set (or replace) a cell's bounding box; `false` outside the text grid.
441    /// A geometry grid is materialized on first use, shaped like
442    /// [`Self::rows`].
443    pub fn set_cell_bbox(&mut self, row: usize, col: usize, bbox: [f32; 4]) -> bool {
444        if self.rows.get(row).and_then(|r| r.get(col)).is_none() {
445            return false;
446        }
447        let boxes = self
448            .cell_boxes
449            .get_or_insert_with(|| self.rows.iter().map(|r| vec![None; r.len()]).collect());
450        // Keep the geometry grid in shape with the text grid even if rows
451        // were edited since it was materialized.
452        while boxes.len() < row + 1 {
453            boxes.push(Vec::new());
454        }
455        let brow = &mut boxes[row];
456        while brow.len() < col + 1 {
457            brow.push(None);
458        }
459        brow[col] = Some(bbox);
460        true
461    }
462
463    /// The grid position whose recorded box overlaps `bbox` best (largest
464    /// intersection-over-union), ties resolved in reading order. `None` when
465    /// nothing overlaps or the table carries no geometry. This is the lookup
466    /// half of the repair workflow: find the cell an external OCR box refers
467    /// to, then [`Self::set_cell_text`] it.
468    pub fn find_cell_by_bbox(&self, bbox: [f32; 4]) -> Option<(usize, usize)> {
469        let boxes = self.cell_boxes.as_ref()?;
470        let area = |b: &[f32; 4]| ((b[2] - b[0]) * (b[3] - b[1])).max(0.0);
471        let mut best: Option<(f32, (usize, usize))> = None;
472        for (r, row) in boxes.iter().enumerate() {
473            for (c, cell) in row.iter().enumerate() {
474                let Some(cb) = cell else { continue };
475                let iw = (bbox[2].min(cb[2]) - bbox[0].max(cb[0])).max(0.0);
476                let ih = (bbox[3].min(cb[3]) - bbox[1].max(cb[1])).max(0.0);
477                let inter = iw * ih;
478                if inter <= 0.0 {
479                    continue;
480                }
481                let iou = inter / (area(&bbox) + area(cb) - inter).max(f32::EPSILON);
482                if best.is_none_or(|(b, _)| iou > b) {
483                    best = Some((iou, (r, c)));
484                }
485            }
486        }
487        best.map(|(_, pos)| pos)
488    }
489
490    /// Locate the cell overlapping `bbox` best and replace its text — the
491    /// one-call form of the OCR-repair loop. Returns the updated position.
492    pub fn update_cell_by_bbox(
493        &mut self,
494        bbox: [f32; 4],
495        text: impl Into<String>,
496    ) -> Option<(usize, usize)> {
497        let (row, col) = self.find_cell_by_bbox(bbox)?;
498        self.set_cell_text(row, col, text);
499        Some((row, col))
500    }
501}
502
503/// OTSL structure overlay for a [`Table`], parallel to [`Table::rows`].
504#[derive(Debug, Clone, PartialEq, Default)]
505pub struct TableStructure {
506    /// Per-row: `true` if the row's non-empty cells are column headers
507    /// (emitted as `<ched/>` rather than `<fcel/>`).
508    pub header_row: Vec<bool>,
509    /// Same shape as [`Table::rows`]; `true` where a cell continues a
510    /// horizontal span from its left neighbour (emitted as `<lcel/>`).
511    pub col_continuation: Vec<Vec<bool>>,
512    /// Same shape as [`Table::rows`]; `true` where a cell continues a
513    /// vertical span from the cell above (emitted as `<ucel/>`). Empty or all
514    /// `false` when the backend has no vertical spans (e.g. USPTO CALS).
515    pub row_continuation: Vec<Vec<bool>>,
516    /// Same shape as [`Table::rows`]; `true` where a non-empty cell is a row
517    /// header (emitted as `<rhed/>`) — a chart's category column. Empty when
518    /// the table has no row headers.
519    pub row_header: Vec<Vec<bool>>,
520    /// Same shape as [`Table::rows`]; `true` where a cell is a *column header*
521    /// cell (an HTML `<th>`). When non-empty this per-cell grid supersedes the
522    /// per-row [`Self::header_row`] for `<ched/>` emission, matching docling's
523    /// cell-level `column_header` flag; the chunker derives its header-row
524    /// count from it.
525    pub col_header: Vec<Vec<bool>>,
526}
527
528impl DoclingDocument {
529    /// Create an empty document with the given name.
530    pub fn new(name: impl Into<String>) -> Self {
531        Self {
532            name: name.into(),
533            nodes: Vec::new(),
534            strict_markdown: false,
535            compact_tables: false,
536            links: Vec::new(),
537            confidence: None,
538        }
539    }
540
541    /// Append a node.
542    /// The document's top-level tables in reading order — the read half of
543    /// the post-extraction table API (#238). [`Node::Located`] wrappers (the
544    /// PDF pipeline attaches layout provenance that way) are looked through;
545    /// tables nested inside rich table cells (`Table::cell_blocks`) are not
546    /// traversed.
547    pub fn tables(&self) -> impl Iterator<Item = &Table> {
548        fn unwrap_table(n: &Node) -> Option<&Table> {
549            match n {
550                Node::Table(t) => Some(t),
551                Node::Located { inner, .. } => unwrap_table(inner),
552                _ => None,
553            }
554        }
555        self.nodes.iter().filter_map(unwrap_table)
556    }
557
558    /// Mutable access to the document's top-level tables, for repair
559    /// workflows (#238): locate a cell via [`Table::find_cell_by_bbox`], fix
560    /// its text with [`Table::set_cell_text`], then re-export — every
561    /// serializer reads the same grid.
562    pub fn tables_mut(&mut self) -> impl Iterator<Item = &mut Table> {
563        fn unwrap_table(n: &mut Node) -> Option<&mut Table> {
564            match n {
565                Node::Table(t) => Some(t),
566                Node::Located { inner, .. } => unwrap_table(inner),
567                _ => None,
568            }
569        }
570        self.nodes.iter_mut().filter_map(unwrap_table)
571    }
572
573    pub fn push(&mut self, node: Node) {
574        self.nodes.push(node);
575    }
576
577    /// Convenience: append a heading.
578    pub fn add_heading(&mut self, level: u8, text: impl Into<String>) {
579        self.push(Node::Heading {
580            level,
581            text: text.into(),
582        });
583    }
584
585    /// Convenience: append a paragraph.
586    pub fn add_paragraph(&mut self, text: impl Into<String>) {
587        self.push(Node::Paragraph { text: text.into() });
588    }
589
590    /// Serialize the document to Markdown.
591    ///
592    /// The Rust equivalent of docling-core's
593    /// `DoclingDocument.export_to_markdown()`. Uses [`Self::strict_markdown`] to
594    /// pick between docling-legacy output (default) and the cleaner, more
595    /// conformant variant.
596    pub fn export_to_markdown(&self) -> String {
597        to_markdown(self, self.strict_markdown)
598    }
599
600    /// Serialize to Markdown, explicitly choosing the mode regardless of
601    /// [`Self::strict_markdown`]. `strict = true` produces cleaner, more
602    /// conformant Markdown (code-fence languages preserved, no inline-run
603    /// spacing artifacts); `strict = false` reproduces docling's legacy output.
604    pub fn export_to_markdown_with(&self, strict: bool) -> String {
605        to_markdown(self, strict)
606    }
607
608    /// Serialize to docling-core's native JSON wire format (`DoclingDocument`
609    /// schema), pretty-printed — the Rust equivalent of
610    /// `DoclingDocument.export_to_dict()` / `save_as_json()`. The output loads
611    /// back into Python docling-core and round-trips to the same Markdown.
612    pub fn export_to_json(&self) -> String {
613        serde_json::to_string_pretty(&self.export_to_json_value())
614            .expect("DoclingDocument JSON is always serializable")
615    }
616
617    /// The same JSON wire format as [`Self::export_to_json`], as a
618    /// `serde_json::Value` — for callers that append response-level extras
619    /// (docling-serve adds the confidence report, #183) before serializing.
620    pub fn export_to_json_value(&self) -> serde_json::Value {
621        crate::json::to_json(self)
622    }
623
624    /// Serialize to DocLang XML (`<doclang version="0.7">…`), the markup that
625    /// lives inside a `.dclx` archive — the Rust counterpart of docling-core's
626    /// `export_to_doclang()` with default parameters. No trailing newline; the
627    /// archive writer appends exactly one.
628    pub fn export_to_doclang(&self) -> String {
629        crate::doclang::export_to_doclang(&self.nodes)
630    }
631
632    /// Serialize to Markdown with an explicit picture [`ImageMode`] (mirrors
633    /// docling's `image_mode`). Returns the Markdown and, for
634    /// [`ImageMode::Referenced`], the `(relative-path, bytes)` of each image the
635    /// caller should write next to the Markdown file. `artifacts_dir` is the
636    /// directory name used in referenced links.
637    pub fn export_to_markdown_with_images(
638        &self,
639        image_mode: ImageMode,
640        artifacts_dir: &str,
641    ) -> (String, Vec<(String, Vec<u8>)>) {
642        to_markdown_images(self, self.strict_markdown, image_mode, artifacts_dir)
643    }
644}
645
646#[cfg(test)]
647mod table_api_tests {
648    use super::*;
649
650    fn table() -> Table {
651        Table {
652            rows: vec![
653                vec!["Year".into(), "Ducks".into()],
654                vec!["2019".into(), "120".into()],
655            ],
656            cell_boxes: Some(vec![
657                vec![Some([0.0, 0.0, 50.0, 10.0]), Some([50.0, 0.0, 100.0, 10.0])],
658                vec![
659                    Some([0.0, 10.0, 50.0, 20.0]),
660                    Some([50.0, 10.0, 100.0, 20.0]),
661                ],
662            ]),
663            ..Default::default()
664        }
665    }
666
667    /// The OCR-repair loop (#238): locate a cell by an external box (best
668    /// IoU), replace its text, and see the fix in the export — the grid is
669    /// the single source of truth for every serializer.
670    #[test]
671    fn bbox_lookup_and_repair_flow_into_exports() {
672        let mut doc = DoclingDocument::new("t");
673        doc.push(Node::Table(table()));
674        assert_eq!(doc.tables().count(), 1);
675
676        let t = doc.tables_mut().next().unwrap();
677        // A slightly-off OCR box still lands on the (1,1) cell.
678        assert_eq!(t.find_cell_by_bbox([52.0, 11.0, 98.0, 19.0]), Some((1, 1)));
679        assert_eq!(
680            t.update_cell_by_bbox([52.0, 11.0, 98.0, 19.0], "125"),
681            Some((1, 1))
682        );
683        assert_eq!(t.cell_text(1, 1), Some("125"));
684        assert!(doc.export_to_markdown().contains("125"));
685
686        // No overlap → no match, nothing changed.
687        let t = doc.tables_mut().next().unwrap();
688        assert_eq!(t.find_cell_by_bbox([500.0, 500.0, 600.0, 600.0]), None);
689    }
690
691    #[test]
692    fn cell_accessors_bound_check_and_geometry_materializes() {
693        let mut t = table();
694        assert_eq!(t.cell_text(0, 0), Some("Year"));
695        assert_eq!(t.cell_text(5, 0), None);
696        assert!(!t.set_cell_text(0, 9, "x"), "outside the grid");
697        assert_eq!(t.cell_bbox(1, 0), Some([0.0, 10.0, 50.0, 20.0]));
698
699        // A geometry-less table materializes its box grid on first set.
700        let mut plain = Table {
701            rows: vec![vec!["a".into(), "b".into()]],
702            ..Default::default()
703        };
704        assert_eq!(plain.cell_bbox(0, 1), None);
705        assert!(!plain.set_cell_bbox(0, 5, [0.0; 4]), "outside the grid");
706        assert!(plain.set_cell_bbox(0, 1, [1.0, 2.0, 3.0, 4.0]));
707        assert_eq!(plain.cell_bbox(0, 1), Some([1.0, 2.0, 3.0, 4.0]));
708        assert_eq!(plain.find_cell_by_bbox([1.5, 2.5, 2.5, 3.5]), Some((0, 1)));
709    }
710}