kreuzberg 4.0.0.rc2 → 4.0.1

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 (446) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -14
  3. data/.rspec +3 -3
  4. data/.rubocop.yaml +1 -1
  5. data/.rubocop.yml +543 -538
  6. data/Gemfile +8 -8
  7. data/Gemfile.lock +194 -6
  8. data/README.md +391 -426
  9. data/Rakefile +34 -25
  10. data/Steepfile +51 -47
  11. data/examples/async_patterns.rb +283 -341
  12. data/ext/kreuzberg_rb/extconf.rb +65 -45
  13. data/ext/kreuzberg_rb/native/.cargo/config.toml +23 -0
  14. data/ext/kreuzberg_rb/native/Cargo.lock +7619 -6535
  15. data/ext/kreuzberg_rb/native/Cargo.toml +75 -44
  16. data/ext/kreuzberg_rb/native/README.md +425 -425
  17. data/ext/kreuzberg_rb/native/build.rs +15 -15
  18. data/ext/kreuzberg_rb/native/include/ieeefp.h +11 -11
  19. data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +14 -14
  20. data/ext/kreuzberg_rb/native/include/strings.h +20 -20
  21. data/ext/kreuzberg_rb/native/include/unistd.h +47 -47
  22. data/ext/kreuzberg_rb/native/src/lib.rs +3802 -2998
  23. data/extconf.rb +60 -28
  24. data/kreuzberg.gemspec +199 -148
  25. data/lib/kreuzberg/api_proxy.rb +126 -142
  26. data/lib/kreuzberg/cache_api.rb +67 -46
  27. data/lib/kreuzberg/cli.rb +47 -55
  28. data/lib/kreuzberg/cli_proxy.rb +117 -127
  29. data/lib/kreuzberg/config.rb +936 -691
  30. data/lib/kreuzberg/error_context.rb +136 -32
  31. data/lib/kreuzberg/errors.rb +116 -118
  32. data/lib/kreuzberg/extraction_api.rb +313 -85
  33. data/lib/kreuzberg/mcp_proxy.rb +177 -186
  34. data/lib/kreuzberg/ocr_backend_protocol.rb +40 -113
  35. data/lib/kreuzberg/post_processor_protocol.rb +15 -86
  36. data/lib/kreuzberg/result.rb +334 -216
  37. data/lib/kreuzberg/setup_lib_path.rb +99 -80
  38. data/lib/kreuzberg/types.rb +170 -0
  39. data/lib/kreuzberg/validator_protocol.rb +16 -89
  40. data/lib/kreuzberg/version.rb +5 -5
  41. data/lib/kreuzberg.rb +96 -103
  42. data/lib/libpdfium.so +0 -0
  43. data/sig/kreuzberg/internal.rbs +184 -184
  44. data/sig/kreuzberg.rbs +561 -520
  45. data/spec/binding/async_operations_spec.rb +473 -0
  46. data/spec/binding/batch_operations_spec.rb +595 -0
  47. data/spec/binding/batch_spec.rb +359 -0
  48. data/spec/binding/cache_spec.rb +227 -227
  49. data/spec/binding/cli_proxy_spec.rb +85 -85
  50. data/spec/binding/cli_spec.rb +55 -55
  51. data/spec/binding/config_result_spec.rb +377 -0
  52. data/spec/binding/config_spec.rb +419 -345
  53. data/spec/binding/config_validation_spec.rb +377 -283
  54. data/spec/binding/embeddings_spec.rb +816 -0
  55. data/spec/binding/error_handling_spec.rb +399 -213
  56. data/spec/binding/error_recovery_spec.rb +488 -0
  57. data/spec/binding/errors_spec.rb +66 -66
  58. data/spec/binding/font_config_spec.rb +220 -0
  59. data/spec/binding/images_spec.rb +738 -0
  60. data/spec/binding/keywords_extraction_spec.rb +600 -0
  61. data/spec/binding/metadata_types_spec.rb +1228 -0
  62. data/spec/binding/pages_extraction_spec.rb +471 -0
  63. data/spec/binding/plugins/ocr_backend_spec.rb +307 -307
  64. data/spec/binding/plugins/postprocessor_spec.rb +269 -269
  65. data/spec/binding/plugins/validator_spec.rb +273 -274
  66. data/spec/binding/tables_spec.rb +641 -0
  67. data/spec/fixtures/config.toml +38 -39
  68. data/spec/fixtures/config.yaml +41 -41
  69. data/spec/fixtures/invalid_config.toml +3 -4
  70. data/spec/smoke/package_spec.rb +177 -178
  71. data/spec/spec_helper.rb +40 -42
  72. data/spec/unit/config/chunking_config_spec.rb +213 -0
  73. data/spec/unit/config/embedding_config_spec.rb +343 -0
  74. data/spec/unit/config/extraction_config_spec.rb +438 -0
  75. data/spec/unit/config/font_config_spec.rb +285 -0
  76. data/spec/unit/config/hierarchy_config_spec.rb +314 -0
  77. data/spec/unit/config/image_extraction_config_spec.rb +209 -0
  78. data/spec/unit/config/image_preprocessing_config_spec.rb +249 -0
  79. data/spec/unit/config/keyword_config_spec.rb +229 -0
  80. data/spec/unit/config/language_detection_config_spec.rb +258 -0
  81. data/spec/unit/config/ocr_config_spec.rb +171 -0
  82. data/spec/unit/config/page_config_spec.rb +221 -0
  83. data/spec/unit/config/pdf_config_spec.rb +267 -0
  84. data/spec/unit/config/postprocessor_config_spec.rb +290 -0
  85. data/spec/unit/config/tesseract_config_spec.rb +181 -0
  86. data/spec/unit/config/token_reduction_config_spec.rb +251 -0
  87. data/test/metadata_types_test.rb +959 -0
  88. data/vendor/Cargo.toml +61 -0
  89. data/vendor/kreuzberg/Cargo.toml +259 -204
  90. data/vendor/kreuzberg/README.md +263 -175
  91. data/vendor/kreuzberg/build.rs +782 -474
  92. data/vendor/kreuzberg/examples/bench_fixes.rs +71 -0
  93. data/vendor/kreuzberg/examples/test_pdfium_fork.rs +62 -0
  94. data/vendor/kreuzberg/src/api/error.rs +81 -81
  95. data/vendor/kreuzberg/src/api/handlers.rs +320 -199
  96. data/vendor/kreuzberg/src/api/mod.rs +94 -79
  97. data/vendor/kreuzberg/src/api/server.rs +518 -353
  98. data/vendor/kreuzberg/src/api/types.rs +206 -170
  99. data/vendor/kreuzberg/src/cache/mod.rs +1167 -1167
  100. data/vendor/kreuzberg/src/chunking/mod.rs +2303 -677
  101. data/vendor/kreuzberg/src/chunking/processor.rs +219 -0
  102. data/vendor/kreuzberg/src/core/batch_mode.rs +95 -95
  103. data/vendor/kreuzberg/src/core/batch_optimizations.rs +385 -0
  104. data/vendor/kreuzberg/src/core/config.rs +1914 -1032
  105. data/vendor/kreuzberg/src/core/config_validation.rs +949 -0
  106. data/vendor/kreuzberg/src/core/extractor.rs +1200 -1024
  107. data/vendor/kreuzberg/src/core/formats.rs +235 -0
  108. data/vendor/kreuzberg/src/core/io.rs +329 -329
  109. data/vendor/kreuzberg/src/core/mime.rs +605 -605
  110. data/vendor/kreuzberg/src/core/mod.rs +61 -45
  111. data/vendor/kreuzberg/src/core/pipeline.rs +1223 -984
  112. data/vendor/kreuzberg/src/core/server_config.rs +1220 -0
  113. data/vendor/kreuzberg/src/embeddings.rs +471 -432
  114. data/vendor/kreuzberg/src/error.rs +431 -431
  115. data/vendor/kreuzberg/src/extraction/archive.rs +959 -954
  116. data/vendor/kreuzberg/src/extraction/capacity.rs +263 -0
  117. data/vendor/kreuzberg/src/extraction/docx.rs +404 -40
  118. data/vendor/kreuzberg/src/extraction/email.rs +855 -854
  119. data/vendor/kreuzberg/src/extraction/excel.rs +697 -688
  120. data/vendor/kreuzberg/src/extraction/html.rs +1830 -553
  121. data/vendor/kreuzberg/src/extraction/image.rs +492 -368
  122. data/vendor/kreuzberg/src/extraction/libreoffice.rs +574 -563
  123. data/vendor/kreuzberg/src/extraction/markdown.rs +216 -213
  124. data/vendor/kreuzberg/src/extraction/mod.rs +93 -81
  125. data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -398
  126. data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -247
  127. data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -240
  128. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +130 -130
  129. data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +284 -287
  130. data/vendor/kreuzberg/src/extraction/pptx.rs +3102 -3000
  131. data/vendor/kreuzberg/src/extraction/structured.rs +491 -490
  132. data/vendor/kreuzberg/src/extraction/table.rs +329 -328
  133. data/vendor/kreuzberg/src/extraction/text.rs +277 -269
  134. data/vendor/kreuzberg/src/extraction/xml.rs +333 -333
  135. data/vendor/kreuzberg/src/extractors/archive.rs +447 -446
  136. data/vendor/kreuzberg/src/extractors/bibtex.rs +470 -469
  137. data/vendor/kreuzberg/src/extractors/docbook.rs +504 -502
  138. data/vendor/kreuzberg/src/extractors/docx.rs +400 -367
  139. data/vendor/kreuzberg/src/extractors/email.rs +157 -143
  140. data/vendor/kreuzberg/src/extractors/epub.rs +696 -707
  141. data/vendor/kreuzberg/src/extractors/excel.rs +385 -343
  142. data/vendor/kreuzberg/src/extractors/fictionbook.rs +492 -491
  143. data/vendor/kreuzberg/src/extractors/html.rs +419 -393
  144. data/vendor/kreuzberg/src/extractors/image.rs +219 -198
  145. data/vendor/kreuzberg/src/extractors/jats.rs +1054 -1051
  146. data/vendor/kreuzberg/src/extractors/jupyter.rs +368 -367
  147. data/vendor/kreuzberg/src/extractors/latex.rs +653 -652
  148. data/vendor/kreuzberg/src/extractors/markdown.rs +701 -700
  149. data/vendor/kreuzberg/src/extractors/mod.rs +429 -365
  150. data/vendor/kreuzberg/src/extractors/odt.rs +628 -628
  151. data/vendor/kreuzberg/src/extractors/opml.rs +635 -634
  152. data/vendor/kreuzberg/src/extractors/orgmode.rs +529 -528
  153. data/vendor/kreuzberg/src/extractors/pdf.rs +761 -493
  154. data/vendor/kreuzberg/src/extractors/pptx.rs +279 -248
  155. data/vendor/kreuzberg/src/extractors/rst.rs +577 -576
  156. data/vendor/kreuzberg/src/extractors/rtf.rs +809 -810
  157. data/vendor/kreuzberg/src/extractors/security.rs +484 -484
  158. data/vendor/kreuzberg/src/extractors/security_tests.rs +367 -367
  159. data/vendor/kreuzberg/src/extractors/structured.rs +142 -140
  160. data/vendor/kreuzberg/src/extractors/text.rs +265 -260
  161. data/vendor/kreuzberg/src/extractors/typst.rs +651 -650
  162. data/vendor/kreuzberg/src/extractors/xml.rs +147 -135
  163. data/vendor/kreuzberg/src/image/dpi.rs +164 -164
  164. data/vendor/kreuzberg/src/image/mod.rs +6 -6
  165. data/vendor/kreuzberg/src/image/preprocessing.rs +417 -417
  166. data/vendor/kreuzberg/src/image/resize.rs +89 -89
  167. data/vendor/kreuzberg/src/keywords/config.rs +154 -154
  168. data/vendor/kreuzberg/src/keywords/mod.rs +237 -237
  169. data/vendor/kreuzberg/src/keywords/processor.rs +275 -267
  170. data/vendor/kreuzberg/src/keywords/rake.rs +293 -293
  171. data/vendor/kreuzberg/src/keywords/types.rs +68 -68
  172. data/vendor/kreuzberg/src/keywords/yake.rs +163 -163
  173. data/vendor/kreuzberg/src/language_detection/mod.rs +985 -942
  174. data/vendor/kreuzberg/src/language_detection/processor.rs +218 -0
  175. data/vendor/kreuzberg/src/lib.rs +114 -105
  176. data/vendor/kreuzberg/src/mcp/mod.rs +35 -32
  177. data/vendor/kreuzberg/src/mcp/server.rs +2090 -1968
  178. data/vendor/kreuzberg/src/ocr/cache.rs +469 -469
  179. data/vendor/kreuzberg/src/ocr/error.rs +37 -37
  180. data/vendor/kreuzberg/src/ocr/hocr.rs +216 -216
  181. data/vendor/kreuzberg/src/ocr/language_registry.rs +520 -0
  182. data/vendor/kreuzberg/src/ocr/mod.rs +60 -58
  183. data/vendor/kreuzberg/src/ocr/processor.rs +858 -863
  184. data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -4
  185. data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -144
  186. data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +456 -450
  187. data/vendor/kreuzberg/src/ocr/types.rs +393 -393
  188. data/vendor/kreuzberg/src/ocr/utils.rs +47 -47
  189. data/vendor/kreuzberg/src/ocr/validation.rs +206 -206
  190. data/vendor/kreuzberg/src/panic_context.rs +154 -154
  191. data/vendor/kreuzberg/src/pdf/bindings.rs +306 -0
  192. data/vendor/kreuzberg/src/pdf/bundled.rs +408 -0
  193. data/vendor/kreuzberg/src/pdf/error.rs +214 -122
  194. data/vendor/kreuzberg/src/pdf/fonts.rs +358 -0
  195. data/vendor/kreuzberg/src/pdf/hierarchy.rs +903 -0
  196. data/vendor/kreuzberg/src/pdf/images.rs +139 -139
  197. data/vendor/kreuzberg/src/pdf/metadata.rs +509 -346
  198. data/vendor/kreuzberg/src/pdf/mod.rs +81 -50
  199. data/vendor/kreuzberg/src/pdf/rendering.rs +369 -369
  200. data/vendor/kreuzberg/src/pdf/table.rs +417 -393
  201. data/vendor/kreuzberg/src/pdf/text.rs +553 -158
  202. data/vendor/kreuzberg/src/plugins/extractor.rs +1042 -1013
  203. data/vendor/kreuzberg/src/plugins/mod.rs +212 -209
  204. data/vendor/kreuzberg/src/plugins/ocr.rs +637 -620
  205. data/vendor/kreuzberg/src/plugins/processor.rs +650 -642
  206. data/vendor/kreuzberg/src/plugins/registry.rs +1339 -1337
  207. data/vendor/kreuzberg/src/plugins/traits.rs +258 -258
  208. data/vendor/kreuzberg/src/plugins/validator.rs +967 -956
  209. data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -1470
  210. data/vendor/kreuzberg/src/text/mod.rs +27 -19
  211. data/vendor/kreuzberg/src/text/quality.rs +710 -697
  212. data/vendor/kreuzberg/src/text/quality_processor.rs +231 -0
  213. data/vendor/kreuzberg/src/text/string_utils.rs +229 -217
  214. data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -164
  215. data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -100
  216. data/vendor/kreuzberg/src/text/token_reduction/core.rs +832 -796
  217. data/vendor/kreuzberg/src/text/token_reduction/filters.rs +923 -902
  218. data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -160
  219. data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -619
  220. data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +148 -147
  221. data/vendor/kreuzberg/src/text/utf8_validation.rs +193 -0
  222. data/vendor/kreuzberg/src/types.rs +1713 -903
  223. data/vendor/kreuzberg/src/utils/mod.rs +31 -17
  224. data/vendor/kreuzberg/src/utils/pool.rs +503 -0
  225. data/vendor/kreuzberg/src/utils/pool_sizing.rs +364 -0
  226. data/vendor/kreuzberg/src/utils/quality.rs +968 -959
  227. data/vendor/kreuzberg/src/utils/string_pool.rs +761 -0
  228. data/vendor/kreuzberg/src/utils/string_utils.rs +381 -381
  229. data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -53
  230. data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -482
  231. data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -261
  232. data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -400
  233. data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -1205
  234. data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -280
  235. data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -425
  236. data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -172
  237. data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -622
  238. data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -849
  239. data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -1300
  240. data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -175
  241. data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -734
  242. data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -37
  243. data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -100
  244. data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -801
  245. data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -849
  246. data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -693
  247. data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -111
  248. data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -162
  249. data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -226
  250. data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -41
  251. data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -196
  252. data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -227
  253. data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -181
  254. data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -791
  255. data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -47
  256. data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -760
  257. data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -634
  258. data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -136
  259. data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -84
  260. data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -681
  261. data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -64
  262. data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -51
  263. data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -476
  264. data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -163
  265. data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -1
  266. data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -101
  267. data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -477
  268. data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -490
  269. data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -415
  270. data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -223
  271. data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -331
  272. data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -562
  273. data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -436
  274. data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -561
  275. data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -193
  276. data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -420
  277. data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -448
  278. data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -32
  279. data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -33
  280. data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -420
  281. data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -76
  282. data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -129
  283. data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -54
  284. data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -118
  285. data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -149
  286. data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -506
  287. data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -75
  288. data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -519
  289. data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -647
  290. data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -62
  291. data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -796
  292. data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -31
  293. data/vendor/kreuzberg/tests/api_embed.rs +360 -0
  294. data/vendor/kreuzberg/tests/api_extract_multipart.rs +52 -52
  295. data/vendor/kreuzberg/tests/api_large_pdf_extraction.rs +471 -0
  296. data/vendor/kreuzberg/tests/api_large_pdf_extraction_diagnostics.rs +289 -0
  297. data/vendor/kreuzberg/tests/api_tests.rs +1472 -966
  298. data/vendor/kreuzberg/tests/archive_integration.rs +545 -543
  299. data/vendor/kreuzberg/tests/batch_orchestration.rs +587 -556
  300. data/vendor/kreuzberg/tests/batch_pooling_benchmark.rs +154 -0
  301. data/vendor/kreuzberg/tests/batch_processing.rs +328 -316
  302. data/vendor/kreuzberg/tests/bibtex_parity_test.rs +421 -421
  303. data/vendor/kreuzberg/tests/concurrency_stress.rs +541 -525
  304. data/vendor/kreuzberg/tests/config_features.rs +612 -598
  305. data/vendor/kreuzberg/tests/config_integration_test.rs +753 -0
  306. data/vendor/kreuzberg/tests/config_loading_tests.rs +416 -415
  307. data/vendor/kreuzberg/tests/core_integration.rs +519 -510
  308. data/vendor/kreuzberg/tests/csv_integration.rs +414 -414
  309. data/vendor/kreuzberg/tests/data/hierarchy_ground_truth.json +294 -0
  310. data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +500 -498
  311. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +122 -122
  312. data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +370 -370
  313. data/vendor/kreuzberg/tests/email_integration.rs +327 -325
  314. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +275 -275
  315. data/vendor/kreuzberg/tests/error_handling.rs +402 -393
  316. data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +228 -228
  317. data/vendor/kreuzberg/tests/format_integration.rs +165 -159
  318. data/vendor/kreuzberg/tests/helpers/mod.rs +202 -142
  319. data/vendor/kreuzberg/tests/html_table_test.rs +551 -551
  320. data/vendor/kreuzberg/tests/image_integration.rs +255 -253
  321. data/vendor/kreuzberg/tests/instrumentation_test.rs +139 -139
  322. data/vendor/kreuzberg/tests/jats_extractor_tests.rs +639 -639
  323. data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +704 -704
  324. data/vendor/kreuzberg/tests/keywords_integration.rs +479 -479
  325. data/vendor/kreuzberg/tests/keywords_quality.rs +509 -509
  326. data/vendor/kreuzberg/tests/latex_extractor_tests.rs +496 -496
  327. data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +490 -490
  328. data/vendor/kreuzberg/tests/mime_detection.rs +429 -428
  329. data/vendor/kreuzberg/tests/ocr_configuration.rs +514 -510
  330. data/vendor/kreuzberg/tests/ocr_errors.rs +698 -676
  331. data/vendor/kreuzberg/tests/ocr_language_registry.rs +191 -0
  332. data/vendor/kreuzberg/tests/ocr_quality.rs +629 -627
  333. data/vendor/kreuzberg/tests/ocr_stress.rs +469 -469
  334. data/vendor/kreuzberg/tests/odt_extractor_tests.rs +674 -695
  335. data/vendor/kreuzberg/tests/opml_extractor_tests.rs +616 -616
  336. data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +822 -822
  337. data/vendor/kreuzberg/tests/page_markers.rs +297 -0
  338. data/vendor/kreuzberg/tests/pdf_hierarchy_detection.rs +301 -0
  339. data/vendor/kreuzberg/tests/pdf_hierarchy_quality.rs +589 -0
  340. data/vendor/kreuzberg/tests/pdf_integration.rs +45 -43
  341. data/vendor/kreuzberg/tests/pdf_ocr_triggering.rs +301 -0
  342. data/vendor/kreuzberg/tests/pdf_text_merging.rs +475 -0
  343. data/vendor/kreuzberg/tests/pdfium_linking.rs +340 -0
  344. data/vendor/kreuzberg/tests/pipeline_integration.rs +1446 -1411
  345. data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +776 -771
  346. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +577 -560
  347. data/vendor/kreuzberg/tests/plugin_system.rs +927 -921
  348. data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -783
  349. data/vendor/kreuzberg/tests/registry_integration_tests.rs +587 -586
  350. data/vendor/kreuzberg/tests/rst_extractor_tests.rs +694 -692
  351. data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +775 -776
  352. data/vendor/kreuzberg/tests/security_validation.rs +416 -415
  353. data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -888
  354. data/vendor/kreuzberg/tests/test_fastembed.rs +631 -609
  355. data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +1260 -1259
  356. data/vendor/kreuzberg/tests/typst_extractor_tests.rs +648 -647
  357. data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -87
  358. data/vendor/kreuzberg-ffi/Cargo.toml +67 -0
  359. data/vendor/kreuzberg-ffi/README.md +851 -0
  360. data/vendor/kreuzberg-ffi/benches/result_view_benchmark.rs +227 -0
  361. data/vendor/kreuzberg-ffi/build.rs +168 -0
  362. data/vendor/kreuzberg-ffi/cbindgen.toml +37 -0
  363. data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +12 -0
  364. data/vendor/kreuzberg-ffi/kreuzberg.h +3012 -0
  365. data/vendor/kreuzberg-ffi/src/batch_streaming.rs +588 -0
  366. data/vendor/kreuzberg-ffi/src/config.rs +1341 -0
  367. data/vendor/kreuzberg-ffi/src/error.rs +901 -0
  368. data/vendor/kreuzberg-ffi/src/extraction.rs +555 -0
  369. data/vendor/kreuzberg-ffi/src/helpers.rs +879 -0
  370. data/vendor/kreuzberg-ffi/src/lib.rs +977 -0
  371. data/vendor/kreuzberg-ffi/src/memory.rs +493 -0
  372. data/vendor/kreuzberg-ffi/src/mime.rs +329 -0
  373. data/vendor/kreuzberg-ffi/src/panic_shield.rs +265 -0
  374. data/vendor/kreuzberg-ffi/src/plugins/document_extractor.rs +442 -0
  375. data/vendor/kreuzberg-ffi/src/plugins/mod.rs +14 -0
  376. data/vendor/kreuzberg-ffi/src/plugins/ocr_backend.rs +628 -0
  377. data/vendor/kreuzberg-ffi/src/plugins/post_processor.rs +438 -0
  378. data/vendor/kreuzberg-ffi/src/plugins/validator.rs +329 -0
  379. data/vendor/kreuzberg-ffi/src/result.rs +510 -0
  380. data/vendor/kreuzberg-ffi/src/result_pool.rs +639 -0
  381. data/vendor/kreuzberg-ffi/src/result_view.rs +773 -0
  382. data/vendor/kreuzberg-ffi/src/string_intern.rs +568 -0
  383. data/vendor/kreuzberg-ffi/src/types.rs +363 -0
  384. data/vendor/kreuzberg-ffi/src/util.rs +210 -0
  385. data/vendor/kreuzberg-ffi/src/validation.rs +848 -0
  386. data/vendor/kreuzberg-ffi/tests.disabled/README.md +48 -0
  387. data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +299 -0
  388. data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +346 -0
  389. data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +232 -0
  390. data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +470 -0
  391. data/vendor/kreuzberg-tesseract/.commitlintrc.json +13 -0
  392. data/vendor/kreuzberg-tesseract/.crate-ignore +2 -0
  393. data/vendor/kreuzberg-tesseract/Cargo.lock +2933 -0
  394. data/vendor/kreuzberg-tesseract/Cargo.toml +57 -0
  395. data/vendor/{rb-sys/LICENSE-MIT → kreuzberg-tesseract/LICENSE} +22 -21
  396. data/vendor/kreuzberg-tesseract/README.md +399 -0
  397. data/vendor/kreuzberg-tesseract/build.rs +1127 -0
  398. data/vendor/kreuzberg-tesseract/patches/README.md +71 -0
  399. data/vendor/kreuzberg-tesseract/patches/tesseract.diff +199 -0
  400. data/vendor/kreuzberg-tesseract/src/api.rs +1371 -0
  401. data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +77 -0
  402. data/vendor/kreuzberg-tesseract/src/enums.rs +297 -0
  403. data/vendor/kreuzberg-tesseract/src/error.rs +81 -0
  404. data/vendor/kreuzberg-tesseract/src/lib.rs +145 -0
  405. data/vendor/kreuzberg-tesseract/src/monitor.rs +57 -0
  406. data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +197 -0
  407. data/vendor/kreuzberg-tesseract/src/page_iterator.rs +253 -0
  408. data/vendor/kreuzberg-tesseract/src/result_iterator.rs +286 -0
  409. data/vendor/kreuzberg-tesseract/src/result_renderer.rs +183 -0
  410. data/vendor/kreuzberg-tesseract/tests/integration_test.rs +211 -0
  411. metadata +196 -45
  412. data/vendor/kreuzberg/benches/otel_overhead.rs +0 -48
  413. data/vendor/kreuzberg/src/extractors/fictionbook.rs.backup2 +0 -738
  414. data/vendor/rb-sys/.cargo-ok +0 -1
  415. data/vendor/rb-sys/.cargo_vcs_info.json +0 -6
  416. data/vendor/rb-sys/Cargo.lock +0 -393
  417. data/vendor/rb-sys/Cargo.toml +0 -70
  418. data/vendor/rb-sys/Cargo.toml.orig +0 -57
  419. data/vendor/rb-sys/LICENSE-APACHE +0 -190
  420. data/vendor/rb-sys/bin/release.sh +0 -21
  421. data/vendor/rb-sys/build/features.rs +0 -108
  422. data/vendor/rb-sys/build/main.rs +0 -246
  423. data/vendor/rb-sys/build/stable_api_config.rs +0 -153
  424. data/vendor/rb-sys/build/version.rs +0 -48
  425. data/vendor/rb-sys/readme.md +0 -36
  426. data/vendor/rb-sys/src/bindings.rs +0 -21
  427. data/vendor/rb-sys/src/hidden.rs +0 -11
  428. data/vendor/rb-sys/src/lib.rs +0 -34
  429. data/vendor/rb-sys/src/macros.rs +0 -371
  430. data/vendor/rb-sys/src/memory.rs +0 -53
  431. data/vendor/rb-sys/src/ruby_abi_version.rs +0 -38
  432. data/vendor/rb-sys/src/special_consts.rs +0 -31
  433. data/vendor/rb-sys/src/stable_api/compiled.c +0 -179
  434. data/vendor/rb-sys/src/stable_api/compiled.rs +0 -257
  435. data/vendor/rb-sys/src/stable_api/ruby_2_6.rs +0 -316
  436. data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +0 -316
  437. data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +0 -324
  438. data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +0 -317
  439. data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +0 -315
  440. data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +0 -326
  441. data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +0 -327
  442. data/vendor/rb-sys/src/stable_api.rs +0 -261
  443. data/vendor/rb-sys/src/symbol.rs +0 -31
  444. data/vendor/rb-sys/src/tracking_allocator.rs +0 -332
  445. data/vendor/rb-sys/src/utils.rs +0 -89
  446. data/vendor/rb-sys/src/value_type.rs +0 -7
