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 /// Where the caption item hangs in the JSON tree (#390); see
149 /// [`CaptionParent`]. Markdown, DocLang and LaTeX ignore it.
150 caption_parent: CaptionParent,
151 },
152 /// A display-math formula item decoded by the CodeFormula enrichment:
153 /// `latex` is the model's LaTeX (no `$$` wrapping), `orig` the raw glyph
154 /// text extracted from the PDF. Markdown renders `$$latex$$`; JSON emits a
155 /// `formula` text item (docling's un-enriched pipeline instead emits a
156 /// placeholder paragraph — see the PDF assembler).
157 Formula {
158 latex: String,
159 orig: String,
160 location: Option<[u16; 4]>,
161 },
162 /// A standalone caption item (docling's `DocItemLabel.CAPTION` text that
163 /// no picture or table claims): the HTML backend emits a `<figure>`'s
164 /// `<figcaption>` this way when the figure produced no picture and its
165 /// first item is not a table (docling#4050). `href` is the caption's
166 /// hyperlink annotation (the first link inside the figcaption); Markdown
167 /// renders it as `[text](href)`, JSON puts `hyperlink` on the caption
168 /// item, DocLang emits the block-form `<caption>` with an `<href>` head.
169 Caption { text: String, href: Option<String> },
170 /// A chart (docling's `PictureItem` classified as a chart, carrying a
171 /// `PictureTabularChartData` annotation). Markdown and JSON render it exactly
172 /// like a [`Node::Picture`] placeholder (an `<!-- image -->` / `picture`
173 /// item); the DocLang serializer emits `<picture class="chart">` with a
174 /// `<label value="{kind}"/>` and the data `table` as a `<tabular>`.
175 Chart {
176 /// docling's classification label, e.g. `bar_chart`, `line_chart`.
177 kind: String,
178 /// The chart's data grid (row 0 is the header band).
179 table: Table,
180 /// The chart title (docling's caption item on the picture).
181 caption: Option<String>,
182 /// DocLang `<location>` provenance for the picture element.
183 location: Option<[u16; 4]>,
184 },
185 /// A logical grouping of child nodes (e.g. a list, a section, a spreadsheet
186 /// sheet). `name` is docling's group name when it differs from the label —
187 /// an xlsx sheet group is `label: "sheet"`, `name: Some("Sheet1")`. `layer`
188 /// puts the group *and* everything it contains on a non-body content layer
189 /// (a hidden sheet is `invisible`), which is how the serializers that only
190 /// render body content know to skip it; DocLang stamps each child with the
191 /// layer token, exactly as a [`Node::Furniture`] wrapper on each would.
192 /// DocLang has no group element, so a group is transparent there.
193 Group {
194 label: String,
195 name: Option<String>,
196 layer: Option<ContentLayer>,
197 children: Vec<Node>,
198 },
199 /// A form key-value region (docling's `field_region`): a set of form fields,
200 /// each pairing an optional marker, key, and value. Backends detect these
201 /// from form structure (e.g. HTML's `keyN` / `keyN_valueM` / `keyN_marker`
202 /// `id`-convention); the serializers render each item's parts as separate
203 /// labelled texts (`marker` / `field_key` / `field_value`).
204 FieldRegion { items: Vec<FieldItem> },
205 /// docling's `KeyValueItem`: a graph of key and value cells and the links
206 /// between them (docling-core's `GraphData`). The XBRL backend builds one
207 /// from an instance's numeric facts and the presentation and calculation
208 /// hierarchies of its taxonomy. Only the JSON export carries the graph:
209 /// docling's Markdown serializer has no rendering for the item and writes
210 /// its `<!-- missing-key-value-item -->` placeholder, which the Markdown
211 /// export reproduces; the other serializers omit it.
212 KeyValueGraph {
213 cells: Vec<GraphCell>,
214 links: Vec<GraphLink>,
215 },
216 /// Rich inline content — docling's `InlineGroup`: a run of styled text
217 /// segments that a backend captured with formatting (`<bold>`, `<italic>`,
218 /// `<underline>`, `<strikethrough>`, sub/superscript, inline `<code>`) the
219 /// flat Markdown text cannot represent. Markdown/JSON render this exactly
220 /// like `Paragraph { text: md_text }` (so their output is unchanged); the
221 /// DocLang serializer uses the structured `runs`. `unwrapped` is set when the
222 /// group's docling parent is a heading/text (no enclosing `<text>` wrapper).
223 InlineGroup {
224 unwrapped: bool,
225 runs: Vec<InlineRun>,
226 md_text: String,
227 },
228 /// A node in a non-body content layer — `furniture` (page headers/footers,
229 /// the HTML `<title>`, site navigation/chrome) or `notes` (docx comments).
230 /// Markdown and JSON omit these layers by default; DocLang renders the wrapped
231 /// node with a `<layer value="{layer}"/>` head.
232 Furniture {
233 layer: ContentLayer,
234 inner: Box<Node>,
235 },
236 /// One reviewer comment: docling's notes-layer `comment_section` group
237 /// holding a single text item. `name` is docling's own — `comment-{id}` for
238 /// a docx `w:comment`, `comment-{sheet}-{cell}` for a spreadsheet cell note.
239 /// JSON emits the group plus its notes-layer text; DocLang emits the flat
240 /// `<text><layer value="notes"/>…</text>` upstream writes (its DocLang
241 /// carries no group for comments); Markdown and LaTeX omit the notes layer.
242 ///
243 /// `refs_note_text` picks what a [`Node::Commented`] annotation points at,
244 /// mirroring an upstream asymmetry: docling-core's `add_comment` appends the
245 /// **note text**'s ref to each target (which is what the xlsx backend gets),
246 /// while the docx backend overwrites that with the **group**'s ref so a
247 /// comment's replies group together.
248 ///
249 /// `grouped` is whether docling wraps the note in a `comment_section`
250 /// group at all: the docx and spreadsheet backends do, while backends that
251 /// call docling-core's `add_comment` directly (Pages, #383) get a bare
252 /// notes-layer text item under the body — JSON then emits no group and
253 /// `name` is unused.
254 CommentSection {
255 name: String,
256 text: String,
257 refs_note_text: bool,
258 grouped: bool,
259 },
260 /// A body item annotated by reviewer comments: `comments` are indices into
261 /// the document's [`Node::CommentSection`] nodes, in document order. JSON
262 /// emits docling's `comments: [{"$ref": …}]` on the item, each ref pointing
263 /// where the section says (see [`Node::CommentSection::refs_note_text`]);
264 /// every other serializer renders `inner` unchanged.
265 Commented {
266 comments: Vec<usize>,
267 inner: Box<Node>,
268 },
269 /// A node carrying layout provenance — the four DocLang `<location>` values
270 /// (`x0,y0,x1,y1`, normalized to 0–511) docling attaches to elements from
271 /// backends with real geometry (e.g. the slide shapes in PPTX). Markdown and
272 /// JSON render the wrapped node unchanged; DocLang emits the `<location>`
273 /// tokens as the element's first children.
274 Located {
275 location: [u16; 4],
276 inner: Box<Node>,
277 },
278 /// Exact page provenance for a backend whose geometry already *is* the
279 /// page's coordinate system — an XLSX item's cell-index box on its sheet,
280 /// which docling writes verbatim (`bbox` in a top-left origin, the
281 /// `charspan` the backend chose) and sizes the page from. The 0–511 grid
282 /// of a [`Node::Located`] cannot round-trip such integers exactly, so the
283 /// JSON export reads this wrapper; every other serializer renders `inner`
284 /// unchanged (DocLang keeps taking its `<location>` tokens from the grid).
285 Prov {
286 page_no: usize,
287 /// `[l, t, r, b]`, page units, top-left origin.
288 bbox: [f32; 4],
289 /// docling's `charspan` for the item (`[0, 0]` for an XLSX table).
290 charspan: [usize; 2],
291 /// The item's creation rank among its siblings, when that differs
292 /// from the node order: docling numbers `#/tables/N` / `#/texts/N` /
293 /// `#/pictures/N` in the order it *creates* items (a sheet's tables,
294 /// then its images, then its charts) and only afterwards sorts the
295 /// container's children by position. The JSON export adds siblings in
296 /// this order and lays their refs out in node order; `None` when the
297 /// two orders coincide.
298 seq: Option<usize>,
299 inner: Box<Node>,
300 },
301 /// A PDF page header or footer (docling's `page_header`/`page_footer`
302 /// furniture): DocLang emits `<page_header>`/`<page_footer>` with a
303 /// `<layer value="furniture"/>` head, the four `<location>` tokens, then the
304 /// text. The JSON writes it as a body-parented `page_header`/`page_footer`
305 /// text item on the furniture layer; Markdown omits it.
306 PageFurniture {
307 footer: bool,
308 location: [u16; 4],
309 text: String,
310 },
311 /// A furniture-layer text item with an explicit docling label — a page
312 /// header / footer paragraph (`page_header` / `page_footer`) or a note
313 /// body (`footnote`) from a backend that builds no item tree (#535: the
314 /// Word 97 `.doc` stories). The JSON writes a body-parented item of that
315 /// label on the furniture layer; DocLang `<{label}>` with a `<layer
316 /// value="furniture"/>` head (docling-core's own shape); Markdown, LaTeX
317 /// and the chunker leave it out like any furniture.
318 FurnitureText { label: String, text: String },
319 /// The text a PDF picture contains, kept the way docling keeps it: every
320 /// regular layout cluster > 80 % inside a picture cluster is that
321 /// `PictureItem`'s child (`LayoutPostprocessor._set_cluster_children`,
322 /// `ReadingOrderModel._add_child_elements`). Emitted right after its
323 /// picture node. The JSON export writes these nodes as items parented to
324 /// that picture, after its caption; docling's Markdown and LaTeX picture
325 /// serializers print only the caption and the image, so every other
326 /// serializer skips it.
327 PictureChildren(Vec<Node>),
328 /// A page boundary — docling's implicit page break between pages. The PPTX
329 /// backend emits one between consecutive slides. DocLang renders it as
330 /// `<page_break/>`; Markdown and JSON omit it (matching docling's default
331 /// exports, which carry page breaks only in the document model).
332 PageBreak,
333 /// An invisible page marker — the first node of every page the PDF paths
334 /// assemble: the 1-based page number and the page size in PDF points. It
335 /// carries exactly what the JSON export needs to populate docling's
336 /// `pages` map and to denormalize the 0–511 `<location>` grid back into
337 /// BOTTOMLEFT point bboxes for per-item `prov` (#171). Every other
338 /// serializer skips it, so Markdown / DocLang / DocTags output is
339 /// byte-for-byte unchanged.
340 PageInfo {
341 /// 1-based page number (0 = "not yet numbered": the assembler emits
342 /// the marker, the document-level collector stamps the real number).
343 page_no: usize,
344 /// Page width in PDF points.
345 width: f32,
346 /// Page height in PDF points.
347 height: f32,
348 },
349 /// A node docling keeps in the document model (and DocLang) but leaves out
350 /// of the Markdown and JSON exports — e.g. an ODF *presentation*'s pictures
351 /// and charts, which appear in the `.dclx` body but not in its `.md`/`.json`.
352 /// DocLang renders the wrapped node in place; Markdown and JSON skip it.
353 DoclangOnly(Box<Node>),
354 /// A verbatim plain-text dump — docling's plain-text backend emits the whole
355 /// file as a single text item (used for legacy USPTO APS `.txt` grants, which
356 /// docling routes to plain text rather than its APS parser). The stored string
357 /// is the file body, one record per line. Markdown/JSON render it as one text
358 /// block; the DocLang serializer reproduces minidom's per-line layout, CDATA-
359 /// escaping only the lines that need it (see `emit_text_dump`).
360 TextDump(String),
361}
362
363/// Vertical text position of an [`InlineRun`] — docling's `Script`.
364#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
365pub enum Script {
366 #[default]
367 Baseline,
368 Sub,
369 Super,
370}
371
372/// One styled segment of a [`Node::InlineGroup`] — the docling.rs analogue of a
373/// `TextItem` inside an `InlineGroup`, carrying the ancestor formatting docling
374/// tracks. `text` is already whitespace-normalized/trimmed (one segment per
375/// source text node). A hyperlink is intentionally not stored: DocLang drops the
376/// target inside inline scope, keeping only the anchor text.
377#[derive(Debug, Clone, PartialEq, Eq, Default)]
378pub struct InlineRun {
379 pub text: String,
380 pub bold: bool,
381 pub italic: bool,
382 pub underline: bool,
383 pub strike: bool,
384 pub script: Script,
385 pub code: bool,
386 /// An inline equation (`text` holds LaTeX): DocLang renders `<formula>…`,
387 /// Markdown/JSON keep the `$…$` already baked into the group's `md_text`.
388 pub formula: bool,
389}
390
391/// A DocLang content layer other than the default `body` (see [`Node::Furniture`]).
392#[derive(Debug, Clone, Copy, PartialEq, Eq)]
393pub enum ContentLayer {
394 /// Page headers/footers, HTML `<title>`, site navigation/chrome.
395 Furniture,
396 /// Editorial notes (docx reviewer comments).
397 Notes,
398 /// Invisible content (hidden spreadsheet sheets).
399 Invisible,
400}
401
402impl ContentLayer {
403 /// The `<layer value="…"/>` token value.
404 pub fn value(self) -> &'static str {
405 match self {
406 ContentLayer::Furniture => "furniture",
407 ContentLayer::Notes => "notes",
408 ContentLayer::Invisible => "invisible",
409 }
410 }
411}
412
413/// A set of content layers, `body` included — docling-core's
414/// `set[ContentLayer]` (`HTMLParams.layers`,
415/// `export_to_html(included_content_layers=…)`), where `body` is a member
416/// like any other. [`Default`] is docling's `DEFAULT_CONTENT_LAYERS`: body
417/// only, so an export built with it is unchanged from before layers could
418/// be chosen (#499).
419#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
420pub struct ContentLayers {
421 /// The main content (items without an explicit layer).
422 pub body: bool,
423 /// [`ContentLayer::Furniture`]: page headers/footers, navigation chrome.
424 pub furniture: bool,
425 /// [`ContentLayer::Notes`]: reviewer comments and other editorial notes.
426 pub notes: bool,
427 /// [`ContentLayer::Invisible`]: hidden content (hidden sheets, …).
428 pub invisible: bool,
429}
430
431impl Default for ContentLayers {
432 fn default() -> Self {
433 Self::BODY
434 }
435}
436
437impl ContentLayers {
438 /// Body only — docling's `DEFAULT_CONTENT_LAYERS`.
439 pub const BODY: Self = Self {
440 body: true,
441 furniture: false,
442 notes: false,
443 invisible: false,
444 };
445 /// Every layer — Python's `set(ContentLayer)`.
446 pub const ALL: Self = Self {
447 body: true,
448 furniture: true,
449 notes: true,
450 invisible: true,
451 };
452 /// No layer at all (nothing renders); build a set from it with
453 /// [`with`](Self::with) / [`with_body`](Self::with_body).
454 pub const NONE: Self = Self {
455 body: false,
456 furniture: false,
457 notes: false,
458 invisible: false,
459 };
460
461 /// The set plus `layer`.
462 pub const fn with(mut self, layer: ContentLayer) -> Self {
463 match layer {
464 ContentLayer::Furniture => self.furniture = true,
465 ContentLayer::Notes => self.notes = true,
466 ContentLayer::Invisible => self.invisible = true,
467 }
468 self
469 }
470
471 /// The set plus the body layer.
472 pub const fn with_body(mut self) -> Self {
473 self.body = true;
474 self
475 }
476
477 /// Whether `layer` is in the set; `None` is the body layer.
478 pub fn contains(&self, layer: Option<ContentLayer>) -> bool {
479 match layer {
480 None => self.body,
481 Some(ContentLayer::Furniture) => self.furniture,
482 Some(ContentLayer::Notes) => self.notes,
483 Some(ContentLayer::Invisible) => self.invisible,
484 }
485 }
486
487 /// Whether the layer spelled `name` (`body`, `furniture`, `notes`,
488 /// `invisible` — the JSON `content_layer` values) is in the set; an
489 /// unknown name is not.
490 pub fn contains_name(&self, name: &str) -> bool {
491 match name {
492 "body" => self.body,
493 "furniture" => self.furniture,
494 "notes" => self.notes,
495 "invisible" => self.invisible,
496 _ => false,
497 }
498 }
499
500 /// Parse a comma-separated list of layer names (`body,furniture`;
501 /// whitespace around a name is ignored, `all` is every layer). An
502 /// unknown name is the error. docling-core 2.99's `background` layer
503 /// (watermarks) has no items in this model, so the name is accepted and
504 /// adds nothing — a set written for Python keeps parsing.
505 pub fn parse_list(list: &str) -> Result<Self, String> {
506 let mut set = Self::NONE;
507 for name in list.split(',').map(str::trim).filter(|n| !n.is_empty()) {
508 match name {
509 "body" => set.body = true,
510 "furniture" => set.furniture = true,
511 "notes" => set.notes = true,
512 "invisible" => set.invisible = true,
513 "background" => {}
514 "all" => set = Self::ALL,
515 other => {
516 return Err(format!(
517 "unknown content layer `{other}` (expected body, furniture, notes, invisible or all)"
518 ))
519 }
520 }
521 }
522 Ok(set)
523 }
524}
525
526/// Options of the HTML export ([`DoclingDocument::export_to_html_with`]):
527/// docling-core's `HTMLParams` subset the port honours. [`Default`] is
528/// upstream's default export — placeholder images, `artifacts` as the
529/// referenced-image directory, the body layer only.
530#[derive(Debug, Clone, PartialEq, Eq)]
531pub struct HtmlExportOptions {
532 /// How pictures render (`HTMLParams.image_mode`): nothing but captions
533 /// and meta for [`ImageMode::Placeholder`], `data:` URIs when embedded,
534 /// `<img src>` paths under [`artifacts_dir`](Self::artifacts_dir) when
535 /// referenced.
536 pub image_mode: ImageMode,
537 /// The directory referenced images are named under, the Markdown
538 /// export's convention (`<artifacts_dir>/image_NNNNNN.<ext>`).
539 pub artifacts_dir: String,
540 /// The content layers rendered (`HTMLParams.layers`, #499): an item on a
541 /// layer outside the set is skipped, its children still walked — exactly
542 /// how docling-core's `get_excluded_refs` reads the set.
543 pub layers: ContentLayers,
544}
545
546impl Default for HtmlExportOptions {
547 fn default() -> Self {
548 Self {
549 image_mode: ImageMode::Placeholder,
550 artifacts_dir: "artifacts".to_string(),
551 layers: ContentLayers::BODY,
552 }
553 }
554}
555
556/// DocLang-only content for a [`Node::ListItem`] whose DocLang form differs from
557/// its flat Markdown `text` (see [`Node::ListItem::dclx`]). `ordered` picks the
558/// enclosing `<list>` kind, `marker` the `<ldiv><marker>`; content is `runs`
559/// (structured equations/formatting) when non-empty, else `text` re-parsed for
560/// inline markers.
561#[derive(Debug, Clone, PartialEq, Eq, Default)]
562pub struct ListItemDclx {
563 pub ordered: bool,
564 pub marker: Option<String>,
565 pub text: String,
566 pub runs: Vec<InlineRun>,
567}
568
569impl InlineRun {
570 /// A run with no active formatting (renders as bare inline text).
571 pub fn is_plain(&self) -> bool {
572 !self.bold
573 && !self.italic
574 && !self.underline
575 && !self.strike
576 && !self.code
577 && !self.formula
578 && self.script == Script::Baseline
579 }
580}
581
582/// Build the [`Node`] for a paragraph of inline content from its structured
583/// `runs` and Markdown text, applying docling's `InlineGroup` boundary:
584///
585/// * a single plain run (or none) → a plain [`Node::Paragraph`] (which the
586/// serializers render as `<text>…</text>`, and a lone hyperlink via `<href>`);
587/// * a single uniformly-formatted run, or two or more runs → a
588/// [`Node::InlineGroup`]. `unwrapped` (the group's docling parent is a
589/// heading, so no enclosing `<text>`) only applies to multi-run groups.
590///
591/// Markdown/JSON render the group's `md_text`, so their output is identical to
592/// emitting a `Paragraph` — the structured runs are DocLang-only.
593pub fn inline_paragraph_node(md_text: String, runs: Vec<InlineRun>, unwrapped: bool) -> Node {
594 let single_plain = runs.len() <= 1 && runs.first().is_none_or(|r| r.is_plain());
595 if single_plain {
596 Node::Paragraph { text: md_text }
597 } else {
598 Node::InlineGroup {
599 unwrapped: unwrapped && runs.len() >= 2,
600 runs,
601 md_text,
602 }
603 }
604}
605
606/// One entry of a [`Node::FieldRegion`]: a marker/key/value triple, any of which
607/// may be absent. Mirrors docling's `field_item` with its `marker` / `field_key`
608/// / `field_value` child texts.
609#[derive(Debug, Clone, PartialEq, Default)]
610pub struct FieldItem {
611 pub marker: Option<String>,
612 pub key: Option<String>,
613 pub value: Option<String>,
614 /// docling's `kind` on the `field_value` item — `read_only` or
615 /// `fillable` (the value is or holds a form control); JSON-only.
616 pub value_kind: Option<String>,
617}
618
619/// One node of a [`Node::KeyValueGraph`] — docling-core's `GraphCell`.
620#[derive(Debug, Clone, PartialEq)]
621pub struct GraphCell {
622 /// docling's `GraphCellLabel` value: `key` or `value` (it also knows
623 /// `unspecified` and `checkbox`).
624 pub label: String,
625 /// The cell's number within its graph, what the links refer to.
626 pub cell_id: usize,
627 pub text: String,
628 /// The text before any cleanup — for an XBRL fact key, the concept's
629 /// qualified name where `text` is its local name.
630 pub orig: String,
631}
632
633/// One edge of a [`Node::KeyValueGraph`] — docling-core's `GraphLink`.
634#[derive(Debug, Clone, PartialEq)]
635pub struct GraphLink {
636 /// docling's `GraphLinkLabel` value: `to_value`, `to_child`, `to_parent`
637 /// or `to_key`.
638 pub label: String,
639 pub source_cell_id: usize,
640 pub target_cell_id: usize,
641}
642
643/// One DocumentPictureClassifier prediction — docling-core's
644/// `PictureClassificationClass` (`class_name` + `confidence`).
645#[derive(Debug, Clone, PartialEq)]
646pub struct PictureClass {
647 /// e.g. `bar_chart`, `logo`, `signature` (the classifier's 26-label set).
648 pub class_name: String,
649 pub confidence: f32,
650}
651
652/// An extracted picture's raw encoded bytes plus its mimetype and pixel size —
653/// the docling.rs analogue of docling-core's `ImageRef`.
654#[derive(Debug, Clone, PartialEq)]
655pub struct PictureImage {
656 /// e.g. `image/png`, `image/jpeg`.
657 pub mimetype: String,
658 pub width: u32,
659 pub height: u32,
660 /// The image file bytes, exactly as embedded (PNG/JPEG/…).
661 pub data: Vec<u8>,
662 /// Pixels per inch of the image relative to the page it came from —
663 /// docling-core's `ImageRef.dpi`, which consumers use to map pixels back
664 /// to points. A crop rendered from a PDF page at `s` px/pt is `72·s`
665 /// (docling: `int(72 * images_scale)`, #519); an image embedded in an
666 /// office file has no render scale and keeps docling's `72`
667 /// ([`Self::DEFAULT_DPI`]).
668 pub dpi: u32,
669}
670
671impl PictureImage {
672 /// docling's `ImageRef` default when no render scale applies.
673 pub const DEFAULT_DPI: u32 = 72;
674
675 /// The `dpi` of an image rendered at `scale` pixels per PDF point
676 /// (`72·scale`, rounded — `int()` would floor 143.99… to 143).
677 pub fn dpi_for_scale(scale: f32) -> u32 {
678 (72.0 * scale).round().max(1.0) as u32
679 }
680
681 /// A `data:` URI for the image (`data:<mimetype>;base64,<…>`).
682 pub fn data_uri(&self) -> String {
683 format!(
684 "data:{};base64,{}",
685 self.mimetype,
686 crate::base64::encode(&self.data)
687 )
688 }
689}
690
691/// One table cell as a first-class object (#240) — the Rust counterpart of
692/// docling's `TableCell`: its text, page geometry, grid rectangle and header
693/// roles. Produced by the PDF TableFormer paths from the predicted OTSL
694/// structure; `bbox` is `[l, t, r, b]` in page points with a top-left origin.
695#[derive(Debug, Clone, PartialEq)]
696pub struct TableCell {
697 pub text: String,
698 /// `[l, t, r, b]`, page points, top-left origin; `None` without geometry.
699 pub bbox: Option<[f32; 4]>,
700 /// Anchor grid position (0-based row/column offsets).
701 pub start_row: usize,
702 pub start_col: usize,
703 /// Span extents (≥ 1); the covered grid positions repeat the cell's text
704 /// in [`Table::rows`].
705 pub row_span: usize,
706 pub col_span: usize,
707 /// OTSL `ched` — a column-header cell.
708 pub column_header: bool,
709 /// OTSL `rhed` — a row-header cell.
710 pub row_header: bool,
711 /// OTSL `srow` — a section-row cell.
712 pub row_section: bool,
713}
714
715/// Where a picture's or table's caption text item hangs in the docling-JSON
716/// tree (#390). docling's backends do not agree, and the JSON structure is
717/// the only surface that shows it (Markdown, DocLang and LaTeX place a
718/// caption by its item, whatever its parent): the PDF pipeline parents a
719/// layout caption to the picture or table it belongs to, while every
720/// declarative backend creates the caption with `doc.add_text(label=CAPTION)`
721/// and no parent — the document body — even when the item itself sits in a
722/// group or under a section header (JATS, LaTeX, HTML, Markdown, EPUB,
723/// AsciiDoc all do). The office backends hang a chart's title caption off the
724/// chart's container (the sheet group, the slide, docx's current parent,
725/// docling#4190) — that is [`Node::Chart`]'s own path, not this choice. A
726/// backend that adds a new captioned item picks the variant matching
727/// upstream's `add_text` call for that format; the default is upstream's
728/// default.
729#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
730pub enum CaptionParent {
731 /// `#/body`, listed after the enclosing top-level item — docling's
732 /// `add_text` default, which every declarative backend leaves alone.
733 #[default]
734 Body,
735 /// The item's own container (its `parent`), listed ahead of the item:
736 /// the caption is created first, as docling's office backends do.
737 Container,
738 /// The item's own container, listed *after* the item: docling's HTML
739 /// backend adds a `<figure>`-wrapped table, then its `<figcaption>` under
740 /// `self.parents[self.level]` (docling#4050).
741 ContainerAfter,
742 /// The item itself — the caption is the picture's or table's first
743 /// child, as docling's PDF pipeline attaches a layout caption.
744 Item,
745}
746
747/// A simple row-major table. By default `rows[0]` is the header row; a
748/// [`TableStructure`] overlay overrides that and adds column spans.
749#[derive(Debug, Clone, PartialEq, Default)]
750pub struct Table {
751 pub rows: Vec<Vec<String>>,
752 /// Optional layout provenance: the four DocLang `<location>` values
753 /// (`x0,y0,x1,y1`, each already normalized to the 0–511 resolution) emitted
754 /// before the table's cells. Set only by backends with real geometry (e.g.
755 /// the spreadsheet backend, whose cell grid yields a bounding box); left
756 /// `None` by declarative backends, which have no coordinates.
757 pub location: Option<[u16; 4]>,
758 /// Optional OTSL structure overlay for backends that parse real table
759 /// geometry (USPTO CALS): explicit header-row count and horizontal-span
760 /// continuations. `None` → the default (row 0 is the header, no spans).
761 /// `rows` still carries the full text grid (span text replicated) for
762 /// Markdown/JSON; DocLang uses this overlay to emit `<ched/>`/`<lcel/>`.
763 pub structure: Option<TableStructure>,
764 /// Optional per-cell block content, parallel to `rows`. A *rich* cell (an
765 /// ODF cell holding a list, several paragraphs, or a nested table) carries
766 /// its DocLang blocks here; the DocLang serializer emits them after the
767 /// cell token instead of the flat `rows` text. Markdown/JSON ignore this
768 /// and render `rows`, so their output is unchanged. `None` (or an empty
769 /// `Vec` for a given cell) → the flat text is used everywhere.
770 pub cell_blocks: Option<Vec<Vec<Vec<Node>>>>,
771 /// Optional caption (docling's `TableItem.captions`): the JATS
772 /// `<table-wrap>` label+caption, an HTML `<caption>`, etc. Markdown renders
773 /// it as a text line *before* the grid; JSON emits a caption text item the
774 /// table references; DocLang emits a `<caption>` as the table's first child.
775 /// `None` → the table has no caption.
776 pub caption: Option<String>,
777 /// Where the caption item hangs in the JSON tree (#390); see
778 /// [`CaptionParent`]. Only the JSON export reads it.
779 pub caption_parent: CaptionParent,
780 /// Optional per-cell bounding boxes, same shape as [`Self::rows`]: `[l, t,
781 /// r, b]` in page points with a **top-left** origin (the PDF pipeline's
782 /// native space). Set by the ML pipeline's TableFormer paths — a spanned
783 /// cell repeats its anchor's box across the covered grid positions — and
784 /// First-class cells (#240): the authoritative per-cell records —
785 /// text, page geometry, spans and header roles — when the backend
786 /// produces them (the PDF TableFormer paths do; declarative backends
787 /// leave `None`). [`Self::rows`] stays the dense text grid every
788 /// serializer renders (a spanning cell's text is replicated across its
789 /// covered positions there); JSON serializes these cells verbatim when
790 /// present, and the DocLang structure overlay is derived from them.
791 pub cells: Option<Vec<TableCell>>,
792}
793
794impl Table {
795 /// A cell's text at a grid position, `None` outside the grid.
796 pub fn cell_text(&self, row: usize, col: usize) -> Option<&str> {
797 self.rows.get(row)?.get(col).map(String::as_str)
798 }
799
800 /// Replace the text at a grid position; `false` (and no change) outside
801 /// the grid. When a first-class cell covers the position, the whole
802 /// cell is updated: its record text and every grid position its span
803 /// covers, so the repair shows once in Markdown, not once per covered
804 /// column.
805 pub fn set_cell_text(&mut self, row: usize, col: usize, text: impl Into<String>) -> bool {
806 if self.rows.get(row).and_then(|r| r.get(col)).is_none() {
807 return false;
808 }
809 let text = text.into();
810 let covering = self.cells.as_mut().and_then(|cells| {
811 cells.iter_mut().find(|c| {
812 (c.start_row..c.start_row + c.row_span).contains(&row)
813 && (c.start_col..c.start_col + c.col_span).contains(&col)
814 })
815 });
816 if let Some(cell) = covering {
817 cell.text = text.clone();
818 let (r0, r1) = (cell.start_row, cell.start_row + cell.row_span);
819 let (c0, c1) = (cell.start_col, cell.start_col + cell.col_span);
820 for r in self.rows.iter_mut().take(r1).skip(r0) {
821 for slot in r.iter_mut().take(c1).skip(c0) {
822 *slot = text.clone();
823 }
824 }
825 } else {
826 self.rows[row][col] = text;
827 }
828 true
829 }
830
831 /// Derive first-class cells (#240) from the dense grid plus the
832 /// [`TableStructure`] overlay — how declarative tables (DOCX/XLSX merged
833 /// regions, HTML `th`/spans, ODF covered cells, USPTO CALS) get real
834 /// `TableCell` records without page geometry. Anchors are the positions
835 /// not marked as span continuations; extents scan the continuation grids
836 /// right/down (matching the DocLang `lcel`/`ucel` reading). Header roles
837 /// come from the per-cell `col_header`/`row_header` grids when present,
838 /// else the `header_row` band, else docling's declarative default (row 0
839 /// is the header). Without any overlay every position is a 1×1 cell.
840 pub fn derive_cells(&self) -> Vec<TableCell> {
841 let s = self.structure.as_ref();
842 let flag = |grid: Option<&Vec<Vec<bool>>>, r: usize, c: usize| {
843 grid.and_then(|g| g.get(r))
844 .and_then(|row| row.get(c))
845 .copied()
846 .unwrap_or(false)
847 };
848 let col_cont = |r: usize, c: usize| flag(s.map(|s| &s.col_continuation), r, c);
849 let row_cont = |r: usize, c: usize| flag(s.map(|s| &s.row_continuation), r, c);
850 let is_col_header = |r: usize, c: usize| match s {
851 Some(st) if !st.col_header.is_empty() => flag(Some(&st.col_header), r, c),
852 Some(st) if !st.header_row.is_empty() => st.header_row.get(r).copied().unwrap_or(false),
853 _ => r == 0,
854 };
855 let mut cells = Vec::new();
856 for (r, row) in self.rows.iter().enumerate() {
857 for (c, text) in row.iter().enumerate() {
858 if col_cont(r, c) || row_cont(r, c) {
859 continue; // covered by a span anchor
860 }
861 let mut col_span = 1;
862 while c + col_span < row.len() && col_cont(r, c + col_span) {
863 col_span += 1;
864 }
865 let mut row_span = 1;
866 while r + row_span < self.rows.len() && row_cont(r + row_span, c) {
867 row_span += 1;
868 }
869 cells.push(TableCell {
870 text: text.clone(),
871 bbox: None,
872 start_row: r,
873 start_col: c,
874 row_span,
875 col_span,
876 column_header: is_col_header(r, c),
877 row_header: flag(s.map(|s| &s.row_header), r, c),
878 row_section: false,
879 });
880 }
881 }
882 cells
883 }
884
885 /// The number of leading grid rows that form the column header —
886 /// docling-core's `_count_header_rows` (docling-core#723, 2.96) shared by
887 /// the Markdown serializer and the chunker's dataframe view: a row counts
888 /// only when a `column_header` cell *starts* on it (a header spanning
889 /// several rows is replicated into each row it covers, and counting those
890 /// would pull the data rows beneath it into the header block). Two
891 /// special cases: `1` when no cell carries the flag at all, so tables from
892 /// backends that never set it keep row 0 as the header; `0` when flags
893 /// exist but none starts on row 0 — then nothing is promotable and every
894 /// row stays in the body. Uses the first-class [`Self::cells`] when
895 /// present (the PDF pipeline's TableFormer flags), else the cells derived
896 /// from the structure overlay.
897 ///
898 /// A pivot table's row headers no longer disturb this: docling#4216 flags
899 /// a spanning `<th>` row as `row_header`, not `column_header`, so the
900 /// first data row is no longer folded into the header block and the
901 /// deviation this port carried for docling-core#765 is gone.
902 pub fn header_row_count(&self) -> usize {
903 if self.rows.is_empty() {
904 return 0;
905 }
906 let derived;
907 let cells: &[TableCell] = match &self.cells {
908 Some(c) if !c.is_empty() => c,
909 _ => {
910 derived = self.derive_cells();
911 &derived
912 }
913 };
914 if !cells.iter().any(|c| c.column_header) {
915 return 1;
916 }
917 (0..self.rows.len())
918 .take_while(|&r| cells.iter().any(|c| c.column_header && c.start_row == r))
919 .count()
920 }
921
922 /// The first-class cell covering a grid position, if any.
923 pub fn cell_at(&self, row: usize, col: usize) -> Option<&TableCell> {
924 self.cells.as_ref()?.iter().find(|c| {
925 (c.start_row..c.start_row + c.row_span).contains(&row)
926 && (c.start_col..c.start_col + c.col_span).contains(&col)
927 })
928 }
929
930 /// A cell's bounding box (`[l, t, r, b]`, page points, top-left origin);
931 /// `None` when no cell with geometry covers the position.
932 pub fn cell_bbox(&self, row: usize, col: usize) -> Option<[f32; 4]> {
933 self.cell_at(row, col)?.bbox
934 }
935
936 /// Set (or replace) the bounding box of the cell covering a grid
937 /// position; `false` outside the text grid. A table without first-class
938 /// cells materializes them first (one 1×1 cell per grid position, texts
939 /// from the grid), so declarative tables can be annotated too.
940 pub fn set_cell_bbox(&mut self, row: usize, col: usize, bbox: [f32; 4]) -> bool {
941 if self.rows.get(row).and_then(|r| r.get(col)).is_none() {
942 return false;
943 }
944 let rows = &self.rows;
945 let cells = self.cells.get_or_insert_with(|| {
946 rows.iter()
947 .enumerate()
948 .flat_map(|(r, cols)| {
949 cols.iter().enumerate().map(move |(c, text)| TableCell {
950 text: text.clone(),
951 bbox: None,
952 start_row: r,
953 start_col: c,
954 row_span: 1,
955 col_span: 1,
956 column_header: false,
957 row_header: false,
958 row_section: false,
959 })
960 })
961 .collect()
962 });
963 match cells.iter_mut().find(|c| {
964 (c.start_row..c.start_row + c.row_span).contains(&row)
965 && (c.start_col..c.start_col + c.col_span).contains(&col)
966 }) {
967 Some(cell) => {
968 cell.bbox = Some(bbox);
969 true
970 }
971 None => {
972 cells.push(TableCell {
973 text: self.rows[row][col].clone(),
974 bbox: Some(bbox),
975 start_row: row,
976 start_col: col,
977 row_span: 1,
978 col_span: 1,
979 column_header: false,
980 row_header: false,
981 row_section: false,
982 });
983 true
984 }
985 }
986 }
987
988 /// The anchor position of the cell whose box overlaps `bbox` best
989 /// (largest intersection-over-union), ties resolved in cell order.
990 /// `None` when nothing overlaps or the table carries no geometry. This
991 /// is the lookup half of the repair workflow: find the cell an external
992 /// OCR box refers to, then [`Self::set_cell_text`] it.
993 pub fn find_cell_by_bbox(&self, bbox: [f32; 4]) -> Option<(usize, usize)> {
994 let area = |b: &[f32; 4]| ((b[2] - b[0]) * (b[3] - b[1])).max(0.0);
995 let mut best: Option<(f32, (usize, usize))> = None;
996 for cell in self.cells.as_deref()?.iter() {
997 let Some(cb) = cell.bbox else { continue };
998 let iw = (bbox[2].min(cb[2]) - bbox[0].max(cb[0])).max(0.0);
999 let ih = (bbox[3].min(cb[3]) - bbox[1].max(cb[1])).max(0.0);
1000 let inter = iw * ih;
1001 if inter <= 0.0 {
1002 continue;
1003 }
1004 let iou = inter / (area(&bbox) + area(&cb) - inter).max(f32::EPSILON);
1005 if best.is_none_or(|(b, _)| iou > b) {
1006 best = Some((iou, (cell.start_row, cell.start_col)));
1007 }
1008 }
1009 best.map(|(_, pos)| pos)
1010 }
1011
1012 /// Locate the cell overlapping `bbox` best and replace its text — the
1013 /// one-call form of the OCR-repair loop. Returns the updated anchor.
1014 pub fn update_cell_by_bbox(
1015 &mut self,
1016 bbox: [f32; 4],
1017 text: impl Into<String>,
1018 ) -> Option<(usize, usize)> {
1019 let (row, col) = self.find_cell_by_bbox(bbox)?;
1020 self.set_cell_text(row, col, text);
1021 Some((row, col))
1022 }
1023}
1024
1025/// OTSL structure overlay for a [`Table`], parallel to [`Table::rows`].
1026#[derive(Debug, Clone, PartialEq, Default)]
1027pub struct TableStructure {
1028 /// Per-row: `true` if the row's non-empty cells are column headers
1029 /// (emitted as `<ched/>` rather than `<fcel/>`).
1030 pub header_row: Vec<bool>,
1031 /// Same shape as [`Table::rows`]; `true` where a cell continues a
1032 /// horizontal span from its left neighbour (emitted as `<lcel/>`).
1033 pub col_continuation: Vec<Vec<bool>>,
1034 /// Same shape as [`Table::rows`]; `true` where a cell continues a
1035 /// vertical span from the cell above (emitted as `<ucel/>`). Empty or all
1036 /// `false` when the backend has no vertical spans (e.g. USPTO CALS).
1037 pub row_continuation: Vec<Vec<bool>>,
1038 /// Same shape as [`Table::rows`]; `true` where a non-empty cell is a row
1039 /// header (emitted as `<rhed/>`) — a chart's category column. Empty when
1040 /// the table has no row headers.
1041 pub row_header: Vec<Vec<bool>>,
1042 /// Same shape as [`Table::rows`]; `true` where a cell is a *column header*
1043 /// cell (an HTML `<th>`). When non-empty this per-cell grid supersedes the
1044 /// per-row [`Self::header_row`] for `<ched/>` emission, matching docling's
1045 /// cell-level `column_header` flag; the chunker derives its header-row
1046 /// count from it.
1047 pub col_header: Vec<Vec<bool>>,
1048}
1049
1050impl DoclingDocument {
1051 /// Create an empty document with the given name.
1052 pub fn new(name: impl Into<String>) -> Self {
1053 Self {
1054 name: name.into(),
1055 nodes: Vec::new(),
1056 strict_markdown: false,
1057 compact_tables: false,
1058 page_break_placeholder: None,
1059 links: Vec::new(),
1060 confidence: None,
1061 tree: None,
1062 page_images: std::collections::BTreeMap::new(),
1063 }
1064 }
1065
1066 /// Append a node.
1067 /// The document's top-level tables in reading order — the read half of
1068 /// the post-extraction table API (#238). [`Node::Located`] wrappers (the
1069 /// PDF pipeline attaches layout provenance that way) are looked through;
1070 /// tables nested inside rich table cells (`Table::cell_blocks`) are not
1071 /// traversed.
1072 pub fn tables(&self) -> impl Iterator<Item = &Table> {
1073 fn unwrap_table(n: &Node) -> Option<&Table> {
1074 match n {
1075 Node::Table(t) => Some(t),
1076 Node::Located { inner, .. } | Node::Prov { inner, .. } => unwrap_table(inner),
1077 _ => None,
1078 }
1079 }
1080 self.nodes.iter().filter_map(unwrap_table)
1081 }
1082
1083 /// Mutable access to the document's top-level tables, for repair
1084 /// workflows (#238): locate a cell via [`Table::find_cell_by_bbox`], fix
1085 /// its text with [`Table::set_cell_text`], then re-export — every
1086 /// serializer reads the same grid.
1087 pub fn tables_mut(&mut self) -> impl Iterator<Item = &mut Table> {
1088 fn unwrap_table(n: &mut Node) -> Option<&mut Table> {
1089 match n {
1090 Node::Table(t) => Some(t),
1091 Node::Located { inner, .. } | Node::Prov { inner, .. } => unwrap_table(inner),
1092 _ => None,
1093 }
1094 }
1095 self.nodes.iter_mut().filter_map(unwrap_table)
1096 }
1097
1098 pub fn push(&mut self, node: Node) {
1099 self.nodes.push(node);
1100 }
1101
1102 /// Convenience: append a heading.
1103 pub fn add_heading(&mut self, level: u8, text: impl Into<String>) {
1104 self.push(Node::Heading {
1105 level,
1106 text: text.into(),
1107 });
1108 }
1109
1110 /// Convenience: append a paragraph.
1111 pub fn add_paragraph(&mut self, text: impl Into<String>) {
1112 self.push(Node::Paragraph { text: text.into() });
1113 }
1114
1115 /// Serialize the document to Markdown.
1116 ///
1117 /// The Rust equivalent of docling-core's
1118 /// `DoclingDocument.export_to_markdown()`. Uses [`Self::strict_markdown`] to
1119 /// pick between docling-legacy output (default) and the cleaner, more
1120 /// conformant variant.
1121 pub fn export_to_markdown(&self) -> String {
1122 to_markdown(self, self.strict_markdown)
1123 }
1124
1125 /// Serialize to Markdown, explicitly choosing the mode regardless of
1126 /// [`Self::strict_markdown`]. `strict = true` produces cleaner, more
1127 /// conformant Markdown (code-fence languages preserved, no inline-run
1128 /// spacing artifacts); `strict = false` reproduces docling's legacy output.
1129 pub fn export_to_markdown_with(&self, strict: bool) -> String {
1130 to_markdown(self, strict)
1131 }
1132
1133 /// Markdown for this document as the *content of a rich table cell*
1134 /// (docling-core's `in_table_cell` serialization, docling-core#540):
1135 /// headings render as plain text since Markdown tables can't hold them.
1136 /// Backends build a sub-document per rich cell and flatten this into the
1137 /// cell text; no trailing newline.
1138 pub fn export_to_table_cell_markdown(&self) -> String {
1139 crate::markdown::to_markdown_table_cell(self, self.strict_markdown)
1140 }
1141
1142 /// Serialize to docling-core's native JSON wire format (`DoclingDocument`
1143 /// schema), pretty-printed — the Rust equivalent of
1144 /// `DoclingDocument.export_to_dict()` / `save_as_json()`. The output loads
1145 /// back into Python docling-core and round-trips to the same Markdown.
1146 pub fn export_to_json(&self) -> String {
1147 serde_json::to_string_pretty(&self.export_to_json_value())
1148 .expect("DoclingDocument JSON is always serializable")
1149 }
1150
1151 /// The same JSON wire format as [`Self::export_to_json`], as a
1152 /// `serde_json::Value` — for callers that append response-level extras
1153 /// (docling-serve adds the confidence report, #183) before serializing.
1154 pub fn export_to_json_value(&self) -> serde_json::Value {
1155 crate::json::to_json(self)
1156 }
1157
1158 /// Serialize to a complete LaTeX document — the Rust counterpart of
1159 /// docling-core's `LaTeXDocSerializer` with default parameters (docling
1160 /// 2.124's `--to latex`, #317). No trailing newline, like the upstream
1161 /// CLI's `<stem>.tex`.
1162 pub fn export_to_latex(&self) -> String {
1163 crate::latex::to_latex(self)
1164 }
1165
1166 /// Serialize to Pandoc's AST as JSON (`pandoc -f json`, #515) with the
1167 /// default options: pictures as captioned figures without image data,
1168 /// body layer only, the [`PANDOC_API_VERSION`](crate::pandoc::PANDOC_API_VERSION)
1169 /// API. One line, no trailing newline. See [`crate::pandoc`].
1170 pub fn export_to_pandoc_json(&self) -> String {
1171 crate::pandoc::to_pandoc(self, &crate::pandoc::PandocExportOptions::default())
1172 .expect("the default Pandoc API version is always supported")
1173 .0
1174 }
1175
1176 /// Pandoc JSON per `options` (image mode, artifacts directory, content
1177 /// layers, required API version). Returns the JSON and, for
1178 /// [`ImageMode::Referenced`], the `(path, bytes)` image files; an
1179 /// unsupported `api_version` is an error.
1180 pub fn export_to_pandoc_json_with(
1181 &self,
1182 options: &crate::pandoc::PandocExportOptions,
1183 ) -> Result<crate::pandoc::PandocOutput, crate::pandoc::PandocError> {
1184 crate::pandoc::to_pandoc(self, options)
1185 }
1186
1187 /// Serialize to DocLang XML (`<doclang version="0.7">…`), the markup that
1188 /// lives inside a `.dclx` archive — the Rust counterpart of docling-core's
1189 /// `export_to_doclang()` with default parameters. No trailing newline; the
1190 /// archive writer appends exactly one.
1191 pub fn export_to_doclang(&self) -> String {
1192 crate::doclang::export_to_doclang(&self.nodes)
1193 }
1194
1195 /// [`Self::export_to_doclang`] plus the picture assets its
1196 /// `<src uri="assets/image_….png"/>` references name, as `(path, bytes)`
1197 /// in document order — the parts docling's `save_as_doclang_archive`
1198 /// stores next to `document.xml`. PNG and JPEG pictures come back as PNG
1199 /// (a JPEG re-encoded from its libjpeg-decoded pixels); other encodings
1200 /// keep their own bytes for the archive writer to convert.
1201 pub fn export_to_doclang_with_assets(&self) -> (String, Vec<(String, Vec<u8>)>) {
1202 crate::doclang::export_to_doclang_with_assets(&self.nodes)
1203 }
1204
1205 /// Serialize to Markdown with an explicit picture [`ImageMode`] (mirrors
1206 /// docling's `image_mode`). Returns the Markdown and, for
1207 /// [`ImageMode::Referenced`], the `(relative-path, bytes)` of each image the
1208 /// caller should write next to the Markdown file. `artifacts_dir` is the
1209 /// directory name used in referenced links.
1210 pub fn export_to_markdown_with_images(
1211 &self,
1212 image_mode: ImageMode,
1213 artifacts_dir: &str,
1214 ) -> (String, Vec<(String, Vec<u8>)>) {
1215 to_markdown_images(self, self.strict_markdown, image_mode, artifacts_dir)
1216 }
1217
1218 /// A complete HTML document — docling-core's `HTMLDocSerializer` with its
1219 /// defaults (#492): pictures stay out (`ImageRefMode.PLACEHOLDER`, only
1220 /// their captions and meta render). See [`export_to_html_with_images`]
1221 /// for the embedded / referenced modes and [`crate::html`]'s module docs
1222 /// for what is reproduced.
1223 ///
1224 /// [`export_to_html_with_images`]: Self::export_to_html_with_images
1225 pub fn export_to_html(&self) -> String {
1226 crate::html::to_html(self, &HtmlExportOptions::default()).0
1227 }
1228
1229 /// HTML with pictures per `image_mode`: `data:` URIs when embedded, and
1230 /// when referenced `<img src>` paths under `artifacts_dir` whose bytes come
1231 /// back as `(path, bytes)` for the caller to write — the Markdown export's
1232 /// contract and file names.
1233 pub fn export_to_html_with_images(
1234 &self,
1235 image_mode: ImageMode,
1236 artifacts_dir: &str,
1237 ) -> (String, Vec<(String, Vec<u8>)>) {
1238 crate::html::to_html(
1239 self,
1240 &HtmlExportOptions {
1241 image_mode,
1242 artifacts_dir: artifacts_dir.to_string(),
1243 ..HtmlExportOptions::default()
1244 },
1245 )
1246 }
1247
1248 /// HTML rendering the content `layers` — docling-core's
1249 /// `export_to_html(included_content_layers=…)` (#499). The default export
1250 /// is body only; `ContentLayers::BODY.with(ContentLayer::Furniture)` adds
1251 /// page headers/footers, `.with(ContentLayer::Notes)` reviewer comments,
1252 /// [`ContentLayers::ALL`] is Python's `set(ContentLayer)`. An item on a
1253 /// layer outside the set is skipped while its children are still walked,
1254 /// and the items that do render go through the same serializers as the
1255 /// body (a page header is a `<p>`, a comment a `<p>`, a header table a
1256 /// `<table>`), exactly as upstream's `HTMLParams.layers` behaves. Pictures
1257 /// stay placeholders; see [`export_to_html_with`] for the full option set.
1258 ///
1259 /// [`export_to_html_with`]: Self::export_to_html_with
1260 pub fn export_to_html_with_layers(&self, layers: ContentLayers) -> String {
1261 crate::html::to_html(
1262 self,
1263 &HtmlExportOptions {
1264 layers,
1265 ..HtmlExportOptions::default()
1266 },
1267 )
1268 .0
1269 }
1270
1271 /// HTML per `options` — image mode, referenced-image directory and content
1272 /// layers in one call; the other `export_to_html*` methods are
1273 /// shorthands for it. Returns the HTML and, for
1274 /// [`ImageMode::Referenced`], the `(path, bytes)` artifacts.
1275 pub fn export_to_html_with(
1276 &self,
1277 options: &HtmlExportOptions,
1278 ) -> (String, Vec<(String, Vec<u8>)>) {
1279 crate::html::to_html(self, options)
1280 }
1281}
1282
1283#[cfg(test)]
1284mod table_api_tests {
1285 use super::*;
1286
1287 fn cell(
1288 text: &str,
1289 bbox: [f32; 4],
1290 (start_row, start_col): (usize, usize),
1291 (row_span, col_span): (usize, usize),
1292 ) -> TableCell {
1293 TableCell {
1294 text: text.into(),
1295 bbox: Some(bbox),
1296 start_row,
1297 start_col,
1298 row_span,
1299 col_span,
1300 column_header: false,
1301 row_header: false,
1302 row_section: false,
1303 }
1304 }
1305
1306 fn table() -> Table {
1307 Table {
1308 rows: vec![
1309 vec!["Year".into(), "Ducks".into()],
1310 vec!["2019".into(), "120".into()],
1311 ],
1312 cells: Some(vec![
1313 cell("Year", [0.0, 0.0, 50.0, 10.0], (0, 0), (1, 1)),
1314 cell("Ducks", [50.0, 0.0, 100.0, 10.0], (0, 1), (1, 1)),
1315 cell("2019", [0.0, 10.0, 50.0, 20.0], (1, 0), (1, 1)),
1316 cell("120", [50.0, 10.0, 100.0, 20.0], (1, 1), (1, 1)),
1317 ]),
1318 ..Default::default()
1319 }
1320 }
1321
1322 /// Declarative tables derive first-class cells from the structure
1323 /// overlay: continuation grids become span extents, `col_header` (or the
1324 /// row-0 fallback) becomes the header role — the XLSX/DOCX/HTML merge
1325 /// path into real `TableCell`s (#240).
1326 #[test]
1327 fn derive_cells_reads_spans_and_headers_from_structure() {
1328 let t = Table {
1329 rows: vec![
1330 vec!["Wide".into(), "Wide".into(), "C".into()],
1331 vec!["a".into(), "b".into(), "c".into()],
1332 ],
1333 structure: Some(TableStructure {
1334 header_row: vec![true, false],
1335 col_continuation: vec![vec![false, true, false], vec![false; 3]],
1336 row_continuation: vec![vec![false; 3], vec![false; 3]],
1337 row_header: Vec::new(),
1338 col_header: Vec::new(),
1339 }),
1340 ..Default::default()
1341 };
1342 let cells = t.derive_cells();
1343 assert_eq!(cells.len(), 5, "two anchors in row 0, three in row 1");
1344 let wide = &cells[0];
1345 assert_eq!((wide.col_span, wide.row_span), (2, 1));
1346 assert!(wide.column_header, "header_row band");
1347 assert!(cells.iter().skip(2).all(|c| !c.column_header));
1348
1349 // Without any overlay: every position 1x1, row 0 the header
1350 // (docling's declarative default — the old JSON synthesis).
1351 let plain = Table {
1352 rows: vec![vec!["h".into()], vec!["x".into()]],
1353 ..Default::default()
1354 };
1355 let cells = plain.derive_cells();
1356 assert_eq!(cells.len(), 2);
1357 assert!(cells[0].column_header && !cells[1].column_header);
1358 }
1359
1360 /// A spanning cell updates once: the record text and every covered grid
1361 /// position — a repair shows once in Markdown, not once per column.
1362 #[test]
1363 fn span_repair_updates_the_whole_cell() {
1364 let mut t = Table {
1365 rows: vec![
1366 vec!["Wide".into(), "Wide".into(), "C".into()],
1367 vec!["a".into(), "b".into(), "c".into()],
1368 ],
1369 cells: Some(vec![
1370 cell("Wide", [0.0, 0.0, 100.0, 10.0], (0, 0), (1, 2)),
1371 cell("C", [100.0, 0.0, 150.0, 10.0], (0, 2), (1, 1)),
1372 ]),
1373 ..Default::default()
1374 };
1375 // Update through the covered (non-anchor) position.
1376 assert!(t.set_cell_text(0, 1, "Fixed"));
1377 assert_eq!(
1378 t.rows[0],
1379 vec!["Fixed".to_string(), "Fixed".into(), "C".into()]
1380 );
1381 assert_eq!(t.cell_at(0, 1).unwrap().text, "Fixed");
1382 assert_eq!(t.cell_at(0, 1).unwrap().col_span, 2);
1383 }
1384
1385 /// The OCR-repair loop (#238): locate a cell by an external box (best
1386 /// IoU), replace its text, and see the fix in the export — the grid is
1387 /// the single source of truth for every serializer.
1388 #[test]
1389 fn bbox_lookup_and_repair_flow_into_exports() {
1390 let mut doc = DoclingDocument::new("t");
1391 doc.push(Node::Table(table()));
1392 assert_eq!(doc.tables().count(), 1);
1393
1394 let t = doc.tables_mut().next().unwrap();
1395 // A slightly-off OCR box still lands on the (1,1) cell.
1396 assert_eq!(t.find_cell_by_bbox([52.0, 11.0, 98.0, 19.0]), Some((1, 1)));
1397 assert_eq!(
1398 t.update_cell_by_bbox([52.0, 11.0, 98.0, 19.0], "125"),
1399 Some((1, 1))
1400 );
1401 assert_eq!(t.cell_text(1, 1), Some("125"));
1402 assert!(doc.export_to_markdown().contains("125"));
1403
1404 // No overlap → no match, nothing changed.
1405 let t = doc.tables_mut().next().unwrap();
1406 assert_eq!(t.find_cell_by_bbox([500.0, 500.0, 600.0, 600.0]), None);
1407 }
1408
1409 #[test]
1410 fn cell_accessors_bound_check_and_geometry_materializes() {
1411 let mut t = table();
1412 assert_eq!(t.cell_text(0, 0), Some("Year"));
1413 assert_eq!(t.cell_text(5, 0), None);
1414 assert!(!t.set_cell_text(0, 9, "x"), "outside the grid");
1415 assert_eq!(t.cell_bbox(1, 0), Some([0.0, 10.0, 50.0, 20.0]));
1416
1417 // A geometry-less table materializes its box grid on first set.
1418 let mut plain = Table {
1419 rows: vec![vec!["a".into(), "b".into()]],
1420 ..Default::default()
1421 };
1422 assert_eq!(plain.cell_bbox(0, 1), None);
1423 assert!(!plain.set_cell_bbox(0, 5, [0.0; 4]), "outside the grid");
1424 assert!(plain.set_cell_bbox(0, 1, [1.0, 2.0, 3.0, 4.0]));
1425 assert_eq!(plain.cell_bbox(0, 1), Some([1.0, 2.0, 3.0, 4.0]));
1426 assert_eq!(plain.find_cell_by_bbox([1.5, 2.5, 2.5, 3.5]), Some((0, 1)));
1427 }
1428}