xberg 1.1.2

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! Built-in document extractors.
//!
//! This module contains the default extractors that ship with Xberg.
//! All extractors implement the `DocumentExtractor` plugin trait.

use crate::Result;
#[cfg(any(feature = "email", feature = "html", feature = "xml",))]
use crate::core::config::ExtractionConfig;
use crate::plugins::registry::get_document_extractor_registry;

#[cfg(any(feature = "email", feature = "html", feature = "xml",))]
use crate::types::internal::InternalDocument;
use once_cell::sync::OnceCell;
use std::sync::Arc;

/// Trait for extractors that share a blocking parser implementation.
///
/// This trait defines a synchronous extraction interface for formats whose async
/// extractor can delegate to a local blocking parser.
///
/// # Implementation
///
/// Extractors can implement this trait in addition to
/// `InternalDocumentExtractor` when their parser does not need an async runtime.
///
/// # MIME Type Validation
///
/// The `mime_type` parameter is guaranteed to be already validated.
///
/// # Example
///
/// ```rust,ignore
/// impl SyncExtractor for PlainTextExtractor {
///     fn extract_sync(&self, content: &[u8], mime_type: &str, config: &ExtractionConfig) -> Result<InternalDocument> {
///         let text = String::from_utf8_lossy(content).to_string();
///         Ok(InternalDocument::from_text(text, mime_type))
///     }
/// }
/// ```
#[cfg(any(feature = "email", feature = "html", feature = "xml",))]
pub(crate) trait SyncExtractor {
    /// Extract content from a byte array synchronously.
    ///
    /// This method performs extraction without requiring an async runtime.
    ///
    /// # Arguments
    ///
    /// * `content` - Raw document bytes
    /// * `mime_type` - MIME type of the document (already validated)
    /// * `config` - Extraction configuration
    ///
    /// # Returns
    ///
    /// An `InternalDocument` containing the extracted elements, metadata, and tables.
    fn extract_sync(&self, content: &[u8], mime_type: &str, config: &ExtractionConfig) -> Result<InternalDocument>;
}

#[cfg(feature = "tree-sitter")]
pub mod code;

pub mod asciidoc;
pub mod csv;
pub mod structured;
pub mod text;
pub mod vtt;

pub mod djot_format;
pub mod frontmatter_utils;

pub(crate) mod annotation_utils;
pub(crate) mod markdown_utils;

pub mod security;
#[cfg(test)]
mod security_tests;

#[cfg(feature = "sqlite")]
pub mod sqlite;

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
pub mod image;

#[cfg(feature = "qr-codes")]
pub mod qr;

#[cfg(feature = "archives")]
pub mod archive;

#[cfg(feature = "transcription")]
pub mod transcription;

#[cfg(feature = "email")]
pub mod email;

#[cfg(feature = "email")]
pub mod pst;

#[cfg(any(feature = "excel", feature = "excel-wasm"))]
pub mod excel;

#[cfg(feature = "hwp")]
pub mod hwp;

#[cfg(feature = "hwpx")]
pub mod hwpx;

#[cfg(feature = "wordperfect")]
pub mod wordperfect;

#[cfg(feature = "iwork")]
pub mod iwork;

#[cfg(feature = "html")]
pub mod html;

#[cfg(feature = "office")]
pub mod bibtex;

#[cfg(feature = "office")]
pub mod citation;

#[cfg(feature = "office")]
pub mod doc;

#[cfg(feature = "office")]
pub mod dbf;

#[cfg(feature = "office")]
pub mod docx;

#[cfg(feature = "office")]
pub mod epub;

#[cfg(feature = "office")]
pub mod fictionbook;

pub mod doctags;

pub mod markdown;
pub(crate) mod myst;

#[cfg(feature = "mdx")]
pub mod mdx;

#[cfg(feature = "office")]
pub mod rst;

#[cfg(feature = "office")]
pub mod latex;

#[cfg(feature = "notebook")]
pub mod jupyter;

#[cfg(feature = "office")]
pub mod orgmode;

#[cfg(feature = "office")]
pub mod odp;

#[cfg(feature = "office")]
pub mod odt;

#[cfg(feature = "office")]
pub mod opml;

#[cfg(feature = "office")]
pub mod typst;

#[cfg(feature = "xml")]
pub mod jats;

#[cfg(feature = "pdf")]
pub mod pdf;

#[cfg(feature = "office")]
pub mod ppt;

#[cfg(feature = "office")]
pub mod pptx;

#[cfg(feature = "office")]
pub mod rtf;

#[cfg(feature = "xml")]
pub mod xml;

#[cfg(feature = "xml")]
pub mod docbook;

#[cfg(feature = "tree-sitter")]
pub use code::CodeExtractor;

