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
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
//! HTML to Markdown conversion functionality.
|
|
2
|
+
|
|
3
|
+
use super::stack_management::check_wasm_size_limit;
|
|
4
|
+
#[cfg(not(target_arch = "wasm32"))]
|
|
5
|
+
use super::stack_management::{html_requires_large_stack, run_on_dedicated_stack};
|
|
6
|
+
use crate::core::config::OutputFormat as KreuzbergOutputFormat;
|
|
7
|
+
use crate::error::{KreuzbergError, Result};
|
|
8
|
+
use crate::types::HtmlMetadata;
|
|
9
|
+
use html_to_markdown_rs::{
|
|
10
|
+
ConversionOptions, MetadataConfig, OutputFormat as LibOutputFormat, convert as convert_html, convert_with_metadata,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/// Map Kreuzberg OutputFormat to html-to-markdown-rs OutputFormat.
|
|
14
|
+
fn map_output_format(format: KreuzbergOutputFormat) -> LibOutputFormat {
|
|
15
|
+
match format {
|
|
16
|
+
KreuzbergOutputFormat::Markdown => LibOutputFormat::Markdown,
|
|
17
|
+
KreuzbergOutputFormat::Djot => LibOutputFormat::Djot,
|
|
18
|
+
// Plain and Html default to Markdown for HTML conversions
|
|
19
|
+
KreuzbergOutputFormat::Plain | KreuzbergOutputFormat::Html => LibOutputFormat::Markdown,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Resolve conversion options with sensible defaults.
|
|
24
|
+
///
|
|
25
|
+
/// If no options are provided, creates defaults with:
|
|
26
|
+
/// - `extract_metadata = true` (parse YAML frontmatter)
|
|
27
|
+
/// - `hocr_spatial_tables = false` (disable hOCR table detection)
|
|
28
|
+
/// - `preprocessing.enabled = false` (disable HTML preprocessing)
|
|
29
|
+
///
|
|
30
|
+
/// Sets output format based on the provided format parameter.
|
|
31
|
+
pub fn resolve_conversion_options(
|
|
32
|
+
options: Option<ConversionOptions>,
|
|
33
|
+
output_format: KreuzbergOutputFormat,
|
|
34
|
+
) -> ConversionOptions {
|
|
35
|
+
let mut opts = options.unwrap_or_else(|| ConversionOptions {
|
|
36
|
+
extract_metadata: true,
|
|
37
|
+
hocr_spatial_tables: false,
|
|
38
|
+
preprocessing: super::types::PreprocessingOptions {
|
|
39
|
+
enabled: false,
|
|
40
|
+
..Default::default()
|
|
41
|
+
},
|
|
42
|
+
..Default::default()
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
opts.output_format = map_output_format(output_format);
|
|
46
|
+
opts
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// Internal conversion helper that applies options to the conversion.
|
|
50
|
+
fn convert_html_with_options(html: &str, options: ConversionOptions) -> Result<String> {
|
|
51
|
+
convert_html(html, Some(options))
|
|
52
|
+
.map_err(|e| KreuzbergError::parsing(format!("Failed to convert HTML to Markdown: {}", e)))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Convert HTML with optional configuration and output format.
|
|
56
|
+
///
|
|
57
|
+
/// Uses sensible defaults if no configuration is provided:
|
|
58
|
+
/// - `extract_metadata = true` (parse YAML frontmatter)
|
|
59
|
+
/// - `hocr_spatial_tables = false` (disable hOCR table detection)
|
|
60
|
+
/// - `preprocessing.enabled = false` (disable HTML preprocessing)
|
|
61
|
+
///
|
|
62
|
+
/// Supports both markdown and djot output based on the output_format parameter.
|
|
63
|
+
/// Defaults to Markdown for backward compatibility.
|
|
64
|
+
///
|
|
65
|
+
/// # WASM Limitations
|
|
66
|
+
///
|
|
67
|
+
/// In WASM builds, HTML files larger than 2MB will be rejected with an error
|
|
68
|
+
/// to prevent stack overflow. For larger files, use the native library.
|
|
69
|
+
///
|
|
70
|
+
/// # Arguments
|
|
71
|
+
///
|
|
72
|
+
/// * `html` - The HTML string to convert
|
|
73
|
+
/// * `options` - Optional conversion options; defaults will be used if None
|
|
74
|
+
/// * `output_format` - Optional output format; defaults to Markdown if None
|
|
75
|
+
///
|
|
76
|
+
/// # Returns
|
|
77
|
+
///
|
|
78
|
+
/// A markdown or djot string, or an error if conversion fails
|
|
79
|
+
pub fn convert_html_to_markdown(
|
|
80
|
+
html: &str,
|
|
81
|
+
options: Option<ConversionOptions>,
|
|
82
|
+
output_format: Option<KreuzbergOutputFormat>,
|
|
83
|
+
) -> Result<String> {
|
|
84
|
+
check_wasm_size_limit(html)?;
|
|
85
|
+
|
|
86
|
+
let format = output_format.unwrap_or(KreuzbergOutputFormat::Markdown);
|
|
87
|
+
let options = resolve_conversion_options(options, format);
|
|
88
|
+
|
|
89
|
+
#[cfg(not(target_arch = "wasm32"))]
|
|
90
|
+
if html_requires_large_stack(html.len()) {
|
|
91
|
+
let html = html.to_string();
|
|
92
|
+
return run_on_dedicated_stack(move || convert_html_with_options(&html, options));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
convert_html_with_options(html, options)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/// Convert HTML with direct metadata extraction and output format support.
|
|
99
|
+
///
|
|
100
|
+
/// Extracts metadata directly from HTML using the metadata extraction
|
|
101
|
+
/// capabilities of the `html-to-markdown-rs` library, without relying
|
|
102
|
+
/// on YAML frontmatter in the converted markdown.
|
|
103
|
+
///
|
|
104
|
+
/// Supports both markdown and djot output based on the output_format parameter.
|
|
105
|
+
/// Defaults to Markdown for backward compatibility.
|
|
106
|
+
///
|
|
107
|
+
/// # WASM Limitations
|
|
108
|
+
///
|
|
109
|
+
/// In WASM builds, HTML files larger than 2MB will be rejected with an error
|
|
110
|
+
/// to prevent stack overflow. For larger files, use the native library.
|
|
111
|
+
///
|
|
112
|
+
/// # Arguments
|
|
113
|
+
///
|
|
114
|
+
/// * `html` - The HTML string to convert
|
|
115
|
+
/// * `options` - Optional conversion options; defaults will be used if None
|
|
116
|
+
/// * `output_format` - Optional output format; defaults to Markdown if None
|
|
117
|
+
///
|
|
118
|
+
/// # Returns
|
|
119
|
+
///
|
|
120
|
+
/// A tuple of (markdown/djot content, optional metadata), or an error if conversion fails
|
|
121
|
+
pub fn convert_html_to_markdown_with_metadata(
|
|
122
|
+
html: &str,
|
|
123
|
+
options: Option<ConversionOptions>,
|
|
124
|
+
output_format: Option<KreuzbergOutputFormat>,
|
|
125
|
+
) -> Result<(String, Option<HtmlMetadata>)> {
|
|
126
|
+
check_wasm_size_limit(html)?;
|
|
127
|
+
|
|
128
|
+
let format = output_format.unwrap_or(KreuzbergOutputFormat::Markdown);
|
|
129
|
+
let options = resolve_conversion_options(options, format);
|
|
130
|
+
let metadata_config = MetadataConfig::default();
|
|
131
|
+
|
|
132
|
+
#[cfg(not(target_arch = "wasm32"))]
|
|
133
|
+
if html_requires_large_stack(html.len()) {
|
|
134
|
+
let html = html.to_string();
|
|
135
|
+
return run_on_dedicated_stack(move || {
|
|
136
|
+
convert_with_metadata(&html, Some(options), metadata_config, None)
|
|
137
|
+
.map_err(|e| KreuzbergError::parsing(format!("HTML metadata extraction failed: {}", e)))
|
|
138
|
+
.map(|(content, extended_metadata)| {
|
|
139
|
+
let html_metadata = HtmlMetadata::from(extended_metadata);
|
|
140
|
+
(
|
|
141
|
+
content,
|
|
142
|
+
if html_metadata.is_empty() {
|
|
143
|
+
None
|
|
144
|
+
} else {
|
|
145
|
+
Some(html_metadata)
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
})
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let (content, extended_metadata) = convert_with_metadata(html, Some(options), metadata_config, None)
|
|
153
|
+
.map_err(|e| KreuzbergError::parsing(format!("HTML metadata extraction failed: {}", e)))?;
|
|
154
|
+
|
|
155
|
+
let html_metadata = HtmlMetadata::from(extended_metadata);
|
|
156
|
+
|
|
157
|
+
Ok((
|
|
158
|
+
content,
|
|
159
|
+
if html_metadata.is_empty() {
|
|
160
|
+
None
|
|
161
|
+
} else {
|
|
162
|
+
Some(html_metadata)
|
|
163
|
+
},
|
|
164
|
+
))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#[cfg(test)]
|
|
168
|
+
mod tests {
|
|
169
|
+
use super::*;
|
|
170
|
+
|
|
171
|
+
#[test]
|
|
172
|
+
fn test_convert_simple_html() {
|
|
173
|
+
let html = "<h1>Hello World</h1><p>This is a test.</p>";
|
|
174
|
+
let result = convert_html_to_markdown(html, None, None).unwrap();
|
|
175
|
+
assert!(result.contains("# Hello World"));
|
|
176
|
+
assert!(result.contains("This is a test."));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
#[test]
|
|
180
|
+
fn test_html_config_heading_style() {
|
|
181
|
+
let html = "<h1>Heading</h1>";
|
|
182
|
+
let options = ConversionOptions {
|
|
183
|
+
heading_style: super::super::types::HeadingStyle::Atx,
|
|
184
|
+
..Default::default()
|
|
185
|
+
};
|
|
186
|
+
let result = convert_html_to_markdown(html, Some(options), None).unwrap();
|
|
187
|
+
assert!(result.contains("# Heading"));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#[test]
|
|
191
|
+
fn test_html_with_list() {
|
|
192
|
+
let html = "<ul><li>Item 1</li><li>Item 2</li></ul>";
|
|
193
|
+
let result = convert_html_to_markdown(html, None, None).unwrap();
|
|
194
|
+
assert!(result.contains("Item 1"));
|
|
195
|
+
assert!(result.contains("Item 2"));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#[test]
|
|
199
|
+
fn test_html_with_table() {
|
|
200
|
+
let html = "<table><tr><th>Header</th></tr><tr><td>Data</td></tr></table>";
|
|
201
|
+
let result = convert_html_to_markdown(html, None, None).unwrap();
|
|
202
|
+
assert!(result.contains("Header"));
|
|
203
|
+
assert!(result.contains("Data"));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#[test]
|
|
207
|
+
fn test_preprocessing_config() {
|
|
208
|
+
let html = "<nav>Navigation</nav><p>Content</p>";
|
|
209
|
+
let mut options = ConversionOptions::default();
|
|
210
|
+
options.preprocessing.enabled = true;
|
|
211
|
+
options.preprocessing.preset = super::super::types::PreprocessingPreset::Standard;
|
|
212
|
+
options.preprocessing.remove_navigation = true;
|
|
213
|
+
let result = convert_html_to_markdown(html, Some(options), None).unwrap();
|
|
214
|
+
assert!(result.contains("Content"));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
#[test]
|
|
218
|
+
fn test_preprocessing_keeps_main_content() {
|
|
219
|
+
let html = r#"
|
|
220
|
+
<!DOCTYPE html>
|
|
221
|
+
<html>
|
|
222
|
+
<body>
|
|
223
|
+
<nav><p>Skip me</p></nav>
|
|
224
|
+
<main id="content">
|
|
225
|
+
<article>
|
|
226
|
+
<h1>Taylor Swift</h1>
|
|
227
|
+
<p>Taylor Alison Swift is an American singer-songwriter.</p>
|
|
228
|
+
</article>
|
|
229
|
+
</main>
|
|
230
|
+
</body>
|
|
231
|
+
</html>
|
|
232
|
+
"#;
|
|
233
|
+
let markdown = convert_html_to_markdown(html, None, None).expect("conversion failed");
|
|
234
|
+
assert!(markdown.contains("Taylor Alison Swift"), "{markdown}");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/// Test extraction of core document metadata fields:
|
|
238
|
+
/// title, description, author, canonical_url, and base_href.
|
|
239
|
+
#[test]
|
|
240
|
+
fn test_metadata_document_fields() {
|
|
241
|
+
let html = r#"<!DOCTYPE html>
|
|
242
|
+
<html>
|
|
243
|
+
<head>
|
|
244
|
+
<title>Amazing Article</title>
|
|
245
|
+
<meta name="description" content="This is a description of the article">
|
|
246
|
+
<meta name="author" content="Jane Doe">
|
|
247
|
+
<link rel="canonical" href="https://example.com/article/amazing">
|
|
248
|
+
<base href="https://example.com/">
|
|
249
|
+
</head>
|
|
250
|
+
<body>
|
|
251
|
+
<h1>Amazing Article</h1>
|
|
252
|
+
<p>Content here.</p>
|
|
253
|
+
</body>
|
|
254
|
+
</html>"#;
|
|
255
|
+
|
|
256
|
+
let (_, metadata) = convert_html_to_markdown_with_metadata(html, None, None).unwrap();
|
|
257
|
+
let metadata = metadata.expect("metadata should be present");
|
|
258
|
+
|
|
259
|
+
assert_eq!(
|
|
260
|
+
metadata.title,
|
|
261
|
+
Some("Amazing Article".to_string()),
|
|
262
|
+
"Title should be extracted from <title> tag"
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
assert_eq!(
|
|
266
|
+
metadata.description,
|
|
267
|
+
Some("This is a description of the article".to_string()),
|
|
268
|
+
"Description should be extracted from meta description tag"
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
assert_eq!(
|
|
272
|
+
metadata.author,
|
|
273
|
+
Some("Jane Doe".to_string()),
|
|
274
|
+
"Author should be extracted from meta author tag"
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
assert_eq!(
|
|
278
|
+
metadata.canonical_url,
|
|
279
|
+
Some("https://example.com/article/amazing".to_string()),
|
|
280
|
+
"Canonical URL should be extracted from link[rel=canonical]"
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
assert_eq!(
|
|
284
|
+
metadata.base_href,
|
|
285
|
+
Some("https://example.com/".to_string()),
|
|
286
|
+
"Base href should be extracted from <base> tag"
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/// Test metadata extraction from an empty HTML string.
|
|
291
|
+
#[test]
|
|
292
|
+
fn test_metadata_empty_html() {
|
|
293
|
+
let html = "";
|
|
294
|
+
|
|
295
|
+
let (_, metadata) = convert_html_to_markdown_with_metadata(html, None, None).unwrap();
|
|
296
|
+
|
|
297
|
+
assert!(
|
|
298
|
+
metadata.is_none() || metadata.as_ref().unwrap().is_empty(),
|
|
299
|
+
"Empty HTML should return None or empty metadata"
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/// Test that HTML with no metadata tags returns defaults.
|
|
304
|
+
#[test]
|
|
305
|
+
fn test_metadata_no_metadata() {
|
|
306
|
+
let html = r#"<!DOCTYPE html>
|
|
307
|
+
<html>
|
|
308
|
+
<body>
|
|
309
|
+
<h1>Simple Page</h1>
|
|
310
|
+
<p>Just content, no metadata tags.</p>
|
|
311
|
+
</body>
|
|
312
|
+
</html>"#;
|
|
313
|
+
|
|
314
|
+
let (_, metadata) = convert_html_to_markdown_with_metadata(html, None, None).unwrap();
|
|
315
|
+
|
|
316
|
+
if let Some(meta) = metadata {
|
|
317
|
+
assert!(
|
|
318
|
+
meta.title.is_none() || meta.title.is_some(),
|
|
319
|
+
"Title might be extracted from h1 or might be None"
|
|
320
|
+
);
|
|
321
|
+
assert!(meta.open_graph.is_empty(), "Open Graph should be empty with no OG tags");
|
|
322
|
+
assert!(
|
|
323
|
+
meta.twitter_card.is_empty(),
|
|
324
|
+
"Twitter Card should be empty with no Twitter tags"
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/// Test that malformed HTML is handled gracefully without panics.
|
|
330
|
+
#[test]
|
|
331
|
+
fn test_metadata_malformed_html() {
|
|
332
|
+
let html = r#"<!DOCTYPE html>
|
|
333
|
+
<html>
|
|
334
|
+
<head>
|
|
335
|
+
<title>Malformed
|
|
336
|
+
<meta name="author content="No closing quote
|
|
337
|
+
</head>
|
|
338
|
+
<body>
|
|
339
|
+
<h1>Title
|
|
340
|
+
<p>Unclosed paragraph
|
|
341
|
+
<div>Unmatched closing tag</div></div>
|
|
342
|
+
</body>
|
|
343
|
+
</html>"#;
|
|
344
|
+
|
|
345
|
+
let result = convert_html_to_markdown_with_metadata(html, None, None);
|
|
346
|
+
assert!(
|
|
347
|
+
result.is_ok(),
|
|
348
|
+
"Malformed HTML should be handled gracefully without error"
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
let (_, metadata) = result.unwrap();
|
|
352
|
+
assert!(
|
|
353
|
+
metadata.is_some() || metadata.is_none(),
|
|
354
|
+
"Should return either Some or None metadata"
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/// Test handling of special characters and HTML entities in metadata values.
|
|
359
|
+
#[test]
|
|
360
|
+
fn test_metadata_special_characters() {
|
|
361
|
+
let html = r#"<!DOCTYPE html>
|
|
362
|
+
<html>
|
|
363
|
+
<head>
|
|
364
|
+
<title>Café & Restaurant "Guide"</title>
|
|
365
|
+
<meta name="description" content="5 stars ★★★★★ < 50% off">
|
|
366
|
+
<meta name="author" content="José García-López">
|
|
367
|
+
<meta property="og:title" content="Quote "Special" & Characters">
|
|
368
|
+
</head>
|
|
369
|
+
<body>
|
|
370
|
+
<h1>Article Title © 2024</h1>
|
|
371
|
+
</body>
|
|
372
|
+
</html>"#;
|
|
373
|
+
|
|
374
|
+
let (_, metadata) = convert_html_to_markdown_with_metadata(html, None, None).unwrap();
|
|
375
|
+
let metadata = metadata.expect("metadata should be present");
|
|
376
|
+
|
|
377
|
+
if let Some(title) = &metadata.title {
|
|
378
|
+
assert!(!title.is_empty(), "Title should be extracted and decoded");
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if let Some(author) = &metadata.author {
|
|
382
|
+
assert!(
|
|
383
|
+
author.contains("García") || author.contains("Jose"),
|
|
384
|
+
"Special characters should be handled correctly"
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if let Some(desc) = &metadata.description {
|
|
389
|
+
assert!(!desc.is_empty(), "Description should be extracted");
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/// Test handling of duplicate meta tags (last value should win or all collected).
|
|
394
|
+
#[test]
|
|
395
|
+
fn test_metadata_duplicate_tags() {
|
|
396
|
+
let html = r#"<!DOCTYPE html>
|
|
397
|
+
<html>
|
|
398
|
+
<head>
|
|
399
|
+
<title>First Title</title>
|
|
400
|
+
<meta name="description" content="First description">
|
|
401
|
+
<meta name="description" content="Second description (should override)">
|
|
402
|
+
<meta name="author" content="Author One">
|
|
403
|
+
<meta name="author" content="Author Two">
|
|
404
|
+
</head>
|
|
405
|
+
<body>
|
|
406
|
+
<p>Content</p>
|
|
407
|
+
</body>
|
|
408
|
+
</html>"#;
|
|
409
|
+
|
|
410
|
+
let (_, metadata) = convert_html_to_markdown_with_metadata(html, None, None).unwrap();
|
|
411
|
+
let metadata = metadata.expect("metadata should be present");
|
|
412
|
+
|
|
413
|
+
if let Some(title) = &metadata.title {
|
|
414
|
+
assert_eq!(
|
|
415
|
+
title, "First Title",
|
|
416
|
+
"Title should be the single value from first title tag"
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if let Some(description) = &metadata.description {
|
|
421
|
+
assert!(
|
|
422
|
+
!description.is_empty(),
|
|
423
|
+
"Description should be populated even with duplicates"
|
|
424
|
+
);
|
|
425
|
+
assert!(
|
|
426
|
+
description.contains("First") || description.contains("Second"),
|
|
427
|
+
"Description should contain one of the duplicate values"
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/// Test graceful handling of malformed JSON-LD structured data
|
|
433
|
+
/// Validates that invalid JSON in script type="application/ld+json"
|
|
434
|
+
/// does not cause panics and is skipped gracefully.
|
|
435
|
+
#[test]
|
|
436
|
+
fn test_malformed_json_ld_graceful_handling() {
|
|
437
|
+
let html = r#"<!DOCTYPE html>
|
|
438
|
+
<html>
|
|
439
|
+
<head>
|
|
440
|
+
<title>Malformed JSON-LD Test</title>
|
|
441
|
+
<script type="application/ld+json">
|
|
442
|
+
{
|
|
443
|
+
"@context": "https://schema.org",
|
|
444
|
+
"@type": "Article",
|
|
445
|
+
"headline": "Test Article",
|
|
446
|
+
"author": "John Doe"
|
|
447
|
+
"datePublished": "2024-01-01"
|
|
448
|
+
}
|
|
449
|
+
</script>
|
|
450
|
+
</head>
|
|
451
|
+
<body>
|
|
452
|
+
<h1>Article Title</h1>
|
|
453
|
+
<p>This HTML contains invalid JSON-LD (missing comma after author field)</p>
|
|
454
|
+
</body>
|
|
455
|
+
</html>"#;
|
|
456
|
+
|
|
457
|
+
let result = convert_html_to_markdown_with_metadata(html, None, None);
|
|
458
|
+
|
|
459
|
+
assert!(
|
|
460
|
+
result.is_ok(),
|
|
461
|
+
"Malformed JSON-LD should not cause panic. Error: {:?}",
|
|
462
|
+
result.err()
|
|
463
|
+
);
|
|
464
|
+
|
|
465
|
+
let (markdown, metadata) = result.unwrap();
|
|
466
|
+
|
|
467
|
+
assert!(
|
|
468
|
+
!markdown.is_empty(),
|
|
469
|
+
"Markdown should be extracted despite invalid JSON-LD"
|
|
470
|
+
);
|
|
471
|
+
assert!(
|
|
472
|
+
markdown.contains("Article Title") || markdown.contains("Article"),
|
|
473
|
+
"Content should be properly converted to Markdown"
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
if let Some(meta) = metadata {
|
|
477
|
+
assert_eq!(
|
|
478
|
+
meta.title,
|
|
479
|
+
Some("Malformed JSON-LD Test".to_string()),
|
|
480
|
+
"Document metadata should be extracted from tags"
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/// Test XSS sanitization in metadata fields
|
|
486
|
+
/// Validates that script tags and malicious content in metadata
|
|
487
|
+
/// are properly handled and don't cause panics.
|
|
488
|
+
/// Note: The actual sanitization is done by the html-to-markdown-rs library,
|
|
489
|
+
/// which may escape, strip, or preserve content depending on context.
|
|
490
|
+
#[test]
|
|
491
|
+
fn test_metadata_xss_sanitization() {
|
|
492
|
+
let html = r#"<!DOCTYPE html>
|
|
493
|
+
<html>
|
|
494
|
+
<head>
|
|
495
|
+
<title>Safe Title <script>alert('xss')</script></title>
|
|
496
|
+
<meta name="description" content="Description with encoded content">
|
|
497
|
+
<meta name="author" content="Author Name">
|
|
498
|
+
<meta property="og:title" content="OG Title">
|
|
499
|
+
<meta property="og:description" content="OG Description">
|
|
500
|
+
</head>
|
|
501
|
+
<body>
|
|
502
|
+
<h1>Title Section</h1>
|
|
503
|
+
<p>Content here</p>
|
|
504
|
+
</body>
|
|
505
|
+
</html>"#;
|
|
506
|
+
|
|
507
|
+
let result = convert_html_to_markdown_with_metadata(html, None, None);
|
|
508
|
+
assert!(
|
|
509
|
+
result.is_ok(),
|
|
510
|
+
"HTML with script-like content should not cause error. Error: {:?}",
|
|
511
|
+
result.err()
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
let (markdown, metadata) = result.unwrap();
|
|
515
|
+
|
|
516
|
+
assert!(!markdown.is_empty(), "Markdown should be generated");
|
|
517
|
+
|
|
518
|
+
if let Some(meta) = metadata {
|
|
519
|
+
if let Some(title) = &meta.title {
|
|
520
|
+
assert!(!title.is_empty(), "Title should be extracted");
|
|
521
|
+
assert!(
|
|
522
|
+
title.contains("Safe") || title.contains("script"),
|
|
523
|
+
"Title should extract content from title tag: {}",
|
|
524
|
+
title
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if let Some(desc) = &meta.description {
|
|
529
|
+
assert!(!desc.is_empty(), "Description should be extracted");
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if let Some(author) = &meta.author {
|
|
533
|
+
assert_eq!(author, "Author Name", "Author should be correctly extracted");
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if !meta.open_graph.is_empty() {
|
|
537
|
+
let og_count = meta.open_graph.len();
|
|
538
|
+
assert!(og_count > 0, "Open Graph tags should be extracted");
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
#[test]
|
|
544
|
+
fn test_convert_html_to_djot() {
|
|
545
|
+
use crate::core::config::OutputFormat;
|
|
546
|
+
|
|
547
|
+
let html = "<h1>Hello World</h1><p>This is a test.</p>";
|
|
548
|
+
let result = convert_html_to_markdown(html, None, Some(OutputFormat::Djot)).unwrap();
|
|
549
|
+
assert!(result.contains("# Hello World"));
|
|
550
|
+
assert!(result.contains("This is a test."));
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
#[test]
|
|
554
|
+
fn test_convert_html_to_djot_with_emphasis() {
|
|
555
|
+
use crate::core::config::OutputFormat;
|
|
556
|
+
|
|
557
|
+
let html = "<p>This is <strong>bold</strong> and <em>italic</em>.</p>";
|
|
558
|
+
let result = convert_html_to_markdown(html, None, Some(OutputFormat::Djot)).unwrap();
|
|
559
|
+
// Djot uses * for strong, _ for emphasis
|
|
560
|
+
assert!(result.contains("*bold*"));
|
|
561
|
+
assert!(result.contains("_italic_"));
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
#[test]
|
|
565
|
+
fn test_convert_html_with_metadata_djot() {
|
|
566
|
+
use crate::core::config::OutputFormat;
|
|
567
|
+
|
|
568
|
+
let html = r#"<!DOCTYPE html>
|
|
569
|
+
<html>
|
|
570
|
+
<head>
|
|
571
|
+
<title>Test Page</title>
|
|
572
|
+
<meta name="description" content="Test description">
|
|
573
|
+
</head>
|
|
574
|
+
<body>
|
|
575
|
+
<h1>Content</h1>
|
|
576
|
+
<p>This is <strong>content</strong>.</p>
|
|
577
|
+
</body>
|
|
578
|
+
</html>"#;
|
|
579
|
+
|
|
580
|
+
let (content, metadata) = convert_html_to_markdown_with_metadata(html, None, Some(OutputFormat::Djot)).unwrap();
|
|
581
|
+
|
|
582
|
+
// Content should be in djot format
|
|
583
|
+
assert!(content.contains("# Content"));
|
|
584
|
+
assert!(content.contains("*content*")); // Djot strong syntax
|
|
585
|
+
|
|
586
|
+
// Metadata should still be extracted
|
|
587
|
+
assert!(metadata.is_some());
|
|
588
|
+
let meta = metadata.unwrap();
|
|
589
|
+
assert_eq!(meta.title, Some("Test Page".to_string()));
|
|
590
|
+
assert_eq!(meta.description, Some("Test description".to_string()));
|
|
591
|
+
}
|
|
592
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
//! Image handling and conversion functionality for HTML extraction.
|
|
2
|
+
|
|
3
|
+
use super::types::ExtractedInlineImage;
|
|
4
|
+
use html_to_markdown_rs::{InlineImage, InlineImageFormat};
|
|
5
|
+
|
|
6
|
+
/// Convert InlineImageFormat to a standardized string representation.
|
|
7
|
+
///
|
|
8
|
+
/// Handles special cases for custom formats, extracting meaningful file extensions
|
|
9
|
+
/// and normalizing MIME types.
|
|
10
|
+
pub fn inline_image_format_to_str(format: &InlineImageFormat) -> String {
|
|
11
|
+
match format {
|
|
12
|
+
InlineImageFormat::Png => "png".to_string(),
|
|
13
|
+
InlineImageFormat::Jpeg => "jpeg".to_string(),
|
|
14
|
+
InlineImageFormat::Gif => "gif".to_string(),
|
|
15
|
+
InlineImageFormat::Bmp => "bmp".to_string(),
|
|
16
|
+
InlineImageFormat::Webp => "webp".to_string(),
|
|
17
|
+
InlineImageFormat::Svg => "svg".to_string(),
|
|
18
|
+
InlineImageFormat::Other(custom) => {
|
|
19
|
+
let trimmed = custom.trim();
|
|
20
|
+
if trimmed.is_empty() {
|
|
21
|
+
return "bin".to_string();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let lower = trimmed.to_ascii_lowercase();
|
|
25
|
+
if lower.starts_with("svg") {
|
|
26
|
+
return "svg".to_string();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let mut result = String::with_capacity(10);
|
|
30
|
+
let mut candidate = lower.as_str();
|
|
31
|
+
|
|
32
|
+
if let Some(idx) = candidate.find(['+', ';']) {
|
|
33
|
+
candidate = &candidate[..idx];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if let Some(idx) = candidate.rfind('.') {
|
|
37
|
+
candidate = &candidate[idx + 1..];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
candidate = candidate.trim_start_matches("x-");
|
|
41
|
+
|
|
42
|
+
if candidate.is_empty() {
|
|
43
|
+
"bin".to_string()
|
|
44
|
+
} else {
|
|
45
|
+
result.push_str(candidate);
|
|
46
|
+
result
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Convert a library InlineImage to an ExtractedInlineImage.
|
|
53
|
+
///
|
|
54
|
+
/// Maps the library's image representation to the extraction API's format,
|
|
55
|
+
/// converting the format enum to a string representation.
|
|
56
|
+
pub fn inline_image_to_extracted(image: InlineImage) -> ExtractedInlineImage {
|
|
57
|
+
ExtractedInlineImage {
|
|
58
|
+
data: image.data,
|
|
59
|
+
format: inline_image_format_to_str(&image.format),
|
|
60
|
+
filename: image.filename,
|
|
61
|
+
description: image.description,
|
|
62
|
+
dimensions: image.dimensions,
|
|
63
|
+
attributes: image.attributes.into_iter().collect(),
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#[cfg(test)]
|
|
68
|
+
mod tests {
|
|
69
|
+
use super::*;
|
|
70
|
+
|
|
71
|
+
#[test]
|
|
72
|
+
fn test_inline_image_format_conversion() {
|
|
73
|
+
assert_eq!(inline_image_format_to_str(&InlineImageFormat::Png), "png");
|
|
74
|
+
assert_eq!(inline_image_format_to_str(&InlineImageFormat::Jpeg), "jpeg");
|
|
75
|
+
assert_eq!(inline_image_format_to_str(&InlineImageFormat::Svg), "svg");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#[test]
|
|
79
|
+
fn test_inline_image_format_other_with_extension() {
|
|
80
|
+
let format = InlineImageFormat::Other("image/x-custom.jpg".to_string());
|
|
81
|
+
assert_eq!(inline_image_format_to_str(&format), "jpg");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#[test]
|
|
85
|
+
fn test_inline_image_format_other_empty() {
|
|
86
|
+
let format = InlineImageFormat::Other("".to_string());
|
|
87
|
+
assert_eq!(inline_image_format_to_str(&format), "bin");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[test]
|
|
91
|
+
fn test_inline_image_format_other_x_prefix() {
|
|
92
|
+
let format = InlineImageFormat::Other("x-custom".to_string());
|
|
93
|
+
assert_eq!(inline_image_format_to_str(&format), "custom");
|
|
94
|
+
}
|
|
95
|
+
}
|