@@ -1,1051 +1,1054 @@
1
- //! JATS (Journal Article Tag Suite) document extractor.
2
- //!
3
- //! This extractor handles JATS XML documents, the standard format for scientific journal articles
4
- //! used by PubMed Central and major academic publishers.
5
- //!
6
- //! It extracts:
7
- //! - Rich metadata (title, subtitle, authors with affiliations, DOI, PII, keywords, dates)
8
- //! - Article abstract (regular and graphical)
9
- //! - Section hierarchy and content (intro, methods, results, discussion)
10
- //! - Paragraphs and text content
11
- //! - Tables with captions
12
- //! - Figures with captions
13
- //! - Citations and references
14
- //! - Supplementary material information
15
-
16
- use crate::Result;
17
- use crate::core::config::ExtractionConfig;
18
- use crate::extraction::cells_to_markdown;
19
- use crate::plugins::{DocumentExtractor, Plugin};
20
- use crate::types::{ExtractionResult, Metadata, Table};
21
- use async_trait::async_trait;
22
- use quick_xml::Reader;
23
- use quick_xml::events::Event;
24
- use std::path::Path;
25
-
26
- /// JATS document extractor.
27
- ///
28
- /// Supports JATS (Journal Article Tag Suite) XML documents in various versions,
29
- /// handling both the full article structure and minimal JATS subsets.
30
- pub struct JatsExtractor;
31
-
32
- impl Default for JatsExtractor {
33
- fn default() -> Self {
34
- Self::new()
35
- }
36
- }
37
-
38
- impl JatsExtractor {
39
- pub fn new() -> Self {
40
- Self
41
- }
42
- }
43
-
44
- /// Extract text content from a JATS element and its children.
45
- fn extract_text_content(reader: &mut Reader<&[u8]>) -> Result<String> {
46
- let mut text = String::new();
47
- let mut depth = 0;
48
-
49
- loop {
50
- match reader.read_event() {
51
- Ok(Event::Start(_)) => {
52
- depth += 1;
53
- }
54
- Ok(Event::End(_)) => {
55
- if depth == 0 {
56
- break;
57
- }
58
- depth -= 1;
59
- if !text.is_empty() && !text.ends_with('\n') {
60
- text.push(' ');
61
- }
62
- }
63
- Ok(Event::Text(t)) => {
64
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
65
- if !decoded.trim().is_empty() {
66
- text.push_str(&decoded);
67
- text.push(' ');
68
- }
69
- }
70
- Ok(Event::CData(t)) => {
71
- let decoded = std::str::from_utf8(t.as_ref()).unwrap_or("").to_string();
72
- if !decoded.trim().is_empty() {
73
- text.push_str(&decoded);
74
- text.push('\n');
75
- }
76
- }
77
- Ok(Event::Eof) => break,
78
- Err(e) => {
79
- return Err(crate::error::KreuzbergError::parsing(format!(
80
- "XML parsing error: {}",
81
- e
82
- )));
83
- }
84
- _ => {}
85
- }
86
- }
87
-
88
- Ok(text.trim().to_string())
89
- }
90
-
91
- /// Structure to hold extracted JATS metadata.
92
- #[derive(Debug, Clone, Default)]
93
- struct JatsMetadataExtracted {
94
- title: String,
95
- subtitle: Option<String>,
96
- authors: Vec<String>,
97
- affiliations: Vec<String>,
98
- doi: Option<String>,
99
- pii: Option<String>,
100
- keywords: Vec<String>,
101
- publication_date: Option<String>,
102
- volume: Option<String>,
103
- issue: Option<String>,
104
- pages: Option<String>,
105
- journal_title: Option<String>,
106
- article_type: Option<String>,
107
- abstract_text: Option<String>,
108
- corresponding_author: Option<String>,
109
- }
110
-
111
- /// Extract all content in a single optimized pass.
112
- /// Combines metadata extraction, content parsing, and table extraction into one pass.
113
- fn extract_jats_all_in_one(content: &str) -> Result<(JatsMetadataExtracted, String, String, Vec<Table>)> {
114
- let mut reader = Reader::from_str(content);
115
- let mut metadata = JatsMetadataExtracted::default();
116
- let mut body_content = String::new();
117
- let mut title = String::new();
118
-
119
- let mut in_article_meta = false;
120
- let mut in_article_title = false;
121
- let mut in_subtitle = false;
122
- let mut in_contrib = false;
123
- let mut in_name = false;
124
- let mut in_aff = false;
125
- let mut in_abstract = false;
126
- let mut in_kwd_group = false;
127
- let mut in_kwd = false;
128
- let mut current_author = String::new();
129
- let mut current_aff = String::new();
130
- let mut abstract_content = String::new();
131
-
132
- let mut in_body = false;
133
- let mut in_section = false;
134
- let mut in_para = false;
135
-
136
- let mut in_table = false;
137
- let mut in_thead = false;
138
- let mut in_tbody = false;
139
- let mut in_row = false;
140
- let mut current_table: Vec<Vec<String>> = Vec::new();
141
- let mut current_row: Vec<String> = Vec::new();
142
- let mut tables = Vec::new();
143
- let mut table_index = 0;
144
-
145
- loop {
146
- match reader.read_event() {
147
- Ok(Event::Start(e)) => {
148
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
149
-
150
- match tag.as_str() {
151
- "article" => {
152
- for attr in e.attributes() {
153
- if let Ok(attr) = attr
154
- && String::from_utf8_lossy(attr.key.as_ref()) == "article-type"
155
- {
156
- metadata.article_type = Some(String::from_utf8_lossy(attr.value.as_ref()).to_string());
157
- }
158
- }
159
- }
160
- "article-meta" => {
161
- in_article_meta = true;
162
- }
163
- "article-title" if in_article_meta => {
164
- in_article_title = true;
165
- }
166
- "subtitle" if in_article_meta => {
167
- in_subtitle = true;
168
- }
169
- "contrib" if in_article_meta => {
170
- in_contrib = true;
171
- current_author.clear();
172
- }
173
- "name" if in_contrib => {
174
- in_name = true;
175
- }
176
- "aff" if in_article_meta => {
177
- in_aff = true;
178
- current_aff.clear();
179
- }
180
- "article-id" if in_article_meta => {
181
- let mut id_type = String::new();
182
- for attr in e.attributes() {
183
- if let Ok(attr) = attr
184
- && String::from_utf8_lossy(attr.key.as_ref()) == "pub-id-type"
185
- {
186
- id_type = String::from_utf8_lossy(attr.value.as_ref()).to_string();
187
- }
188
- }
189
-
190
- let id_text = extract_text_content(&mut reader)?;
191
- match id_type.as_str() {
192
- "doi" => metadata.doi = Some(id_text),
193
- "pii" => metadata.pii = Some(id_text),
194
- _ => {}
195
- }
196
- continue;
197
- }
198
- "volume" if in_article_meta => {
199
- let vol_text = extract_text_content(&mut reader)?;
200
- metadata.volume = Some(vol_text);
201
- continue;
202
- }
203
- "issue" if in_article_meta => {
204
- let issue_text = extract_text_content(&mut reader)?;
205
- metadata.issue = Some(issue_text);
206
- continue;
207
- }
208
- "fpage" | "lpage" if in_article_meta => {
209
- let page_text = extract_text_content(&mut reader)?;
210
- if let Some(pages) = &mut metadata.pages {
211
- pages.push('-');
212
- pages.push_str(&page_text);
213
- } else {
214
- metadata.pages = Some(page_text);
215
- }
216
- continue;
217
- }
218
- "pub-date" if in_article_meta => {
219
- let date_text = extract_text_content(&mut reader)?;
220
- if metadata.publication_date.is_none() {
221
- metadata.publication_date = Some(date_text);
222
- }
223
- continue;
224
- }
225
- "journal-title" if in_article_meta => {
226
- let journal_text = extract_text_content(&mut reader)?;
227
- if metadata.journal_title.is_none() {
228
- metadata.journal_title = Some(journal_text);
229
- }
230
- continue;
231
- }
232
- "abstract" if in_article_meta => {
233
- in_abstract = true;
234
- abstract_content.clear();
235
- }
236
- "kwd-group" if in_article_meta => {
237
- in_kwd_group = true;
238
- }
239
- "kwd" if in_kwd_group => {
240
- in_kwd = true;
241
- }
242
- "corresp" if in_article_meta => {
243
- let corresp_text = extract_text_content(&mut reader)?;
244
- metadata.corresponding_author = Some(corresp_text);
245
- continue;
246
- }
247
- "body" => {
248
- in_body = true;
249
- }
250
- "sec" if in_body => {
251
- in_section = true;
252
- }
253
- "title" if (in_section || in_body) && !in_article_title => {
254
- let section_title = extract_text_content(&mut reader)?;
255
- if !section_title.is_empty() {
256
- body_content.push_str("## ");
257
- body_content.push_str(&section_title);
258
- body_content.push_str("\n\n");
259
- }
260
- continue;
261
- }
262
- "p" if in_body || in_section => {
263
- in_para = true;
264
- }
265
- "table" => {
266
- in_table = true;
267
- current_table.clear();
268
- }
269
- "thead" if in_table => {
270
- in_thead = true;
271
- }
272
- "tbody" if in_table => {
273
- in_tbody = true;
274
- }
275
- "tr" if (in_thead || in_tbody) && in_table => {
276
- in_row = true;
277
- current_row.clear();
278
- }
279
- "td" | "th" if in_row => {
280
- let mut cell_text = String::new();
281
- let mut cell_depth = 0;
282
-
283
- loop {
284
- match reader.read_event() {
285
- Ok(Event::Start(_)) => {
286
- cell_depth += 1;
287
- }
288
- Ok(Event::End(e)) => {
289
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
290
- if (tag == "td" || tag == "th") && cell_depth == 0 {
291
- break;
292
- }
293
- if cell_depth > 0 {
294
- cell_depth -= 1;
295
- }
296
- }
297
- Ok(Event::Text(t)) => {
298
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
299
- if !decoded.trim().is_empty() {
300
- if !cell_text.is_empty() {
301
- cell_text.push(' ');
302
- }
303
- cell_text.push_str(decoded.trim());
304
- }
305
- }
306
- Ok(Event::Eof) => break,
307
- Err(e) => {
308
- return Err(crate::error::KreuzbergError::parsing(format!(
309
- "XML parsing error: {}",
310
- e
311
- )));
312
- }
313
- _ => {}
314
- }
315
- }
316
-
317
- current_row.push(cell_text);
318
- }
319
- _ => {}
320
- }
321
- }
322
- Ok(Event::End(e)) => {
323
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
324
-
325
- match tag.as_str() {
326
- "article-meta" => {
327
- in_article_meta = false;
328
- }
329
- "article-title" if in_article_title => {
330
- in_article_title = false;
331
- }
332
- "subtitle" if in_subtitle => {
333
- in_subtitle = false;
334
- }
335
- "contrib" if in_contrib => {
336
- if !current_author.is_empty() {
337
- metadata.authors.push(current_author.clone());
338
- }
339
- in_contrib = false;
340
- current_author.clear();
341
- }
342
- "name" if in_name => {
343
- in_name = false;
344
- }
345
- "aff" if in_aff => {
346
- if !current_aff.is_empty() {
347
- metadata.affiliations.push(current_aff.clone());
348
- }
349
- in_aff = false;
350
- current_aff.clear();
351
- }
352
- "abstract" if in_abstract => {
353
- in_abstract = false;
354
- metadata.abstract_text = Some(abstract_content.trim().to_string());
355
- }
356
- "kwd-group" if in_kwd_group => {
357
- in_kwd_group = false;
358
- }
359
- "kwd" if in_kwd => {
360
- in_kwd = false;
361
- }
362
- "body" => {
363
- in_body = false;
364
- }
365
- "sec" if in_section => {
366
- in_section = false;
367
- }
368
- "p" if in_para => {
369
- in_para = false;
370
- }
371
- "table" if in_table => {
372
- if !current_table.is_empty() {
373
- let markdown = cells_to_markdown(&current_table);
374
- tables.push(Table {
375
- cells: current_table.clone(),
376
- markdown,
377
- page_number: table_index + 1,
378
- });
379
- table_index += 1;
380
- current_table.clear();
381
- }
382
- in_table = false;
383
- }
384
- "thead" if in_thead => {
385
- in_thead = false;
386
- }
387
- "tbody" if in_tbody => {
388
- in_tbody = false;
389
- }
390
- "tr" if in_row => {
391
- if !current_row.is_empty() {
392
- current_table.push(current_row.clone());
393
- current_row.clear();
394
- }
395
- in_row = false;
396
- }
397
- _ => {}
398
- }
399
- }
400
- Ok(Event::Text(t)) => {
401
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
402
- let trimmed = decoded.trim();
403
-
404
- if !trimmed.is_empty() {
405
- if in_article_title && metadata.title.is_empty() {
406
- metadata.title.push_str(trimmed);
407
- } else if in_subtitle && metadata.subtitle.is_none() {
408
- metadata.subtitle = Some(trimmed.to_string());
409
- } else if in_name {
410
- if !current_author.is_empty() {
411
- current_author.push(' ');
412
- }
413
- current_author.push_str(trimmed);
414
- } else if in_aff {
415
- if !current_aff.is_empty() {
416
- current_aff.push(' ');
417
- }
418
- current_aff.push_str(trimmed);
419
- } else if in_abstract {
420
- if !abstract_content.is_empty() {
421
- abstract_content.push(' ');
422
- }
423
- abstract_content.push_str(trimmed);
424
- } else if in_kwd {
425
- metadata.keywords.push(trimmed.to_string());
426
- } else if in_para && in_body {
427
- body_content.push_str(trimmed);
428
- body_content.push_str("\n\n");
429
- }
430
- }
431
- }
432
- Ok(Event::Eof) => break,
433
- Err(e) => {
434
- return Err(crate::error::KreuzbergError::parsing(format!(
435
- "XML parsing error: {}",
436
- e
437
- )));
438
- }
439
- _ => {}
440
- }
441
- }
442
-
443
- let mut final_output = body_content;
444
- if !metadata.title.is_empty() {
445
- final_output = format!("# {}\n\n{}", metadata.title, final_output);
446
- title = metadata.title.clone();
447
- }
448
-
449
- Ok((metadata, final_output.trim().to_string(), title, tables))
450
- }
451
-
452
- impl Plugin for JatsExtractor {
453
- fn name(&self) -> &str {
454
- "jats-extractor"
455
- }
456
-
457
- fn version(&self) -> String {
458
- env!("CARGO_PKG_VERSION").to_string()
459
- }
460
-
461
- fn initialize(&self) -> Result<()> {
462
- Ok(())
463
- }
464
-
465
- fn shutdown(&self) -> Result<()> {
466
- Ok(())
467
- }
468
- }
469
-
470
- #[async_trait]
471
- impl DocumentExtractor for JatsExtractor {
472
- #[cfg_attr(
473
- feature = "otel",
474
- tracing::instrument(
475
- skip(self, content, config),
476
- fields(
477
- extractor.name = self.name(),
478
- content.size_bytes = content.len(),
479
- )
480
- )
481
- )]
482
- async fn extract_bytes(
483
- &self,
484
- content: &[u8],
485
- mime_type: &str,
486
- config: &ExtractionConfig,
487
- ) -> Result<ExtractionResult> {
488
- let _ = config;
489
- let jats_content = std::str::from_utf8(content)
490
- .map(|s| s.to_string())
491
- .unwrap_or_else(|_| String::from_utf8_lossy(content).to_string());
492
-
493
- let (jats_metadata, extracted_content, _title, tables) = extract_jats_all_in_one(&jats_content)?;
494
-
495
- let mut metadata = Metadata::default();
496
- let mut subject_parts = Vec::new();
497
-
498
- if !jats_metadata.title.is_empty() {
499
- metadata.subject = Some(jats_metadata.title.clone());
500
- subject_parts.push(format!("Title: {}", jats_metadata.title));
501
- }
502
-
503
- if let Some(subtitle) = &jats_metadata.subtitle {
504
- subject_parts.push(format!("Subtitle: {}", subtitle));
505
- }
506
-
507
- if !jats_metadata.authors.is_empty() {
508
- subject_parts.push(format!("Authors: {}", jats_metadata.authors.join("; ")));
509
- }
510
-
511
- if !jats_metadata.affiliations.is_empty() {
512
- subject_parts.push(format!("Affiliations: {}", jats_metadata.affiliations.join("; ")));
513
- }
514
-
515
- if let Some(doi) = &jats_metadata.doi {
516
- subject_parts.push(format!("DOI: {}", doi));
517
- }
518
-
519
- if let Some(pii) = &jats_metadata.pii {
520
- subject_parts.push(format!("PII: {}", pii));
521
- }
522
-
523
- if !jats_metadata.keywords.is_empty() {
524
- subject_parts.push(format!("Keywords: {}", jats_metadata.keywords.join("; ")));
525
- }
526
-
527
- if let Some(date) = &jats_metadata.publication_date {
528
- metadata.date = Some(date.clone());
529
- subject_parts.push(format!("Publication Date: {}", date));
530
- }
531
-
532
- if let Some(volume) = &jats_metadata.volume {
533
- subject_parts.push(format!("Volume: {}", volume));
534
- }
535
-
536
- if let Some(issue) = &jats_metadata.issue {
537
- subject_parts.push(format!("Issue: {}", issue));
538
- }
539
-
540
- if let Some(pages) = &jats_metadata.pages {
541
- subject_parts.push(format!("Pages: {}", pages));
542
- }
543
-
544
- if let Some(journal_title) = &jats_metadata.journal_title {
545
- subject_parts.push(format!("Journal: {}", journal_title));
546
- }
547
-
548
- if let Some(article_type) = &jats_metadata.article_type {
549
- subject_parts.push(format!("Article Type: {}", article_type));
550
- }
551
-
552
- if let Some(abstract_text) = &jats_metadata.abstract_text {
553
- subject_parts.push(format!("Abstract: {}", abstract_text));
554
- }
555
-
556
- if let Some(corresp_author) = &jats_metadata.corresponding_author {
557
- subject_parts.push(format!("Corresponding Author: {}", corresp_author));
558
- }
559
-
560
- if !subject_parts.is_empty() {
561
- metadata.subject = Some(subject_parts.join(" | "));
562
- }
563
-
564
- Ok(ExtractionResult {
565
- content: extracted_content,
566
- mime_type: mime_type.to_string(),
567
- metadata,
568
- tables,
569
- detected_languages: None,
570
- chunks: None,
571
- images: None,
572
- })
573
- }
574
-
575
- #[cfg(feature = "tokio-runtime")]
576
- #[cfg_attr(
577
- feature = "otel",
578
- tracing::instrument(
579
- skip(self, path, config),
580
- fields(
581
- extractor.name = self.name(),
582
- )
583
- )
584
- )]
585
- async fn extract_file(&self, path: &Path, mime_type: &str, config: &ExtractionConfig) -> Result<ExtractionResult> {
586
- let bytes = tokio::fs::read(path).await?;
587
- self.extract_bytes(&bytes, mime_type, config).await
588
- }
589
-
590
- fn supported_mime_types(&self) -> &[&str] {
591
- &["application/x-jats+xml", "text/jats"]
592
- }
593
-
594
- fn priority(&self) -> i32 {
595
- 50
596
- }
597
- }
598
-
599
- #[cfg(test)]
600
- mod tests {
601
- use super::*;
602
-
603
- #[test]
604
- fn test_jats_extractor_plugin_interface() {
605
- let extractor = JatsExtractor::new();
606
- assert_eq!(extractor.name(), "jats-extractor");
607
- assert!(extractor.initialize().is_ok());
608
- assert!(extractor.shutdown().is_ok());
609
- }
610
-
611
- #[test]
612
- fn test_jats_extractor_supported_mime_types() {
613
- let extractor = JatsExtractor::new();
614
- let mime_types = extractor.supported_mime_types();
615
- assert_eq!(mime_types.len(), 2);
616
- assert!(mime_types.contains(&"application/x-jats+xml"));
617
- assert!(mime_types.contains(&"text/jats"));
618
- }
619
-
620
- #[test]
621
- fn test_jats_extractor_priority() {
622
- let extractor = JatsExtractor::new();
623
- assert_eq!(extractor.priority(), 50);
624
- }
625
-
626
- #[test]
627
- fn test_parse_simple_jats_article() {
628
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
629
- <article>
630
- <front>
631
- <article-meta>
632
- <article-title>Test Article Title</article-title>
633
- </article-meta>
634
- </front>
635
- <body>
636
- <p>Test content paragraph.</p>
637
- </body>
638
- </article>"#;
639
-
640
- let (metadata, content, title, _tables) = extract_jats_all_in_one(jats).expect("Parse failed");
641
- assert_eq!(title, "Test Article Title");
642
- assert_eq!(metadata.title, "Test Article Title");
643
- assert!(content.contains("Test Article Title"));
644
- assert!(content.contains("Test content"));
645
- }
646
-
647
- #[test]
648
- fn test_extract_jats_title() {
649
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
650
- <article>
651
- <front>
652
- <article-meta>
653
- <article-title>Effects of Caffeine on Human Health</article-title>
654
- </article-meta>
655
- </front>
656
- </article>"#;
657
-
658
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
659
- assert_eq!(metadata.title, "Effects of Caffeine on Human Health");
660
- }
661
-
662
- #[test]
663
- fn test_extract_jats_subtitle() {
664
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
665
- <article>
666
- <front>
667
- <article-meta>
668
- <article-title>Main Title</article-title>
669
- <subtitle>A Systematic Review</subtitle>
670
- </article-meta>
671
- </front>
672
- </article>"#;
673
-
674
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
675
- assert_eq!(metadata.title, "Main Title");
676
- assert_eq!(metadata.subtitle, Some("A Systematic Review".to_string()));
677
- }
678
-
679
- #[test]
680
- fn test_extract_jats_authors() {
681
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
682
- <article>
683
- <front>
684
- <article-meta>
685
- <contrib-group>
686
- <contrib contrib-type="author">
687
- <name>
688
- <surname>Smith</surname>
689
- <given-names>John A.</given-names>
690
- </name>
691
- </contrib>
692
- <contrib contrib-type="author">
693
- <name>
694
- <surname>Johnson</surname>
695
- <given-names>Jane B.</given-names>
696
- </name>
697
- </contrib>
698
- </contrib-group>
699
- </article-meta>
700
- </front>
701
- </article>"#;
702
-
703
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
704
- assert_eq!(metadata.authors.len(), 2);
705
- assert!(metadata.authors[0].contains("Smith"));
706
- assert!(metadata.authors[1].contains("Johnson"));
707
- }
708
-
709
- #[test]
710
- fn test_extract_jats_affiliations() {
711
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
712
- <article>
713
- <front>
714
- <article-meta>
715
- <aff id="aff1">Department of Medicine, Harvard University, Cambridge, MA</aff>
716
- <aff id="aff2">Center for Health Research, Boston Medical Center, Boston, MA</aff>
717
- </article-meta>
718
- </front>
719
- </article>"#;
720
-
721
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
722
- assert_eq!(metadata.affiliations.len(), 2);
723
- assert!(metadata.affiliations[0].contains("Harvard"));
724
- assert!(metadata.affiliations[1].contains("Boston"));
725
- }
726
-
727
- #[test]
728
- fn test_extract_jats_doi_and_pii() {
729
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
730
- <article>
731
- <front>
732
- <article-meta>
733
- <article-id pub-id-type="doi">10.1371/journal.pmed.0020124</article-id>
734
- <article-id pub-id-type="pii">05-PLME-RA-0071R2</article-id>
735
- </article-meta>
736
- </front>
737
- </article>"#;
738
-
739
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
740
- assert_eq!(metadata.doi, Some("10.1371/journal.pmed.0020124".to_string()));
741
- assert_eq!(metadata.pii, Some("05-PLME-RA-0071R2".to_string()));
742
- }
743
-
744
- #[test]
745
- fn test_extract_jats_keywords() {
746
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
747
- <article>
748
- <front>
749
- <article-meta>
750
- <kwd-group>
751
- <kwd>caffeine</kwd>
752
- <kwd>meta-analysis</kwd>
753
- <kwd>systematic review</kwd>
754
- </kwd-group>
755
- </article-meta>
756
- </front>
757
- </article>"#;
758
-
759
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
760
- assert_eq!(metadata.keywords.len(), 3);
761
- assert!(metadata.keywords.contains(&"caffeine".to_string()));
762
- assert!(metadata.keywords.contains(&"meta-analysis".to_string()));
763
- }
764
-
765
- #[test]
766
- fn test_extract_jats_publication_info() {
767
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
768
- <article>
769
- <front>
770
- <article-meta>
771
- <pub-date pub-type="epub">
772
- <day>18</day>
773
- <month>04</month>
774
- <year>2005</year>
775
- </pub-date>
776
- <volume>2</volume>
777
- <issue>4</issue>
778
- <fpage>e124</fpage>
779
- <lpage>e132</lpage>
780
- </article-meta>
781
- </front>
782
- </article>"#;
783
-
784
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
785
- assert!(metadata.publication_date.is_some());
786
- assert_eq!(metadata.volume, Some("2".to_string()));
787
- assert_eq!(metadata.issue, Some("4".to_string()));
788
- assert!(metadata.pages.is_some());
789
- }
790
-
791
- #[test]
792
- fn test_extract_jats_abstract() {
793
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
794
- <article>
795
- <front>
796
- <article-meta>
797
- <abstract>
798
- <sec>
799
- <title>Background</title>
800
- <p>This is the background information of the study.</p>
801
- </sec>
802
- <sec>
803
- <title>Methods</title>
804
- <p>We used quantitative analysis to evaluate the hypothesis.</p>
805
- </sec>
806
- </abstract>
807
- </article-meta>
808
- </front>
809
- </article>"#;
810
-
811
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
812
- assert!(metadata.abstract_text.is_some());
813
- let abstract_text = metadata.abstract_text.unwrap();
814
- assert!(abstract_text.contains("background"));
815
- assert!(abstract_text.contains("quantitative"));
816
- }
817
-
818
- #[test]
819
- fn test_extract_jats_tables_basic() {
820
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
821
- <article>
822
- <body>
823
- <table-wrap id="tbl1">
824
- <table>
825
- <thead>
826
- <tr>
827
- <th>Study</th>
828
- <th>Year</th>
829
- </tr>
830
- </thead>
831
- <tbody>
832
- <tr>
833
- <td>Study A</td>
834
- <td>2003</td>
835
- </tr>
836
- <tr>
837
- <td>Study B</td>
838
- <td>2004</td>
839
- </tr>
840
- </tbody>
841
- </table>
842
- </table-wrap>
843
- </body>
844
- </article>"#;
845
-
846
- let (_metadata, _content, _title, tables) = extract_jats_all_in_one(jats).expect("Table extraction failed");
847
- assert_eq!(tables.len(), 1);
848
- assert_eq!(tables[0].cells.len(), 3);
849
- }
850
-
851
- #[test]
852
- fn test_extract_jats_corresponding_author() {
853
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
854
- <article>
855
- <front>
856
- <article-meta>
857
- <author-notes>
858
- <corresp id="cor1">To whom correspondence should be addressed. E-mail: rwilliams@yale.edu</corresp>
859
- </author-notes>
860
- </article-meta>
861
- </front>
862
- </article>"#;
863
-
864
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
865
- assert!(metadata.corresponding_author.is_some());
866
- let corresp = metadata.corresponding_author.unwrap();
867
- assert!(corresp.contains("rwilliams"));
868
- }
869
-
870
- #[test]
871
- fn test_extract_jats_section_hierarchy() {
872
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
873
- <article>
874
- <front>
875
- <article-meta>
876
- <article-title>Article Title</article-title>
877
- </article-meta>
878
- </front>
879
- <body>
880
- <sec id="s1">
881
- <title>Introduction</title>
882
- <p>Intro content.</p>
883
- </sec>
884
- <sec id="s2">
885
- <title>Methods</title>
886
- <p>Methods content.</p>
887
- </sec>
888
- <sec id="s3">
889
- <title>Results</title>
890
- <p>Results content.</p>
891
- </sec>
892
- </body>
893
- </article>"#;
894
-
895
- let (_metadata, content, _title, _tables) = extract_jats_all_in_one(jats).expect("Parse failed");
896
- assert!(content.contains("Introduction"));
897
- assert!(content.contains("Methods"));
898
- assert!(content.contains("Results"));
899
- }
900
-
901
- #[test]
902
- fn test_jats_extractor_full_metadata_extraction() {
903
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
904
- <article>
905
- <front>
906
- <article-meta>
907
- <article-title>Sample Article</article-title>
908
- <contrib-group>
909
- <contrib contrib-type="author">
910
- <name>
911
- <surname>Smith</surname>
912
- <given-names>John</given-names>
913
- </name>
914
- </contrib>
915
- </contrib-group>
916
- <article-id pub-id-type="doi">10.1234/test</article-id>
917
- <kwd-group>
918
- <kwd>test</kwd>
919
- </kwd-group>
920
- <abstract>
921
- <p>Test abstract.</p>
922
- </abstract>
923
- </article-meta>
924
- </front>
925
- <body>
926
- <p>Sample content.</p>
927
- </body>
928
- </article>"#;
929
-
930
- let (metadata_extracted, _content, _title, _tables) =
931
- extract_jats_all_in_one(jats).expect("Metadata extraction failed");
932
- assert_eq!(metadata_extracted.title, "Sample Article");
933
- assert_eq!(metadata_extracted.authors.len(), 1);
934
- assert_eq!(metadata_extracted.doi, Some("10.1234/test".to_string()));
935
- assert_eq!(metadata_extracted.keywords.len(), 1);
936
- assert!(metadata_extracted.abstract_text.is_some());
937
- }
938
-
939
- #[test]
940
- fn test_jats_extractor_empty_article() {
941
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
942
- <article>
943
- <front>
944
- <article-meta>
945
- </article-meta>
946
- </front>
947
- <body>
948
- </body>
949
- </article>"#;
950
-
951
- let (metadata, content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
952
- assert!(metadata.title.is_empty());
953
- assert!(content.is_empty() || content.trim().is_empty());
954
- }
955
-
956
- #[test]
957
- fn test_extract_jats_journal_title() {
958
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
959
- <article>
960
- <front>
961
- <article-meta>
962
- <article-title>Test Article</article-title>
963
- <journal-title>Nature Medicine</journal-title>
964
- </article-meta>
965
- </front>
966
- </article>"#;
967
-
968
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
969
- assert_eq!(metadata.journal_title, Some("Nature Medicine".to_string()));
970
- }
971
-
972
- #[test]
973
- fn test_extract_jats_article_type() {
974
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
975
- <article article-type="research-article">
976
- <front>
977
- <article-meta>
978
- <article-title>Test Article</article-title>
979
- </article-meta>
980
- </front>
981
- </article>"#;
982
-
983
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
984
- assert_eq!(metadata.article_type, Some("research-article".to_string()));
985
- }
986
-
987
- #[test]
988
- fn test_extract_all_13_metadata_fields() {
989
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
990
- <article article-type="research-article">
991
- <front>
992
- <article-meta>
993
- <article-title>Full Metadata Test</article-title>
994
- <subtitle>A Complete Example</subtitle>
995
- <contrib-group>
996
- <contrib contrib-type="author">
997
- <name>
998
- <surname>Author</surname>
999
- <given-names>First</given-names>
1000
- </name>
1001
- </contrib>
1002
- </contrib-group>
1003
- <aff>Department of Testing, Test University</aff>
1004
- <article-id pub-id-type="doi">10.1234/full-test</article-id>
1005
- <article-id pub-id-type="pii">TEST-001</article-id>
1006
- <kwd-group>
1007
- <kwd>testing</kwd>
1008
- <kwd>metadata</kwd>
1009
- </kwd-group>
1010
- <pub-date pub-type="epub">
1011
- <year>2024</year>
1012
- </pub-date>
1013
- <volume>5</volume>
1014
- <issue>3</issue>
1015
- <fpage>100</fpage>
1016
- <lpage>110</lpage>
1017
- <journal-title>Test Journal</journal-title>
1018
- <abstract>
1019
- <p>This is a test abstract for all metadata fields.</p>
1020
- </abstract>
1021
- <author-notes>
1022
- <corresp>Correspondence: test@example.com</corresp>
1023
- </author-notes>
1024
- </article-meta>
1025
- </front>
1026
- <body>
1027
- <p>Test content.</p>
1028
- </body>
1029
- </article>"#;
1030
-
1031
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
1032
-
1033
- assert_eq!(metadata.title, "Full Metadata Test");
1034
- assert_eq!(metadata.subtitle, Some("A Complete Example".to_string()));
1035
- assert_eq!(metadata.authors.len(), 1);
1036
- assert!(metadata.authors[0].contains("Author"));
1037
- assert_eq!(metadata.affiliations.len(), 1);
1038
- assert!(metadata.affiliations[0].contains("Testing"));
1039
- assert_eq!(metadata.doi, Some("10.1234/full-test".to_string()));
1040
- assert_eq!(metadata.pii, Some("TEST-001".to_string()));
1041
- assert_eq!(metadata.keywords.len(), 2);
1042
- assert!(metadata.publication_date.is_some());
1043
- assert_eq!(metadata.volume, Some("5".to_string()));
1044
- assert_eq!(metadata.issue, Some("3".to_string()));
1045
- assert!(metadata.pages.is_some());
1046
- assert_eq!(metadata.journal_title, Some("Test Journal".to_string()));
1047
- assert_eq!(metadata.article_type, Some("research-article".to_string()));
1048
- assert!(metadata.abstract_text.is_some());
1049
- assert!(metadata.corresponding_author.is_some());
1050
- }
1051
- }
1
+ //! JATS (Journal Article Tag Suite) document extractor.
2
+ //!
3
+ //! This extractor handles JATS XML documents, the standard format for scientific journal articles
4
+ //! used by PubMed Central and major academic publishers.
5
+ //!
6
+ //! It extracts:
7
+ //! - Rich metadata (title, subtitle, authors with affiliations, DOI, PII, keywords, dates)
8
+ //! - Article abstract (regular and graphical)
9
+ //! - Section hierarchy and content (intro, methods, results, discussion)
10
+ //! - Paragraphs and text content
11
+ //! - Tables with captions
12
+ //! - Figures with captions
13
+ //! - Citations and references
14
+ //! - Supplementary material information
15
+
16
+ use crate::Result;
17
+ use crate::core::config::ExtractionConfig;
18
+ use crate::extraction::cells_to_markdown;
19
+ use crate::plugins::{DocumentExtractor, Plugin};
20
+ use crate::types::{ExtractionResult, Metadata, Table};
21
+ use async_trait::async_trait;
22
+ use quick_xml::Reader;
23
+ use quick_xml::events::Event;
24
+ #[cfg(feature = "tokio-runtime")]
25
+ use std::path::Path;
26
+
27
+ /// JATS document extractor.
28
+ ///
29
+ /// Supports JATS (Journal Article Tag Suite) XML documents in various versions,
30
+ /// handling both the full article structure and minimal JATS subsets.
31
+ pub struct JatsExtractor;
32
+
33
+ impl Default for JatsExtractor {
34
+ fn default() -> Self {
35
+ Self::new()
36
+ }
37
+ }
38
+
39
+ impl JatsExtractor {
40
+ pub fn new() -> Self {
41
+ Self
42
+ }
43
+ }
44
+
45
+ /// Extract text content from a JATS element and its children.
46
+ fn extract_text_content(reader: &mut Reader<&[u8]>) -> Result<String> {
47
+ let mut text = String::new();
48
+ let mut depth = 0;
49
+
50
+ loop {
51
+ match reader.read_event() {
52
+ Ok(Event::Start(_)) => {
53
+ depth += 1;
54
+ }
55
+ Ok(Event::End(_)) => {
56
+ if depth == 0 {
57
+ break;
58
+ }
59
+ depth -= 1;
60
+ if !text.is_empty() && !text.ends_with('\n') {
61
+ text.push(' ');
62
+ }
63
+ }
64
+ Ok(Event::Text(t)) => {
65
+ let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
66
+ if !decoded.trim().is_empty() {
67
+ text.push_str(&decoded);
68
+ text.push(' ');
69
+ }
70
+ }
71
+ Ok(Event::CData(t)) => {
72
+ let decoded = std::str::from_utf8(t.as_ref()).unwrap_or("").to_string();
73
+ if !decoded.trim().is_empty() {
74
+ text.push_str(&decoded);
75
+ text.push('\n');
76
+ }
77
+ }
78
+ Ok(Event::Eof) => break,
79
+ Err(e) => {
80
+ return Err(crate::error::KreuzbergError::parsing(format!(
81
+ "XML parsing error: {}",
82
+ e
83
+ )));
84
+ }
85
+ _ => {}
86
+ }
87
+ }
88
+
89
+ Ok(text.trim().to_string())
90
+ }
91
+
92
+ /// Structure to hold extracted JATS metadata.
93
+ #[derive(Debug, Clone, Default)]
94
+ struct JatsMetadataExtracted {
95
+ title: String,
96
+ subtitle: Option<String>,
97
+ authors: Vec<String>,
98
+ affiliations: Vec<String>,
99
+ doi: Option<String>,
100
+ pii: Option<String>,
101
+ keywords: Vec<String>,
102
+ publication_date: Option<String>,
103
+ volume: Option<String>,
104
+ issue: Option<String>,
105
+ pages: Option<String>,
106
+ journal_title: Option<String>,
107
+ article_type: Option<String>,
108
+ abstract_text: Option<String>,
109
+ corresponding_author: Option<String>,
110
+ }
111
+
112
+ /// Extract all content in a single optimized pass.
113
+ /// Combines metadata extraction, content parsing, and table extraction into one pass.
114
+ fn extract_jats_all_in_one(content: &str) -> Result<(JatsMetadataExtracted, String, String, Vec<Table>)> {
115
+ let mut reader = Reader::from_str(content);
116
+ let mut metadata = JatsMetadataExtracted::default();
117
+ let mut body_content = String::new();
118
+ let mut title = String::new();
119
+
120
+ let mut in_article_meta = false;
121
+ let mut in_article_title = false;
122
+ let mut in_subtitle = false;
123
+ let mut in_contrib = false;
124
+ let mut in_name = false;
125
+ let mut in_aff = false;
126
+ let mut in_abstract = false;
127
+ let mut in_kwd_group = false;
128
+ let mut in_kwd = false;
129
+ let mut current_author = String::new();
130
+ let mut current_aff = String::new();
131
+ let mut abstract_content = String::new();
132
+
133
+ let mut in_body = false;
134
+ let mut in_section = false;
135
+ let mut in_para = false;
136
+
137
+ let mut in_table = false;
138
+ let mut in_thead = false;
139
+ let mut in_tbody = false;
140
+ let mut in_row = false;
141
+ let mut current_table: Vec<Vec<String>> = Vec::new();
142
+ let mut current_row: Vec<String> = Vec::new();
143
+ let mut tables = Vec::new();
144
+ let mut table_index = 0;
145
+
146
+ loop {
147
+ match reader.read_event() {
148
+ Ok(Event::Start(e)) => {
149
+ let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
150
+
151
+ match tag.as_str() {
152
+ "article" => {
153
+ for attr in e.attributes() {
154
+ if let Ok(attr) = attr
155
+ && String::from_utf8_lossy(attr.key.as_ref()) == "article-type"
156
+ {
157
+ metadata.article_type = Some(String::from_utf8_lossy(attr.value.as_ref()).to_string());
158
+ }
159
+ }
160
+ }
161
+ "article-meta" => {
162
+ in_article_meta = true;
163
+ }
164
+ "article-title" if in_article_meta => {
165
+ in_article_title = true;
166
+ }
167
+ "subtitle" if in_article_meta => {
168
+ in_subtitle = true;
169
+ }
170
+ "contrib" if in_article_meta => {
171
+ in_contrib = true;
172
+ current_author.clear();
173
+ }
174
+ "name" if in_contrib => {
175
+ in_name = true;
176
+ }
177
+ "aff" if in_article_meta => {
178
+ in_aff = true;
179
+ current_aff.clear();
180
+ }
181
+ "article-id" if in_article_meta => {
182
+ let mut id_type = String::new();
183
+ for attr in e.attributes() {
184
+ if let Ok(attr) = attr
185
+ && String::from_utf8_lossy(attr.key.as_ref()) == "pub-id-type"
186
+ {
187
+ id_type = String::from_utf8_lossy(attr.value.as_ref()).to_string();
188
+ }
189
+ }
190
+
191
+ let id_text = extract_text_content(&mut reader)?;
192
+ match id_type.as_str() {
193
+ "doi" => metadata.doi = Some(id_text),
194
+ "pii" => metadata.pii = Some(id_text),
195
+ _ => {}
196
+ }
197
+ continue;
198
+ }
199
+ "volume" if in_article_meta => {
200
+ let vol_text = extract_text_content(&mut reader)?;
201
+ metadata.volume = Some(vol_text);
202
+ continue;
203
+ }
204
+ "issue" if in_article_meta => {
205
+ let issue_text = extract_text_content(&mut reader)?;
206
+ metadata.issue = Some(issue_text);
207
+ continue;
208
+ }
209
+ "fpage" | "lpage" if in_article_meta => {
210
+ let page_text = extract_text_content(&mut reader)?;
211
+ if let Some(pages) = &mut metadata.pages {
212
+ pages.push('-');
213
+ pages.push_str(&page_text);
214
+ } else {
215
+ metadata.pages = Some(page_text);
216
+ }
217
+ continue;
218
+ }
219
+ "pub-date" if in_article_meta => {
220
+ let date_text = extract_text_content(&mut reader)?;
221
+ if metadata.publication_date.is_none() {
222
+ metadata.publication_date = Some(date_text);
223
+ }
224
+ continue;
225
+ }
226
+ "journal-title" if in_article_meta => {
227
+ let journal_text = extract_text_content(&mut reader)?;
228
+ if metadata.journal_title.is_none() {
229
+ metadata.journal_title = Some(journal_text);
230
+ }
231
+ continue;
232
+ }
233
+ "abstract" if in_article_meta => {
234
+ in_abstract = true;
235
+ abstract_content.clear();
236
+ }
237
+ "kwd-group" if in_article_meta => {
238
+ in_kwd_group = true;
239
+ }
240
+ "kwd" if in_kwd_group => {
241
+ in_kwd = true;
242
+ }
243
+ "corresp" if in_article_meta => {
244
+ let corresp_text = extract_text_content(&mut reader)?;
245
+ metadata.corresponding_author = Some(corresp_text);
246
+ continue;
247
+ }
248
+ "body" => {
249
+ in_body = true;
250
+ }
251
+ "sec" if in_body => {
252
+ in_section = true;
253
+ }
254
+ "title" if (in_section || in_body) && !in_article_title => {
255
+ let section_title = extract_text_content(&mut reader)?;
256
+ if !section_title.is_empty() {
257
+ body_content.push_str("## ");
258
+ body_content.push_str(&section_title);
259
+ body_content.push_str("\n\n");
260
+ }
261
+ continue;
262
+ }
263
+ "p" if in_body || in_section => {
264
+ in_para = true;
265
+ }
266
+ "table" => {
267
+ in_table = true;
268
+ current_table.clear();
269
+ }
270
+ "thead" if in_table => {
271
+ in_thead = true;
272
+ }
273
+ "tbody" if in_table => {
274
+ in_tbody = true;
275
+ }
276
+ "tr" if (in_thead || in_tbody) && in_table => {
277
+ in_row = true;
278
+ current_row.clear();
279
+ }
280
+ "td" | "th" if in_row => {
281
+ let mut cell_text = String::new();
282
+ let mut cell_depth = 0;
283
+
284
+ loop {
285
+ match reader.read_event() {
286
+ Ok(Event::Start(_)) => {
287
+ cell_depth += 1;
288
+ }
289
+ Ok(Event::End(e)) => {
290
+ let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
291
+ if (tag == "td" || tag == "th") && cell_depth == 0 {
292
+ break;
293
+ }
294
+ if cell_depth > 0 {
295
+ cell_depth -= 1;
296
+ }
297
+ }
298
+ Ok(Event::Text(t)) => {
299
+ let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
300
+ if !decoded.trim().is_empty() {
301
+ if !cell_text.is_empty() {
302
+ cell_text.push(' ');
303
+ }
304
+ cell_text.push_str(decoded.trim());
305
+ }
306
+ }
307
+ Ok(Event::Eof) => break,
308
+ Err(e) => {
309
+ return Err(crate::error::KreuzbergError::parsing(format!(
310
+ "XML parsing error: {}",
311
+ e
312
+ )));
313
+ }
314
+ _ => {}
315
+ }
316
+ }
317
+
318
+ current_row.push(cell_text);
319
+ }
320
+ _ => {}
321
+ }
322
+ }
323
+ Ok(Event::End(e)) => {
324
+ let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
325
+
326
+ match tag.as_str() {
327
+ "article-meta" => {
328
+ in_article_meta = false;
329
+ }
330
+ "article-title" if in_article_title => {
331
+ in_article_title = false;
332
+ }
333
+ "subtitle" if in_subtitle => {
334
+ in_subtitle = false;
335
+ }
336
+ "contrib" if in_contrib => {
337
+ if !current_author.is_empty() {
338
+ metadata.authors.push(current_author.clone());
339
+ }
340
+ in_contrib = false;
341
+ current_author.clear();
342
+ }
343
+ "name" if in_name => {
344
+ in_name = false;
345
+ }
346
+ "aff" if in_aff => {
347
+ if !current_aff.is_empty() {
348
+ metadata.affiliations.push(current_aff.clone());
349
+ }
350
+ in_aff = false;
351
+ current_aff.clear();
352
+ }
353
+ "abstract" if in_abstract => {
354
+ in_abstract = false;
355
+ metadata.abstract_text = Some(abstract_content.trim().to_string());
356
+ }
357
+ "kwd-group" if in_kwd_group => {
358
+ in_kwd_group = false;
359
+ }
360
+ "kwd" if in_kwd => {
361
+ in_kwd = false;
362
+ }
363
+ "body" => {
364
+ in_body = false;
365
+ }
366
+ "sec" if in_section => {
367
+ in_section = false;
368
+ }
369
+ "p" if in_para => {
370
+ in_para = false;
371
+ }
372
+ "table" if in_table => {
373
+ if !current_table.is_empty() {
374
+ let markdown = cells_to_markdown(&current_table);
375
+ tables.push(Table {
376
+ cells: current_table.clone(),
377
+ markdown,
378
+ page_number: table_index + 1,
379
+ });
380
+ table_index += 1;
381
+ current_table.clear();
382
+ }
383
+ in_table = false;
384
+ }
385
+ "thead" if in_thead => {
386
+ in_thead = false;
387
+ }
388
+ "tbody" if in_tbody => {
389
+ in_tbody = false;
390
+ }
391
+ "tr" if in_row => {
392
+ if !current_row.is_empty() {
393
+ current_table.push(current_row.clone());
394
+ current_row.clear();
395
+ }
396
+ in_row = false;
397
+ }
398
+ _ => {}
399
+ }
400
+ }
401
+ Ok(Event::Text(t)) => {
402
+ let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
403
+ let trimmed = decoded.trim();
404
+
405
+ if !trimmed.is_empty() {
406
+ if in_article_title && metadata.title.is_empty() {
407
+ metadata.title.push_str(trimmed);
408
+ } else if in_subtitle && metadata.subtitle.is_none() {
409
+ metadata.subtitle = Some(trimmed.to_string());
410
+ } else if in_name {
411
+ if !current_author.is_empty() {
412
+ current_author.push(' ');
413
+ }
414
+ current_author.push_str(trimmed);
415
+ } else if in_aff {
416
+ if !current_aff.is_empty() {
417
+ current_aff.push(' ');
418
+ }
419
+ current_aff.push_str(trimmed);
420
+ } else if in_abstract {
421
+ if !abstract_content.is_empty() {
422
+ abstract_content.push(' ');
423
+ }
424
+ abstract_content.push_str(trimmed);
425
+ } else if in_kwd {
426
+ metadata.keywords.push(trimmed.to_string());
427
+ } else if in_para && in_body {
428
+ body_content.push_str(trimmed);
429
+ body_content.push_str("\n\n");
430
+ }
431
+ }
432
+ }
433
+ Ok(Event::Eof) => break,
434
+ Err(e) => {
435
+ return Err(crate::error::KreuzbergError::parsing(format!(
436
+ "XML parsing error: {}",
437
+ e
438
+ )));
439
+ }
440
+ _ => {}
441
+ }
442
+ }
443
+
444
+ let mut final_output = body_content;
445
+ if !metadata.title.is_empty() {
446
+ final_output = format!("# {}\n\n{}", metadata.title, final_output);
447
+ title = metadata.title.clone();
448
+ }
449
+
450
+ Ok((metadata, final_output.trim().to_string(), title, tables))
451
+ }
452
+
453
+ impl Plugin for JatsExtractor {
454
+ fn name(&self) -> &str {
455
+ "jats-extractor"
456
+ }
457
+
458
+ fn version(&self) -> String {
459
+ env!("CARGO_PKG_VERSION").to_string()
460
+ }
461
+
462
+ fn initialize(&self) -> Result<()> {
463
+ Ok(())
464
+ }
465
+
466
+ fn shutdown(&self) -> Result<()> {
467
+ Ok(())
468
+ }
469
+ }
470
+
471
+ #[async_trait]
472
+ impl DocumentExtractor for JatsExtractor {
473
+ #[cfg_attr(
474
+ feature = "otel",
475
+ tracing::instrument(
476
+ skip(self, content, config),
477
+ fields(
478
+ extractor.name = self.name(),
479
+ content.size_bytes = content.len(),
480
+ )
481
+ )
482
+ )]
483
+ async fn extract_bytes(
484
+ &self,
485
+ content: &[u8],
486
+ mime_type: &str,
487
+ config: &ExtractionConfig,
488
+ ) -> Result<ExtractionResult> {
489
+ let _ = config;
490
+ let jats_content = std::str::from_utf8(content)
491
+ .map(|s| s.to_string())
492
+ .unwrap_or_else(|_| String::from_utf8_lossy(content).to_string());
493
+
494
+ let (jats_metadata, extracted_content, _title, tables) = extract_jats_all_in_one(&jats_content)?;
495
+
496
+ let mut metadata = Metadata::default();
497
+ let mut subject_parts = Vec::new();
498
+
499
+ if !jats_metadata.title.is_empty() {
500
+ metadata.subject = Some(jats_metadata.title.clone());
501
+ subject_parts.push(format!("Title: {}", jats_metadata.title));
502
+ }
503
+
504
+ if let Some(subtitle) = &jats_metadata.subtitle {
505
+ subject_parts.push(format!("Subtitle: {}", subtitle));
506
+ }
507
+
508
+ if !jats_metadata.authors.is_empty() {
509
+ subject_parts.push(format!("Authors: {}", jats_metadata.authors.join("; ")));
510
+ }
511
+
512
+ if !jats_metadata.affiliations.is_empty() {
513
+ subject_parts.push(format!("Affiliations: {}", jats_metadata.affiliations.join("; ")));
514
+ }
515
+
516
+ if let Some(doi) = &jats_metadata.doi {
517
+ subject_parts.push(format!("DOI: {}", doi));
518
+ }
519
+
520
+ if let Some(pii) = &jats_metadata.pii {
521
+ subject_parts.push(format!("PII: {}", pii));
522
+ }
523
+
524
+ if !jats_metadata.keywords.is_empty() {
525
+ subject_parts.push(format!("Keywords: {}", jats_metadata.keywords.join("; ")));
526
+ }
527
+
528
+ if let Some(date) = &jats_metadata.publication_date {
529
+ metadata.created_at = Some(date.clone());
530
+ subject_parts.push(format!("Publication Date: {}", date));
531
+ }
532
+
533
+ if let Some(volume) = &jats_metadata.volume {
534
+ subject_parts.push(format!("Volume: {}", volume));
535
+ }
536
+
537
+ if let Some(issue) = &jats_metadata.issue {
538
+ subject_parts.push(format!("Issue: {}", issue));
539
+ }
540
+
541
+ if let Some(pages) = &jats_metadata.pages {
542
+ subject_parts.push(format!("Pages: {}", pages));
543
+ }
544
+
545
+ if let Some(journal_title) = &jats_metadata.journal_title {
546
+ subject_parts.push(format!("Journal: {}", journal_title));
547
+ }
548
+
549
+ if let Some(article_type) = &jats_metadata.article_type {
550
+ subject_parts.push(format!("Article Type: {}", article_type));
551
+ }
552
+
553
+ if let Some(abstract_text) = &jats_metadata.abstract_text {
554
+ subject_parts.push(format!("Abstract: {}", abstract_text));
555
+ }
556
+
557
+ if let Some(corresp_author) = &jats_metadata.corresponding_author {
558
+ subject_parts.push(format!("Corresponding Author: {}", corresp_author));
559
+ }
560
+
561
+ if !subject_parts.is_empty() {
562
+ metadata.subject = Some(subject_parts.join(" | "));
563
+ }
564
+
565
+ Ok(ExtractionResult {
566
+ content: extracted_content,
567
+ mime_type: mime_type.to_string(),
568
+ metadata,
569
+ tables,
570
+ detected_languages: None,
571
+ chunks: None,
572
+ images: None,
573
+ pages: None,
574
+ })
575
+ }
576
+
577
+ #[cfg(feature = "tokio-runtime")]
578
+ #[cfg_attr(
579
+ feature = "otel",
580
+ tracing::instrument(
581
+ skip(self, path, config),
582
+ fields(
583
+ extractor.name = self.name(),
584
+ )
585
+ )
586
+ )]
587
+ #[cfg(feature = "tokio-runtime")]
588
+ async fn extract_file(&self, path: &Path, mime_type: &str, config: &ExtractionConfig) -> Result<ExtractionResult> {
589
+ let bytes = tokio::fs::read(path).await?;
590
+ self.extract_bytes(&bytes, mime_type, config).await
591
+ }
592
+
593
+ fn supported_mime_types(&self) -> &[&str] {
594
+ &["application/x-jats+xml", "text/jats"]
595
+ }
596
+
597
+ fn priority(&self) -> i32 {
598
+ 50
599
+ }
600
+ }
601
+
602
+ #[cfg(test)]
603
+ mod tests {
604
+ use super::*;
605
+
606
+ #[test]
607
+ fn test_jats_extractor_plugin_interface() {
608
+ let extractor = JatsExtractor::new();
609
+ assert_eq!(extractor.name(), "jats-extractor");
610
+ assert!(extractor.initialize().is_ok());
611
+ assert!(extractor.shutdown().is_ok());
612
+ }
613
+
614
+ #[test]
615
+ fn test_jats_extractor_supported_mime_types() {
616
+ let extractor = JatsExtractor::new();
617
+ let mime_types = extractor.supported_mime_types();
618
+ assert_eq!(mime_types.len(), 2);
619
+ assert!(mime_types.contains(&"application/x-jats+xml"));
620
+ assert!(mime_types.contains(&"text/jats"));
621
+ }
622
+
623
+ #[test]
624
+ fn test_jats_extractor_priority() {
625
+ let extractor = JatsExtractor::new();
626
+ assert_eq!(extractor.priority(), 50);
627
+ }
628
+
629
+ #[test]
630
+ fn test_parse_simple_jats_article() {
631
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
632
+ <article>
633
+ <front>
634
+ <article-meta>
635
+ <article-title>Test Article Title</article-title>
636
+ </article-meta>
637
+ </front>
638
+ <body>
639
+ <p>Test content paragraph.</p>
640
+ </body>
641
+ </article>"#;
642
+
643
+ let (metadata, content, title, _tables) = extract_jats_all_in_one(jats).expect("Parse failed");
644
+ assert_eq!(title, "Test Article Title");
645
+ assert_eq!(metadata.title, "Test Article Title");
646
+ assert!(content.contains("Test Article Title"));
647
+ assert!(content.contains("Test content"));
648
+ }
649
+
650
+ #[test]
651
+ fn test_extract_jats_title() {
652
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
653
+ <article>
654
+ <front>
655
+ <article-meta>
656
+ <article-title>Effects of Caffeine on Human Health</article-title>
657
+ </article-meta>
658
+ </front>
659
+ </article>"#;
660
+
661
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
662
+ assert_eq!(metadata.title, "Effects of Caffeine on Human Health");
663
+ }
664
+
665
+ #[test]
666
+ fn test_extract_jats_subtitle() {
667
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
668
+ <article>
669
+ <front>
670
+ <article-meta>
671
+ <article-title>Main Title</article-title>
672
+ <subtitle>A Systematic Review</subtitle>
673
+ </article-meta>
674
+ </front>
675
+ </article>"#;
676
+
677
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
678
+ assert_eq!(metadata.title, "Main Title");
679
+ assert_eq!(metadata.subtitle, Some("A Systematic Review".to_string()));
680
+ }
681
+
682
+ #[test]
683
+ fn test_extract_jats_authors() {
684
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
685
+ <article>
686
+ <front>
687
+ <article-meta>
688
+ <contrib-group>
689
+ <contrib contrib-type="author">
690
+ <name>
691
+ <surname>Smith</surname>
692
+ <given-names>John A.</given-names>
693
+ </name>
694
+ </contrib>
695
+ <contrib contrib-type="author">
696
+ <name>
697
+ <surname>Johnson</surname>
698
+ <given-names>Jane B.</given-names>
699
+ </name>
700
+ </contrib>
701
+ </contrib-group>
702
+ </article-meta>
703
+ </front>
704
+ </article>"#;
705
+
706
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
707
+ assert_eq!(metadata.authors.len(), 2);
708
+ assert!(metadata.authors[0].contains("Smith"));
709
+ assert!(metadata.authors[1].contains("Johnson"));
710
+ }
711
+
712
+ #[test]
713
+ fn test_extract_jats_affiliations() {
714
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
715
+ <article>
716
+ <front>
717
+ <article-meta>
718
+ <aff id="aff1">Department of Medicine, Harvard University, Cambridge, MA</aff>
719
+ <aff id="aff2">Center for Health Research, Boston Medical Center, Boston, MA</aff>
720
+ </article-meta>
721
+ </front>
722
+ </article>"#;
723
+
724
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
725
+ assert_eq!(metadata.affiliations.len(), 2);
726
+ assert!(metadata.affiliations[0].contains("Harvard"));
727
+ assert!(metadata.affiliations[1].contains("Boston"));
728
+ }
729
+
730
+ #[test]
731
+ fn test_extract_jats_doi_and_pii() {
732
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
733
+ <article>
734
+ <front>
735
+ <article-meta>
736
+ <article-id pub-id-type="doi">10.1371/journal.pmed.0020124</article-id>
737
+ <article-id pub-id-type="pii">05-PLME-RA-0071R2</article-id>
738
+ </article-meta>
739
+ </front>
740
+ </article>"#;
741
+
742
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
743
+ assert_eq!(metadata.doi, Some("10.1371/journal.pmed.0020124".to_string()));
744
+ assert_eq!(metadata.pii, Some("05-PLME-RA-0071R2".to_string()));
745
+ }
746
+
747
+ #[test]
748
+ fn test_extract_jats_keywords() {
749
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
750
+ <article>
751
+ <front>
752
+ <article-meta>
753
+ <kwd-group>
754
+ <kwd>caffeine</kwd>
755
+ <kwd>meta-analysis</kwd>
756
+ <kwd>systematic review</kwd>
757
+ </kwd-group>
758
+ </article-meta>
759
+ </front>
760
+ </article>"#;
761
+
762
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
763
+ assert_eq!(metadata.keywords.len(), 3);
764
+ assert!(metadata.keywords.contains(&"caffeine".to_string()));
765
+ assert!(metadata.keywords.contains(&"meta-analysis".to_string()));
766
+ }
767
+
768
+ #[test]
769
+ fn test_extract_jats_publication_info() {
770
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
771
+ <article>
772
+ <front>
773
+ <article-meta>
774
+ <pub-date pub-type="epub">
775
+ <day>18</day>
776
+ <month>04</month>
777
+ <year>2005</year>
778
+ </pub-date>
779
+ <volume>2</volume>
780
+ <issue>4</issue>
781
+ <fpage>e124</fpage>
782
+ <lpage>e132</lpage>
783
+ </article-meta>
784
+ </front>
785
+ </article>"#;
786
+
787
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
788
+ assert!(metadata.publication_date.is_some());
789
+ assert_eq!(metadata.volume, Some("2".to_string()));
790
+ assert_eq!(metadata.issue, Some("4".to_string()));
791
+ assert!(metadata.pages.is_some());
792
+ }
793
+
794
+ #[test]
795
+ fn test_extract_jats_abstract() {
796
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
797
+ <article>
798
+ <front>
799
+ <article-meta>
800
+ <abstract>
801
+ <sec>
802
+ <title>Background</title>
803
+ <p>This is the background information of the study.</p>
804
+ </sec>
805
+ <sec>
806
+ <title>Methods</title>
807
+ <p>We used quantitative analysis to evaluate the hypothesis.</p>
808
+ </sec>
809
+ </abstract>
810
+ </article-meta>
811
+ </front>
812
+ </article>"#;
813
+
814
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
815
+ assert!(metadata.abstract_text.is_some());
816
+ let abstract_text = metadata.abstract_text.unwrap();
817
+ assert!(abstract_text.contains("background"));
818
+ assert!(abstract_text.contains("quantitative"));
819
+ }
820
+
821
+ #[test]
822
+ fn test_extract_jats_tables_basic() {
823
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
824
+ <article>
825
+ <body>
826
+ <table-wrap id="tbl1">
827
+ <table>
828
+ <thead>
829
+ <tr>
830
+ <th>Study</th>
831
+ <th>Year</th>
832
+ </tr>
833
+ </thead>
834
+ <tbody>
835
+ <tr>
836
+ <td>Study A</td>
837
+ <td>2003</td>
838
+ </tr>
839
+ <tr>
840
+ <td>Study B</td>
841
+ <td>2004</td>
842
+ </tr>
843
+ </tbody>
844
+ </table>
845
+ </table-wrap>
846
+ </body>
847
+ </article>"#;
848
+
849
+ let (_metadata, _content, _title, tables) = extract_jats_all_in_one(jats).expect("Table extraction failed");
850
+ assert_eq!(tables.len(), 1);
851
+ assert_eq!(tables[0].cells.len(), 3);
852
+ }
853
+
854
+ #[test]
855
+ fn test_extract_jats_corresponding_author() {
856
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
857
+ <article>
858
+ <front>
859
+ <article-meta>
860
+ <author-notes>
861
+ <corresp id="cor1">To whom correspondence should be addressed. E-mail: rwilliams@yale.edu</corresp>
862
+ </author-notes>
863
+ </article-meta>
864
+ </front>
865
+ </article>"#;
866
+
867
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
868
+ assert!(metadata.corresponding_author.is_some());
869
+ let corresp = metadata.corresponding_author.unwrap();
870
+ assert!(corresp.contains("rwilliams"));
871
+ }
872
+
873
+ #[test]
874
+ fn test_extract_jats_section_hierarchy() {
875
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
876
+ <article>
877
+ <front>
878
+ <article-meta>
879
+ <article-title>Article Title</article-title>
880
+ </article-meta>
881
+ </front>
882
+ <body>
883
+ <sec id="s1">
884
+ <title>Introduction</title>
885
+ <p>Intro content.</p>
886
+ </sec>
887
+ <sec id="s2">
888
+ <title>Methods</title>
889
+ <p>Methods content.</p>
890
+ </sec>
891
+ <sec id="s3">
892
+ <title>Results</title>
893
+ <p>Results content.</p>
894
+ </sec>
895
+ </body>
896
+ </article>"#;
897
+
898
+ let (_metadata, content, _title, _tables) = extract_jats_all_in_one(jats).expect("Parse failed");
899
+ assert!(content.contains("Introduction"));
900
+ assert!(content.contains("Methods"));
901
+ assert!(content.contains("Results"));
902
+ }
903
+
904
+ #[test]
905
+ fn test_jats_extractor_full_metadata_extraction() {
906
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
907
+ <article>
908
+ <front>
909
+ <article-meta>
910
+ <article-title>Sample Article</article-title>
911
+ <contrib-group>
912
+ <contrib contrib-type="author">
913
+ <name>
914
+ <surname>Smith</surname>
915
+ <given-names>John</given-names>
916
+ </name>
917
+ </contrib>
918
+ </contrib-group>
919
+ <article-id pub-id-type="doi">10.1234/test</article-id>
920
+ <kwd-group>
921
+ <kwd>test</kwd>
922
+ </kwd-group>
923
+ <abstract>
924
+ <p>Test abstract.</p>
925
+ </abstract>
926
+ </article-meta>
927
+ </front>
928
+ <body>
929
+ <p>Sample content.</p>
930
+ </body>
931
+ </article>"#;
932
+
933
+ let (metadata_extracted, _content, _title, _tables) =
934
+ extract_jats_all_in_one(jats).expect("Metadata extraction failed");
935
+ assert_eq!(metadata_extracted.title, "Sample Article");
936
+ assert_eq!(metadata_extracted.authors.len(), 1);
937
+ assert_eq!(metadata_extracted.doi, Some("10.1234/test".to_string()));
938
+ assert_eq!(metadata_extracted.keywords.len(), 1);
939
+ assert!(metadata_extracted.abstract_text.is_some());
940
+ }
941
+
942
+ #[test]
943
+ fn test_jats_extractor_empty_article() {
944
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
945
+ <article>
946
+ <front>
947
+ <article-meta>
948
+ </article-meta>
949
+ </front>
950
+ <body>
951
+ </body>
952
+ </article>"#;
953
+
954
+ let (metadata, content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
955
+ assert!(metadata.title.is_empty());
956
+ assert!(content.is_empty() || content.trim().is_empty());
957
+ }
958
+
959
+ #[test]
960
+ fn test_extract_jats_journal_title() {
961
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
962
+ <article>
963
+ <front>
964
+ <article-meta>
965
+ <article-title>Test Article</article-title>
966
+ <journal-title>Nature Medicine</journal-title>
967
+ </article-meta>
968
+ </front>
969
+ </article>"#;
970
+
971
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
972
+ assert_eq!(metadata.journal_title, Some("Nature Medicine".to_string()));
973
+ }
974
+
975
+ #[test]
976
+ fn test_extract_jats_article_type() {
977
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
978
+ <article article-type="research-article">
979
+ <front>
980
+ <article-meta>
981
+ <article-title>Test Article</article-title>
982
+ </article-meta>
983
+ </front>
984
+ </article>"#;
985
+
986
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
987
+ assert_eq!(metadata.article_type, Some("research-article".to_string()));
988
+ }
989
+
990
+ #[test]
991
+ fn test_extract_all_13_metadata_fields() {
992
+ let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
993
+ <article article-type="research-article">
994
+ <front>
995
+ <article-meta>
996
+ <article-title>Full Metadata Test</article-title>
997
+ <subtitle>A Complete Example</subtitle>
998
+ <contrib-group>
999
+ <contrib contrib-type="author">
1000
+ <name>
1001
+ <surname>Author</surname>
1002
+ <given-names>First</given-names>
1003
+ </name>
1004
+ </contrib>
1005
+ </contrib-group>
1006
+ <aff>Department of Testing, Test University</aff>
1007
+ <article-id pub-id-type="doi">10.1234/full-test</article-id>
1008
+ <article-id pub-id-type="pii">TEST-001</article-id>
1009
+ <kwd-group>
1010
+ <kwd>testing</kwd>
1011
+ <kwd>metadata</kwd>
1012
+ </kwd-group>
1013
+ <pub-date pub-type="epub">
1014
+ <year>2024</year>
1015
+ </pub-date>
1016
+ <volume>5</volume>
1017
+ <issue>3</issue>
1018
+ <fpage>100</fpage>
1019
+ <lpage>110</lpage>
1020
+ <journal-title>Test Journal</journal-title>
1021
+ <abstract>
1022
+ <p>This is a test abstract for all metadata fields.</p>
1023
+ </abstract>
1024
+ <author-notes>
1025
+ <corresp>Correspondence: test@example.com</corresp>
1026
+ </author-notes>
1027
+ </article-meta>
1028
+ </front>
1029
+ <body>
1030
+ <p>Test content.</p>
1031
+ </body>
1032
+ </article>"#;
1033
+
1034
+ let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
1035
+
1036
+ assert_eq!(metadata.title, "Full Metadata Test");
1037
+ assert_eq!(metadata.subtitle, Some("A Complete Example".to_string()));
1038
+ assert_eq!(metadata.authors.len(), 1);
1039
+ assert!(metadata.authors[0].contains("Author"));
1040
+ assert_eq!(metadata.affiliations.len(), 1);
1041
+ assert!(metadata.affiliations[0].contains("Testing"));
1042
+ assert_eq!(metadata.doi, Some("10.1234/full-test".to_string()));
1043
+ assert_eq!(metadata.pii, Some("TEST-001".to_string()));
1044
+ assert_eq!(metadata.keywords.len(), 2);
1045
+ assert!(metadata.publication_date.is_some());
1046
+ assert_eq!(metadata.volume, Some("5".to_string()));
1047
+ assert_eq!(metadata.issue, Some("3".to_string()));
1048
+ assert!(metadata.pages.is_some());
1049
+ assert_eq!(metadata.journal_title, Some("Test Journal".to_string()));
1050
+ assert_eq!(metadata.article_type, Some("research-article".to_string()));
1051
+ assert!(metadata.abstract_text.is_some());
1052
+ assert!(metadata.corresponding_author.is_some());
1053
+ }
1054
+ }