kreuzberg 4.0.0.pre.rc.13 → 4.0.0.pre.rc.14

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 (369) 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 +538 -538
  6. data/Gemfile +8 -8
  7. data/Gemfile.lock +105 -2
  8. data/README.md +454 -454
  9. data/Rakefile +33 -25
  10. data/Steepfile +47 -47
  11. data/examples/async_patterns.rb +341 -341
  12. data/ext/kreuzberg_rb/extconf.rb +45 -45
  13. data/ext/kreuzberg_rb/native/.cargo/config.toml +2 -2
  14. data/ext/kreuzberg_rb/native/Cargo.lock +6940 -6941
  15. data/ext/kreuzberg_rb/native/Cargo.toml +54 -54
  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 +3158 -3158
  23. data/extconf.rb +28 -28
  24. data/kreuzberg.gemspec +214 -214
  25. data/lib/kreuzberg/api_proxy.rb +142 -142
  26. data/lib/kreuzberg/cache_api.rb +81 -81
  27. data/lib/kreuzberg/cli.rb +55 -55
  28. data/lib/kreuzberg/cli_proxy.rb +127 -127
  29. data/lib/kreuzberg/config.rb +724 -724
  30. data/lib/kreuzberg/error_context.rb +80 -80
  31. data/lib/kreuzberg/errors.rb +118 -118
  32. data/lib/kreuzberg/extraction_api.rb +340 -340
  33. data/lib/kreuzberg/mcp_proxy.rb +186 -186
  34. data/lib/kreuzberg/ocr_backend_protocol.rb +113 -113
  35. data/lib/kreuzberg/post_processor_protocol.rb +86 -86
  36. data/lib/kreuzberg/result.rb +279 -279
  37. data/lib/kreuzberg/setup_lib_path.rb +80 -80
  38. data/lib/kreuzberg/validator_protocol.rb +89 -89
  39. data/lib/kreuzberg/version.rb +5 -5
  40. data/lib/kreuzberg.rb +109 -109
  41. data/lib/{pdfium.dll → libpdfium.dylib} +0 -0
  42. data/sig/kreuzberg/internal.rbs +184 -184
  43. data/sig/kreuzberg.rbs +546 -546
  44. data/spec/binding/cache_spec.rb +227 -227
  45. data/spec/binding/cli_proxy_spec.rb +85 -85
  46. data/spec/binding/cli_spec.rb +55 -55
  47. data/spec/binding/config_spec.rb +345 -345
  48. data/spec/binding/config_validation_spec.rb +283 -283
  49. data/spec/binding/error_handling_spec.rb +213 -213
  50. data/spec/binding/errors_spec.rb +66 -66
  51. data/spec/binding/plugins/ocr_backend_spec.rb +307 -307
  52. data/spec/binding/plugins/postprocessor_spec.rb +269 -269
  53. data/spec/binding/plugins/validator_spec.rb +274 -274
  54. data/spec/fixtures/config.toml +39 -39
  55. data/spec/fixtures/config.yaml +41 -41
  56. data/spec/fixtures/invalid_config.toml +4 -4
  57. data/spec/smoke/package_spec.rb +178 -178
  58. data/spec/spec_helper.rb +42 -42
  59. data/vendor/Cargo.toml +1 -1
  60. data/vendor/kreuzberg/Cargo.toml +5 -5
  61. data/vendor/kreuzberg/README.md +230 -230
  62. data/vendor/kreuzberg/benches/otel_overhead.rs +48 -48
  63. data/vendor/kreuzberg/build.rs +843 -843
  64. data/vendor/kreuzberg/src/api/error.rs +81 -81
  65. data/vendor/kreuzberg/src/api/handlers.rs +199 -199
  66. data/vendor/kreuzberg/src/api/mod.rs +79 -79
  67. data/vendor/kreuzberg/src/api/server.rs +353 -353
  68. data/vendor/kreuzberg/src/api/types.rs +170 -170
  69. data/vendor/kreuzberg/src/cache/mod.rs +1167 -1167
  70. data/vendor/kreuzberg/src/chunking/mod.rs +1877 -1877
  71. data/vendor/kreuzberg/src/chunking/processor.rs +220 -220
  72. data/vendor/kreuzberg/src/core/batch_mode.rs +95 -95
  73. data/vendor/kreuzberg/src/core/config.rs +1080 -1080
  74. data/vendor/kreuzberg/src/core/extractor.rs +1156 -1156
  75. data/vendor/kreuzberg/src/core/io.rs +329 -329
  76. data/vendor/kreuzberg/src/core/mime.rs +605 -605
  77. data/vendor/kreuzberg/src/core/mod.rs +47 -47
  78. data/vendor/kreuzberg/src/core/pipeline.rs +1184 -1184
  79. data/vendor/kreuzberg/src/embeddings.rs +500 -500
  80. data/vendor/kreuzberg/src/error.rs +431 -431
  81. data/vendor/kreuzberg/src/extraction/archive.rs +954 -954
  82. data/vendor/kreuzberg/src/extraction/docx.rs +398 -398
  83. data/vendor/kreuzberg/src/extraction/email.rs +854 -854
  84. data/vendor/kreuzberg/src/extraction/excel.rs +688 -688
  85. data/vendor/kreuzberg/src/extraction/html.rs +601 -601
  86. data/vendor/kreuzberg/src/extraction/image.rs +491 -491
  87. data/vendor/kreuzberg/src/extraction/libreoffice.rs +574 -574
  88. data/vendor/kreuzberg/src/extraction/markdown.rs +213 -213
  89. data/vendor/kreuzberg/src/extraction/mod.rs +81 -81
  90. data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -398
  91. data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -247
  92. data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -240
  93. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +130 -130
  94. data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +284 -284
  95. data/vendor/kreuzberg/src/extraction/pptx.rs +3100 -3100
  96. data/vendor/kreuzberg/src/extraction/structured.rs +490 -490
  97. data/vendor/kreuzberg/src/extraction/table.rs +328 -328
  98. data/vendor/kreuzberg/src/extraction/text.rs +269 -269
  99. data/vendor/kreuzberg/src/extraction/xml.rs +333 -333
  100. data/vendor/kreuzberg/src/extractors/archive.rs +447 -447
  101. data/vendor/kreuzberg/src/extractors/bibtex.rs +470 -470
  102. data/vendor/kreuzberg/src/extractors/docbook.rs +504 -504
  103. data/vendor/kreuzberg/src/extractors/docx.rs +400 -400
  104. data/vendor/kreuzberg/src/extractors/email.rs +157 -157
  105. data/vendor/kreuzberg/src/extractors/epub.rs +708 -708
  106. data/vendor/kreuzberg/src/extractors/excel.rs +345 -345
  107. data/vendor/kreuzberg/src/extractors/fictionbook.rs +492 -492
  108. data/vendor/kreuzberg/src/extractors/html.rs +407 -407
  109. data/vendor/kreuzberg/src/extractors/image.rs +219 -219
  110. data/vendor/kreuzberg/src/extractors/jats.rs +1054 -1054
  111. data/vendor/kreuzberg/src/extractors/jupyter.rs +368 -368
  112. data/vendor/kreuzberg/src/extractors/latex.rs +653 -653
  113. data/vendor/kreuzberg/src/extractors/markdown.rs +701 -701
  114. data/vendor/kreuzberg/src/extractors/mod.rs +429 -429
  115. data/vendor/kreuzberg/src/extractors/odt.rs +628 -628
  116. data/vendor/kreuzberg/src/extractors/opml.rs +635 -635
  117. data/vendor/kreuzberg/src/extractors/orgmode.rs +529 -529
  118. data/vendor/kreuzberg/src/extractors/pdf.rs +749 -749
  119. data/vendor/kreuzberg/src/extractors/pptx.rs +267 -267
  120. data/vendor/kreuzberg/src/extractors/rst.rs +577 -577
  121. data/vendor/kreuzberg/src/extractors/rtf.rs +809 -809
  122. data/vendor/kreuzberg/src/extractors/security.rs +484 -484
  123. data/vendor/kreuzberg/src/extractors/security_tests.rs +367 -367
  124. data/vendor/kreuzberg/src/extractors/structured.rs +142 -142
  125. data/vendor/kreuzberg/src/extractors/text.rs +265 -265
  126. data/vendor/kreuzberg/src/extractors/typst.rs +651 -651
  127. data/vendor/kreuzberg/src/extractors/xml.rs +147 -147
  128. data/vendor/kreuzberg/src/image/dpi.rs +164 -164
  129. data/vendor/kreuzberg/src/image/mod.rs +6 -6
  130. data/vendor/kreuzberg/src/image/preprocessing.rs +417 -417
  131. data/vendor/kreuzberg/src/image/resize.rs +89 -89
  132. data/vendor/kreuzberg/src/keywords/config.rs +154 -154
  133. data/vendor/kreuzberg/src/keywords/mod.rs +237 -237
  134. data/vendor/kreuzberg/src/keywords/processor.rs +275 -275
  135. data/vendor/kreuzberg/src/keywords/rake.rs +293 -293
  136. data/vendor/kreuzberg/src/keywords/types.rs +68 -68
  137. data/vendor/kreuzberg/src/keywords/yake.rs +163 -163
  138. data/vendor/kreuzberg/src/language_detection/mod.rs +985 -985
  139. data/vendor/kreuzberg/src/language_detection/processor.rs +219 -219
  140. data/vendor/kreuzberg/src/lib.rs +113 -113
  141. data/vendor/kreuzberg/src/mcp/mod.rs +35 -35
  142. data/vendor/kreuzberg/src/mcp/server.rs +2076 -2076
  143. data/vendor/kreuzberg/src/ocr/cache.rs +469 -469
  144. data/vendor/kreuzberg/src/ocr/error.rs +37 -37
  145. data/vendor/kreuzberg/src/ocr/hocr.rs +216 -216
  146. data/vendor/kreuzberg/src/ocr/mod.rs +58 -58
  147. data/vendor/kreuzberg/src/ocr/processor.rs +863 -863
  148. data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -4
  149. data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -144
  150. data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +452 -452
  151. data/vendor/kreuzberg/src/ocr/types.rs +393 -393
  152. data/vendor/kreuzberg/src/ocr/utils.rs +47 -47
  153. data/vendor/kreuzberg/src/ocr/validation.rs +206 -206
  154. data/vendor/kreuzberg/src/panic_context.rs +154 -154
  155. data/vendor/kreuzberg/src/pdf/bindings.rs +44 -44
  156. data/vendor/kreuzberg/src/pdf/bundled.rs +346 -346
  157. data/vendor/kreuzberg/src/pdf/error.rs +130 -130
  158. data/vendor/kreuzberg/src/pdf/images.rs +139 -139
  159. data/vendor/kreuzberg/src/pdf/metadata.rs +489 -489
  160. data/vendor/kreuzberg/src/pdf/mod.rs +68 -68
  161. data/vendor/kreuzberg/src/pdf/rendering.rs +368 -368
  162. data/vendor/kreuzberg/src/pdf/table.rs +420 -420
  163. data/vendor/kreuzberg/src/pdf/text.rs +240 -240
  164. data/vendor/kreuzberg/src/plugins/extractor.rs +1044 -1044
  165. data/vendor/kreuzberg/src/plugins/mod.rs +212 -212
  166. data/vendor/kreuzberg/src/plugins/ocr.rs +639 -639
  167. data/vendor/kreuzberg/src/plugins/processor.rs +650 -650
  168. data/vendor/kreuzberg/src/plugins/registry.rs +1339 -1339
  169. data/vendor/kreuzberg/src/plugins/traits.rs +258 -258
  170. data/vendor/kreuzberg/src/plugins/validator.rs +967 -967
  171. data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -1470
  172. data/vendor/kreuzberg/src/text/mod.rs +25 -25
  173. data/vendor/kreuzberg/src/text/quality.rs +697 -697
  174. data/vendor/kreuzberg/src/text/quality_processor.rs +219 -219
  175. data/vendor/kreuzberg/src/text/string_utils.rs +217 -217
  176. data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -164
  177. data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -100
  178. data/vendor/kreuzberg/src/text/token_reduction/core.rs +796 -796
  179. data/vendor/kreuzberg/src/text/token_reduction/filters.rs +902 -902
  180. data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -160
  181. data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -619
  182. data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +147 -147
  183. data/vendor/kreuzberg/src/types.rs +1055 -1055
  184. data/vendor/kreuzberg/src/utils/mod.rs +17 -17
  185. data/vendor/kreuzberg/src/utils/quality.rs +959 -959
  186. data/vendor/kreuzberg/src/utils/string_utils.rs +381 -381
  187. data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -53
  188. data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -482
  189. data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -261
  190. data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -400
  191. data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -1205
  192. data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -280
  193. data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -425
  194. data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -172
  195. data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -622
  196. data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -849
  197. data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -1300
  198. data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -175
  199. data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -734
  200. data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -37
  201. data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -100
  202. data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -801
  203. data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -849
  204. data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -693
  205. data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -111
  206. data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -162
  207. data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -226
  208. data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -41
  209. data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -196
  210. data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -227
  211. data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -181
  212. data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -791
  213. data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -47
  214. data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -760
  215. data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -634
  216. data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -136
  217. data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -84
  218. data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -681
  219. data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -64
  220. data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -51
  221. data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -476
  222. data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -163
  223. data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -1
  224. data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -101
  225. data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -477
  226. data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -490
  227. data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -415
  228. data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -223
  229. data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -331
  230. data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -562
  231. data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -436
  232. data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -561
  233. data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -193
  234. data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -420
  235. data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -448
  236. data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -32
  237. data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -33
  238. data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -420
  239. data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -76
  240. data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -129
  241. data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -54
  242. data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -118
  243. data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -149
  244. data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -506
  245. data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -75
  246. data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -519
  247. data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -647
  248. data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -62
  249. data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -796
  250. data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -31
  251. data/vendor/kreuzberg/tests/api_extract_multipart.rs +52 -52
  252. data/vendor/kreuzberg/tests/api_tests.rs +966 -966
  253. data/vendor/kreuzberg/tests/archive_integration.rs +545 -545
  254. data/vendor/kreuzberg/tests/batch_orchestration.rs +556 -556
  255. data/vendor/kreuzberg/tests/batch_processing.rs +318 -318
  256. data/vendor/kreuzberg/tests/bibtex_parity_test.rs +421 -421
  257. data/vendor/kreuzberg/tests/concurrency_stress.rs +533 -533
  258. data/vendor/kreuzberg/tests/config_features.rs +612 -612
  259. data/vendor/kreuzberg/tests/config_loading_tests.rs +416 -416
  260. data/vendor/kreuzberg/tests/core_integration.rs +510 -510
  261. data/vendor/kreuzberg/tests/csv_integration.rs +414 -414
  262. data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +500 -500
  263. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +122 -122
  264. data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +370 -370
  265. data/vendor/kreuzberg/tests/email_integration.rs +327 -327
  266. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +275 -275
  267. data/vendor/kreuzberg/tests/error_handling.rs +402 -402
  268. data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +228 -228
  269. data/vendor/kreuzberg/tests/format_integration.rs +164 -164
  270. data/vendor/kreuzberg/tests/helpers/mod.rs +142 -142
  271. data/vendor/kreuzberg/tests/html_table_test.rs +551 -551
  272. data/vendor/kreuzberg/tests/image_integration.rs +255 -255
  273. data/vendor/kreuzberg/tests/instrumentation_test.rs +139 -139
  274. data/vendor/kreuzberg/tests/jats_extractor_tests.rs +639 -639
  275. data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +704 -704
  276. data/vendor/kreuzberg/tests/keywords_integration.rs +479 -479
  277. data/vendor/kreuzberg/tests/keywords_quality.rs +509 -509
  278. data/vendor/kreuzberg/tests/latex_extractor_tests.rs +496 -496
  279. data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +490 -490
  280. data/vendor/kreuzberg/tests/mime_detection.rs +429 -429
  281. data/vendor/kreuzberg/tests/ocr_configuration.rs +514 -514
  282. data/vendor/kreuzberg/tests/ocr_errors.rs +698 -698
  283. data/vendor/kreuzberg/tests/ocr_quality.rs +629 -629
  284. data/vendor/kreuzberg/tests/ocr_stress.rs +469 -469
  285. data/vendor/kreuzberg/tests/odt_extractor_tests.rs +674 -674
  286. data/vendor/kreuzberg/tests/opml_extractor_tests.rs +616 -616
  287. data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +822 -822
  288. data/vendor/kreuzberg/tests/pdf_integration.rs +45 -45
  289. data/vendor/kreuzberg/tests/pdfium_linking.rs +374 -374
  290. data/vendor/kreuzberg/tests/pipeline_integration.rs +1436 -1436
  291. data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +776 -776
  292. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +560 -560
  293. data/vendor/kreuzberg/tests/plugin_system.rs +927 -927
  294. data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -783
  295. data/vendor/kreuzberg/tests/registry_integration_tests.rs +587 -587
  296. data/vendor/kreuzberg/tests/rst_extractor_tests.rs +694 -694
  297. data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +775 -775
  298. data/vendor/kreuzberg/tests/security_validation.rs +416 -416
  299. data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -888
  300. data/vendor/kreuzberg/tests/test_fastembed.rs +631 -631
  301. data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +1260 -1260
  302. data/vendor/kreuzberg/tests/typst_extractor_tests.rs +648 -648
  303. data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -87
  304. data/vendor/kreuzberg-ffi/Cargo.toml +1 -1
  305. data/vendor/kreuzberg-ffi/README.md +851 -851
  306. data/vendor/kreuzberg-ffi/build.rs +176 -176
  307. data/vendor/kreuzberg-ffi/cbindgen.toml +27 -27
  308. data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +12 -12
  309. data/vendor/kreuzberg-ffi/kreuzberg.h +1087 -1087
  310. data/vendor/kreuzberg-ffi/src/lib.rs +3616 -3616
  311. data/vendor/kreuzberg-ffi/src/panic_shield.rs +247 -247
  312. data/vendor/kreuzberg-ffi/tests.disabled/README.md +48 -48
  313. data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +299 -299
  314. data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +346 -346
  315. data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +232 -232
  316. data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +470 -470
  317. data/vendor/kreuzberg-tesseract/.commitlintrc.json +13 -13
  318. data/vendor/kreuzberg-tesseract/.crate-ignore +2 -2
  319. data/vendor/kreuzberg-tesseract/Cargo.lock +2933 -2933
  320. data/vendor/kreuzberg-tesseract/Cargo.toml +2 -2
  321. data/vendor/kreuzberg-tesseract/LICENSE +22 -22
  322. data/vendor/kreuzberg-tesseract/README.md +399 -399
  323. data/vendor/kreuzberg-tesseract/build.rs +1354 -1354
  324. data/vendor/kreuzberg-tesseract/patches/README.md +71 -71
  325. data/vendor/kreuzberg-tesseract/patches/tesseract.diff +199 -199
  326. data/vendor/kreuzberg-tesseract/src/api.rs +1371 -1371
  327. data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +77 -77
  328. data/vendor/kreuzberg-tesseract/src/enums.rs +297 -297
  329. data/vendor/kreuzberg-tesseract/src/error.rs +81 -81
  330. data/vendor/kreuzberg-tesseract/src/lib.rs +145 -145
  331. data/vendor/kreuzberg-tesseract/src/monitor.rs +57 -57
  332. data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +197 -197
  333. data/vendor/kreuzberg-tesseract/src/page_iterator.rs +253 -253
  334. data/vendor/kreuzberg-tesseract/src/result_iterator.rs +286 -286
  335. data/vendor/kreuzberg-tesseract/src/result_renderer.rs +183 -183
  336. data/vendor/kreuzberg-tesseract/tests/integration_test.rs +211 -211
  337. data/vendor/rb-sys/.cargo_vcs_info.json +5 -5
  338. data/vendor/rb-sys/Cargo.lock +393 -393
  339. data/vendor/rb-sys/Cargo.toml +70 -70
  340. data/vendor/rb-sys/Cargo.toml.orig +57 -57
  341. data/vendor/rb-sys/LICENSE-APACHE +190 -190
  342. data/vendor/rb-sys/LICENSE-MIT +21 -21
  343. data/vendor/rb-sys/build/features.rs +111 -111
  344. data/vendor/rb-sys/build/main.rs +286 -286
  345. data/vendor/rb-sys/build/stable_api_config.rs +155 -155
  346. data/vendor/rb-sys/build/version.rs +50 -50
  347. data/vendor/rb-sys/readme.md +36 -36
  348. data/vendor/rb-sys/src/bindings.rs +21 -21
  349. data/vendor/rb-sys/src/hidden.rs +11 -11
  350. data/vendor/rb-sys/src/lib.rs +35 -35
  351. data/vendor/rb-sys/src/macros.rs +371 -371
  352. data/vendor/rb-sys/src/memory.rs +53 -53
  353. data/vendor/rb-sys/src/ruby_abi_version.rs +38 -38
  354. data/vendor/rb-sys/src/special_consts.rs +31 -31
  355. data/vendor/rb-sys/src/stable_api/compiled.c +179 -179
  356. data/vendor/rb-sys/src/stable_api/compiled.rs +257 -257
  357. data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +324 -324
  358. data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +332 -332
  359. data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +325 -325
  360. data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +323 -323
  361. data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +339 -339
  362. data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +339 -339
  363. data/vendor/rb-sys/src/stable_api.rs +260 -260
  364. data/vendor/rb-sys/src/symbol.rs +31 -31
  365. data/vendor/rb-sys/src/tracking_allocator.rs +330 -330
  366. data/vendor/rb-sys/src/utils.rs +89 -89
  367. data/vendor/rb-sys/src/value_type.rs +7 -7
  368. metadata +73 -4
  369. data/vendor/kreuzberg-ffi/kreuzberg-ffi-install.pc +0 -12
@@ -1,1054 +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
- #[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.date = 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
- }
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.date = 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
+ }