pub use asciidoc::AsciiDocExtractor;
pub use csv::CsvExtractor;
pub use doctags::DocTagsExtractor;
pub use markdown::MarkdownExtractor;
pub use structured::StructuredExtractor;
pub use text::PlainTextExtractor;
pub use vtt::WebVttExtractor;

#[cfg(feature = "sqlite")]
pub use sqlite::SqliteExtractor;

#[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
pub use image::ImageExtractor;

#[cfg(feature = "archives")]
pub use archive::{GzipExtractor, SevenZExtractor, TarExtractor, ZipExtractor};

#[cfg(feature = "email")]
pub use email::EmailExtractor;

#[cfg(feature = "email")]
pub use pst::PstExtractor;

#[cfg(any(feature = "excel", feature = "excel-wasm"))]
pub use excel::ExcelExtractor;

#[cfg(feature = "hwp")]
pub use hwp::HwpExtractor;

#[cfg(feature = "hwpx")]
pub use hwpx::HwpxExtractor;

#[cfg(feature = "wordperfect")]
pub use wordperfect::WordPerfectExtractor;

#[cfg(feature = "iwork")]
pub use iwork::{keynote::KeynoteExtractor, numbers::NumbersExtractor, pages::PagesExtractor};

#[cfg(feature = "html")]
pub use html::HtmlExtractor;

#[cfg(feature = "office")]
pub use bibtex::BibtexExtractor;

#[cfg(feature = "office")]
pub use citation::CitationExtractor;

#[cfg(feature = "office")]
pub use dbf::DbfExtractor;

#[cfg(feature = "office")]
pub use doc::DocExtractor;

#[cfg(feature = "office")]
pub use docx::DocxExtractor;

#[cfg(feature = "office")]
pub use epub::EpubExtractor;

#[cfg(feature = "office")]
pub use fictionbook::FictionBookExtractor;

pub use djot_format::DjotExtractor;

#[cfg(feature = "mdx")]
pub use mdx::MdxExtractor;

#[cfg(feature = "office")]
pub use rst::RstExtractor;

#[cfg(feature = "office")]
pub use latex::LatexExtractor;

#[cfg(feature = "notebook")]
pub use jupyter::JupyterExtractor;

#[cfg(feature = "office")]
pub use orgmode::OrgModeExtractor;

#[cfg(feature = "office")]
pub use odp::OdpExtractor;
#[cfg(feature = "office")]
pub use odt::OdtExtractor;

#[cfg(feature = "xml")]
pub use jats::JatsExtractor;

#[cfg(feature = "office")]
pub use opml::OpmlExtractor;

#[cfg(feature = "office")]
pub use typst::TypstExtractor;

#[cfg(feature = "pdf")]
pub use pdf::PdfExtractor;

#[cfg(feature = "office")]
pub use ppt::PptExtractor;

#[cfg(feature = "office")]
pub use pptx::PptxExtractor;

#[cfg(feature = "office")]
pub use rtf::RtfExtractor;

#[cfg(feature = "xml")]
pub use xml::XmlExtractor;

#[cfg(feature = "xml")]
pub use docbook::DocbookExtractor;

#[cfg(feature = "transcription")]
pub use transcription::TranscriptionExtractor;

/// One-time initialization guard for the built-in extractor registry.
///
/// Set to `()` once registration succeeds. If registration fails the cell remains
/// empty, so the next call will retry — unlike `Lazy<Result<()>>` which would
/// permanently cache the error and prevent recovery.
static EXTRACTORS_INITIALIZED: OnceCell<()> = OnceCell::new();

/// Ensure built-in extractors are registered.
///
/// This function is called automatically on first extraction operation.
/// It's safe to call multiple times - registration only happens once,
/// unless the registry was cleared, in which case extractors are re-registered.
///
/// Public so a caller that wants to *inspect* the registry — rather than extract —
/// can populate it directly. Without this the only way to trigger registration is to
/// run a real extraction, which `xberg formats` would otherwise have to fake (#233).
pub fn ensure_initialized() -> Result<()> {
    EXTRACTORS_INITIALIZED.get_or_try_init(register_default_extractors)?;

    let registry = get_document_extractor_registry();
    let registry_guard = registry.read();

    if registry_guard.list().is_empty() {
        drop(registry_guard);
        register_default_extractors()?;
    }

    Ok(())
}

