kreuzberg 4.0.0.pre.rc.6 → 4.0.0.rc1
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/.gitignore +0 -6
- data/.rubocop.yaml +534 -1
- data/Gemfile +2 -1
- data/Gemfile.lock +11 -11
- data/README.md +5 -10
- data/examples/async_patterns.rb +0 -1
- data/ext/kreuzberg_rb/extconf.rb +0 -10
- data/ext/kreuzberg_rb/native/Cargo.toml +15 -23
- data/ext/kreuzberg_rb/native/build.rs +2 -0
- data/ext/kreuzberg_rb/native/include/ieeefp.h +1 -1
- data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +1 -1
- data/ext/kreuzberg_rb/native/include/strings.h +2 -2
- data/ext/kreuzberg_rb/native/include/unistd.h +1 -1
- data/ext/kreuzberg_rb/native/src/lib.rs +16 -75
- data/kreuzberg.gemspec +14 -57
- data/lib/kreuzberg/cache_api.rb +0 -1
- data/lib/kreuzberg/cli.rb +2 -2
- data/lib/kreuzberg/config.rb +2 -9
- data/lib/kreuzberg/errors.rb +7 -75
- data/lib/kreuzberg/extraction_api.rb +0 -1
- data/lib/kreuzberg/setup_lib_path.rb +0 -1
- data/lib/kreuzberg/version.rb +1 -1
- data/lib/kreuzberg.rb +0 -21
- data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
- data/sig/kreuzberg.rbs +3 -55
- data/spec/binding/cli_proxy_spec.rb +4 -2
- data/spec/binding/cli_spec.rb +11 -12
- data/spec/examples.txt +104 -0
- data/spec/fixtures/config.yaml +1 -0
- data/spec/spec_helper.rb +1 -1
- data/vendor/kreuzberg/Cargo.toml +42 -112
- data/vendor/kreuzberg/README.md +2 -2
- data/vendor/kreuzberg/build.rs +4 -18
- data/vendor/kreuzberg/src/bin/profile_extract.rs +455 -0
- data/vendor/kreuzberg/src/cache/mod.rs +3 -27
- data/vendor/kreuzberg/src/core/batch_mode.rs +0 -60
- data/vendor/kreuzberg/src/core/extractor.rs +81 -202
- data/vendor/kreuzberg/src/core/io.rs +2 -4
- data/vendor/kreuzberg/src/core/mime.rs +12 -2
- data/vendor/kreuzberg/src/core/mod.rs +1 -4
- data/vendor/kreuzberg/src/core/pipeline.rs +33 -111
- data/vendor/kreuzberg/src/embeddings.rs +16 -125
- data/vendor/kreuzberg/src/error.rs +1 -1
- data/vendor/kreuzberg/src/extraction/docx.rs +1 -1
- data/vendor/kreuzberg/src/extraction/image.rs +13 -13
- data/vendor/kreuzberg/src/extraction/libreoffice.rs +1 -0
- data/vendor/kreuzberg/src/extraction/mod.rs +5 -9
- data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +0 -2
- data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +275 -0
- data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +178 -0
- data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +491 -0
- data/vendor/kreuzberg/src/extraction/pandoc/server.rs +496 -0
- data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +1188 -0
- data/vendor/kreuzberg/src/extraction/pandoc/version.rs +162 -0
- data/vendor/kreuzberg/src/extractors/archive.rs +0 -21
- data/vendor/kreuzberg/src/extractors/docx.rs +128 -16
- data/vendor/kreuzberg/src/extractors/email.rs +0 -14
- data/vendor/kreuzberg/src/extractors/excel.rs +20 -19
- data/vendor/kreuzberg/src/extractors/html.rs +154 -137
- data/vendor/kreuzberg/src/extractors/image.rs +4 -7
- data/vendor/kreuzberg/src/extractors/mod.rs +9 -106
- data/vendor/kreuzberg/src/extractors/pandoc.rs +201 -0
- data/vendor/kreuzberg/src/extractors/pdf.rs +15 -12
- data/vendor/kreuzberg/src/extractors/pptx.rs +3 -17
- data/vendor/kreuzberg/src/extractors/structured.rs +0 -14
- data/vendor/kreuzberg/src/extractors/text.rs +5 -23
- data/vendor/kreuzberg/src/extractors/xml.rs +0 -7
- data/vendor/kreuzberg/src/keywords/rake.rs +1 -0
- data/vendor/kreuzberg/src/lib.rs +1 -4
- data/vendor/kreuzberg/src/mcp/mod.rs +1 -1
- data/vendor/kreuzberg/src/mcp/server.rs +3 -5
- data/vendor/kreuzberg/src/ocr/processor.rs +2 -18
- data/vendor/kreuzberg/src/pdf/error.rs +1 -1
- data/vendor/kreuzberg/src/pdf/table.rs +44 -17
- data/vendor/kreuzberg/src/pdf/text.rs +3 -0
- data/vendor/kreuzberg/src/plugins/extractor.rs +5 -8
- data/vendor/kreuzberg/src/plugins/ocr.rs +11 -2
- data/vendor/kreuzberg/src/plugins/processor.rs +1 -2
- data/vendor/kreuzberg/src/plugins/registry.rs +0 -13
- data/vendor/kreuzberg/src/plugins/validator.rs +8 -9
- data/vendor/kreuzberg/src/stopwords/mod.rs +2 -2
- data/vendor/kreuzberg/src/types.rs +12 -42
- data/vendor/kreuzberg/tests/batch_orchestration.rs +5 -19
- data/vendor/kreuzberg/tests/batch_processing.rs +3 -15
- data/vendor/kreuzberg/tests/chunking_offset_demo.rs +92 -0
- data/vendor/kreuzberg/tests/concurrency_stress.rs +1 -17
- data/vendor/kreuzberg/tests/config_features.rs +0 -18
- data/vendor/kreuzberg/tests/config_loading_tests.rs +39 -15
- data/vendor/kreuzberg/tests/core_integration.rs +7 -24
- data/vendor/kreuzberg/tests/csv_integration.rs +81 -71
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +25 -23
- data/vendor/kreuzberg/tests/pandoc_integration.rs +503 -0
- data/vendor/kreuzberg/tests/pipeline_integration.rs +1 -0
- data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +1 -0
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +22 -1
- data/vendor/kreuzberg/tests/security_validation.rs +1 -12
- metadata +25 -90
- data/.rubocop.yml +0 -538
- data/ext/kreuzberg_rb/native/Cargo.lock +0 -6535
- data/lib/kreuzberg/error_context.rb +0 -32
- data/vendor/kreuzberg/benches/otel_overhead.rs +0 -48
- data/vendor/kreuzberg/src/extraction/markdown.rs +0 -213
- data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +0 -287
- data/vendor/kreuzberg/src/extractors/bibtex.rs +0 -469
- data/vendor/kreuzberg/src/extractors/docbook.rs +0 -502
- data/vendor/kreuzberg/src/extractors/epub.rs +0 -707
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +0 -491
- data/vendor/kreuzberg/src/extractors/fictionbook.rs.backup2 +0 -738
- data/vendor/kreuzberg/src/extractors/jats.rs +0 -1051
- data/vendor/kreuzberg/src/extractors/jupyter.rs +0 -367
- data/vendor/kreuzberg/src/extractors/latex.rs +0 -652
- data/vendor/kreuzberg/src/extractors/markdown.rs +0 -700
- data/vendor/kreuzberg/src/extractors/odt.rs +0 -628
- data/vendor/kreuzberg/src/extractors/opml.rs +0 -634
- data/vendor/kreuzberg/src/extractors/orgmode.rs +0 -528
- data/vendor/kreuzberg/src/extractors/rst.rs +0 -576
- data/vendor/kreuzberg/src/extractors/rtf.rs +0 -810
- data/vendor/kreuzberg/src/extractors/security.rs +0 -484
- data/vendor/kreuzberg/src/extractors/security_tests.rs +0 -367
- data/vendor/kreuzberg/src/extractors/typst.rs +0 -650
- data/vendor/kreuzberg/src/panic_context.rs +0 -154
- data/vendor/kreuzberg/tests/api_extract_multipart.rs +0 -52
- data/vendor/kreuzberg/tests/bibtex_parity_test.rs +0 -421
- data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +0 -498
- data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +0 -370
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +0 -275
- data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +0 -228
- data/vendor/kreuzberg/tests/html_table_test.rs +0 -551
- data/vendor/kreuzberg/tests/instrumentation_test.rs +0 -139
- data/vendor/kreuzberg/tests/jats_extractor_tests.rs +0 -639
- data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +0 -704
- data/vendor/kreuzberg/tests/latex_extractor_tests.rs +0 -496
- data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +0 -490
- data/vendor/kreuzberg/tests/odt_extractor_tests.rs +0 -695
- data/vendor/kreuzberg/tests/opml_extractor_tests.rs +0 -616
- data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +0 -822
- data/vendor/kreuzberg/tests/rst_extractor_tests.rs +0 -692
- data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +0 -776
- data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +0 -1259
- data/vendor/kreuzberg/tests/typst_extractor_tests.rs +0 -647
- data/vendor/rb-sys/.cargo-ok +0 -1
- data/vendor/rb-sys/.cargo_vcs_info.json +0 -6
- data/vendor/rb-sys/Cargo.lock +0 -393
- data/vendor/rb-sys/Cargo.toml +0 -70
- data/vendor/rb-sys/Cargo.toml.orig +0 -57
- data/vendor/rb-sys/LICENSE-APACHE +0 -190
- data/vendor/rb-sys/LICENSE-MIT +0 -21
- data/vendor/rb-sys/bin/release.sh +0 -21
- data/vendor/rb-sys/build/features.rs +0 -108
- data/vendor/rb-sys/build/main.rs +0 -246
- data/vendor/rb-sys/build/stable_api_config.rs +0 -153
- data/vendor/rb-sys/build/version.rs +0 -48
- data/vendor/rb-sys/readme.md +0 -36
- data/vendor/rb-sys/src/bindings.rs +0 -21
- data/vendor/rb-sys/src/hidden.rs +0 -11
- data/vendor/rb-sys/src/lib.rs +0 -34
- data/vendor/rb-sys/src/macros.rs +0 -371
- data/vendor/rb-sys/src/memory.rs +0 -53
- data/vendor/rb-sys/src/ruby_abi_version.rs +0 -38
- data/vendor/rb-sys/src/special_consts.rs +0 -31
- data/vendor/rb-sys/src/stable_api/compiled.c +0 -179
- data/vendor/rb-sys/src/stable_api/compiled.rs +0 -257
- data/vendor/rb-sys/src/stable_api/ruby_2_6.rs +0 -316
- data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +0 -316
- data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +0 -324
- data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +0 -317
- data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +0 -315
- data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +0 -326
- data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +0 -327
- data/vendor/rb-sys/src/stable_api.rs +0 -261
- data/vendor/rb-sys/src/symbol.rs +0 -31
- data/vendor/rb-sys/src/tracking_allocator.rs +0 -332
- data/vendor/rb-sys/src/utils.rs +0 -89
- data/vendor/rb-sys/src/value_type.rs +0 -7
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
//! Jupyter Notebook extractor for .ipynb files.
|
|
2
|
-
//!
|
|
3
|
-
//! This extractor provides native Rust parsing of Jupyter notebooks,
|
|
4
|
-
//! extracting:
|
|
5
|
-
//! - Notebook metadata (kernelspec, language_info, nbformat)
|
|
6
|
-
//! - Cell content (markdown and code cells in order)
|
|
7
|
-
//! - Cell outputs (text, HTML, images)
|
|
8
|
-
//! - Cell metadata (execution_count, tags)
|
|
9
|
-
//!
|
|
10
|
-
//! Requires the `office` feature.
|
|
11
|
-
|
|
12
|
-
#[cfg(feature = "office")]
|
|
13
|
-
use crate::Result;
|
|
14
|
-
#[cfg(feature = "office")]
|
|
15
|
-
use crate::core::config::ExtractionConfig;
|
|
16
|
-
#[cfg(feature = "office")]
|
|
17
|
-
use crate::plugins::{DocumentExtractor, Plugin};
|
|
18
|
-
#[cfg(feature = "office")]
|
|
19
|
-
use crate::types::{ExtractionResult, Metadata};
|
|
20
|
-
#[cfg(feature = "office")]
|
|
21
|
-
use async_trait::async_trait;
|
|
22
|
-
#[cfg(feature = "office")]
|
|
23
|
-
use serde_json::{Value, json};
|
|
24
|
-
#[cfg(feature = "office")]
|
|
25
|
-
use std::collections::HashMap;
|
|
26
|
-
|
|
27
|
-
/// Jupyter Notebook extractor.
|
|
28
|
-
///
|
|
29
|
-
/// Extracts content from Jupyter notebook JSON files, including:
|
|
30
|
-
/// - Notebook metadata (kernel, language, nbformat version)
|
|
31
|
-
/// - Cell content (code and markdown)
|
|
32
|
-
/// - Cell outputs (text, HTML, etc.)
|
|
33
|
-
/// - Cell-level metadata (tags, execution counts)
|
|
34
|
-
#[cfg(feature = "office")]
|
|
35
|
-
pub struct JupyterExtractor;
|
|
36
|
-
|
|
37
|
-
#[cfg(feature = "office")]
|
|
38
|
-
impl JupyterExtractor {
|
|
39
|
-
/// Create a new Jupyter extractor.
|
|
40
|
-
pub fn new() -> Self {
|
|
41
|
-
Self
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/// Extract content from a Jupyter notebook.
|
|
45
|
-
fn extract_notebook(content: &[u8]) -> Result<(String, HashMap<String, Value>)> {
|
|
46
|
-
let notebook: Value = serde_json::from_slice(content)
|
|
47
|
-
.map_err(|e| crate::KreuzbergError::parsing(format!("Failed to parse JSON: {}", e)))?;
|
|
48
|
-
|
|
49
|
-
let mut extracted_content = String::new();
|
|
50
|
-
let mut metadata = HashMap::new();
|
|
51
|
-
|
|
52
|
-
if let Some(notebook_metadata) = notebook.get("metadata").and_then(|m| m.as_object()) {
|
|
53
|
-
if let Some(kernelspec) = notebook_metadata.get("kernelspec")
|
|
54
|
-
&& let Some(name) = kernelspec.get("name").and_then(|n| n.as_str())
|
|
55
|
-
{
|
|
56
|
-
extracted_content.push_str(&format!("Kernelspec: {}\n", name));
|
|
57
|
-
metadata.insert("kernelspec".to_string(), kernelspec.clone());
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if let Some(language_info) = notebook_metadata.get("language_info")
|
|
61
|
-
&& let Some(name) = language_info.get("name").and_then(|n| n.as_str())
|
|
62
|
-
{
|
|
63
|
-
extracted_content.push_str(&format!("Language: {}\n", name));
|
|
64
|
-
metadata.insert("language_info".to_string(), language_info.clone());
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if let Some(nbformat) = notebook.get("nbformat") {
|
|
69
|
-
extracted_content.push_str(&format!("NBFormat: {}\n", nbformat));
|
|
70
|
-
metadata.insert("nbformat".to_string(), nbformat.clone());
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
extracted_content.push('\n');
|
|
74
|
-
|
|
75
|
-
if let Some(cells) = notebook.get("cells").and_then(|c| c.as_array()) {
|
|
76
|
-
for (cell_idx, cell) in cells.iter().enumerate() {
|
|
77
|
-
Self::extract_cell(cell, cell_idx, &mut extracted_content, &mut metadata)?;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
Ok((extracted_content, metadata))
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/// Extract content from a single cell.
|
|
85
|
-
fn extract_cell(
|
|
86
|
-
cell: &Value,
|
|
87
|
-
cell_idx: usize,
|
|
88
|
-
content: &mut String,
|
|
89
|
-
_metadata: &mut HashMap<String, Value>,
|
|
90
|
-
) -> Result<()> {
|
|
91
|
-
let cell_type = cell.get("cell_type").and_then(|t| t.as_str()).unwrap_or("unknown");
|
|
92
|
-
|
|
93
|
-
let cell_id = cell.get("id").and_then(|id| id.as_str());
|
|
94
|
-
|
|
95
|
-
if let Some(id) = cell_id {
|
|
96
|
-
content.push_str(&format!(":::: {{#{} .cell .{}}}\n", id, cell_type));
|
|
97
|
-
} else {
|
|
98
|
-
content.push_str(&format!(":::: {{#cell_{} .cell .{}}}\n", cell_idx, cell_type));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if let Some(cell_metadata) = cell.get("metadata").and_then(|m| m.as_object())
|
|
102
|
-
&& let Some(tags) = cell_metadata.get("tags").and_then(|t| t.as_array())
|
|
103
|
-
{
|
|
104
|
-
let tag_strs: Vec<String> = tags
|
|
105
|
-
.iter()
|
|
106
|
-
.filter_map(|tag| tag.as_str().map(|s| s.to_string()))
|
|
107
|
-
.collect();
|
|
108
|
-
if !tag_strs.is_empty() {
|
|
109
|
-
content.push_str(&format!(" tags=[{}]", tag_strs.join(", ")));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
content.push('\n');
|
|
113
|
-
|
|
114
|
-
match cell_type {
|
|
115
|
-
"markdown" => Self::extract_markdown_cell(cell, content)?,
|
|
116
|
-
"code" => Self::extract_code_cell(cell, content)?,
|
|
117
|
-
"raw" => Self::extract_raw_cell(cell, content)?,
|
|
118
|
-
_ => {
|
|
119
|
-
content.push_str(&format!("Unknown cell type: {}\n", cell_type));
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
content.push_str("::::\n\n");
|
|
124
|
-
Ok(())
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/// Extract markdown cell content.
|
|
128
|
-
fn extract_markdown_cell(cell: &Value, content: &mut String) -> Result<()> {
|
|
129
|
-
if let Some(source) = cell.get("source") {
|
|
130
|
-
let cell_text = Self::extract_source(source);
|
|
131
|
-
content.push_str(&cell_text);
|
|
132
|
-
}
|
|
133
|
-
Ok(())
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/// Extract code cell content and outputs.
|
|
137
|
-
fn extract_code_cell(cell: &Value, content: &mut String) -> Result<()> {
|
|
138
|
-
if let Some(exec_count) = cell.get("execution_count")
|
|
139
|
-
&& !exec_count.is_null()
|
|
140
|
-
{
|
|
141
|
-
content.push_str(&format!("::: {{execution_count={}}}\n", exec_count));
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if let Some(source) = cell.get("source") {
|
|
145
|
-
let cell_text = Self::extract_source(source);
|
|
146
|
-
content.push_str("```python\n");
|
|
147
|
-
content.push_str(&cell_text);
|
|
148
|
-
content.push_str("```\n");
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if let Some(outputs) = cell.get("outputs").and_then(|o| o.as_array()) {
|
|
152
|
-
for output in outputs {
|
|
153
|
-
Self::extract_output(output, content)?;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
Ok(())
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/// Extract raw cell content.
|
|
161
|
-
fn extract_raw_cell(cell: &Value, content: &mut String) -> Result<()> {
|
|
162
|
-
if let Some(source) = cell.get("source") {
|
|
163
|
-
let cell_text = Self::extract_source(source);
|
|
164
|
-
content.push_str(&cell_text);
|
|
165
|
-
}
|
|
166
|
-
Ok(())
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/// Extract source content from various formats.
|
|
170
|
-
///
|
|
171
|
-
/// Source can be either a string or an array of strings.
|
|
172
|
-
fn extract_source(source: &Value) -> String {
|
|
173
|
-
match source {
|
|
174
|
-
Value::String(s) => s.clone(),
|
|
175
|
-
Value::Array(arr) => arr.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>().join(""),
|
|
176
|
-
_ => String::new(),
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/// Extract output from a cell.
|
|
181
|
-
fn extract_output(output: &Value, content: &mut String) -> Result<()> {
|
|
182
|
-
let output_type = output.get("output_type").and_then(|t| t.as_str()).unwrap_or("unknown");
|
|
183
|
-
|
|
184
|
-
content.push_str(&format!("::: {{.output .{}", output_type));
|
|
185
|
-
|
|
186
|
-
if let Some(exec_count) = output.get("execution_count")
|
|
187
|
-
&& !exec_count.is_null()
|
|
188
|
-
{
|
|
189
|
-
content.push_str(&format!(" execution_count={}", exec_count));
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
content.push_str("}\n");
|
|
193
|
-
|
|
194
|
-
match output_type {
|
|
195
|
-
"stream" => Self::extract_stream_output(output, content)?,
|
|
196
|
-
"execute_result" | "display_data" => Self::extract_data_output(output, content)?,
|
|
197
|
-
"error" => Self::extract_error_output(output, content)?,
|
|
198
|
-
_ => {
|
|
199
|
-
content.push_str(&format!("Unknown output type: {}\n", output_type));
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
content.push_str(":::\n");
|
|
204
|
-
Ok(())
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/// Extract stream output (stdout, stderr).
|
|
208
|
-
fn extract_stream_output(output: &Value, content: &mut String) -> Result<()> {
|
|
209
|
-
if let Some(name) = output.get("name").and_then(|n| n.as_str()) {
|
|
210
|
-
content.push_str(&format!("Stream: {}\n", name));
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if let Some(text) = output.get("text") {
|
|
214
|
-
let text_content = Self::extract_source(text);
|
|
215
|
-
content.push_str(&text_content);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
Ok(())
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/// Extract data output (execute_result or display_data).
|
|
222
|
-
fn extract_data_output(output: &Value, content: &mut String) -> Result<()> {
|
|
223
|
-
if let Some(data) = output.get("data").and_then(|d| d.as_object()) {
|
|
224
|
-
let mime_types = vec![
|
|
225
|
-
"text/markdown",
|
|
226
|
-
"text/html",
|
|
227
|
-
"image/svg+xml",
|
|
228
|
-
"image/png",
|
|
229
|
-
"image/jpeg",
|
|
230
|
-
"application/json",
|
|
231
|
-
"text/plain",
|
|
232
|
-
];
|
|
233
|
-
|
|
234
|
-
for mime_type in mime_types {
|
|
235
|
-
if let Some(mime_content) = data.get(mime_type) {
|
|
236
|
-
content.push_str(&format!("MIME: {}\n", mime_type));
|
|
237
|
-
let mime_text = Self::extract_source(mime_content);
|
|
238
|
-
if !mime_text.is_empty() {
|
|
239
|
-
content.push_str(&mime_text);
|
|
240
|
-
content.push('\n');
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
Ok(())
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/// Extract error output.
|
|
250
|
-
fn extract_error_output(output: &Value, content: &mut String) -> Result<()> {
|
|
251
|
-
if let Some(ename) = output.get("ename").and_then(|e| e.as_str()) {
|
|
252
|
-
content.push_str(&format!("Error: {}\n", ename));
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if let Some(evalue) = output.get("evalue").and_then(|e| e.as_str()) {
|
|
256
|
-
content.push_str(&format!("Value: {}\n", evalue));
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if let Some(traceback) = output.get("traceback").and_then(|t| t.as_array()) {
|
|
260
|
-
for line in traceback {
|
|
261
|
-
if let Some(line_str) = line.as_str() {
|
|
262
|
-
content.push_str(line_str);
|
|
263
|
-
content.push('\n');
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
Ok(())
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
#[cfg(feature = "office")]
|
|
273
|
-
impl Default for JupyterExtractor {
|
|
274
|
-
fn default() -> Self {
|
|
275
|
-
Self::new()
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
#[cfg(feature = "office")]
|
|
280
|
-
impl Plugin for JupyterExtractor {
|
|
281
|
-
fn name(&self) -> &str {
|
|
282
|
-
"jupyter-extractor"
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
fn version(&self) -> String {
|
|
286
|
-
env!("CARGO_PKG_VERSION").to_string()
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
fn initialize(&self) -> Result<()> {
|
|
290
|
-
Ok(())
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
fn shutdown(&self) -> Result<()> {
|
|
294
|
-
Ok(())
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
fn description(&self) -> &str {
|
|
298
|
-
"Extracts content from Jupyter notebooks (.ipynb files)"
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
fn author(&self) -> &str {
|
|
302
|
-
"Kreuzberg Team"
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
#[cfg(feature = "office")]
|
|
307
|
-
#[async_trait]
|
|
308
|
-
impl DocumentExtractor for JupyterExtractor {
|
|
309
|
-
#[cfg_attr(
|
|
310
|
-
feature = "otel",
|
|
311
|
-
tracing::instrument(
|
|
312
|
-
skip(self, content, _config),
|
|
313
|
-
fields(
|
|
314
|
-
extractor.name = self.name(),
|
|
315
|
-
content.size_bytes = content.len(),
|
|
316
|
-
)
|
|
317
|
-
)
|
|
318
|
-
)]
|
|
319
|
-
async fn extract_bytes(
|
|
320
|
-
&self,
|
|
321
|
-
content: &[u8],
|
|
322
|
-
mime_type: &str,
|
|
323
|
-
_config: &ExtractionConfig,
|
|
324
|
-
) -> Result<ExtractionResult> {
|
|
325
|
-
let (extracted_content, additional_metadata) = Self::extract_notebook(content)?;
|
|
326
|
-
|
|
327
|
-
let mut metadata_additional = HashMap::new();
|
|
328
|
-
for (key, value) in additional_metadata {
|
|
329
|
-
metadata_additional.insert(key, json!(value));
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
Ok(ExtractionResult {
|
|
333
|
-
content: extracted_content,
|
|
334
|
-
mime_type: mime_type.to_string(),
|
|
335
|
-
metadata: Metadata {
|
|
336
|
-
additional: metadata_additional,
|
|
337
|
-
..Default::default()
|
|
338
|
-
},
|
|
339
|
-
tables: vec![],
|
|
340
|
-
detected_languages: None,
|
|
341
|
-
chunks: None,
|
|
342
|
-
images: None,
|
|
343
|
-
})
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
fn supported_mime_types(&self) -> &[&str] {
|
|
347
|
-
&["application/x-ipynb+json"]
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
fn priority(&self) -> i32 {
|
|
351
|
-
50
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
#[cfg(test)]
|
|
356
|
-
mod tests {
|
|
357
|
-
use super::*;
|
|
358
|
-
|
|
359
|
-
#[test]
|
|
360
|
-
fn test_jupyter_extractor_plugin_interface() {
|
|
361
|
-
let extractor = JupyterExtractor::new();
|
|
362
|
-
assert_eq!(extractor.name(), "jupyter-extractor");
|
|
363
|
-
assert_eq!(extractor.version(), env!("CARGO_PKG_VERSION"));
|
|
364
|
-
assert_eq!(extractor.priority(), 50);
|
|
365
|
-
assert!(extractor.supported_mime_types().contains(&"application/x-ipynb+json"));
|
|
366
|
-
}
|
|
367
|
-
}
|