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.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -6
  3. data/.rubocop.yaml +534 -1
  4. data/Gemfile +2 -1
  5. data/Gemfile.lock +11 -11
  6. data/README.md +5 -10
  7. data/examples/async_patterns.rb +0 -1
  8. data/ext/kreuzberg_rb/extconf.rb +0 -10
  9. data/ext/kreuzberg_rb/native/Cargo.toml +15 -23
  10. data/ext/kreuzberg_rb/native/build.rs +2 -0
  11. data/ext/kreuzberg_rb/native/include/ieeefp.h +1 -1
  12. data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +1 -1
  13. data/ext/kreuzberg_rb/native/include/strings.h +2 -2
  14. data/ext/kreuzberg_rb/native/include/unistd.h +1 -1
  15. data/ext/kreuzberg_rb/native/src/lib.rs +16 -75
  16. data/kreuzberg.gemspec +14 -57
  17. data/lib/kreuzberg/cache_api.rb +0 -1
  18. data/lib/kreuzberg/cli.rb +2 -2
  19. data/lib/kreuzberg/config.rb +2 -9
  20. data/lib/kreuzberg/errors.rb +7 -75
  21. data/lib/kreuzberg/extraction_api.rb +0 -1
  22. data/lib/kreuzberg/setup_lib_path.rb +0 -1
  23. data/lib/kreuzberg/version.rb +1 -1
  24. data/lib/kreuzberg.rb +0 -21
  25. data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
  26. data/sig/kreuzberg.rbs +3 -55
  27. data/spec/binding/cli_proxy_spec.rb +4 -2
  28. data/spec/binding/cli_spec.rb +11 -12
  29. data/spec/examples.txt +104 -0
  30. data/spec/fixtures/config.yaml +1 -0
  31. data/spec/spec_helper.rb +1 -1
  32. data/vendor/kreuzberg/Cargo.toml +42 -112
  33. data/vendor/kreuzberg/README.md +2 -2
  34. data/vendor/kreuzberg/build.rs +4 -18
  35. data/vendor/kreuzberg/src/bin/profile_extract.rs +455 -0
  36. data/vendor/kreuzberg/src/cache/mod.rs +3 -27
  37. data/vendor/kreuzberg/src/core/batch_mode.rs +0 -60
  38. data/vendor/kreuzberg/src/core/extractor.rs +81 -202
  39. data/vendor/kreuzberg/src/core/io.rs +2 -4
  40. data/vendor/kreuzberg/src/core/mime.rs +12 -2
  41. data/vendor/kreuzberg/src/core/mod.rs +1 -4
  42. data/vendor/kreuzberg/src/core/pipeline.rs +33 -111
  43. data/vendor/kreuzberg/src/embeddings.rs +16 -125
  44. data/vendor/kreuzberg/src/error.rs +1 -1
  45. data/vendor/kreuzberg/src/extraction/docx.rs +1 -1
  46. data/vendor/kreuzberg/src/extraction/image.rs +13 -13
  47. data/vendor/kreuzberg/src/extraction/libreoffice.rs +1 -0
  48. data/vendor/kreuzberg/src/extraction/mod.rs +5 -9
  49. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +0 -2
  50. data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +275 -0
  51. data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +178 -0
  52. data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +491 -0
  53. data/vendor/kreuzberg/src/extraction/pandoc/server.rs +496 -0
  54. data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +1188 -0
  55. data/vendor/kreuzberg/src/extraction/pandoc/version.rs +162 -0
  56. data/vendor/kreuzberg/src/extractors/archive.rs +0 -21
  57. data/vendor/kreuzberg/src/extractors/docx.rs +128 -16
  58. data/vendor/kreuzberg/src/extractors/email.rs +0 -14
  59. data/vendor/kreuzberg/src/extractors/excel.rs +20 -19
  60. data/vendor/kreuzberg/src/extractors/html.rs +154 -137
  61. data/vendor/kreuzberg/src/extractors/image.rs +4 -7
  62. data/vendor/kreuzberg/src/extractors/mod.rs +9 -106
  63. data/vendor/kreuzberg/src/extractors/pandoc.rs +201 -0
  64. data/vendor/kreuzberg/src/extractors/pdf.rs +15 -12
  65. data/vendor/kreuzberg/src/extractors/pptx.rs +3 -17
  66. data/vendor/kreuzberg/src/extractors/structured.rs +0 -14
  67. data/vendor/kreuzberg/src/extractors/text.rs +5 -23
  68. data/vendor/kreuzberg/src/extractors/xml.rs +0 -7
  69. data/vendor/kreuzberg/src/keywords/rake.rs +1 -0
  70. data/vendor/kreuzberg/src/lib.rs +1 -4
  71. data/vendor/kreuzberg/src/mcp/mod.rs +1 -1
  72. data/vendor/kreuzberg/src/mcp/server.rs +3 -5
  73. data/vendor/kreuzberg/src/ocr/processor.rs +2 -18
  74. data/vendor/kreuzberg/src/pdf/error.rs +1 -1
  75. data/vendor/kreuzberg/src/pdf/table.rs +44 -17
  76. data/vendor/kreuzberg/src/pdf/text.rs +3 -0
  77. data/vendor/kreuzberg/src/plugins/extractor.rs +5 -8
  78. data/vendor/kreuzberg/src/plugins/ocr.rs +11 -2
  79. data/vendor/kreuzberg/src/plugins/processor.rs +1 -2
  80. data/vendor/kreuzberg/src/plugins/registry.rs +0 -13
  81. data/vendor/kreuzberg/src/plugins/validator.rs +8 -9
  82. data/vendor/kreuzberg/src/stopwords/mod.rs +2 -2
  83. data/vendor/kreuzberg/src/types.rs +12 -42
  84. data/vendor/kreuzberg/tests/batch_orchestration.rs +5 -19
  85. data/vendor/kreuzberg/tests/batch_processing.rs +3 -15
  86. data/vendor/kreuzberg/tests/chunking_offset_demo.rs +92 -0
  87. data/vendor/kreuzberg/tests/concurrency_stress.rs +1 -17
  88. data/vendor/kreuzberg/tests/config_features.rs +0 -18
  89. data/vendor/kreuzberg/tests/config_loading_tests.rs +39 -15
  90. data/vendor/kreuzberg/tests/core_integration.rs +7 -24
  91. data/vendor/kreuzberg/tests/csv_integration.rs +81 -71
  92. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +25 -23
  93. data/vendor/kreuzberg/tests/pandoc_integration.rs +503 -0
  94. data/vendor/kreuzberg/tests/pipeline_integration.rs +1 -0
  95. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +1 -0
  96. data/vendor/kreuzberg/tests/registry_integration_tests.rs +22 -1
  97. data/vendor/kreuzberg/tests/security_validation.rs +1 -12
  98. metadata +25 -90
  99. data/.rubocop.yml +0 -538
  100. data/ext/kreuzberg_rb/native/Cargo.lock +0 -6535
  101. data/lib/kreuzberg/error_context.rb +0 -32
  102. data/vendor/kreuzberg/benches/otel_overhead.rs +0 -48
  103. data/vendor/kreuzberg/src/extraction/markdown.rs +0 -213
  104. data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +0 -287
  105. data/vendor/kreuzberg/src/extractors/bibtex.rs +0 -469
  106. data/vendor/kreuzberg/src/extractors/docbook.rs +0 -502
  107. data/vendor/kreuzberg/src/extractors/epub.rs +0 -707
  108. data/vendor/kreuzberg/src/extractors/fictionbook.rs +0 -491
  109. data/vendor/kreuzberg/src/extractors/fictionbook.rs.backup2 +0 -738
  110. data/vendor/kreuzberg/src/extractors/jats.rs +0 -1051
  111. data/vendor/kreuzberg/src/extractors/jupyter.rs +0 -367
  112. data/vendor/kreuzberg/src/extractors/latex.rs +0 -652
  113. data/vendor/kreuzberg/src/extractors/markdown.rs +0 -700
  114. data/vendor/kreuzberg/src/extractors/odt.rs +0 -628
  115. data/vendor/kreuzberg/src/extractors/opml.rs +0 -634
  116. data/vendor/kreuzberg/src/extractors/orgmode.rs +0 -528
  117. data/vendor/kreuzberg/src/extractors/rst.rs +0 -576
  118. data/vendor/kreuzberg/src/extractors/rtf.rs +0 -810
  119. data/vendor/kreuzberg/src/extractors/security.rs +0 -484
  120. data/vendor/kreuzberg/src/extractors/security_tests.rs +0 -367
  121. data/vendor/kreuzberg/src/extractors/typst.rs +0 -650
  122. data/vendor/kreuzberg/src/panic_context.rs +0 -154
  123. data/vendor/kreuzberg/tests/api_extract_multipart.rs +0 -52
  124. data/vendor/kreuzberg/tests/bibtex_parity_test.rs +0 -421
  125. data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +0 -498
  126. data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +0 -370
  127. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +0 -275
  128. data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +0 -228
  129. data/vendor/kreuzberg/tests/html_table_test.rs +0 -551
  130. data/vendor/kreuzberg/tests/instrumentation_test.rs +0 -139
  131. data/vendor/kreuzberg/tests/jats_extractor_tests.rs +0 -639
  132. data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +0 -704
  133. data/vendor/kreuzberg/tests/latex_extractor_tests.rs +0 -496
  134. data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +0 -490
  135. data/vendor/kreuzberg/tests/odt_extractor_tests.rs +0 -695
  136. data/vendor/kreuzberg/tests/opml_extractor_tests.rs +0 -616
  137. data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +0 -822
  138. data/vendor/kreuzberg/tests/rst_extractor_tests.rs +0 -692
  139. data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +0 -776
  140. data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +0 -1259
  141. data/vendor/kreuzberg/tests/typst_extractor_tests.rs +0 -647
  142. data/vendor/rb-sys/.cargo-ok +0 -1
  143. data/vendor/rb-sys/.cargo_vcs_info.json +0 -6
  144. data/vendor/rb-sys/Cargo.lock +0 -393
  145. data/vendor/rb-sys/Cargo.toml +0 -70
  146. data/vendor/rb-sys/Cargo.toml.orig +0 -57
  147. data/vendor/rb-sys/LICENSE-APACHE +0 -190
  148. data/vendor/rb-sys/LICENSE-MIT +0 -21
  149. data/vendor/rb-sys/bin/release.sh +0 -21
  150. data/vendor/rb-sys/build/features.rs +0 -108
  151. data/vendor/rb-sys/build/main.rs +0 -246
  152. data/vendor/rb-sys/build/stable_api_config.rs +0 -153
  153. data/vendor/rb-sys/build/version.rs +0 -48
  154. data/vendor/rb-sys/readme.md +0 -36
  155. data/vendor/rb-sys/src/bindings.rs +0 -21
  156. data/vendor/rb-sys/src/hidden.rs +0 -11
  157. data/vendor/rb-sys/src/lib.rs +0 -34
  158. data/vendor/rb-sys/src/macros.rs +0 -371
  159. data/vendor/rb-sys/src/memory.rs +0 -53
  160. data/vendor/rb-sys/src/ruby_abi_version.rs +0 -38
  161. data/vendor/rb-sys/src/special_consts.rs +0 -31
  162. data/vendor/rb-sys/src/stable_api/compiled.c +0 -179
  163. data/vendor/rb-sys/src/stable_api/compiled.rs +0 -257
  164. data/vendor/rb-sys/src/stable_api/ruby_2_6.rs +0 -316
  165. data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +0 -316
  166. data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +0 -324
  167. data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +0 -317
  168. data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +0 -315
  169. data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +0 -326
  170. data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +0 -327
  171. data/vendor/rb-sys/src/stable_api.rs +0 -261
  172. data/vendor/rb-sys/src/symbol.rs +0 -31
  173. data/vendor/rb-sys/src/tracking_allocator.rs +0 -332
  174. data/vendor/rb-sys/src/utils.rs +0 -89
  175. data/vendor/rb-sys/src/value_type.rs +0 -7
