kreuzberg 4.0.8 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +1 -1
- data/ext/kreuzberg_rb/native/Cargo.lock +94 -98
- data/ext/kreuzberg_rb/native/Cargo.toml +4 -2
- data/ext/kreuzberg_rb/native/src/batch.rs +139 -0
- data/ext/kreuzberg_rb/native/src/config/mod.rs +10 -0
- data/ext/kreuzberg_rb/native/src/config/types.rs +1058 -0
- data/ext/kreuzberg_rb/native/src/error_handling.rs +125 -0
- data/ext/kreuzberg_rb/native/src/extraction.rs +79 -0
- data/ext/kreuzberg_rb/native/src/gc_guarded_value.rs +35 -0
- data/ext/kreuzberg_rb/native/src/helpers.rs +176 -0
- data/ext/kreuzberg_rb/native/src/lib.rs +342 -3622
- data/ext/kreuzberg_rb/native/src/metadata.rs +34 -0
- data/ext/kreuzberg_rb/native/src/plugins/mod.rs +92 -0
- data/ext/kreuzberg_rb/native/src/plugins/ocr_backend.rs +159 -0
- data/ext/kreuzberg_rb/native/src/plugins/post_processor.rs +126 -0
- data/ext/kreuzberg_rb/native/src/plugins/validator.rs +99 -0
- data/ext/kreuzberg_rb/native/src/result.rs +326 -0
- data/ext/kreuzberg_rb/native/src/validation.rs +4 -0
- data/lib/kreuzberg/config.rb +66 -0
- data/lib/kreuzberg/result.rb +107 -2
- data/lib/kreuzberg/types.rb +104 -0
- data/lib/kreuzberg/version.rb +1 -1
- data/lib/kreuzberg.rb +0 -4
- data/sig/kreuzberg.rbs +105 -1
- data/vendor/Cargo.toml +3 -3
- data/vendor/kreuzberg/Cargo.toml +4 -3
- data/vendor/kreuzberg/README.md +1 -1
- data/vendor/kreuzberg/src/api/config.rs +69 -0
- data/vendor/kreuzberg/src/api/handlers.rs +99 -2
- data/vendor/kreuzberg/src/api/mod.rs +14 -7
- data/vendor/kreuzberg/src/api/router.rs +214 -0
- data/vendor/kreuzberg/src/api/startup.rs +243 -0
- data/vendor/kreuzberg/src/api/types.rs +78 -0
- data/vendor/kreuzberg/src/cache/cleanup.rs +277 -0
- data/vendor/kreuzberg/src/cache/core.rs +428 -0
- data/vendor/kreuzberg/src/cache/mod.rs +21 -843
- data/vendor/kreuzberg/src/cache/utilities.rs +156 -0
- data/vendor/kreuzberg/src/chunking/boundaries.rs +301 -0
- data/vendor/kreuzberg/src/chunking/builder.rs +294 -0
- data/vendor/kreuzberg/src/chunking/config.rs +52 -0
- data/vendor/kreuzberg/src/chunking/core.rs +1017 -0
- data/vendor/kreuzberg/src/chunking/mod.rs +14 -2211
- data/vendor/kreuzberg/src/chunking/processor.rs +10 -0
- data/vendor/kreuzberg/src/chunking/validation.rs +686 -0
- data/vendor/kreuzberg/src/core/config/extraction/core.rs +169 -0
- data/vendor/kreuzberg/src/core/config/extraction/env.rs +179 -0
- data/vendor/kreuzberg/src/core/config/extraction/loaders.rs +204 -0
- data/vendor/kreuzberg/src/core/config/extraction/mod.rs +42 -0
- data/vendor/kreuzberg/src/core/config/extraction/types.rs +93 -0
- data/vendor/kreuzberg/src/core/config/formats.rs +135 -0
- data/vendor/kreuzberg/src/core/config/mod.rs +20 -0
- data/vendor/kreuzberg/src/core/config/ocr.rs +73 -0
- data/vendor/kreuzberg/src/core/config/page.rs +57 -0
- data/vendor/kreuzberg/src/core/config/pdf.rs +111 -0
- data/vendor/kreuzberg/src/core/config/processing.rs +312 -0
- data/vendor/kreuzberg/src/core/config_validation/dependencies.rs +187 -0
- data/vendor/kreuzberg/src/core/config_validation/mod.rs +386 -0
- data/vendor/kreuzberg/src/core/config_validation/sections.rs +401 -0
- data/vendor/kreuzberg/src/core/extractor/batch.rs +246 -0
- data/vendor/kreuzberg/src/core/extractor/bytes.rs +116 -0
- data/vendor/kreuzberg/src/core/extractor/file.rs +240 -0
- data/vendor/kreuzberg/src/core/extractor/helpers.rs +71 -0
- data/vendor/kreuzberg/src/core/extractor/legacy.rs +62 -0
- data/vendor/kreuzberg/src/core/extractor/mod.rs +490 -0
- data/vendor/kreuzberg/src/core/extractor/sync.rs +208 -0
- data/vendor/kreuzberg/src/core/mod.rs +4 -1
- data/vendor/kreuzberg/src/core/pipeline/cache.rs +60 -0
- data/vendor/kreuzberg/src/core/pipeline/execution.rs +89 -0
- data/vendor/kreuzberg/src/core/pipeline/features.rs +108 -0
- data/vendor/kreuzberg/src/core/pipeline/format.rs +392 -0
- data/vendor/kreuzberg/src/core/pipeline/initialization.rs +67 -0
- data/vendor/kreuzberg/src/core/pipeline/mod.rs +135 -0
- data/vendor/kreuzberg/src/core/pipeline/tests.rs +975 -0
- data/vendor/kreuzberg/src/core/server_config/env.rs +90 -0
- data/vendor/kreuzberg/src/core/server_config/loader.rs +202 -0
- data/vendor/kreuzberg/src/core/server_config/mod.rs +380 -0
- data/vendor/kreuzberg/src/core/server_config/tests/basic_tests.rs +124 -0
- data/vendor/kreuzberg/src/core/server_config/tests/env_tests.rs +216 -0
- data/vendor/kreuzberg/src/core/server_config/tests/file_loading_tests.rs +341 -0
- data/vendor/kreuzberg/src/core/server_config/tests/mod.rs +5 -0
- data/vendor/kreuzberg/src/core/server_config/validation.rs +17 -0
- data/vendor/kreuzberg/src/embeddings.rs +136 -13
- data/vendor/kreuzberg/src/extraction/{archive.rs → archive/mod.rs} +45 -239
- data/vendor/kreuzberg/src/extraction/archive/sevenz.rs +98 -0
- data/vendor/kreuzberg/src/extraction/archive/tar.rs +118 -0
- data/vendor/kreuzberg/src/extraction/archive/zip.rs +101 -0
- data/vendor/kreuzberg/src/extraction/html/converter.rs +592 -0
- data/vendor/kreuzberg/src/extraction/html/image_handling.rs +95 -0
- data/vendor/kreuzberg/src/extraction/html/mod.rs +53 -0
- data/vendor/kreuzberg/src/extraction/html/processor.rs +659 -0
- data/vendor/kreuzberg/src/extraction/html/stack_management.rs +103 -0
- data/vendor/kreuzberg/src/extraction/html/types.rs +28 -0
- data/vendor/kreuzberg/src/extraction/mod.rs +6 -2
- data/vendor/kreuzberg/src/extraction/pptx/container.rs +159 -0
- data/vendor/kreuzberg/src/extraction/pptx/content_builder.rs +168 -0
- data/vendor/kreuzberg/src/extraction/pptx/elements.rs +132 -0
- data/vendor/kreuzberg/src/extraction/pptx/image_handling.rs +57 -0
- data/vendor/kreuzberg/src/extraction/pptx/metadata.rs +160 -0
- data/vendor/kreuzberg/src/extraction/pptx/mod.rs +558 -0
- data/vendor/kreuzberg/src/extraction/pptx/parser.rs +379 -0
- data/vendor/kreuzberg/src/extraction/transform/content.rs +205 -0
- data/vendor/kreuzberg/src/extraction/transform/elements.rs +211 -0
- data/vendor/kreuzberg/src/extraction/transform/mod.rs +480 -0
- data/vendor/kreuzberg/src/extraction/transform/types.rs +27 -0
- data/vendor/kreuzberg/src/extractors/archive.rs +2 -0
- data/vendor/kreuzberg/src/extractors/bibtex.rs +2 -0
- data/vendor/kreuzberg/src/extractors/djot_format/attributes.rs +134 -0
- data/vendor/kreuzberg/src/extractors/djot_format/conversion.rs +223 -0
- data/vendor/kreuzberg/src/extractors/djot_format/extractor.rs +172 -0
- data/vendor/kreuzberg/src/extractors/djot_format/mod.rs +24 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/block_handlers.rs +271 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/content_extraction.rs +257 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/event_handlers.rs +101 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/inline_handlers.rs +201 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/mod.rs +16 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/state.rs +78 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/table_extraction.rs +68 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/text_extraction.rs +61 -0
- data/vendor/kreuzberg/src/extractors/djot_format/rendering.rs +452 -0
- data/vendor/kreuzberg/src/extractors/docbook.rs +2 -0
- data/vendor/kreuzberg/src/extractors/docx.rs +12 -1
- data/vendor/kreuzberg/src/extractors/email.rs +2 -0
- data/vendor/kreuzberg/src/extractors/epub/content.rs +333 -0
- data/vendor/kreuzberg/src/extractors/epub/metadata.rs +137 -0
- data/vendor/kreuzberg/src/extractors/epub/mod.rs +186 -0
- data/vendor/kreuzberg/src/extractors/epub/parsing.rs +86 -0
- data/vendor/kreuzberg/src/extractors/excel.rs +4 -0
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +2 -0
- data/vendor/kreuzberg/src/extractors/frontmatter_utils.rs +466 -0
- data/vendor/kreuzberg/src/extractors/html.rs +80 -8
- data/vendor/kreuzberg/src/extractors/image.rs +8 -1
- data/vendor/kreuzberg/src/extractors/jats/elements.rs +350 -0
- data/vendor/kreuzberg/src/extractors/jats/metadata.rs +21 -0
- data/vendor/kreuzberg/src/extractors/{jats.rs → jats/mod.rs} +10 -412
- data/vendor/kreuzberg/src/extractors/jats/parser.rs +52 -0
- data/vendor/kreuzberg/src/extractors/jupyter.rs +2 -0
- data/vendor/kreuzberg/src/extractors/latex/commands.rs +93 -0
- data/vendor/kreuzberg/src/extractors/latex/environments.rs +157 -0
- data/vendor/kreuzberg/src/extractors/latex/metadata.rs +27 -0
- data/vendor/kreuzberg/src/extractors/latex/mod.rs +146 -0
- data/vendor/kreuzberg/src/extractors/latex/parser.rs +231 -0
- data/vendor/kreuzberg/src/extractors/latex/utilities.rs +126 -0
- data/vendor/kreuzberg/src/extractors/markdown.rs +39 -162
- data/vendor/kreuzberg/src/extractors/mod.rs +9 -1
- data/vendor/kreuzberg/src/extractors/odt.rs +2 -0
- data/vendor/kreuzberg/src/extractors/opml/core.rs +165 -0
- data/vendor/kreuzberg/src/extractors/opml/mod.rs +31 -0
- data/vendor/kreuzberg/src/extractors/opml/parser.rs +479 -0
- data/vendor/kreuzberg/src/extractors/orgmode.rs +2 -0
- data/vendor/kreuzberg/src/extractors/pdf/extraction.rs +106 -0
- data/vendor/kreuzberg/src/extractors/{pdf.rs → pdf/mod.rs} +25 -324
- data/vendor/kreuzberg/src/extractors/pdf/ocr.rs +214 -0
- data/vendor/kreuzberg/src/extractors/pdf/pages.rs +51 -0
- data/vendor/kreuzberg/src/extractors/pptx.rs +9 -2
- data/vendor/kreuzberg/src/extractors/rst.rs +2 -0
- data/vendor/kreuzberg/src/extractors/rtf/encoding.rs +116 -0
- data/vendor/kreuzberg/src/extractors/rtf/formatting.rs +24 -0
- data/vendor/kreuzberg/src/extractors/rtf/images.rs +72 -0
- data/vendor/kreuzberg/src/extractors/rtf/metadata.rs +216 -0
- data/vendor/kreuzberg/src/extractors/rtf/mod.rs +142 -0
- data/vendor/kreuzberg/src/extractors/rtf/parser.rs +259 -0
- data/vendor/kreuzberg/src/extractors/rtf/tables.rs +83 -0
- data/vendor/kreuzberg/src/extractors/structured.rs +2 -0
- data/vendor/kreuzberg/src/extractors/text.rs +4 -0
- data/vendor/kreuzberg/src/extractors/typst.rs +2 -0
- data/vendor/kreuzberg/src/extractors/xml.rs +2 -0
- data/vendor/kreuzberg/src/keywords/processor.rs +14 -0
- data/vendor/kreuzberg/src/language_detection/processor.rs +10 -0
- data/vendor/kreuzberg/src/lib.rs +2 -2
- data/vendor/kreuzberg/src/mcp/errors.rs +312 -0
- data/vendor/kreuzberg/src/mcp/format.rs +211 -0
- data/vendor/kreuzberg/src/mcp/mod.rs +9 -3
- data/vendor/kreuzberg/src/mcp/params.rs +196 -0
- data/vendor/kreuzberg/src/mcp/server.rs +39 -1438
- data/vendor/kreuzberg/src/mcp/tools/cache.rs +179 -0
- data/vendor/kreuzberg/src/mcp/tools/extraction.rs +403 -0
- data/vendor/kreuzberg/src/mcp/tools/mime.rs +150 -0
- data/vendor/kreuzberg/src/mcp/tools/mod.rs +11 -0
- data/vendor/kreuzberg/src/ocr/backends/easyocr.rs +96 -0
- data/vendor/kreuzberg/src/ocr/backends/mod.rs +7 -0
- data/vendor/kreuzberg/src/ocr/backends/paddleocr.rs +27 -0
- data/vendor/kreuzberg/src/ocr/backends/tesseract.rs +134 -0
- data/vendor/kreuzberg/src/ocr/hocr.rs +60 -16
- data/vendor/kreuzberg/src/ocr/language_registry.rs +11 -235
- data/vendor/kreuzberg/src/ocr/mod.rs +1 -0
- data/vendor/kreuzberg/src/ocr/processor/config.rs +203 -0
- data/vendor/kreuzberg/src/ocr/processor/execution.rs +494 -0
- data/vendor/kreuzberg/src/ocr/processor/mod.rs +265 -0
- data/vendor/kreuzberg/src/ocr/processor/validation.rs +145 -0
- data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +41 -24
- data/vendor/kreuzberg/src/pdf/bindings.rs +21 -8
- data/vendor/kreuzberg/src/pdf/hierarchy/bounding_box.rs +289 -0
- data/vendor/kreuzberg/src/pdf/hierarchy/clustering.rs +199 -0
- data/vendor/kreuzberg/src/pdf/{hierarchy.rs → hierarchy/extraction.rs} +6 -346
- data/vendor/kreuzberg/src/pdf/hierarchy/mod.rs +18 -0
- data/vendor/kreuzberg/src/plugins/extractor/mod.rs +319 -0
- data/vendor/kreuzberg/src/plugins/extractor/registry.rs +434 -0
- data/vendor/kreuzberg/src/plugins/extractor/trait.rs +391 -0
- data/vendor/kreuzberg/src/plugins/mod.rs +13 -0
- data/vendor/kreuzberg/src/plugins/ocr.rs +11 -0
- data/vendor/kreuzberg/src/plugins/processor/mod.rs +365 -0
- data/vendor/kreuzberg/src/plugins/processor/registry.rs +37 -0
- data/vendor/kreuzberg/src/plugins/processor/trait.rs +284 -0
- data/vendor/kreuzberg/src/plugins/registry/extractor.rs +416 -0
- data/vendor/kreuzberg/src/plugins/registry/mod.rs +116 -0
- data/vendor/kreuzberg/src/plugins/registry/ocr.rs +293 -0
- data/vendor/kreuzberg/src/plugins/registry/processor.rs +304 -0
- data/vendor/kreuzberg/src/plugins/registry/validator.rs +238 -0
- data/vendor/kreuzberg/src/plugins/validator/mod.rs +424 -0
- data/vendor/kreuzberg/src/plugins/validator/registry.rs +355 -0
- data/vendor/kreuzberg/src/plugins/validator/trait.rs +276 -0
- data/vendor/kreuzberg/src/stopwords/languages/asian.rs +40 -0
- data/vendor/kreuzberg/src/stopwords/languages/germanic.rs +36 -0
- data/vendor/kreuzberg/src/stopwords/languages/mod.rs +10 -0
- data/vendor/kreuzberg/src/stopwords/languages/other.rs +44 -0
- data/vendor/kreuzberg/src/stopwords/languages/romance.rs +36 -0
- data/vendor/kreuzberg/src/stopwords/languages/slavic.rs +36 -0
- data/vendor/kreuzberg/src/stopwords/mod.rs +7 -33
- data/vendor/kreuzberg/src/text/quality.rs +1 -1
- data/vendor/kreuzberg/src/text/quality_processor.rs +10 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/analysis.rs +238 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/mod.rs +8 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/punctuation.rs +54 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/reducer.rs +384 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/sentence_selection.rs +68 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/word_filtering.rs +156 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters/general.rs +377 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters/html.rs +51 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters/markdown.rs +285 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters.rs +131 -246
- data/vendor/kreuzberg/src/types/djot.rs +209 -0
- data/vendor/kreuzberg/src/types/extraction.rs +301 -0
- data/vendor/kreuzberg/src/types/formats.rs +443 -0
- data/vendor/kreuzberg/src/types/metadata.rs +560 -0
- data/vendor/kreuzberg/src/types/mod.rs +281 -0
- data/vendor/kreuzberg/src/types/page.rs +182 -0
- data/vendor/kreuzberg/src/types/serde_helpers.rs +132 -0
- data/vendor/kreuzberg/src/types/tables.rs +39 -0
- data/vendor/kreuzberg/src/utils/quality/heuristics.rs +58 -0
- data/vendor/kreuzberg/src/utils/{quality.rs → quality/mod.rs} +168 -489
- data/vendor/kreuzberg/src/utils/quality/patterns.rs +117 -0
- data/vendor/kreuzberg/src/utils/quality/scoring.rs +178 -0
- data/vendor/kreuzberg/src/utils/string_pool/buffer_pool.rs +325 -0
- data/vendor/kreuzberg/src/utils/string_pool/interned.rs +102 -0
- data/vendor/kreuzberg/src/utils/string_pool/language_pool.rs +119 -0
- data/vendor/kreuzberg/src/utils/string_pool/mime_pool.rs +235 -0
- data/vendor/kreuzberg/src/utils/string_pool/mod.rs +41 -0
- data/vendor/kreuzberg/tests/api_chunk.rs +313 -0
- data/vendor/kreuzberg/tests/api_embed.rs +6 -9
- data/vendor/kreuzberg/tests/batch_orchestration.rs +1 -0
- data/vendor/kreuzberg/tests/concurrency_stress.rs +7 -0
- data/vendor/kreuzberg/tests/core_integration.rs +1 -0
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +130 -0
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +5 -14
- data/vendor/kreuzberg/tests/format_integration.rs +2 -0
- data/vendor/kreuzberg/tests/helpers/mod.rs +1 -0
- data/vendor/kreuzberg/tests/html_table_test.rs +11 -11
- data/vendor/kreuzberg/tests/ocr_configuration.rs +16 -0
- data/vendor/kreuzberg/tests/ocr_errors.rs +18 -0
- data/vendor/kreuzberg/tests/ocr_quality.rs +9 -0
- data/vendor/kreuzberg/tests/ocr_stress.rs +1 -0
- data/vendor/kreuzberg/tests/pipeline_integration.rs +50 -0
- data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +13 -0
- data/vendor/kreuzberg/tests/plugin_system.rs +12 -0
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +2 -0
- data/vendor/kreuzberg-ffi/Cargo.toml +2 -1
- data/vendor/kreuzberg-ffi/benches/result_view_benchmark.rs +2 -0
- data/vendor/kreuzberg-ffi/kreuzberg.h +347 -178
- data/vendor/kreuzberg-ffi/src/config/html.rs +318 -0
- data/vendor/kreuzberg-ffi/src/config/loader.rs +154 -0
- data/vendor/kreuzberg-ffi/src/config/merge.rs +104 -0
- data/vendor/kreuzberg-ffi/src/config/mod.rs +385 -0
- data/vendor/kreuzberg-ffi/src/config/parse.rs +91 -0
- data/vendor/kreuzberg-ffi/src/config/serialize.rs +118 -0
- data/vendor/kreuzberg-ffi/src/config_builder.rs +598 -0
- data/vendor/kreuzberg-ffi/src/error.rs +46 -14
- data/vendor/kreuzberg-ffi/src/helpers.rs +10 -0
- data/vendor/kreuzberg-ffi/src/html_options.rs +421 -0
- data/vendor/kreuzberg-ffi/src/lib.rs +16 -0
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +11 -0
- data/vendor/kreuzberg-ffi/src/plugins/ocr_backend.rs +2 -0
- data/vendor/kreuzberg-ffi/src/result.rs +148 -122
- data/vendor/kreuzberg-ffi/src/result_view.rs +4 -0
- data/vendor/kreuzberg-tesseract/Cargo.toml +2 -2
- metadata +200 -28
- data/vendor/kreuzberg/src/api/server.rs +0 -518
- data/vendor/kreuzberg/src/core/config.rs +0 -1914
- data/vendor/kreuzberg/src/core/config_validation.rs +0 -949
- data/vendor/kreuzberg/src/core/extractor.rs +0 -1200
- data/vendor/kreuzberg/src/core/pipeline.rs +0 -1223
- data/vendor/kreuzberg/src/core/server_config.rs +0 -1220
- data/vendor/kreuzberg/src/extraction/html.rs +0 -1830
- data/vendor/kreuzberg/src/extraction/pptx.rs +0 -3102
- data/vendor/kreuzberg/src/extractors/epub.rs +0 -696
- data/vendor/kreuzberg/src/extractors/latex.rs +0 -653
- data/vendor/kreuzberg/src/extractors/opml.rs +0 -635
- data/vendor/kreuzberg/src/extractors/rtf.rs +0 -809
- data/vendor/kreuzberg/src/ocr/processor.rs +0 -858
- data/vendor/kreuzberg/src/plugins/extractor.rs +0 -1042
- data/vendor/kreuzberg/src/plugins/processor.rs +0 -650
- data/vendor/kreuzberg/src/plugins/registry.rs +0 -1339
- data/vendor/kreuzberg/src/plugins/validator.rs +0 -967
- data/vendor/kreuzberg/src/text/token_reduction/core.rs +0 -832
- data/vendor/kreuzberg/src/types.rs +0 -1713
- data/vendor/kreuzberg/src/utils/string_pool.rs +0 -762
- data/vendor/kreuzberg-ffi/src/config.rs +0 -1341
|
@@ -1,809 +0,0 @@
|
|
|
1
|
-
//! RTF (Rich Text Format) extractor.
|
|
2
|
-
//!
|
|
3
|
-
//! Supports: Rich Text Format (.rtf)
|
|
4
|
-
//!
|
|
5
|
-
//! This native Rust extractor provides text extraction from RTF documents with:
|
|
6
|
-
//! - Character encoding support (Windows-1252 for 0x80-0x9F range)
|
|
7
|
-
//! - Common RTF control words (paragraph breaks, tabs, bullets, quotes, dashes)
|
|
8
|
-
//! - Unicode escape sequences
|
|
9
|
-
//! - Image metadata extraction
|
|
10
|
-
//! - Whitespace normalization
|
|
11
|
-
|
|
12
|
-
use crate::Result;
|
|
13
|
-
use crate::core::config::ExtractionConfig;
|
|
14
|
-
use crate::extraction::cells_to_markdown;
|
|
15
|
-
use crate::plugins::{DocumentExtractor, Plugin};
|
|
16
|
-
use crate::types::{ExtractionResult, Metadata, Table};
|
|
17
|
-
use async_trait::async_trait;
|
|
18
|
-
use serde_json::Value;
|
|
19
|
-
use std::collections::HashMap;
|
|
20
|
-
|
|
21
|
-
/// Native Rust RTF extractor.
|
|
22
|
-
///
|
|
23
|
-
/// Extracts text content, metadata, and structure from RTF documents
|
|
24
|
-
pub struct RtfExtractor;
|
|
25
|
-
|
|
26
|
-
impl RtfExtractor {
|
|
27
|
-
/// Create a new RTF extractor.
|
|
28
|
-
pub fn new() -> Self {
|
|
29
|
-
Self
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
impl Default for RtfExtractor {
|
|
34
|
-
fn default() -> Self {
|
|
35
|
-
Self::new()
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
impl Plugin for RtfExtractor {
|
|
40
|
-
fn name(&self) -> &str {
|
|
41
|
-
"rtf-extractor"
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
fn version(&self) -> String {
|
|
45
|
-
env!("CARGO_PKG_VERSION").to_string()
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
fn initialize(&self) -> Result<()> {
|
|
49
|
-
Ok(())
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
fn shutdown(&self) -> Result<()> {
|
|
53
|
-
Ok(())
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
fn description(&self) -> &str {
|
|
57
|
-
"Extracts content from RTF (Rich Text Format) files with native Rust parsing"
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
fn author(&self) -> &str {
|
|
61
|
-
"Kreuzberg Team"
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/// Convert a hex digit character to its numeric value.
|
|
66
|
-
///
|
|
67
|
-
/// Returns None if the character is not a valid hex digit.
|
|
68
|
-
#[inline]
|
|
69
|
-
fn hex_digit_to_u8(c: char) -> Option<u8> {
|
|
70
|
-
match c {
|
|
71
|
-
'0'..='9' => Some((c as u8) - b'0'),
|
|
72
|
-
'a'..='f' => Some((c as u8) - b'a' + 10),
|
|
73
|
-
'A'..='F' => Some((c as u8) - b'A' + 10),
|
|
74
|
-
_ => None,
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/// Parse a hex-encoded byte from two characters.
|
|
79
|
-
///
|
|
80
|
-
/// Returns the decoded byte if both characters are valid hex digits.
|
|
81
|
-
#[inline]
|
|
82
|
-
fn parse_hex_byte(h1: char, h2: char) -> Option<u8> {
|
|
83
|
-
let high = hex_digit_to_u8(h1)?;
|
|
84
|
-
let low = hex_digit_to_u8(h2)?;
|
|
85
|
-
Some((high << 4) | low)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/// Parse an RTF control word and extract its value.
|
|
89
|
-
///
|
|
90
|
-
/// Returns a tuple of (control_word, optional_numeric_value)
|
|
91
|
-
fn parse_rtf_control_word(chars: &mut std::iter::Peekable<std::str::Chars>) -> (String, Option<i32>) {
|
|
92
|
-
let mut word = String::new();
|
|
93
|
-
let mut num_str = String::new();
|
|
94
|
-
let mut is_negative = false;
|
|
95
|
-
|
|
96
|
-
while let Some(&c) = chars.peek() {
|
|
97
|
-
if c.is_alphabetic() {
|
|
98
|
-
word.push(c);
|
|
99
|
-
chars.next();
|
|
100
|
-
} else {
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if let Some(&c) = chars.peek()
|
|
106
|
-
&& c == '-'
|
|
107
|
-
{
|
|
108
|
-
is_negative = true;
|
|
109
|
-
chars.next();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
while let Some(&c) = chars.peek() {
|
|
113
|
-
if c.is_ascii_digit() {
|
|
114
|
-
num_str.push(c);
|
|
115
|
-
chars.next();
|
|
116
|
-
} else {
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
let num_value = if !num_str.is_empty() {
|
|
122
|
-
let val = num_str.parse::<i32>().unwrap_or(0);
|
|
123
|
-
Some(if is_negative { -val } else { val })
|
|
124
|
-
} else {
|
|
125
|
-
None
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
(word, num_value)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/// Extract text and image metadata from RTF document.
|
|
132
|
-
///
|
|
133
|
-
/// This function extracts plain text from an RTF document by:
|
|
134
|
-
/// 1. Tokenizing control sequences and text
|
|
135
|
-
/// 2. Converting encoded characters to Unicode
|
|
136
|
-
/// 3. Extracting text while skipping formatting groups
|
|
137
|
-
/// 4. Detecting and extracting image metadata (\pict sections)
|
|
138
|
-
/// 5. Normalizing whitespace
|
|
139
|
-
fn extract_text_from_rtf(content: &str) -> (String, Vec<Table>) {
|
|
140
|
-
struct TableState {
|
|
141
|
-
rows: Vec<Vec<String>>,
|
|
142
|
-
current_row: Vec<String>,
|
|
143
|
-
current_cell: String,
|
|
144
|
-
in_row: bool,
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
fn push_cell(state: &mut TableState) {
|
|
148
|
-
let cell = state.current_cell.trim().to_string();
|
|
149
|
-
state.current_row.push(cell);
|
|
150
|
-
state.current_cell.clear();
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
fn push_row(state: &mut TableState) {
|
|
154
|
-
if state.in_row || !state.current_cell.is_empty() {
|
|
155
|
-
push_cell(state);
|
|
156
|
-
state.in_row = false;
|
|
157
|
-
}
|
|
158
|
-
if !state.current_row.is_empty() {
|
|
159
|
-
state.rows.push(state.current_row.clone());
|
|
160
|
-
state.current_row.clear();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
fn finalize_table(state_opt: &mut Option<TableState>, tables: &mut Vec<Table>) {
|
|
165
|
-
if let Some(mut state) = state_opt.take() {
|
|
166
|
-
if state.in_row || !state.current_cell.is_empty() || !state.current_row.is_empty() {
|
|
167
|
-
push_row(&mut state);
|
|
168
|
-
}
|
|
169
|
-
if !state.rows.is_empty() {
|
|
170
|
-
let markdown = cells_to_markdown(&state.rows);
|
|
171
|
-
tables.push(Table {
|
|
172
|
-
cells: state.rows,
|
|
173
|
-
markdown,
|
|
174
|
-
page_number: 1,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
let mut result = String::new();
|
|
181
|
-
let mut chars = content.chars().peekable();
|
|
182
|
-
let mut tables: Vec<Table> = Vec::new();
|
|
183
|
-
let mut table_state: Option<TableState> = None;
|
|
184
|
-
|
|
185
|
-
let ensure_table = |table_state: &mut Option<TableState>| {
|
|
186
|
-
if table_state.is_none() {
|
|
187
|
-
*table_state = Some(TableState {
|
|
188
|
-
rows: Vec::new(),
|
|
189
|
-
current_row: Vec::new(),
|
|
190
|
-
current_cell: String::new(),
|
|
191
|
-
in_row: false,
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
while let Some(ch) = chars.next() {
|
|
197
|
-
match ch {
|
|
198
|
-
'\\' => {
|
|
199
|
-
if let Some(&next_ch) = chars.peek() {
|
|
200
|
-
match next_ch {
|
|
201
|
-
'\\' | '{' | '}' => {
|
|
202
|
-
chars.next();
|
|
203
|
-
result.push(next_ch);
|
|
204
|
-
}
|
|
205
|
-
'\'' => {
|
|
206
|
-
chars.next();
|
|
207
|
-
let hex1 = chars.next();
|
|
208
|
-
let hex2 = chars.next();
|
|
209
|
-
if let (Some(h1), Some(h2)) = (hex1, hex2)
|
|
210
|
-
&& let Some(byte) = parse_hex_byte(h1, h2)
|
|
211
|
-
{
|
|
212
|
-
let decoded = match byte {
|
|
213
|
-
0x80 => '\u{20AC}',
|
|
214
|
-
0x81 => '?',
|
|
215
|
-
0x82 => '\u{201A}',
|
|
216
|
-
0x83 => '\u{0192}',
|
|
217
|
-
0x84 => '\u{201E}',
|
|
218
|
-
0x85 => '\u{2026}',
|
|
219
|
-
0x86 => '\u{2020}',
|
|
220
|
-
0x87 => '\u{2021}',
|
|
221
|
-
0x88 => '\u{02C6}',
|
|
222
|
-
0x89 => '\u{2030}',
|
|
223
|
-
0x8A => '\u{0160}',
|
|
224
|
-
0x8B => '\u{2039}',
|
|
225
|
-
0x8C => '\u{0152}',
|
|
226
|
-
0x8D => '?',
|
|
227
|
-
0x8E => '\u{017D}',
|
|
228
|
-
0x8F => '?',
|
|
229
|
-
0x90 => '?',
|
|
230
|
-
0x91 => '\u{2018}',
|
|
231
|
-
0x92 => '\u{2019}',
|
|
232
|
-
0x93 => '\u{201C}',
|
|
233
|
-
0x94 => '\u{201D}',
|
|
234
|
-
0x95 => '\u{2022}',
|
|
235
|
-
0x96 => '\u{2013}',
|
|
236
|
-
0x97 => '\u{2014}',
|
|
237
|
-
0x98 => '\u{02DC}',
|
|
238
|
-
0x99 => '\u{2122}',
|
|
239
|
-
0x9A => '\u{0161}',
|
|
240
|
-
0x9B => '\u{203A}',
|
|
241
|
-
0x9C => '\u{0153}',
|
|
242
|
-
0x9D => '?',
|
|
243
|
-
0x9E => '\u{017E}',
|
|
244
|
-
0x9F => '\u{0178}',
|
|
245
|
-
_ => byte as char,
|
|
246
|
-
};
|
|
247
|
-
result.push(decoded);
|
|
248
|
-
if let Some(state) = table_state.as_mut()
|
|
249
|
-
&& state.in_row
|
|
250
|
-
{
|
|
251
|
-
state.current_cell.push(decoded);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
'u' => {
|
|
256
|
-
chars.next();
|
|
257
|
-
let mut num_str = String::new();
|
|
258
|
-
while let Some(&c) = chars.peek() {
|
|
259
|
-
if c.is_ascii_digit() || c == '-' {
|
|
260
|
-
num_str.push(c);
|
|
261
|
-
chars.next();
|
|
262
|
-
} else {
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
if let Ok(code_num) = num_str.parse::<i32>() {
|
|
267
|
-
let code_u = if code_num < 0 {
|
|
268
|
-
(code_num + 65536) as u32
|
|
269
|
-
} else {
|
|
270
|
-
code_num as u32
|
|
271
|
-
};
|
|
272
|
-
if let Some(c) = char::from_u32(code_u) {
|
|
273
|
-
result.push(c);
|
|
274
|
-
if let Some(state) = table_state.as_mut()
|
|
275
|
-
&& state.in_row
|
|
276
|
-
{
|
|
277
|
-
state.current_cell.push(c);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
_ => {
|
|
283
|
-
let (control_word, _) = parse_rtf_control_word(&mut chars);
|
|
284
|
-
|
|
285
|
-
match control_word.as_str() {
|
|
286
|
-
"pict" => {
|
|
287
|
-
let image_metadata = extract_image_metadata(&mut chars);
|
|
288
|
-
if !image_metadata.is_empty() {
|
|
289
|
-
result.push('!');
|
|
290
|
-
result.push('[');
|
|
291
|
-
result.push_str("image");
|
|
292
|
-
result.push(']');
|
|
293
|
-
result.push('(');
|
|
294
|
-
result.push_str(&image_metadata);
|
|
295
|
-
result.push(')');
|
|
296
|
-
result.push(' ');
|
|
297
|
-
if let Some(state) = table_state.as_mut()
|
|
298
|
-
&& state.in_row
|
|
299
|
-
{
|
|
300
|
-
state.current_cell.push('!');
|
|
301
|
-
state.current_cell.push('[');
|
|
302
|
-
state.current_cell.push_str("image");
|
|
303
|
-
state.current_cell.push(']');
|
|
304
|
-
state.current_cell.push('(');
|
|
305
|
-
state.current_cell.push_str(&image_metadata);
|
|
306
|
-
state.current_cell.push(')');
|
|
307
|
-
state.current_cell.push(' ');
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
"par" => {
|
|
312
|
-
if table_state.is_some() {
|
|
313
|
-
finalize_table(&mut table_state, &mut tables);
|
|
314
|
-
}
|
|
315
|
-
if !result.is_empty() && !result.ends_with('\n') {
|
|
316
|
-
result.push('\n');
|
|
317
|
-
result.push('\n');
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
"tab" => {
|
|
321
|
-
result.push('\t');
|
|
322
|
-
if let Some(state) = table_state.as_mut()
|
|
323
|
-
&& state.in_row
|
|
324
|
-
{
|
|
325
|
-
state.current_cell.push('\t');
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
"bullet" => {
|
|
329
|
-
result.push('•');
|
|
330
|
-
}
|
|
331
|
-
"lquote" => {
|
|
332
|
-
result.push('\u{2018}');
|
|
333
|
-
}
|
|
334
|
-
"rquote" => {
|
|
335
|
-
result.push('\u{2019}');
|
|
336
|
-
}
|
|
337
|
-
"ldblquote" => {
|
|
338
|
-
result.push('\u{201C}');
|
|
339
|
-
}
|
|
340
|
-
"rdblquote" => {
|
|
341
|
-
result.push('\u{201D}');
|
|
342
|
-
}
|
|
343
|
-
"endash" => {
|
|
344
|
-
result.push('\u{2013}');
|
|
345
|
-
}
|
|
346
|
-
"emdash" => {
|
|
347
|
-
result.push('\u{2014}');
|
|
348
|
-
}
|
|
349
|
-
"trowd" => {
|
|
350
|
-
ensure_table(&mut table_state);
|
|
351
|
-
if let Some(state) = table_state.as_mut() {
|
|
352
|
-
if state.in_row {
|
|
353
|
-
push_row(state);
|
|
354
|
-
}
|
|
355
|
-
state.in_row = true;
|
|
356
|
-
state.current_cell.clear();
|
|
357
|
-
state.current_row.clear();
|
|
358
|
-
}
|
|
359
|
-
if !result.is_empty() && !result.ends_with('\n') {
|
|
360
|
-
result.push('\n');
|
|
361
|
-
}
|
|
362
|
-
if !result.ends_with('|') {
|
|
363
|
-
result.push('|');
|
|
364
|
-
result.push(' ');
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
"cell" => {
|
|
368
|
-
if !result.ends_with('|') {
|
|
369
|
-
if !result.ends_with(' ') && !result.is_empty() {
|
|
370
|
-
result.push(' ');
|
|
371
|
-
}
|
|
372
|
-
result.push('|');
|
|
373
|
-
}
|
|
374
|
-
if !result.ends_with(' ') {
|
|
375
|
-
result.push(' ');
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
"row" => {
|
|
379
|
-
ensure_table(&mut table_state);
|
|
380
|
-
if let Some(state) = table_state.as_mut()
|
|
381
|
-
&& (state.in_row || !state.current_cell.is_empty())
|
|
382
|
-
{
|
|
383
|
-
push_row(state);
|
|
384
|
-
}
|
|
385
|
-
if !result.ends_with('|') {
|
|
386
|
-
result.push('|');
|
|
387
|
-
}
|
|
388
|
-
if !result.ends_with('\n') {
|
|
389
|
-
result.push('\n');
|
|
390
|
-
}
|
|
391
|
-
if let Some(state) = table_state.as_ref()
|
|
392
|
-
&& !state.in_row
|
|
393
|
-
&& !state.rows.is_empty()
|
|
394
|
-
{}
|
|
395
|
-
}
|
|
396
|
-
_ => {}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
'{' | '}' => {
|
|
403
|
-
if !result.is_empty() && !result.ends_with(' ') {
|
|
404
|
-
result.push(' ');
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
' ' | '\t' | '\n' | '\r' => {
|
|
408
|
-
if !result.is_empty() && !result.ends_with(' ') {
|
|
409
|
-
result.push(' ');
|
|
410
|
-
}
|
|
411
|
-
if let Some(state) = table_state.as_mut()
|
|
412
|
-
&& state.in_row
|
|
413
|
-
&& !state.current_cell.ends_with(' ')
|
|
414
|
-
{
|
|
415
|
-
state.current_cell.push(' ');
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
_ => {
|
|
419
|
-
if let Some(state) = table_state.as_ref()
|
|
420
|
-
&& !state.in_row
|
|
421
|
-
&& !state.rows.is_empty()
|
|
422
|
-
{
|
|
423
|
-
finalize_table(&mut table_state, &mut tables);
|
|
424
|
-
}
|
|
425
|
-
result.push(ch);
|
|
426
|
-
if let Some(state) = table_state.as_mut()
|
|
427
|
-
&& state.in_row
|
|
428
|
-
{
|
|
429
|
-
state.current_cell.push(ch);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
if table_state.is_some() {
|
|
436
|
-
finalize_table(&mut table_state, &mut tables);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
(normalize_whitespace(&result), tables)
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/// Normalize whitespace in a string using a single-pass algorithm.
|
|
443
|
-
///
|
|
444
|
-
/// Collapses multiple consecutive whitespace characters into single spaces
|
|
445
|
-
/// and trims leading/trailing whitespace.
|
|
446
|
-
fn normalize_whitespace(s: &str) -> String {
|
|
447
|
-
let mut result = String::with_capacity(s.len());
|
|
448
|
-
let mut last_was_space = false;
|
|
449
|
-
|
|
450
|
-
for ch in s.chars() {
|
|
451
|
-
if ch.is_whitespace() {
|
|
452
|
-
if !last_was_space {
|
|
453
|
-
result.push(' ');
|
|
454
|
-
last_was_space = true;
|
|
455
|
-
}
|
|
456
|
-
} else {
|
|
457
|
-
result.push(ch);
|
|
458
|
-
last_was_space = false;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
result.trim().to_string()
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/// Parse a `{\\creatim ...}` or `{\\revtim ...}` RTF info block into ISO 8601 format.
|
|
466
|
-
fn parse_rtf_datetime(segment: &str) -> Option<String> {
|
|
467
|
-
let mut year: Option<i32> = None;
|
|
468
|
-
let mut month: Option<i32> = None;
|
|
469
|
-
let mut day: Option<i32> = None;
|
|
470
|
-
let mut hour: Option<i32> = None;
|
|
471
|
-
let mut minute: Option<i32> = None;
|
|
472
|
-
|
|
473
|
-
let mut chars = segment.chars().peekable();
|
|
474
|
-
while let Some(&ch) = chars.peek() {
|
|
475
|
-
if ch != '\\' {
|
|
476
|
-
chars.next();
|
|
477
|
-
continue;
|
|
478
|
-
}
|
|
479
|
-
chars.next();
|
|
480
|
-
let (word, value) = parse_rtf_control_word(&mut chars);
|
|
481
|
-
if let Some(v) = value {
|
|
482
|
-
match word.as_str() {
|
|
483
|
-
"yr" => year = Some(v),
|
|
484
|
-
"mo" => month = Some(v),
|
|
485
|
-
"dy" => day = Some(v),
|
|
486
|
-
"hr" => hour = Some(v),
|
|
487
|
-
"min" => minute = Some(v),
|
|
488
|
-
_ => {}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
let year = year?;
|
|
494
|
-
let month = month.unwrap_or(1).max(1) as u32;
|
|
495
|
-
let day = day.unwrap_or(1).max(1) as u32;
|
|
496
|
-
let hour = hour.unwrap_or(0).max(0) as u32;
|
|
497
|
-
let minute = minute.unwrap_or(0).max(0) as u32;
|
|
498
|
-
|
|
499
|
-
Some(format!(
|
|
500
|
-
"{:04}-{:02}-{:02}T{:02}:{:02}:00Z",
|
|
501
|
-
year, month, day, hour, minute
|
|
502
|
-
))
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
/// Extract metadata from the RTF `\\info` block and augment with computed statistics.
|
|
506
|
-
fn extract_rtf_metadata(rtf_content: &str, extracted_text: &str) -> HashMap<String, Value> {
|
|
507
|
-
let mut metadata: HashMap<String, Value> = HashMap::new();
|
|
508
|
-
|
|
509
|
-
if let Some(start) = rtf_content.find("{\\info") {
|
|
510
|
-
let slice = &rtf_content[start..];
|
|
511
|
-
let mut depth = 0usize;
|
|
512
|
-
let mut end_offset: Option<usize> = None;
|
|
513
|
-
|
|
514
|
-
for (idx, ch) in slice.char_indices() {
|
|
515
|
-
match ch {
|
|
516
|
-
'{' => depth += 1,
|
|
517
|
-
'}' => {
|
|
518
|
-
if depth == 0 {
|
|
519
|
-
break;
|
|
520
|
-
}
|
|
521
|
-
depth -= 1;
|
|
522
|
-
if depth == 0 {
|
|
523
|
-
end_offset = Some(idx + 1);
|
|
524
|
-
break;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
_ => {}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
let info_block = end_offset.map(|end| &slice[..end]).unwrap_or(slice);
|
|
532
|
-
|
|
533
|
-
let mut segments: Vec<String> = Vec::new();
|
|
534
|
-
let mut seg_depth = 0usize;
|
|
535
|
-
let mut current = String::new();
|
|
536
|
-
let mut in_segment = false;
|
|
537
|
-
|
|
538
|
-
for ch in info_block.chars() {
|
|
539
|
-
if ch == '{' {
|
|
540
|
-
seg_depth += 1;
|
|
541
|
-
if seg_depth == 2 {
|
|
542
|
-
in_segment = true;
|
|
543
|
-
current.clear();
|
|
544
|
-
continue;
|
|
545
|
-
}
|
|
546
|
-
} else if ch == '}' {
|
|
547
|
-
if seg_depth == 2 && in_segment {
|
|
548
|
-
segments.push(current.clone());
|
|
549
|
-
in_segment = false;
|
|
550
|
-
}
|
|
551
|
-
seg_depth = seg_depth.saturating_sub(1);
|
|
552
|
-
continue;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
if in_segment {
|
|
556
|
-
current.push(ch);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
for segment in segments {
|
|
561
|
-
if !segment.starts_with('\\') {
|
|
562
|
-
continue;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
let cleaned_segment = if segment.starts_with("\\*\\") {
|
|
566
|
-
segment.replacen("\\*\\", "\\", 1)
|
|
567
|
-
} else {
|
|
568
|
-
segment.clone()
|
|
569
|
-
};
|
|
570
|
-
|
|
571
|
-
let mut chars = cleaned_segment.chars().peekable();
|
|
572
|
-
chars.next();
|
|
573
|
-
let (keyword, numeric) = parse_rtf_control_word(&mut chars);
|
|
574
|
-
let remaining: String = chars.collect();
|
|
575
|
-
let trimmed = remaining.trim();
|
|
576
|
-
|
|
577
|
-
match keyword.as_str() {
|
|
578
|
-
"author" => {
|
|
579
|
-
if !trimmed.is_empty() {
|
|
580
|
-
let author = trimmed.to_string();
|
|
581
|
-
metadata.insert("created_by".to_string(), Value::String(author.clone()));
|
|
582
|
-
metadata.insert("authors".to_string(), Value::Array(vec![Value::String(author)]));
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
"operator" => {
|
|
586
|
-
if !trimmed.is_empty() {
|
|
587
|
-
metadata.insert("modified_by".to_string(), Value::String(trimmed.to_string()));
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
"title" => {
|
|
591
|
-
if !trimmed.is_empty() {
|
|
592
|
-
metadata.insert("title".to_string(), Value::String(trimmed.to_string()));
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
"subject" => {
|
|
596
|
-
if !trimmed.is_empty() {
|
|
597
|
-
metadata.insert("subject".to_string(), Value::String(trimmed.to_string()));
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
"generator" => {
|
|
601
|
-
if !trimmed.is_empty() {
|
|
602
|
-
metadata.insert("generator".to_string(), Value::String(trimmed.to_string()));
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
"creatim" => {
|
|
606
|
-
if let Some(dt) = parse_rtf_datetime(trimmed) {
|
|
607
|
-
metadata.insert("created_at".to_string(), Value::String(dt));
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
"revtim" => {
|
|
611
|
-
if let Some(dt) = parse_rtf_datetime(trimmed) {
|
|
612
|
-
metadata.insert("modified_at".to_string(), Value::String(dt));
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
"version" => {
|
|
616
|
-
if let Some(val) = numeric.or_else(|| trimmed.parse::<i32>().ok()) {
|
|
617
|
-
metadata.insert("revision".to_string(), Value::String(val.to_string()));
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
"nofpages" => {
|
|
621
|
-
if let Some(val) = numeric.or_else(|| trimmed.parse::<i32>().ok()) {
|
|
622
|
-
metadata.insert("page_count".to_string(), Value::Number(val.into()));
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
"nofwords" => {
|
|
626
|
-
if let Some(val) = numeric.or_else(|| trimmed.parse::<i32>().ok()) {
|
|
627
|
-
metadata.insert("word_count".to_string(), Value::Number(val.into()));
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
"nofchars" => {
|
|
631
|
-
if let Some(val) = numeric.or_else(|| trimmed.parse::<i32>().ok()) {
|
|
632
|
-
metadata.insert("character_count".to_string(), Value::Number(val.into()));
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
"lines" => {
|
|
636
|
-
if let Some(val) = numeric.or_else(|| trimmed.parse::<i32>().ok()) {
|
|
637
|
-
metadata.insert("line_count".to_string(), Value::Number(val.into()));
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
"paragraphs" => {
|
|
641
|
-
if let Some(val) = numeric.or_else(|| trimmed.parse::<i32>().ok()) {
|
|
642
|
-
metadata.insert("paragraph_count".to_string(), Value::Number(val.into()));
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
_ => {}
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
let cleaned_text = extracted_text.trim();
|
|
651
|
-
if !cleaned_text.is_empty() {
|
|
652
|
-
let word_count = cleaned_text.split_whitespace().count() as i64;
|
|
653
|
-
metadata
|
|
654
|
-
.entry("word_count".to_string())
|
|
655
|
-
.or_insert(Value::Number(word_count.into()));
|
|
656
|
-
|
|
657
|
-
let character_count = cleaned_text.chars().count() as i64;
|
|
658
|
-
metadata
|
|
659
|
-
.entry("character_count".to_string())
|
|
660
|
-
.or_insert(Value::Number(character_count.into()));
|
|
661
|
-
|
|
662
|
-
let line_count = cleaned_text.lines().count() as i64;
|
|
663
|
-
metadata
|
|
664
|
-
.entry("line_count".to_string())
|
|
665
|
-
.or_insert(Value::Number(line_count.into()));
|
|
666
|
-
|
|
667
|
-
let paragraph_count = cleaned_text.split("\n\n").filter(|p| !p.trim().is_empty()).count() as i64;
|
|
668
|
-
metadata
|
|
669
|
-
.entry("paragraph_count".to_string())
|
|
670
|
-
.or_insert(Value::Number(paragraph_count.into()));
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
metadata
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
/// Extract image metadata from within a \pict group.
|
|
677
|
-
///
|
|
678
|
-
/// Looks for image type (jpegblip, pngblip, etc.) and dimensions.
|
|
679
|
-
fn extract_image_metadata(chars: &mut std::iter::Peekable<std::str::Chars>) -> String {
|
|
680
|
-
let mut metadata = String::new();
|
|
681
|
-
let mut image_type: Option<&str> = None;
|
|
682
|
-
let mut width_goal: Option<i32> = None;
|
|
683
|
-
let mut height_goal: Option<i32> = None;
|
|
684
|
-
let mut depth = 0;
|
|
685
|
-
|
|
686
|
-
while let Some(&ch) = chars.peek() {
|
|
687
|
-
match ch {
|
|
688
|
-
'{' => {
|
|
689
|
-
depth += 1;
|
|
690
|
-
chars.next();
|
|
691
|
-
}
|
|
692
|
-
'}' => {
|
|
693
|
-
if depth == 0 {
|
|
694
|
-
break;
|
|
695
|
-
}
|
|
696
|
-
depth -= 1;
|
|
697
|
-
chars.next();
|
|
698
|
-
}
|
|
699
|
-
'\\' => {
|
|
700
|
-
chars.next();
|
|
701
|
-
let (control_word, value) = parse_rtf_control_word(chars);
|
|
702
|
-
|
|
703
|
-
match control_word.as_str() {
|
|
704
|
-
"jpegblip" => image_type = Some("jpg"),
|
|
705
|
-
"pngblip" => image_type = Some("png"),
|
|
706
|
-
"wmetafile" => image_type = Some("wmf"),
|
|
707
|
-
"dibitmap" => image_type = Some("bmp"),
|
|
708
|
-
"picwgoal" => width_goal = value,
|
|
709
|
-
"pichgoal" => height_goal = value,
|
|
710
|
-
"bin" => break,
|
|
711
|
-
_ => {}
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
' ' => {
|
|
715
|
-
chars.next();
|
|
716
|
-
}
|
|
717
|
-
_ => {
|
|
718
|
-
chars.next();
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
if let Some(itype) = image_type {
|
|
724
|
-
metadata.push_str("image.");
|
|
725
|
-
metadata.push_str(itype);
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
if let Some(width) = width_goal {
|
|
729
|
-
let width_inches = f64::from(width) / 1440.0;
|
|
730
|
-
metadata.push_str(&format!(" width=\"{:.1}in\"", width_inches));
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
if let Some(height) = height_goal {
|
|
734
|
-
let height_inches = f64::from(height) / 1440.0;
|
|
735
|
-
metadata.push_str(&format!(" height=\"{:.1}in\"", height_inches));
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
if metadata.is_empty() {
|
|
739
|
-
metadata.push_str("image.jpg");
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
metadata
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
#[async_trait]
|
|
746
|
-
impl DocumentExtractor for RtfExtractor {
|
|
747
|
-
#[cfg_attr(feature = "otel", tracing::instrument(
|
|
748
|
-
skip(self, content, _config),
|
|
749
|
-
fields(
|
|
750
|
-
extractor.name = self.name(),
|
|
751
|
-
content.size_bytes = content.len(),
|
|
752
|
-
)
|
|
753
|
-
))]
|
|
754
|
-
async fn extract_bytes(
|
|
755
|
-
&self,
|
|
756
|
-
content: &[u8],
|
|
757
|
-
mime_type: &str,
|
|
758
|
-
_config: &ExtractionConfig,
|
|
759
|
-
) -> Result<ExtractionResult> {
|
|
760
|
-
let rtf_content = String::from_utf8_lossy(content);
|
|
761
|
-
|
|
762
|
-
let (extracted_text, tables) = extract_text_from_rtf(&rtf_content);
|
|
763
|
-
let metadata_map = extract_rtf_metadata(&rtf_content, &extracted_text);
|
|
764
|
-
|
|
765
|
-
Ok(ExtractionResult {
|
|
766
|
-
content: extracted_text,
|
|
767
|
-
mime_type: mime_type.to_string(),
|
|
768
|
-
metadata: Metadata {
|
|
769
|
-
additional: metadata_map,
|
|
770
|
-
..Default::default()
|
|
771
|
-
},
|
|
772
|
-
pages: None,
|
|
773
|
-
tables,
|
|
774
|
-
detected_languages: None,
|
|
775
|
-
chunks: None,
|
|
776
|
-
images: None,
|
|
777
|
-
})
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
fn supported_mime_types(&self) -> &[&str] {
|
|
781
|
-
&["application/rtf", "text/rtf"]
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
fn priority(&self) -> i32 {
|
|
785
|
-
50
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
#[cfg(test)]
|
|
790
|
-
mod tests {
|
|
791
|
-
use super::*;
|
|
792
|
-
|
|
793
|
-
#[tokio::test]
|
|
794
|
-
async fn test_rtf_extractor_plugin_interface() {
|
|
795
|
-
let extractor = RtfExtractor::new();
|
|
796
|
-
assert_eq!(extractor.name(), "rtf-extractor");
|
|
797
|
-
assert_eq!(extractor.version(), env!("CARGO_PKG_VERSION"));
|
|
798
|
-
assert!(extractor.supported_mime_types().contains(&"application/rtf"));
|
|
799
|
-
assert_eq!(extractor.priority(), 50);
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
#[test]
|
|
803
|
-
fn test_simple_rtf_extraction() {
|
|
804
|
-
let _extractor = RtfExtractor;
|
|
805
|
-
let rtf_content = r#"{\rtf1 Hello World}"#;
|
|
806
|
-
let (extracted, _) = extract_text_from_rtf(rtf_content);
|
|
807
|
-
assert!(extracted.contains("Hello") || extracted.contains("World"));
|
|
808
|
-
}
|
|
809
|
-
}
|