kreuzberg 4.9.7

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 91+ formats and 248 programming languages via tree-sitter code intelligence with async/sync APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Render an `InternalDocument` to plain text.
//!
//! Emits text only, with no formatting. Double newlines separate blocks.
//! Annotations are stripped. Tables are rendered as space-separated columns.

use crate::types::document_structure::ContentLayer;
use crate::types::internal::{ElementKind, InternalDocument};

use super::common::{get_admonition_kind, get_admonition_title, parse_metadata_entries, render_table_plain};

/// Render an `InternalDocument` to plain text.
pub fn render_plain(doc: &InternalDocument) -> String {
    let mut out = String::with_capacity(doc.elements.len() * 80);

    for elem in &doc.elements {
        // Only render body-layer elements in main pass
        if elem.layer != ContentLayer::Body {
            continue;
        }

        // Skip container markers
        if elem.kind.is_container_start() || elem.kind.is_container_end() {
            continue;
        }

        match elem.kind {
            ElementKind::Title | ElementKind::Heading { .. } | ElementKind::Paragraph => {
                if !elem.text.is_empty() {
                    out.push_str(&elem.text);
                    out.push_str("\n\n");
                }
            }
            ElementKind::ListItem { .. } => {
                out.push_str(&elem.text);
                out.push('\n');
            }
            ElementKind::Code => {
                out.push_str(&elem.text);
                if !elem.text.ends_with('\n') {
                    out.push('\n');
                }
                out.push('\n');
            }
            ElementKind::Formula => {
                out.push_str(&elem.text);
                out.push_str("\n\n");
            }
            ElementKind::Table { table_index } => {
                if let Some(table) = doc.tables.get(table_index as usize) {
                    let table_str = if !table.cells.is_empty() {
                        render_table_plain(&table.cells)
                    } else {
                        // TATR produces markdown directly without populating cells.
                        table.markdown.clone()
                    };
                    if !table_str.trim().is_empty() {
                        out.push_str(&table_str);
                        out.push('\n');
                    }
                }
            }
            ElementKind::Image { image_index } => {
                if let Some(img) = doc.images.get(image_index as usize) {
                    if let Some(ref desc) = img.description
                        && !desc.is_empty()
                    {
                        out.push_str("[Image: ");
                        out.push_str(desc);
                        out.push_str("]\n\n");
                    }

                    // If the image has an OCR result, append its content
                    if let Some(ocr_result) = &img.ocr_result
                        && !ocr_result.content.is_empty()
                    {
                        out.push_str(&ocr_result.content);
                        out.push_str("\n\n");
                    }
                }
            }
            ElementKind::FootnoteRef => {
                // Skip in plain text
            }
            ElementKind::FootnoteDefinition => {
                // Skip in body pass; footnotes rendered at end
            }
            ElementKind::Citation => {
                // Render just the text
                if !elem.text.is_empty() {
                    out.push_str(&elem.text);
                    out.push_str("\n\n");
                }
            }
            ElementKind::PageBreak => {
                out.push('\n');
            }
            ElementKind::Slide { .. } => {
                if !elem.text.is_empty() {
                    out.push_str(&elem.text);
                    out.push_str("\n\n");
                }
            }
            ElementKind::DefinitionTerm => {
                out.push_str(&elem.text);
                out.push_str(": ");
            }
            ElementKind::DefinitionDescription => {
                out.push_str(&elem.text);
                out.push_str("\n\n");
            }
            ElementKind::Admonition => {
                let title = get_admonition_title(elem);
                if let Some(t) = title {
                    out.push_str(t);
                } else {
                    out.push_str(get_admonition_kind(elem));
                }
                out.push_str("\n\n");
                if !elem.text.is_empty() {
                    out.push_str(&elem.text);
                    out.push_str("\n\n");
                }
            }
            ElementKind::RawBlock => {
                out.push_str(&elem.text);
                if !elem.text.ends_with('\n') {
                    out.push('\n');
                }
                out.push('\n');
            }
            ElementKind::MetadataBlock => {
                let entries = parse_metadata_entries(&elem.text);
                if !entries.is_empty() {
                    for (key, value) in &entries {
                        out.push_str(key);
                        out.push_str(": ");
                        out.push_str(value);
                        out.push('\n');
                    }
                    out.push('\n');
                } else if !elem.text.is_empty() {
                    out.push_str(&elem.text);
                    if !elem.text.ends_with('\n') {
                        out.push('\n');
                    }
                    out.push('\n');
                }
            }
            ElementKind::OcrText { .. } => {
                if !elem.text.is_empty() {
                    out.push_str(&elem.text);
                    out.push_str("\n\n");
                }
            }
            // Container markers handled above
            ElementKind::ListStart { .. }
            | ElementKind::ListEnd
            | ElementKind::QuoteStart
            | ElementKind::QuoteEnd
            | ElementKind::GroupStart
            | ElementKind::GroupEnd => {}
        }
    }

    // Footnotes at end
    let has_footnotes = doc
        .elements
        .iter()
        .any(|e| e.kind == ElementKind::FootnoteDefinition && e.layer == ContentLayer::Footnote);
    if has_footnotes {
        out.push('\n');
        for elem in &doc.elements {
            if elem.kind == ElementKind::FootnoteDefinition && elem.layer == ContentLayer::Footnote {
                out.push_str(&elem.text);
                out.push_str("\n\n");
            }
        }
    }

    // Plain text content string: trim trailing whitespace, no trailing newline
    // (matches derive_content_string behavior — post-processors expect this)
    out.truncate(out.trim_end().len());
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::document_structure::ContentLayer;
    use crate::types::internal_builder::InternalDocumentBuilder;

    // ========================================================================
    // 1. Element rendering tests
    // ========================================================================

    #[test]
    fn test_render_plain_title() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_title("My Document", None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert_eq!(out, "My Document");
    }

    #[test]
    fn test_render_plain_heading() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_heading(2, "Section", None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert_eq!(out, "Section");
    }

    #[test]
    fn test_render_plain_paragraph() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_paragraph("Hello world.", vec![], None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert_eq!(out, "Hello world.");
    }

    #[test]
    fn test_render_plain_list_items() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_list(false);
        b.push_list_item("Alpha", false, vec![], None, None);
        b.push_list_item("Beta", false, vec![], None, None);
        b.end_list();
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Alpha\n"), "got: {}", out);
        assert!(out.contains("Beta"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_ordered_list() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_list(true);
        b.push_list_item("First", true, vec![], None, None);
        b.push_list_item("Second", true, vec![], None, None);
        b.end_list();
        let doc = b.build();
        let out = render_plain(&doc);
        // Plain text just outputs the text, no numbering
        assert!(out.contains("First"), "got: {}", out);
        assert!(out.contains("Second"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_code_block() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_code("fn main() {}", Some("rust"), None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("fn main() {}"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_formula() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_formula("E = mc^2", None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("E = mc^2"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_table() {
        let mut b = InternalDocumentBuilder::new("test");
        let cells = vec![
            vec!["Name".to_string(), "Age".to_string()],
            vec!["Alice".to_string(), "30".to_string()],
        ];
        b.push_table_from_cells(&cells, None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Name Age"), "got: {}", out);
        assert!(out.contains("Alice 30"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_image() {
        let mut b = InternalDocumentBuilder::new("test");
        let image = crate::types::ExtractedImage {
            data: bytes::Bytes::new(),
            format: std::borrow::Cow::Borrowed("png"),
            image_index: 0,
            page_number: None,
            width: None,
            height: None,
            colorspace: None,
            bits_per_component: None,
            is_mask: false,
            description: Some("A nice photo".to_string()),
            ocr_result: None,
            bounding_box: None,
            source_path: None,
        };
        b.push_image(Some("A nice photo"), image, None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("[Image: A nice photo]"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_page_break() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_paragraph("Before", vec![], None, None);
        b.push_page_break();
        b.push_paragraph("After", vec![], None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Before"), "got: {}", out);
        assert!(out.contains("After"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_slide() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_slide(1, Some("Slide Title"), None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Slide Title"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_definition_term_and_description() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_definition_term("Rust", None);
        b.push_definition_description("A systems language", None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Rust: "), "got: {}", out);
        assert!(out.contains("A systems language"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_admonition_with_title() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_admonition("warning", Some("Be careful"), None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Be careful"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_admonition_without_title() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_admonition("note", None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("note"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_raw_block() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_raw_block("tex", "\\LaTeX{}", None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("\\LaTeX{}"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_metadata_block() {
        let mut b = InternalDocumentBuilder::new("test");
        let entries = vec![("Author".to_string(), "Alice".to_string())];
        b.push_metadata_block(&entries, None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Author: Alice"), "got: {}", out);
    }

    #[test]
    fn test_render_plain_empty_document() {
        let b = InternalDocumentBuilder::new("test");
        let doc = b.build();
        let out = render_plain(&doc);
        assert_eq!(out, "");
    }

    // ========================================================================
    // 2. Plain text has no annotations (stripped)
    // ========================================================================

    #[test]
    fn test_render_plain_strips_annotations() {
        use crate::types::document_structure::{AnnotationKind, TextAnnotation};
        let mut b = InternalDocumentBuilder::new("test");
        let ann = vec![TextAnnotation {
            start: 0,
            end: 5,
            kind: AnnotationKind::Bold,
        }];
        b.push_paragraph("Hello world", ann, None, None);
        let doc = b.build();
        let out = render_plain(&doc);
        // No formatting markers, just raw text
        assert_eq!(out, "Hello world");
    }

    // ========================================================================
    // 3. Nested structure tests (containers skipped in plain)
    // ========================================================================

    #[test]
    fn test_render_plain_blockquote_content() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_quote_start();
        b.push_paragraph("Quoted text.", vec![], None, None);
        b.push_quote_end();
        let doc = b.build();
        let out = render_plain(&doc);
        // Plain text just outputs the content, no quote markers
        assert!(out.contains("Quoted text."), "got: {}", out);
    }

    #[test]
    fn test_render_plain_nested_list() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_list(false);
        b.push_list_item("Outer", false, vec![], None, None);
        b.push_list(false);
        b.push_list_item("Inner", false, vec![], None, None);
        b.end_list();
        b.end_list();
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Outer"), "got: {}", out);
        assert!(out.contains("Inner"), "got: {}", out);
    }

    // ========================================================================
    // 4. Footnote tests
    // ========================================================================

    #[test]
    fn test_render_plain_footnote_definitions_at_end() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_paragraph("Main text", vec![], None, None);
        b.push_footnote_ref("1", "fn1", None);
        let def = b.push_footnote_definition("A note.", "fn1", None);
        b.set_layer(def, ContentLayer::Footnote);
        let doc = b.build();
        let out = render_plain(&doc);
        // Footnote refs are skipped in plain text, but definitions appear at end
        assert!(out.contains("Main text"), "got: {}", out);
        assert!(out.contains("A note."), "got: {}", out);
    }

    #[test]
    fn test_render_plain_citation() {
        let mut b = InternalDocumentBuilder::new("test");
        b.push_citation("Smith 2024", "smith2024", None);
        let doc = b.build();
        let out = render_plain(&doc);
        assert!(out.contains("Smith 2024"), "got: {}", out);
    }
}