@@ -1,502 +0,0 @@
1
- //! DocBook document extractor supporting both 4.x and 5.x formats.
2
- //!
3
- //! This extractor handles DocBook XML documents in both traditional (4.x, no namespace)
4
- //! and modern (5.x, with http://docbook.org/ns/docbook namespace) formats.
5
- //!
6
- //! Single-pass architecture that extracts in one document traversal:
7
- //! - Document metadata (title, author, date, abstract)
8
- //! - Section hierarchy and content
9
- //! - Paragraphs and text content
10
- //! - Lists (itemizedlist, orderedlist)
11
- //! - Code blocks (programlisting, screen)
12
- //! - Blockquotes
13
- //! - Figures and mediaobjects
14
- //! - Footnotes
15
- //! - Tables
16
- //! - Cross-references and links
17
-
18
- use crate::Result;
19
- use crate::core::config::ExtractionConfig;
20
- use crate::extraction::cells_to_markdown;
21
- use crate::plugins::{DocumentExtractor, Plugin};
22
- use crate::types::{ExtractionResult, Metadata, Table};
23
- use async_trait::async_trait;
24
- use quick_xml::Reader;
25
- use quick_xml::events::Event;
26
- use std::path::Path;
27
-
28
- /// Strip namespace prefix from XML tag names.
29
- /// Converts "{http://docbook.org/ns/docbook}title" to "title"
30
- /// and leaves non-namespaced "title" unchanged.
31
- fn strip_namespace(tag: &str) -> &str {
32
- if tag.starts_with('{')
33
- && let Some(pos) = tag.find('}')
34
- {
35
- return &tag[pos + 1..];
36
- }
37
- tag
38
- }
39
-
40
- /// State machine for tracking nested elements during extraction
41
- #[derive(Debug, Clone, Copy)]
42
- struct ParsingState {
43
- in_info: bool,
44
- in_table: bool,
45
- in_tgroup: bool,
46
- in_thead: bool,
47
- in_tbody: bool,
48
- in_row: bool,
49
- in_list: bool,
50
- in_list_item: bool,
51
- }
52
-
53
- /// DocBook document extractor.
54
- ///
55
- /// Supports both DocBook 4.x (no namespace) and 5.x (with namespace) formats.
56
- pub struct DocbookExtractor;
57
-
58
- impl Default for DocbookExtractor {
59
- fn default() -> Self {
60
- Self::new()
61
- }
62
- }
63
-
64
- impl DocbookExtractor {
65
- pub fn new() -> Self {
66
- Self
67
- }
68
- }
69
-
70
- /// Type alias for DocBook parsing results: (content, title, author, date, tables)
71
- type DocBookParseResult = (String, String, Option<String>, Option<String>, Vec<Table>);
72
-
73
- /// Single-pass DocBook parser that extracts all content in one document traversal.
74
- /// Returns: (content, title, author, date, tables)
75
- fn parse_docbook_single_pass(content: &str) -> Result<DocBookParseResult> {
76
- let mut reader = Reader::from_str(content);
77
- let mut output = String::new();
78
- let mut title = String::new();
79
- let mut author = Option::None;
80
- let mut date = Option::None;
81
- let mut tables = Vec::new();
82
- let mut table_index = 0;
83
-
84
- let mut state = ParsingState {
85
- in_info: false,
86
- in_table: false,
87
- in_tgroup: false,
88
- in_thead: false,
89
- in_tbody: false,
90
- in_row: false,
91
- in_list: false,
92
- in_list_item: false,
93
- };
94
-
95
- let mut title_extracted = false;
96
- let mut current_table: Vec<Vec<String>> = Vec::new();
97
- let mut current_row: Vec<String> = Vec::new();
98
- let mut list_type = "";
99
-
100
- loop {
101
- match reader.read_event() {
102
- Ok(Event::Start(e)) => {
103
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
104
- let tag = strip_namespace(&tag);
105
-
106
- match tag {
107
- "info" | "articleinfo" | "bookinfo" | "chapterinfo" => {
108
- state.in_info = true;
109
- }
110
- "title" if !title_extracted && state.in_info => {
111
- title = extract_element_text(&mut reader)?;
112
- title_extracted = true;
113
- }
114
- "title" if !title_extracted => {
115
- title = extract_element_text(&mut reader)?;
116
- title_extracted = true;
117
- }
118
- "title" if title_extracted => {
119
- let section_title = extract_element_text(&mut reader)?;
120
- if !section_title.is_empty() {
121
- output.push_str("## ");
122
- output.push_str(&section_title);
123
- output.push_str("\n\n");
124
- }
125
- }
126
- "author" | "personname" if state.in_info && author.is_none() => {
127
- author = Some(extract_element_text(&mut reader)?);
128
- }
129
- "date" if state.in_info && date.is_none() => {
130
- let date_text = extract_element_text(&mut reader)?;
131
- if !date_text.is_empty() {
132
- date = Some(date_text);
133
- }
134
- }
135
-
136
- "para" => {
137
- let para_text = extract_element_text(&mut reader)?;
138
- if !para_text.is_empty() {
139
- output.push_str(&para_text);
140
- output.push_str("\n\n");
141
- }
142
- }
143
-
144
- "programlisting" | "screen" => {
145
- let code_text = extract_element_text(&mut reader)?;
146
- if !code_text.is_empty() {
147
- output.push_str("```\n");
148
- output.push_str(&code_text);
149
- output.push_str("\n```\n\n");
150
- }
151
- }
152
-
153
- "itemizedlist" => {
154
- state.in_list = true;
155
- list_type = "itemized";
156
- }
157
- "orderedlist" => {
158
- state.in_list = true;
159
- list_type = "ordered";
160
- }
161
- "listitem" if state.in_list => {
162
- state.in_list_item = true;
163
- let prefix = if list_type == "ordered" { "1. " } else { "- " };
164
- output.push_str(prefix);
165
- let item_text = extract_element_text(&mut reader)?;
166
- if !item_text.is_empty() {
167
- output.push_str(&item_text);
168
- }
169
- output.push('\n');
170
- state.in_list_item = false;
171
- }
172
-
173
- "blockquote" => {
174
- output.push_str("> ");
175
- let quote_text = extract_element_text(&mut reader)?;
176
- if !quote_text.is_empty() {
177
- output.push_str(&quote_text);
178
- }
179
- output.push_str("\n\n");
180
- }
181
-
182
- "figure" => {
183
- let figure_text = extract_element_text(&mut reader)?;
184
- if !figure_text.is_empty() {
185
- output.push_str("**Figure:** ");
186
- output.push_str(&figure_text);
187
- output.push_str("\n\n");
188
- }
189
- }
190
-
191
- "footnote" => {
192
- output.push('[');
193
- let footnote_text = extract_element_text(&mut reader)?;
194
- if !footnote_text.is_empty() {
195
- output.push_str(&footnote_text);
196
- }
197
- output.push(']');
198
- }
199
-
200
- "table" | "informaltable" => {
201
- state.in_table = true;
202
- current_table.clear();
203
- }
204
- "tgroup" if state.in_table => {
205
- state.in_tgroup = true;
206
- }
207
- "thead" if state.in_tgroup => {
208
- state.in_thead = true;
209
- }
210
- "tbody" if state.in_tgroup => {
211
- state.in_tbody = true;
212
- }
213
- "row" if (state.in_thead || state.in_tbody) && state.in_tgroup => {
214
- state.in_row = true;
215
- current_row.clear();
216
- }
217
- "entry" if state.in_row => {
218
- let entry_text = extract_element_text(&mut reader)?;
219
- current_row.push(entry_text);
220
- }
221
-
222
- _ => {}
223
- }
224
- }
225
- Ok(Event::End(e)) => {
226
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
227
- let tag = strip_namespace(&tag);
228
-
229
- match tag {
230
- "info" | "articleinfo" | "bookinfo" | "chapterinfo" => {
231
- state.in_info = false;
232
- }
233
- "itemizedlist" | "orderedlist" if state.in_list => {
234
- output.push('\n');
235
- state.in_list = false;
236
- }
237
- "table" | "informaltable" if state.in_table => {
238
- if !current_table.is_empty() {
239
- let markdown = cells_to_markdown(&current_table);
240
- tables.push(Table {
241
- cells: current_table.clone(),
242
- markdown,
243
- page_number: table_index + 1,
244
- });
245
- table_index += 1;
246
- current_table.clear();
247
- }
248
- state.in_table = false;
249
- }
250
- "tgroup" if state.in_tgroup => {
251
- state.in_tgroup = false;
252
- }
253
- "thead" if state.in_thead => {
254
- state.in_thead = false;
255
- }
256
- "tbody" if state.in_tbody => {
257
- state.in_tbody = false;
258
- }
259
- "row" if state.in_row => {
260
- if !current_row.is_empty() {
261
- current_table.push(current_row.clone());
262
- current_row.clear();
263
- }
264
- state.in_row = false;
265
- }
266
- _ => {}
267
- }
268
- }
269
- Ok(Event::Eof) => break,
270
- Err(e) => {
271
- return Err(crate::error::KreuzbergError::parsing(format!(
272
- "XML parsing error: {}",
273
- e
274
- )));
275
- }
276
- _ => {}
277
- }
278
- }
279
-
280
- let mut final_output = output;
281
- if !title.is_empty() {
282
- final_output = format!("{}\n\n{}", title, final_output);
283
- }
284
-
285
- Ok((final_output.trim().to_string(), title, author, date, tables))
286
- }
287
-
288
- /// Extract text content from a DocBook element and its children.
289
- /// Used for extracting nested content within elements.
290
- fn extract_element_text(reader: &mut Reader<&[u8]>) -> Result<String> {
291
- let mut text = String::new();
292
- let mut depth = 0;
293
-
294
- loop {
295
- match reader.read_event() {
296
- Ok(Event::Start(_)) => {
297
- depth += 1;
298
- }
299
- Ok(Event::End(_)) => {
300
- if depth == 0 {
301
- break;
302
- }
303
- depth -= 1;
304
- }
305
- Ok(Event::Text(t)) => {
306
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
307
- if !decoded.trim().is_empty() {
308
- if !text.is_empty() && !text.ends_with(' ') && !text.ends_with('\n') {
309
- text.push(' ');
310
- }
311
- text.push_str(decoded.trim());
312
- }
313
- }
314
- Ok(Event::CData(t)) => {
315
- let decoded = std::str::from_utf8(t.as_ref()).unwrap_or("").to_string();
316
- if !decoded.trim().is_empty() {
317
- if !text.is_empty() {
318
- text.push(' ');
319
- }
320
- text.push_str(decoded.trim());
321
- }
322
- }
323
- Ok(Event::Eof) => break,
324
- Err(e) => {
325
- return Err(crate::error::KreuzbergError::parsing(format!(
326
- "XML parsing error: {}",
327
- e
328
- )));
329
- }
330
- _ => {}
331
- }
332
- }
333
-
334
- Ok(text.trim().to_string())
335
- }
336
-
337
- impl Plugin for DocbookExtractor {
338
- fn name(&self) -> &str {
339
- "docbook-extractor"
340
- }
341
-
342
- fn version(&self) -> String {
343
- env!("CARGO_PKG_VERSION").to_string()
344
- }
345
-
346
- fn initialize(&self) -> Result<()> {
347
- Ok(())
348
- }
349
-
350
- fn shutdown(&self) -> Result<()> {
351
- Ok(())
352
- }
353
- }
354
-
355
- #[async_trait]
356
- impl DocumentExtractor for DocbookExtractor {
357
- #[cfg_attr(
358
- feature = "otel",
359
- tracing::instrument(
360
- skip(self, content, config),
361
- fields(
362
- extractor.name = self.name(),
363
- content.size_bytes = content.len(),
364
- )
365
- )
366
- )]
367
- async fn extract_bytes(
368
- &self,
369
- content: &[u8],
370
- mime_type: &str,
371
- config: &ExtractionConfig,
372
- ) -> Result<ExtractionResult> {
373
- let _ = config;
374
- let docbook_content = std::str::from_utf8(content)
375
- .map(|s| s.to_string())
376
- .unwrap_or_else(|_| String::from_utf8_lossy(content).to_string());
377
-
378
- let (extracted_content, title, author, date, tables) = parse_docbook_single_pass(&docbook_content)?;
379
-
380
- let mut metadata = Metadata::default();
381
- let mut subject_parts = Vec::new();
382
-
383
- if !title.is_empty() {
384
- subject_parts.push(format!("Title: {}", title));
385
- }
386
- if let Some(author) = &author {
387
- subject_parts.push(format!("Author: {}", author));
388
- }
389
-
390
- if !subject_parts.is_empty() {
391
- metadata.subject = Some(subject_parts.join("; "));
392
- }
393
-
394
- if let Some(date_val) = date {
395
- metadata.date = Some(date_val);
396
- }
397
-
398
- Ok(ExtractionResult {
399
- content: extracted_content,
400
- mime_type: mime_type.to_string(),
401
- metadata,
402
- tables,
403
- detected_languages: None,
404
- chunks: None,
405
- images: None,
406
- })
407
- }
408
-
409
- #[cfg(feature = "tokio-runtime")]
410
- #[cfg_attr(
411
- feature = "otel",
412
- tracing::instrument(
413
- skip(self, path, config),
414
- fields(
415
- extractor.name = self.name(),
416
- )
417
- )
418
- )]
419
- async fn extract_file(&self, path: &Path, mime_type: &str, config: &ExtractionConfig) -> Result<ExtractionResult> {
420
- let bytes = tokio::fs::read(path).await?;
421
- self.extract_bytes(&bytes, mime_type, config).await
422
- }
423
-
424
- fn supported_mime_types(&self) -> &[&str] {
425
- &["application/docbook+xml", "text/docbook"]
426
- }
427
-
428
- fn priority(&self) -> i32 {
429
- 50
430
- }
431
- }
432
-
433
- #[cfg(test)]
434
- mod tests {
435
- use super::*;
436
-
437
- #[test]
438
- fn test_docbook_extractor_plugin_interface() {
439
- let extractor = DocbookExtractor::new();
440
- assert_eq!(extractor.name(), "docbook-extractor");
441
- assert!(extractor.initialize().is_ok());
442
- assert!(extractor.shutdown().is_ok());
443
- }
444
-
445
- #[test]
446
- fn test_docbook_extractor_supported_mime_types() {
447
- let extractor = DocbookExtractor::new();
448
- let mime_types = extractor.supported_mime_types();
449
- assert_eq!(mime_types.len(), 2);
450
- assert!(mime_types.contains(&"application/docbook+xml"));
451
- assert!(mime_types.contains(&"text/docbook"));
452
- }
453
-
454
- #[test]
455
- fn test_docbook_extractor_priority() {
456
- let extractor = DocbookExtractor::new();
457
- assert_eq!(extractor.priority(), 50);
458
- }
459
-
460
- #[test]
461
- fn test_parse_simple_docbook() {
462
- let docbook = r#"<?xml version="1.0" encoding="UTF-8"?>
463
- <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
464
- "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
465
- <article>
466
- <title>Test Article</title>
467
- <para>Test content.</para>
468
- </article>"#;
469
-
470
- let (content, title, _, _, _) = parse_docbook_single_pass(docbook).expect("Parse failed");
471
- assert_eq!(title, "Test Article");
472
- assert!(content.contains("Test content"));
473
- }
474
-
475
- #[test]
476
- fn test_extract_docbook_tables_basic() {
477
- let docbook = r#"<?xml version="1.0" encoding="UTF-8"?>
478
- <article>
479
- <table>
480
- <tgroup cols="2">
481
- <thead>
482
- <row>
483
- <entry>Col1</entry>
484
- <entry>Col2</entry>
485
- </row>
486
- </thead>
487
- <tbody>
488
- <row>
489
- <entry>Data1</entry>
490
- <entry>Data2</entry>
491
- </row>
492
- </tbody>
493
- </tgroup>
494
- </table>
495
- </article>"#;
496
-
497
- let (_, _, _, _, tables) = parse_docbook_single_pass(docbook).expect("Table extraction failed");
498
- assert_eq!(tables.len(), 1);
499
- assert_eq!(tables[0].cells.len(), 2);
500
- assert_eq!(tables[0].cells[0], vec!["Col1", "Col2"]);
501
- }
502
- }