/// Register all built-in extractors with the global registry.
///
/// This function should be called once at application startup to register
/// the default extractors (PlainText, Markdown, XML, etc.).
///
/// **Note:** This is called automatically on first extraction operation.
/// Explicit calling is optional.
///
/// # Example
///
/// ```ignore
/// use xberg::extractors::register_default_extractors;
///
/// # fn main() -> xberg::Result<()> {
/// register_default_extractors()?;
/// # Ok(())
/// # }
/// ```
pub(crate) fn register_default_extractors() -> Result<()> {
    let registry = get_document_extractor_registry();
    let mut registry = registry.write();

    registry.register_internal(Arc::new(PlainTextExtractor::new()))?;
    registry.register_internal(Arc::new(AsciiDocExtractor::new()))?;
    registry.register_internal(Arc::new(WebVttExtractor::new()))?;
    registry.register_internal(Arc::new(MarkdownExtractor::new()))?;
    registry.register_internal(Arc::new(StructuredExtractor::new()))?;
    registry.register_internal(Arc::new(CsvExtractor::new()))?;
    registry.register_internal(Arc::new(DocTagsExtractor::new()))?;

    #[cfg(feature = "sqlite")]
    registry.register_internal(Arc::new(SqliteExtractor::new()))?;

    #[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
    registry.register_internal(Arc::new(ImageExtractor::new()))?;

    #[cfg(feature = "xml")]
    {
        registry.register_internal(Arc::new(XmlExtractor::new()))?;
        registry.register_internal(Arc::new(JatsExtractor::new()))?;
        registry.register_internal(Arc::new(DocbookExtractor::new()))?;
    }

    #[cfg(feature = "pdf")]
    registry.register_internal(Arc::new(PdfExtractor::new()))?;

    #[cfg(any(feature = "excel", feature = "excel-wasm"))]
    registry.register_internal(Arc::new(ExcelExtractor::new()))?;

    registry.register_internal(Arc::new(DjotExtractor::new()))?;

    #[cfg(feature = "notebook")]
    registry.register_internal(Arc::new(JupyterExtractor::new()))?;

    #[cfg(feature = "office")]
    {
        registry.register_internal(Arc::new(BibtexExtractor::new()))?;
        registry.register_internal(Arc::new(CitationExtractor::new()))?;
        registry.register_internal(Arc::new(EpubExtractor::new()))?;
        registry.register_internal(Arc::new(FictionBookExtractor::new()))?;
        registry.register_internal(Arc::new(RtfExtractor::new()))?;
        registry.register_internal(Arc::new(RstExtractor::new()))?;
        registry.register_internal(Arc::new(LatexExtractor::new()))?;
        registry.register_internal(Arc::new(OrgModeExtractor::new()))?;
        registry.register_internal(Arc::new(OpmlExtractor::new()))?;
        registry.register_internal(Arc::new(TypstExtractor::new()))?;
        registry.register_internal(Arc::new(DocExtractor::new()))?;
        registry.register_internal(Arc::new(DocxExtractor::new()))?;
        registry.register_internal(Arc::new(PptExtractor::new()))?;
        registry.register_internal(Arc::new(PptxExtractor::new()))?;
        registry.register_internal(Arc::new(OdtExtractor::new()))?;
        registry.register_internal(Arc::new(OdpExtractor::new()))?;
        registry.register_internal(Arc::new(DbfExtractor::new()))?;
    }

    #[cfg(feature = "hwp")]
    {
        registry.register_internal(Arc::new(HwpExtractor::new()))?;
    }

    #[cfg(feature = "hwpx")]
    {
        registry.register_internal(Arc::new(HwpxExtractor::new()))?;
    }

    #[cfg(feature = "wordperfect")]
    {
        registry.register_internal(Arc::new(WordPerfectExtractor::new()))?;
    }

    #[cfg(feature = "iwork")]
    {
        registry.register_internal(Arc::new(PagesExtractor::new()))?;
        registry.register_internal(Arc::new(NumbersExtractor::new()))?;
        registry.register_internal(Arc::new(KeynoteExtractor::new()))?;
    }

    #[cfg(feature = "mdx")]
    registry.register_internal(Arc::new(MdxExtractor::new()))?;

    #[cfg(feature = "email")]
    {
        registry.register_internal(Arc::new(EmailExtractor::new()))?;
        registry.register_internal(Arc::new(PstExtractor::new()))?;
    }

    #[cfg(feature = "html")]
    registry.register_internal(Arc::new(HtmlExtractor::new()))?;

    #[cfg(feature = "tree-sitter")]
    registry.register_internal(Arc::new(CodeExtractor::new()))?;

    #[cfg(feature = "archives")]
    {
        registry.register_internal(Arc::new(ZipExtractor::new()))?;
        registry.register_internal(Arc::new(TarExtractor::new()))?;
        registry.register_internal(Arc::new(SevenZExtractor::new()))?;
        registry.register_internal(Arc::new(GzipExtractor::new()))?;
    }

    #[cfg(feature = "transcription")]
    registry.register_internal(Arc::new(TranscriptionExtractor))?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_register_default_extractors() {
        let registry = get_document_extractor_registry();
        {
            let mut reg = registry.write();
            *reg = crate::plugins::registry::DocumentExtractorRegistry::new();
        }

        register_default_extractors().expect("Failed to register extractors");

        let reg = registry.read();
        let extractor_names = reg.list();

        #[allow(unused_mut)]
        let mut expected_count = 8;
        assert!(extractor_names.contains(&"plain-text-extractor".to_string()));
        assert!(extractor_names.contains(&"asciidoc-extractor".to_string()));
        assert!(extractor_names.contains(&"webvtt-extractor".to_string()));
        assert!(extractor_names.contains(&"markdown-extractor".to_string()));
        assert!(extractor_names.contains(&"structured-extractor".to_string()));
        assert!(extractor_names.contains(&"djot-extractor".to_string()));
        assert!(extractor_names.contains(&"csv-extractor".to_string()));
        assert!(extractor_names.contains(&"doctags-extractor".to_string()));

        #[cfg(feature = "sqlite")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"sqlite-extractor".to_string()));
        }

        #[cfg(any(feature = "ocr", feature = "ocr-wasm", feature = "ocr-pipeline"))]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"image-extractor".to_string()));
        }

        #[cfg(feature = "xml")]
        {
            expected_count += 3;
            assert!(extractor_names.contains(&"xml-extractor".to_string()));
            assert!(extractor_names.contains(&"jats-extractor".to_string()));
            assert!(extractor_names.contains(&"docbook-extractor".to_string()));
        }

        #[cfg(feature = "pdf")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"pdf-extractor".to_string()));
        }

        #[cfg(any(feature = "excel", feature = "excel-wasm"))]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"excel-extractor".to_string()));
        }

        #[cfg(feature = "notebook")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"jupyter-extractor".to_string()));
        }

        #[cfg(feature = "office")]
        {
            expected_count += 17;
            assert!(extractor_names.contains(&"bibtex-extractor".to_string()));
            assert!(extractor_names.contains(&"citation-extractor".to_string()));
            assert!(extractor_names.contains(&"epub-extractor".to_string()));
            assert!(extractor_names.contains(&"fictionbook-extractor".to_string()));
            assert!(extractor_names.contains(&"rtf-extractor".to_string()));
            assert!(extractor_names.contains(&"rst-extractor".to_string()));
            assert!(extractor_names.contains(&"latex-extractor".to_string()));
            assert!(extractor_names.contains(&"orgmode-extractor".to_string()));
            assert!(extractor_names.contains(&"opml-extractor".to_string()));
            assert!(extractor_names.contains(&"typst-extractor".to_string()));
            assert!(extractor_names.contains(&"dbf-extractor".to_string()));
            assert!(extractor_names.contains(&"doc-extractor".to_string()));
            assert!(extractor_names.contains(&"docx-extractor".to_string()));
            assert!(extractor_names.contains(&"ppt-extractor".to_string()));
            assert!(extractor_names.contains(&"pptx-extractor".to_string()));
            assert!(extractor_names.contains(&"odt-extractor".to_string()));
            assert!(extractor_names.contains(&"odp-extractor".to_string()));
        }

        #[cfg(feature = "hwp")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"hwp-extractor".to_string()));
        }

        #[cfg(feature = "hwpx")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"hwpx-extractor".to_string()));
        }

        #[cfg(feature = "wordperfect")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"wordperfect-extractor".to_string()));
        }

        #[cfg(feature = "iwork")]
        {
            expected_count += 3;
            assert!(extractor_names.contains(&"iwork-pages-extractor".to_string()));
            assert!(extractor_names.contains(&"iwork-numbers-extractor".to_string()));
            assert!(extractor_names.contains(&"iwork-keynote-extractor".to_string()));
        }

        #[cfg(feature = "mdx")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"mdx-extractor".to_string()));
        }

        #[cfg(feature = "email")]
        {
            expected_count += 2;
            assert!(extractor_names.contains(&"email-extractor".to_string()));
            assert!(extractor_names.contains(&"pst-extractor".to_string()));
        }

        #[cfg(feature = "html")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"html-extractor".to_string()));
        }

        #[cfg(feature = "tree-sitter")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"code-extractor".to_string()));
        }

        #[cfg(feature = "archives")]
        {
            expected_count += 4;
            assert!(extractor_names.contains(&"zip-extractor".to_string()));
            assert!(extractor_names.contains(&"tar-extractor".to_string()));
            assert!(extractor_names.contains(&"7z-extractor".to_string()));
            assert!(extractor_names.contains(&"gzip-extractor".to_string()));
        }

        #[cfg(feature = "transcription")]
        {
            expected_count += 1;
            assert!(extractor_names.contains(&"transcription".to_string()));
        }

        assert_eq!(
            extractor_names.len(),
            expected_count,
            "Expected {} extractors based on enabled features",
            expected_count
        );
    }

    #[test]
    fn test_ensure_initialized() {
        ensure_initialized().expect("Failed to ensure extractors initialized");
    }
}