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,650 +1,651 @@
1
- //! Native Rust Typst document extractor.
2
- //!
3
- //! This extractor provides Typst document parsing and text extraction.
4
- //! It uses a hybrid approach combining regex patterns and character-level parsing
5
- //! to extract text while preserving document structure.
6
- //!
7
- //! Features:
8
- //! - Metadata extraction: title, author, date, subject, keywords from `#set document()`
9
- //! - Section hierarchy: `=`, `==`, `===`, etc. heading levels
10
- //! - Inline formatting: `*bold*`, `_italic_`, `` `code` ``
11
- //! - Lists: extraction of list content (both `+` and `-` markers)
12
- //! - Links: extraction of URLs and link text from `#link("url")[text]` syntax
13
- //! - Math: inline (`$...$`) and display math preservation
14
- //! - Code blocks: triple-backtick code blocks with language specifiers
15
- //! - Tables: extraction of `#table()` function content
16
- //! - Complex formatting: handling of nested and combined formatting
17
- //!
18
- //! Requires the `office` feature.
19
-
20
- #[cfg(feature = "office")]
21
- use crate::Result;
22
- #[cfg(feature = "office")]
23
- use crate::core::config::ExtractionConfig;
24
- #[cfg(feature = "office")]
25
- use crate::plugins::{DocumentExtractor, Plugin};
26
- #[cfg(feature = "office")]
27
- use crate::types::{ExtractionResult, Metadata};
28
- #[cfg(feature = "office")]
29
- use async_trait::async_trait;
30
- #[cfg(feature = "office")]
31
- use regex::Regex;
32
-
33
- /// Typst document extractor
34
- #[cfg(feature = "office")]
35
- pub struct TypstExtractor;
36
-
37
- #[cfg(feature = "office")]
38
- impl TypstExtractor {
39
- /// Create a new Typst extractor.
40
- pub fn new() -> Self {
41
- Self
42
- }
43
-
44
- /// Parse Typst content and extract text.
45
- fn extract_from_typst(content: &str) -> (String, Metadata) {
46
- let mut extractor = TypstParser::new(content);
47
- let text = extractor.parse();
48
- let metadata = extractor.metadata;
49
-
50
- (text, metadata)
51
- }
52
- }
53
-
54
- #[cfg(feature = "office")]
55
- impl Default for TypstExtractor {
56
- fn default() -> Self {
57
- Self::new()
58
- }
59
- }
60
-
61
- #[cfg(feature = "office")]
62
- impl Plugin for TypstExtractor {
63
- fn name(&self) -> &str {
64
- "typst-extractor"
65
- }
66
-
67
- fn version(&self) -> String {
68
- env!("CARGO_PKG_VERSION").to_string()
69
- }
70
-
71
- fn initialize(&self) -> Result<()> {
72
- Ok(())
73
- }
74
-
75
- fn shutdown(&self) -> Result<()> {
76
- Ok(())
77
- }
78
-
79
- fn description(&self) -> &str {
80
- "Native Rust Typst document extractor with metadata support"
81
- }
82
-
83
- fn author(&self) -> &str {
84
- "Kreuzberg Team"
85
- }
86
- }
87
-
88
- #[cfg(feature = "office")]
89
- #[async_trait]
90
- impl DocumentExtractor for TypstExtractor {
91
- #[cfg_attr(feature = "otel", tracing::instrument(
92
- skip(self, content, _config),
93
- fields(
94
- extractor.name = self.name(),
95
- content.size_bytes = content.len(),
96
- )
97
- ))]
98
- async fn extract_bytes(
99
- &self,
100
- content: &[u8],
101
- mime_type: &str,
102
- _config: &ExtractionConfig,
103
- ) -> Result<ExtractionResult> {
104
- let typst_str = String::from_utf8_lossy(content).to_string();
105
- let (text, metadata) = Self::extract_from_typst(&typst_str);
106
-
107
- Ok(ExtractionResult {
108
- content: text,
109
- mime_type: mime_type.to_string(),
110
- metadata,
111
- tables: Vec::new(),
112
- detected_languages: None,
113
- chunks: None,
114
- images: None,
115
- })
116
- }
117
-
118
- fn supported_mime_types(&self) -> &[&str] {
119
- &["application/x-typst", "text/x-typst"]
120
- }
121
-
122
- fn priority(&self) -> i32 {
123
- 50
124
- }
125
- }
126
-
127
- /// Internal Typst parser
128
- #[cfg(feature = "office")]
129
- struct TypstParser {
130
- content: String,
131
- metadata: Metadata,
132
- }
133
-
134
- #[cfg(feature = "office")]
135
- impl TypstParser {
136
- fn new(content: &str) -> Self {
137
- Self {
138
- content: content.to_string(),
139
- metadata: Metadata::default(),
140
- }
141
- }
142
-
143
- fn parse(&mut self) -> String {
144
- self.extract_metadata();
145
-
146
- self.extract_content()
147
- }
148
-
149
- fn extract_metadata(&mut self) {
150
- if let Some(title) = self.extract_quoted_value("title") {
151
- self.metadata.additional.insert("title".to_string(), title.into());
152
- }
153
-
154
- if let Some(author) = self.extract_quoted_value("author") {
155
- self.metadata.additional.insert("author".to_string(), author.into());
156
- }
157
-
158
- if let Some(date) = self.extract_quoted_value("date") {
159
- self.metadata.date = Some(date);
160
- }
161
-
162
- if let Some(subject) = self.extract_quoted_value("subject") {
163
- self.metadata.additional.insert("subject".to_string(), subject.into());
164
- }
165
-
166
- if let Some(keywords) = self.extract_keywords() {
167
- self.metadata.additional.insert("keywords".to_string(), keywords.into());
168
- }
169
- }
170
-
171
- fn extract_quoted_value(&self, field: &str) -> Option<String> {
172
- let pattern = format!(r#"{}:\s*"([^"]*)""#, regex::escape(field));
173
- if let Ok(re) = Regex::new(&pattern)
174
- && let Some(caps) = re.captures(&self.content)
175
- {
176
- return caps.get(1).map(|m| m.as_str().to_string());
177
- }
178
- None
179
- }
180
-
181
- fn extract_keywords(&self) -> Option<String> {
182
- let pattern = r#"keywords:\s*(?:"([^"]*)"|(\([^)]*\)))"#;
183
- if let Ok(re) = Regex::new(pattern)
184
- && let Some(caps) = re.captures(&self.content)
185
- {
186
- if let Some(m) = caps.get(1) {
187
- return Some(m.as_str().to_string());
188
- }
189
- if let Some(m) = caps.get(2) {
190
- let array_str = m.as_str();
191
- let mut keywords = Vec::new();
192
- let item_pattern = r#""([^"]*)""#;
193
- if let Ok(item_re) = Regex::new(item_pattern) {
194
- for item_caps in item_re.captures_iter(array_str) {
195
- if let Some(keyword) = item_caps.get(1) {
196
- keywords.push(keyword.as_str().to_string());
197
- }
198
- }
199
- }
200
- if !keywords.is_empty() {
201
- return Some(keywords.join(", "));
202
- }
203
- }
204
- }
205
- None
206
- }
207
-
208
- fn extract_content(&self) -> String {
209
- let mut output = String::new();
210
- let mut lines = self.content.lines().peekable();
211
- let mut in_code_block = false;
212
- let mut code_block_fence = String::new();
213
-
214
- while let Some(line) = lines.next() {
215
- let trimmed = line.trim();
216
-
217
- if trimmed.starts_with("```") {
218
- if in_code_block {
219
- if trimmed == "```" {
220
- in_code_block = false;
221
- code_block_fence.clear();
222
- output.push_str("```\n");
223
- continue;
224
- }
225
- } else {
226
- in_code_block = true;
227
- code_block_fence = "```".to_string();
228
- output.push_str("```");
229
- if let Some(lang) = trimmed.strip_prefix("```") {
230
- let lang = lang.trim();
231
- if !lang.is_empty() {
232
- output.push_str(lang);
233
- }
234
- }
235
- output.push('\n');
236
- continue;
237
- }
238
- }
239
-
240
- if in_code_block {
241
- output.push_str(line);
242
- output.push('\n');
243
- continue;
244
- }
245
-
246
- if trimmed.starts_with("#set ") || trimmed.starts_with("#let ") {
247
- continue;
248
- }
249
-
250
- if trimmed.starts_with("#import ") || trimmed.starts_with("#include ") {
251
- continue;
252
- }
253
-
254
- if trimmed.starts_with("#table(") {
255
- output.push_str("TABLE:\n");
256
- let table_content = self.extract_table_content(trimmed, &mut lines);
257
- output.push_str(&table_content);
258
- output.push('\n');
259
- continue;
260
- }
261
-
262
- if trimmed.starts_with('=') {
263
- let next_char_pos = trimmed.find(|c: char| c != '=');
264
- if next_char_pos.is_some() {
265
- let heading_level = trimmed.chars().take_while(|&c| c == '=').count();
266
- let heading_text = trimmed[heading_level..].trim();
267
-
268
- for _ in 0..heading_level {
269
- output.push('=');
270
- }
271
- output.push(' ');
272
- output.push_str(heading_text);
273
- output.push('\n');
274
- continue;
275
- }
276
- }
277
-
278
- if (trimmed.starts_with('+') || trimmed.starts_with('-'))
279
- && trimmed.len() > 1
280
- && trimmed.chars().nth(1).is_some_and(|c| !c.is_alphanumeric())
281
- {
282
- output.push_str("- ");
283
- output.push_str(trimmed[1..].trim());
284
- output.push('\n');
285
- continue;
286
- }
287
-
288
- if trimmed.starts_with('#')
289
- && !trimmed.starts_with("#set")
290
- && !trimmed.starts_with("#let")
291
- && !trimmed.starts_with("#import")
292
- && !trimmed.starts_with("#include")
293
- {
294
- if trimmed.contains('[')
295
- && trimmed.contains(']')
296
- && let Some(content) = self.extract_text_from_brackets(trimmed)
297
- {
298
- let processed = self.process_line(&content);
299
- if !processed.is_empty() {
300
- output.push_str(&processed);
301
- output.push('\n');
302
- }
303
- }
304
- continue;
305
- }
306
-
307
- if !trimmed.is_empty() {
308
- let processed = self.process_line(trimmed);
309
- if !processed.is_empty() {
310
- output.push_str(&processed);
311
- output.push('\n');
312
- }
313
- } else {
314
- output.push('\n');
315
- }
316
- }
317
-
318
- output
319
- }
320
-
321
- /// Extract content from #table() function calls
322
- fn extract_table_content<'a, I>(&self, first_line: &str, lines: &mut std::iter::Peekable<I>) -> String
323
- where
324
- I: Iterator<Item = &'a str>,
325
- {
326
- let mut table_content = String::new();
327
- let mut content = first_line.to_string();
328
- let mut bracket_depth = 0;
329
- let mut paren_depth = if first_line.contains('(') { 1 } else { 0 };
330
-
331
- for ch in first_line.chars() {
332
- match ch {
333
- '(' => paren_depth += 1,
334
- ')' => paren_depth -= 1,
335
- '[' => bracket_depth += 1,
336
- ']' => bracket_depth -= 1,
337
- _ => {}
338
- }
339
- }
340
-
341
- while paren_depth > 0 || bracket_depth > 0 {
342
- if let Some(next_line) = lines.next() {
343
- content.push('\n');
344
- content.push_str(next_line);
345
- for ch in next_line.chars() {
346
- match ch {
347
- '(' => paren_depth += 1,
348
- ')' => paren_depth -= 1,
349
- '[' => bracket_depth += 1,
350
- ']' => bracket_depth -= 1,
351
- _ => {}
352
- }
353
- }
354
- } else {
355
- break;
356
- }
357
- }
358
-
359
- let mut in_bracket = false;
360
- let mut cell = String::new();
361
- for ch in content.chars() {
362
- match ch {
363
- '[' => {
364
- in_bracket = true;
365
- cell.clear();
366
- }
367
- ']' => {
368
- if in_bracket {
369
- let trimmed = cell.trim();
370
- if !trimmed.is_empty() {
371
- table_content.push_str(trimmed);
372
- table_content.push_str(" | ");
373
- }
374
- in_bracket = false;
375
- cell.clear();
376
- }
377
- }
378
- _ if in_bracket => {
379
- cell.push(ch);
380
- }
381
- _ => {}
382
- }
383
- }
384
-
385
- if table_content.ends_with(" | ") {
386
- table_content.truncate(table_content.len() - 3);
387
- }
388
-
389
- table_content
390
- }
391
-
392
- fn process_line(&self, line: &str) -> String {
393
- let mut result = String::new();
394
- let mut chars = line.chars().peekable();
395
-
396
- while let Some(ch) = chars.next() {
397
- match ch {
398
- '`' => {
399
- result.push('`');
400
- for c in chars.by_ref() {
401
- result.push(c);
402
- if c == '`' {
403
- break;
404
- }
405
- }
406
- }
407
- '$' => {
408
- result.push('$');
409
- for c in chars.by_ref() {
410
- result.push(c);
411
- if c == '$' {
412
- break;
413
- }
414
- }
415
- }
416
- '*' => {
417
- result.push('*');
418
- for c in chars.by_ref() {
419
- result.push(c);
420
- if c == '*' {
421
- break;
422
- }
423
- }
424
- }
425
- '_' => {
426
- result.push('_');
427
- for c in chars.by_ref() {
428
- result.push(c);
429
- if c == '_' {
430
- break;
431
- }
432
- }
433
- }
434
- '#' if chars.peek() == Some(&'l') => {
435
- result.push(ch);
436
- }
437
- _ => {
438
- result.push(ch);
439
- }
440
- }
441
- }
442
-
443
- self.extract_link_text(&result)
444
- }
445
-
446
- fn extract_link_text(&self, line: &str) -> String {
447
- let pattern = r#"link\("([^"]*)"\)\[([^\]]*)\]"#;
448
- if let Ok(re) = Regex::new(pattern) {
449
- return re
450
- .replace_all(line, |caps: &regex::Captures| {
451
- let url = caps.get(1).map(|m| m.as_str()).unwrap_or("");
452
- let text = caps.get(2).map(|m| m.as_str()).unwrap_or("");
453
- format!("[{}]({})", text, url)
454
- })
455
- .to_string();
456
- }
457
- line.to_string()
458
- }
459
-
460
- fn extract_text_from_brackets(&self, line: &str) -> Option<String> {
461
- if let Some(start) = line.find('[')
462
- && let Some(end) = line.rfind(']')
463
- && end > start
464
- {
465
- let text = &line[start + 1..end];
466
- return Some(text.to_string());
467
- }
468
- None
469
- }
470
- }
471
-
472
- #[cfg(test)]
473
- mod tests {
474
- use super::*;
475
-
476
- #[test]
477
- fn test_extract_metadata() {
478
- let content = r#"#set document(
479
- title: "Test Document",
480
- author: "Test Author"
481
- )
482
-
483
- = Heading
484
- Some text
485
- "#;
486
-
487
- let (_, metadata) = TypstExtractor::extract_from_typst(content);
488
-
489
- assert!(metadata.additional.contains_key("title"));
490
- assert!(metadata.additional.contains_key("author"));
491
- }
492
-
493
- #[test]
494
- fn test_extract_headings() {
495
- let content = r#"= Level 1
496
- Content
497
-
498
- == Level 2
499
- More content
500
- "#;
501
-
502
- let (output, _) = TypstExtractor::extract_from_typst(content);
503
-
504
- assert!(output.contains("= Level 1"));
505
- assert!(output.contains("== Level 2"));
506
- }
507
-
508
- #[test]
509
- fn test_extract_formatting() {
510
- let content = r#"Some *bold* and _italic_ text with `code`."#;
511
-
512
- let (output, _) = TypstExtractor::extract_from_typst(content);
513
-
514
- assert!(output.contains("*bold*") || output.contains("bold"));
515
- assert!(output.contains("_italic_") || output.contains("italic"));
516
- assert!(output.contains("`code`") || output.contains("code"));
517
- }
518
-
519
- #[test]
520
- fn test_extract_code_blocks() {
521
- let content = r#"Here is code:
522
-
523
- ```python
524
- def hello():
525
- print("world")
526
- ```
527
-
528
- Done."#;
529
-
530
- let (output, _) = TypstExtractor::extract_from_typst(content);
531
-
532
- assert!(output.contains("```python"));
533
- assert!(output.contains("def hello"));
534
- assert!(output.contains("print"));
535
- }
536
-
537
- #[test]
538
- fn test_extract_links() {
539
- let content = r#"Visit #link("https://example.com")[example site] for info."#;
540
-
541
- let (output, _) = TypstExtractor::extract_from_typst(content);
542
-
543
- assert!(
544
- output.contains("example.com")
545
- || output.contains("example site")
546
- || output.contains("[example site](https://example.com)")
547
- );
548
- }
549
-
550
- #[test]
551
- fn test_extract_list_items() {
552
- let content = r#"= Lists
553
-
554
- + First item
555
- + Second item
556
- + Third item"#;
557
-
558
- let (output, _) = TypstExtractor::extract_from_typst(content);
559
-
560
- assert!(output.contains("First item"));
561
- assert!(output.contains("Second item"));
562
- assert!(output.contains("Third item"));
563
- }
564
-
565
- #[test]
566
- fn test_extract_tables() {
567
- let content = r#"== Tables
568
-
569
- #table(
570
- columns: 2,
571
- [Name], [Age],
572
- [Alice], [30],
573
- )"#;
574
-
575
- let (output, _) = TypstExtractor::extract_from_typst(content);
576
-
577
- assert!(output.contains("TABLE:") || output.contains("Name") || output.contains("Alice"));
578
- }
579
-
580
- #[test]
581
- fn test_extract_math() {
582
- let content = r#"The formula $E = mc^2$ is important.
583
-
584
- Display:
585
- $ a^2 + b^2 = c^2 $"#;
586
-
587
- let (output, _) = TypstExtractor::extract_from_typst(content);
588
-
589
- assert!(output.contains("$") && output.contains("mc"));
590
- }
591
-
592
- #[test]
593
- fn test_metadata_extraction_comprehensive() {
594
- let content = r#"#set document(
595
- title: "Advanced Document",
596
- author: "John Doe",
597
- date: "2024-12-06",
598
- subject: "Test Subject",
599
- keywords: ("test", "example", "rust")
600
- )
601
-
602
- Content here."#;
603
-
604
- let (_, metadata) = TypstExtractor::extract_from_typst(content);
605
-
606
- assert!(metadata.additional.contains_key("title"), "Title should be extracted");
607
- assert!(metadata.additional.contains_key("author"), "Author should be extracted");
608
- assert!(metadata.date.is_some(), "Date should be extracted");
609
- assert!(
610
- metadata.additional.contains_key("subject"),
611
- "Subject should be extracted"
612
- );
613
- assert!(
614
- metadata
615
- .additional
616
- .get("keywords")
617
- .map(|v| !v.to_string().is_empty())
618
- .unwrap_or(false)
619
- );
620
- }
621
-
622
- #[test]
623
- fn test_skip_directives() {
624
- let content = r#"#set heading(numbering: "1.")
625
- #let x = 5
626
- #import "@preview/foo:1.0"
627
- #include "other.typ"
628
-
629
- = Heading
630
- Actual content"#;
631
-
632
- let (output, _) = TypstExtractor::extract_from_typst(content);
633
-
634
- assert!(!output.contains("#set"));
635
- assert!(!output.contains("#let"));
636
- assert!(!output.contains("#import"));
637
- assert!(!output.contains("#include"));
638
- assert!(output.contains("Heading"));
639
- assert!(output.contains("content"));
640
- }
641
-
642
- #[test]
643
- fn test_combined_formatting() {
644
- let content = r#"This is *bold with _nested italic_* and more."#;
645
-
646
- let (output, _) = TypstExtractor::extract_from_typst(content);
647
-
648
- assert!(output.contains("*") || output.contains("_") || (output.contains("bold") && output.contains("italic")));
649
- }
650
- }
1
+ //! Native Rust Typst document extractor.
2
+ //!
3
+ //! This extractor provides Typst document parsing and text extraction.
4
+ //! It uses a hybrid approach combining regex patterns and character-level parsing
5
+ //! to extract text while preserving document structure.
6
+ //!
7
+ //! Features:
8
+ //! - Metadata extraction: title, author, date, subject, keywords from `#set document()`
9
+ //! - Section hierarchy: `=`, `==`, `===`, etc. heading levels
10
+ //! - Inline formatting: `*bold*`, `_italic_`, `` `code` ``
11
+ //! - Lists: extraction of list content (both `+` and `-` markers)
12
+ //! - Links: extraction of URLs and link text from `#link("url")[text]` syntax
13
+ //! - Math: inline (`$...$`) and display math preservation
14
+ //! - Code blocks: triple-backtick code blocks with language specifiers
15
+ //! - Tables: extraction of `#table()` function content
16
+ //! - Complex formatting: handling of nested and combined formatting
17
+ //!
18
+ //! Requires the `office` feature.
19
+
20
+ #[cfg(feature = "office")]
21
+ use crate::Result;
22
+ #[cfg(feature = "office")]
23
+ use crate::core::config::ExtractionConfig;
24
+ #[cfg(feature = "office")]
25
+ use crate::plugins::{DocumentExtractor, Plugin};
26
+ #[cfg(feature = "office")]
27
+ use crate::types::{ExtractionResult, Metadata};
28
+ #[cfg(feature = "office")]
29
+ use async_trait::async_trait;
30
+ #[cfg(feature = "office")]
31
+ use regex::Regex;
32
+
33
+ /// Typst document extractor
34
+ #[cfg(feature = "office")]
35
+ pub struct TypstExtractor;
36
+
37
+ #[cfg(feature = "office")]
38
+ impl TypstExtractor {
39
+ /// Create a new Typst extractor.
40
+ pub fn new() -> Self {
41
+ Self
42
+ }
43
+
44
+ /// Parse Typst content and extract text.
45
+ fn extract_from_typst(content: &str) -> (String, Metadata) {
46
+ let mut extractor = TypstParser::new(content);
47
+ let text = extractor.parse();
48
+ let metadata = extractor.metadata;
49
+
50
+ (text, metadata)
51
+ }
52
+ }
53
+
54
+ #[cfg(feature = "office")]
55
+ impl Default for TypstExtractor {
56
+ fn default() -> Self {
57
+ Self::new()
58
+ }
59
+ }
60
+
61
+ #[cfg(feature = "office")]
62
+ impl Plugin for TypstExtractor {
63
+ fn name(&self) -> &str {
64
+ "typst-extractor"
65
+ }
66
+
67
+ fn version(&self) -> String {
68
+ env!("CARGO_PKG_VERSION").to_string()
69
+ }
70
+
71
+ fn initialize(&self) -> Result<()> {
72
+ Ok(())
73
+ }
74
+
75
+ fn shutdown(&self) -> Result<()> {
76
+ Ok(())
77
+ }
78
+
79
+ fn description(&self) -> &str {
80
+ "Native Rust Typst document extractor with metadata support"
81
+ }
82
+
83
+ fn author(&self) -> &str {
84
+ "Kreuzberg Team"
85
+ }
86
+ }
87
+
88
+ #[cfg(feature = "office")]
89
+ #[async_trait]
90
+ impl DocumentExtractor for TypstExtractor {
91
+ #[cfg_attr(feature = "otel", tracing::instrument(
92
+ skip(self, content, _config),
93
+ fields(
94
+ extractor.name = self.name(),
95
+ content.size_bytes = content.len(),
96
+ )
97
+ ))]
98
+ async fn extract_bytes(
99
+ &self,
100
+ content: &[u8],
101
+ mime_type: &str,
102
+ _config: &ExtractionConfig,
103
+ ) -> Result<ExtractionResult> {
104
+ let typst_str = String::from_utf8_lossy(content).to_string();
105
+ let (text, metadata) = Self::extract_from_typst(&typst_str);
106
+
107
+ Ok(ExtractionResult {
108
+ content: text,
109
+ mime_type: mime_type.to_string(),
110
+ metadata,
111
+ tables: Vec::new(),
112
+ detected_languages: None,
113
+ chunks: None,
114
+ images: None,
115
+ pages: None,
116
+ })
117
+ }
118
+
119
+ fn supported_mime_types(&self) -> &[&str] {
120
+ &["application/x-typst", "text/x-typst"]
121
+ }
122
+
123
+ fn priority(&self) -> i32 {
124
+ 50
125
+ }
126
+ }
127
+
128
+ /// Internal Typst parser
129
+ #[cfg(feature = "office")]
130
+ struct TypstParser {
131
+ content: String,
132
+ metadata: Metadata,
133
+ }
134
+
135
+ #[cfg(feature = "office")]
136
+ impl TypstParser {
137
+ fn new(content: &str) -> Self {
138
+ Self {
139
+ content: content.to_string(),
140
+ metadata: Metadata::default(),
141
+ }
142
+ }
143
+
144
+ fn parse(&mut self) -> String {
145
+ self.extract_metadata();
146
+
147
+ self.extract_content()
148
+ }
149
+
150
+ fn extract_metadata(&mut self) {
151
+ if let Some(title) = self.extract_quoted_value("title") {
152
+ self.metadata.additional.insert("title".to_string(), title.into());
153
+ }
154
+
155
+ if let Some(author) = self.extract_quoted_value("author") {
156
+ self.metadata.additional.insert("author".to_string(), author.into());
157
+ }
158
+
159
+ if let Some(date) = self.extract_quoted_value("date") {
160
+ self.metadata.created_at = Some(date);
161
+ }
162
+
163
+ if let Some(subject) = self.extract_quoted_value("subject") {
164
+ self.metadata.additional.insert("subject".to_string(), subject.into());
165
+ }
166
+
167
+ if let Some(keywords) = self.extract_keywords() {
168
+ self.metadata.additional.insert("keywords".to_string(), keywords.into());
169
+ }
170
+ }
171
+
172
+ fn extract_quoted_value(&self, field: &str) -> Option<String> {
173
+ let pattern = format!(r#"{}:\s*"([^"]*)""#, regex::escape(field));
174
+ if let Ok(re) = Regex::new(&pattern)
175
+ && let Some(caps) = re.captures(&self.content)
176
+ {
177
+ return caps.get(1).map(|m| m.as_str().to_string());
178
+ }
179
+ None
180
+ }
181
+
182
+ fn extract_keywords(&self) -> Option<String> {
183
+ let pattern = r#"keywords:\s*(?:"([^"]*)"|(\([^)]*\)))"#;
184
+ if let Ok(re) = Regex::new(pattern)
185
+ && let Some(caps) = re.captures(&self.content)
186
+ {
187
+ if let Some(m) = caps.get(1) {
188
+ return Some(m.as_str().to_string());
189
+ }
190
+ if let Some(m) = caps.get(2) {
191
+ let array_str = m.as_str();
192
+ let mut keywords = Vec::new();
193
+ let item_pattern = r#""([^"]*)""#;
194
+ if let Ok(item_re) = Regex::new(item_pattern) {
195
+ for item_caps in item_re.captures_iter(array_str) {
196
+ if let Some(keyword) = item_caps.get(1) {
197
+ keywords.push(keyword.as_str().to_string());
198
+ }
199
+ }
200
+ }
201
+ if !keywords.is_empty() {
202
+ return Some(keywords.join(", "));
203
+ }
204
+ }
205
+ }
206
+ None
207
+ }
208
+
209
+ fn extract_content(&self) -> String {
210
+ let mut output = String::new();
211
+ let mut lines = self.content.lines().peekable();
212
+ let mut in_code_block = false;
213
+ let mut code_block_fence = String::new();
214
+
215
+ while let Some(line) = lines.next() {
216
+ let trimmed = line.trim();
217
+
218
+ if trimmed.starts_with("```") {
219
+ if in_code_block {
220
+ if trimmed == "```" {
221
+ in_code_block = false;
222
+ code_block_fence.clear();
223
+ output.push_str("```\n");
224
+ continue;
225
+ }
226
+ } else {
227
+ in_code_block = true;
228
+ code_block_fence = "```".to_string();
229
+ output.push_str("```");
230
+ if let Some(lang) = trimmed.strip_prefix("```") {
231
+ let lang = lang.trim();
232
+ if !lang.is_empty() {
233
+ output.push_str(lang);
234
+ }
235
+ }
236
+ output.push('\n');
237
+ continue;
238
+ }
239
+ }
240
+
241
+ if in_code_block {
242
+ output.push_str(line);
243
+ output.push('\n');
244
+ continue;
245
+ }
246
+
247
+ if trimmed.starts_with("#set ") || trimmed.starts_with("#let ") {
248
+ continue;
249
+ }
250
+
251
+ if trimmed.starts_with("#import ") || trimmed.starts_with("#include ") {
252
+ continue;
253
+ }
254
+
255
+ if trimmed.starts_with("#table(") {
256
+ output.push_str("TABLE:\n");
257
+ let table_content = self.extract_table_content(trimmed, &mut lines);
258
+ output.push_str(&table_content);
259
+ output.push('\n');
260
+ continue;
261
+ }
262
+
263
+ if trimmed.starts_with('=') {
264
+ let next_char_pos = trimmed.find(|c: char| c != '=');
265
+ if next_char_pos.is_some() {
266
+ let heading_level = trimmed.chars().take_while(|&c| c == '=').count();
267
+ let heading_text = trimmed[heading_level..].trim();
268
+
269
+ for _ in 0..heading_level {
270
+ output.push('=');
271
+ }
272
+ output.push(' ');
273
+ output.push_str(heading_text);
274
+ output.push('\n');
275
+ continue;
276
+ }
277
+ }
278
+
279
+ if (trimmed.starts_with('+') || trimmed.starts_with('-'))
280
+ && trimmed.len() > 1
281
+ && trimmed.chars().nth(1).is_some_and(|c| !c.is_alphanumeric())
282
+ {
283
+ output.push_str("- ");
284
+ output.push_str(trimmed[1..].trim());
285
+ output.push('\n');
286
+ continue;
287
+ }
288
+
289
+ if trimmed.starts_with('#')
290
+ && !trimmed.starts_with("#set")
291
+ && !trimmed.starts_with("#let")
292
+ && !trimmed.starts_with("#import")
293
+ && !trimmed.starts_with("#include")
294
+ {
295
+ if trimmed.contains('[')
296
+ && trimmed.contains(']')
297
+ && let Some(content) = self.extract_text_from_brackets(trimmed)
298
+ {
299
+ let processed = self.process_line(&content);
300
+ if !processed.is_empty() {
301
+ output.push_str(&processed);
302
+ output.push('\n');
303
+ }
304
+ }
305
+ continue;
306
+ }
307
+
308
+ if !trimmed.is_empty() {
309
+ let processed = self.process_line(trimmed);
310
+ if !processed.is_empty() {
311
+ output.push_str(&processed);
312
+ output.push('\n');
313
+ }
314
+ } else {
315
+ output.push('\n');
316
+ }
317
+ }
318
+
319
+ output
320
+ }
321
+
322
+ /// Extract content from #table() function calls
323
+ fn extract_table_content<'a, I>(&self, first_line: &str, lines: &mut std::iter::Peekable<I>) -> String
324
+ where
325
+ I: Iterator<Item = &'a str>,
326
+ {
327
+ let mut table_content = String::new();
328
+ let mut content = first_line.to_string();
329
+ let mut bracket_depth = 0;
330
+ let mut paren_depth = if first_line.contains('(') { 1 } else { 0 };
331
+
332
+ for ch in first_line.chars() {
333
+ match ch {
334
+ '(' => paren_depth += 1,
335
+ ')' => paren_depth -= 1,
336
+ '[' => bracket_depth += 1,
337
+ ']' => bracket_depth -= 1,
338
+ _ => {}
339
+ }
340
+ }
341
+
342
+ while paren_depth > 0 || bracket_depth > 0 {
343
+ if let Some(next_line) = lines.next() {
344
+ content.push('\n');
345
+ content.push_str(next_line);
346
+ for ch in next_line.chars() {
347
+ match ch {
348
+ '(' => paren_depth += 1,
349
+ ')' => paren_depth -= 1,
350
+ '[' => bracket_depth += 1,
351
+ ']' => bracket_depth -= 1,
352
+ _ => {}
353
+ }
354
+ }
355
+ } else {
356
+ break;
357
+ }
358
+ }
359
+
360
+ let mut in_bracket = false;
361
+ let mut cell = String::new();
362
+ for ch in content.chars() {
363
+ match ch {
364
+ '[' => {
365
+ in_bracket = true;
366
+ cell.clear();
367
+ }
368
+ ']' => {
369
+ if in_bracket {
370
+ let trimmed = cell.trim();
371
+ if !trimmed.is_empty() {
372
+ table_content.push_str(trimmed);
373
+ table_content.push_str(" | ");
374
+ }
375
+ in_bracket = false;
376
+ cell.clear();
377
+ }
378
+ }
379
+ _ if in_bracket => {
380
+ cell.push(ch);
381
+ }
382
+ _ => {}
383
+ }
384
+ }
385
+
386
+ if table_content.ends_with(" | ") {
387
+ table_content.truncate(table_content.len() - 3);
388
+ }
389
+
390
+ table_content
391
+ }
392
+
393
+ fn process_line(&self, line: &str) -> String {
394
+ let mut result = String::new();
395
+ let mut chars = line.chars().peekable();
396
+
397
+ while let Some(ch) = chars.next() {
398
+ match ch {
399
+ '`' => {
400
+ result.push('`');
401
+ for c in chars.by_ref() {
402
+ result.push(c);
403
+ if c == '`' {
404
+ break;
405
+ }
406
+ }
407
+ }
408
+ '$' => {
409
+ result.push('$');
410
+ for c in chars.by_ref() {
411
+ result.push(c);
412
+ if c == '$' {
413
+ break;
414
+ }
415
+ }
416
+ }
417
+ '*' => {
418
+ result.push('*');
419
+ for c in chars.by_ref() {
420
+ result.push(c);
421
+ if c == '*' {
422
+ break;
423
+ }
424
+ }
425
+ }
426
+ '_' => {
427
+ result.push('_');
428
+ for c in chars.by_ref() {
429
+ result.push(c);
430
+ if c == '_' {
431
+ break;
432
+ }
433
+ }
434
+ }
435
+ '#' if chars.peek() == Some(&'l') => {
436
+ result.push(ch);
437
+ }
438
+ _ => {
439
+ result.push(ch);
440
+ }
441
+ }
442
+ }
443
+
444
+ self.extract_link_text(&result)
445
+ }
446
+
447
+ fn extract_link_text(&self, line: &str) -> String {
448
+ let pattern = r#"link\("([^"]*)"\)\[([^\]]*)\]"#;
449
+ if let Ok(re) = Regex::new(pattern) {
450
+ return re
451
+ .replace_all(line, |caps: &regex::Captures| {
452
+ let url = caps.get(1).map(|m| m.as_str()).unwrap_or("");
453
+ let text = caps.get(2).map(|m| m.as_str()).unwrap_or("");
454
+ format!("[{}]({})", text, url)
455
+ })
456
+ .to_string();
457
+ }
458
+ line.to_string()
459
+ }
460
+
461
+ fn extract_text_from_brackets(&self, line: &str) -> Option<String> {
462
+ if let Some(start) = line.find('[')
463
+ && let Some(end) = line.rfind(']')
464
+ && end > start
465
+ {
466
+ let text = &line[start + 1..end];
467
+ return Some(text.to_string());
468
+ }
469
+ None
470
+ }
471
+ }
472
+
473
+ #[cfg(test)]
474
+ mod tests {
475
+ use super::*;
476
+
477
+ #[test]
478
+ fn test_extract_metadata() {
479
+ let content = r#"#set document(
480
+ title: "Test Document",
481
+ author: "Test Author"
482
+ )
483
+
484
+ = Heading
485
+ Some text
486
+ "#;
487
+
488
+ let (_, metadata) = TypstExtractor::extract_from_typst(content);
489
+
490
+ assert!(metadata.additional.contains_key("title"));
491
+ assert!(metadata.additional.contains_key("author"));
492
+ }
493
+
494
+ #[test]
495
+ fn test_extract_headings() {
496
+ let content = r#"= Level 1
497
+ Content
498
+
499
+ == Level 2
500
+ More content
501
+ "#;
502
+
503
+ let (output, _) = TypstExtractor::extract_from_typst(content);
504
+
505
+ assert!(output.contains("= Level 1"));
506
+ assert!(output.contains("== Level 2"));
507
+ }
508
+
509
+ #[test]
510
+ fn test_extract_formatting() {
511
+ let content = r#"Some *bold* and _italic_ text with `code`."#;
512
+
513
+ let (output, _) = TypstExtractor::extract_from_typst(content);
514
+
515
+ assert!(output.contains("*bold*") || output.contains("bold"));
516
+ assert!(output.contains("_italic_") || output.contains("italic"));
517
+ assert!(output.contains("`code`") || output.contains("code"));
518
+ }
519
+
520
+ #[test]
521
+ fn test_extract_code_blocks() {
522
+ let content = r#"Here is code:
523
+
524
+ ```python
525
+ def hello():
526
+ print("world")
527
+ ```
528
+
529
+ Done."#;
530
+
531
+ let (output, _) = TypstExtractor::extract_from_typst(content);
532
+
533
+ assert!(output.contains("```python"));
534
+ assert!(output.contains("def hello"));
535
+ assert!(output.contains("print"));
536
+ }
537
+
538
+ #[test]
539
+ fn test_extract_links() {
540
+ let content = r#"Visit #link("https://example.com")[example site] for info."#;
541
+
542
+ let (output, _) = TypstExtractor::extract_from_typst(content);
543
+
544
+ assert!(
545
+ output.contains("example.com")
546
+ || output.contains("example site")
547
+ || output.contains("[example site](https://example.com)")
548
+ );
549
+ }
550
+
551
+ #[test]
552
+ fn test_extract_list_items() {
553
+ let content = r#"= Lists
554
+
555
+ + First item
556
+ + Second item
557
+ + Third item"#;
558
+
559
+ let (output, _) = TypstExtractor::extract_from_typst(content);
560
+
561
+ assert!(output.contains("First item"));
562
+ assert!(output.contains("Second item"));
563
+ assert!(output.contains("Third item"));
564
+ }
565
+
566
+ #[test]
567
+ fn test_extract_tables() {
568
+ let content = r#"== Tables
569
+
570
+ #table(
571
+ columns: 2,
572
+ [Name], [Age],
573
+ [Alice], [30],
574
+ )"#;
575
+
576
+ let (output, _) = TypstExtractor::extract_from_typst(content);
577
+
578
+ assert!(output.contains("TABLE:") || output.contains("Name") || output.contains("Alice"));
579
+ }
580
+
581
+ #[test]
582
+ fn test_extract_math() {
583
+ let content = r#"The formula $E = mc^2$ is important.
584
+
585
+ Display:
586
+ $ a^2 + b^2 = c^2 $"#;
587
+
588
+ let (output, _) = TypstExtractor::extract_from_typst(content);
589
+
590
+ assert!(output.contains("$") && output.contains("mc"));
591
+ }
592
+
593
+ #[test]
594
+ fn test_metadata_extraction_comprehensive() {
595
+ let content = r#"#set document(
596
+ title: "Advanced Document",
597
+ author: "John Doe",
598
+ date: "2024-12-06",
599
+ subject: "Test Subject",
600
+ keywords: ("test", "example", "rust")
601
+ )
602
+
603
+ Content here."#;
604
+
605
+ let (_, metadata) = TypstExtractor::extract_from_typst(content);
606
+
607
+ assert!(metadata.additional.contains_key("title"), "Title should be extracted");
608
+ assert!(metadata.additional.contains_key("author"), "Author should be extracted");
609
+ assert!(metadata.created_at.is_some(), "Date should be extracted");
610
+ assert!(
611
+ metadata.additional.contains_key("subject"),
612
+ "Subject should be extracted"
613
+ );
614
+ assert!(
615
+ metadata
616
+ .additional
617
+ .get("keywords")
618
+ .map(|v| !v.to_string().is_empty())
619
+ .unwrap_or(false)
620
+ );
621
+ }
622
+
623
+ #[test]
624
+ fn test_skip_directives() {
625
+ let content = r#"#set heading(numbering: "1.")
626
+ #let x = 5
627
+ #import "@preview/foo:1.0"
628
+ #include "other.typ"
629
+
630
+ = Heading
631
+ Actual content"#;
632
+
633
+ let (output, _) = TypstExtractor::extract_from_typst(content);
634
+
635
+ assert!(!output.contains("#set"));
636
+ assert!(!output.contains("#let"));
637
+ assert!(!output.contains("#import"));
638
+ assert!(!output.contains("#include"));
639
+ assert!(output.contains("Heading"));
640
+ assert!(output.contains("content"));
641
+ }
642
+
643
+ #[test]
644
+ fn test_combined_formatting() {
645
+ let content = r#"This is *bold with _nested italic_* and more."#;
646
+
647
+ let (output, _) = TypstExtractor::extract_from_typst(content);
648
+
649
+ assert!(output.contains("*") || output.contains("_") || (output.contains("bold") && output.contains("italic")));
650
+ }
651
+ }