kreuzberg 4.0.0.pre.rc.13 → 4.0.0.pre.rc.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +14 -14
- data/.rspec +3 -3
- data/.rubocop.yaml +1 -1
- data/.rubocop.yml +538 -538
- data/Gemfile +8 -8
- data/Gemfile.lock +104 -2
- data/README.md +454 -454
- data/Rakefile +33 -25
- data/Steepfile +47 -47
- data/examples/async_patterns.rb +341 -341
- data/ext/kreuzberg_rb/extconf.rb +45 -45
- data/ext/kreuzberg_rb/native/.cargo/config.toml +2 -2
- data/ext/kreuzberg_rb/native/Cargo.lock +6750 -6941
- data/ext/kreuzberg_rb/native/Cargo.toml +53 -54
- data/ext/kreuzberg_rb/native/README.md +425 -425
- data/ext/kreuzberg_rb/native/build.rs +52 -15
- data/ext/kreuzberg_rb/native/include/ieeefp.h +11 -11
- data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +14 -14
- data/ext/kreuzberg_rb/native/include/strings.h +20 -20
- data/ext/kreuzberg_rb/native/include/unistd.h +47 -47
- data/ext/kreuzberg_rb/native/src/lib.rs +3158 -3158
- data/extconf.rb +28 -28
- data/kreuzberg.gemspec +214 -214
- data/lib/kreuzberg/api_proxy.rb +142 -142
- data/lib/kreuzberg/cache_api.rb +81 -81
- data/lib/kreuzberg/cli.rb +55 -55
- data/lib/kreuzberg/cli_proxy.rb +127 -127
- data/lib/kreuzberg/config.rb +724 -724
- data/lib/kreuzberg/error_context.rb +80 -80
- data/lib/kreuzberg/errors.rb +118 -118
- data/lib/kreuzberg/extraction_api.rb +340 -340
- data/lib/kreuzberg/mcp_proxy.rb +186 -186
- data/lib/kreuzberg/ocr_backend_protocol.rb +113 -113
- data/lib/kreuzberg/post_processor_protocol.rb +86 -86
- data/lib/kreuzberg/result.rb +279 -279
- data/lib/kreuzberg/setup_lib_path.rb +80 -80
- data/lib/kreuzberg/validator_protocol.rb +89 -89
- data/lib/kreuzberg/version.rb +5 -5
- data/lib/kreuzberg.rb +109 -109
- data/lib/{pdfium.dll → libpdfium.so} +0 -0
- data/sig/kreuzberg/internal.rbs +184 -184
- data/sig/kreuzberg.rbs +546 -546
- data/spec/binding/cache_spec.rb +227 -227
- data/spec/binding/cli_proxy_spec.rb +85 -85
- data/spec/binding/cli_spec.rb +55 -55
- data/spec/binding/config_spec.rb +345 -345
- data/spec/binding/config_validation_spec.rb +283 -283
- data/spec/binding/error_handling_spec.rb +213 -213
- data/spec/binding/errors_spec.rb +66 -66
- data/spec/binding/plugins/ocr_backend_spec.rb +307 -307
- data/spec/binding/plugins/postprocessor_spec.rb +269 -269
- data/spec/binding/plugins/validator_spec.rb +274 -274
- data/spec/fixtures/config.toml +39 -39
- data/spec/fixtures/config.yaml +41 -41
- data/spec/fixtures/invalid_config.toml +4 -4
- data/spec/smoke/package_spec.rb +178 -178
- data/spec/spec_helper.rb +42 -42
- data/vendor/Cargo.toml +2 -2
- data/vendor/kreuzberg/Cargo.toml +5 -5
- data/vendor/kreuzberg/README.md +230 -230
- data/vendor/kreuzberg/benches/otel_overhead.rs +48 -48
- data/vendor/kreuzberg/build.rs +887 -843
- data/vendor/kreuzberg/src/api/error.rs +81 -81
- data/vendor/kreuzberg/src/api/handlers.rs +199 -199
- data/vendor/kreuzberg/src/api/mod.rs +87 -79
- data/vendor/kreuzberg/src/api/server.rs +353 -353
- data/vendor/kreuzberg/src/api/types.rs +170 -170
- data/vendor/kreuzberg/src/cache/mod.rs +1167 -1167
- data/vendor/kreuzberg/src/chunking/mod.rs +1877 -1877
- data/vendor/kreuzberg/src/chunking/processor.rs +220 -220
- data/vendor/kreuzberg/src/core/batch_mode.rs +95 -95
- data/vendor/kreuzberg/src/core/config.rs +1080 -1080
- data/vendor/kreuzberg/src/core/extractor.rs +1156 -1156
- data/vendor/kreuzberg/src/core/io.rs +329 -329
- data/vendor/kreuzberg/src/core/mime.rs +605 -605
- data/vendor/kreuzberg/src/core/mod.rs +47 -47
- data/vendor/kreuzberg/src/core/pipeline.rs +1184 -1184
- data/vendor/kreuzberg/src/embeddings.rs +500 -500
- data/vendor/kreuzberg/src/error.rs +431 -431
- data/vendor/kreuzberg/src/extraction/archive.rs +954 -954
- data/vendor/kreuzberg/src/extraction/docx.rs +398 -398
- data/vendor/kreuzberg/src/extraction/email.rs +854 -854
- data/vendor/kreuzberg/src/extraction/excel.rs +688 -688
- data/vendor/kreuzberg/src/extraction/html.rs +634 -601
- data/vendor/kreuzberg/src/extraction/image.rs +491 -491
- data/vendor/kreuzberg/src/extraction/libreoffice.rs +574 -574
- data/vendor/kreuzberg/src/extraction/markdown.rs +213 -213
- data/vendor/kreuzberg/src/extraction/mod.rs +81 -81
- data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -398
- data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -247
- data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -240
- data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +130 -130
- data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +284 -284
- data/vendor/kreuzberg/src/extraction/pptx.rs +3100 -3100
- data/vendor/kreuzberg/src/extraction/structured.rs +490 -490
- data/vendor/kreuzberg/src/extraction/table.rs +328 -328
- data/vendor/kreuzberg/src/extraction/text.rs +269 -269
- data/vendor/kreuzberg/src/extraction/xml.rs +333 -333
- data/vendor/kreuzberg/src/extractors/archive.rs +447 -447
- data/vendor/kreuzberg/src/extractors/bibtex.rs +470 -470
- data/vendor/kreuzberg/src/extractors/docbook.rs +504 -504
- data/vendor/kreuzberg/src/extractors/docx.rs +400 -400
- data/vendor/kreuzberg/src/extractors/email.rs +157 -157
- data/vendor/kreuzberg/src/extractors/epub.rs +708 -708
- data/vendor/kreuzberg/src/extractors/excel.rs +345 -345
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +492 -492
- data/vendor/kreuzberg/src/extractors/html.rs +407 -407
- data/vendor/kreuzberg/src/extractors/image.rs +219 -219
- data/vendor/kreuzberg/src/extractors/jats.rs +1054 -1054
- data/vendor/kreuzberg/src/extractors/jupyter.rs +368 -368
- data/vendor/kreuzberg/src/extractors/latex.rs +653 -653
- data/vendor/kreuzberg/src/extractors/markdown.rs +701 -701
- data/vendor/kreuzberg/src/extractors/mod.rs +429 -429
- data/vendor/kreuzberg/src/extractors/odt.rs +628 -628
- data/vendor/kreuzberg/src/extractors/opml.rs +635 -635
- data/vendor/kreuzberg/src/extractors/orgmode.rs +529 -529
- data/vendor/kreuzberg/src/extractors/pdf.rs +749 -749
- data/vendor/kreuzberg/src/extractors/pptx.rs +267 -267
- data/vendor/kreuzberg/src/extractors/rst.rs +577 -577
- data/vendor/kreuzberg/src/extractors/rtf.rs +809 -809
- data/vendor/kreuzberg/src/extractors/security.rs +484 -484
- data/vendor/kreuzberg/src/extractors/security_tests.rs +367 -367
- data/vendor/kreuzberg/src/extractors/structured.rs +142 -142
- data/vendor/kreuzberg/src/extractors/text.rs +265 -265
- data/vendor/kreuzberg/src/extractors/typst.rs +651 -651
- data/vendor/kreuzberg/src/extractors/xml.rs +147 -147
- data/vendor/kreuzberg/src/image/dpi.rs +164 -164
- data/vendor/kreuzberg/src/image/mod.rs +6 -6
- data/vendor/kreuzberg/src/image/preprocessing.rs +417 -417
- data/vendor/kreuzberg/src/image/resize.rs +89 -89
- data/vendor/kreuzberg/src/keywords/config.rs +154 -154
- data/vendor/kreuzberg/src/keywords/mod.rs +237 -237
- data/vendor/kreuzberg/src/keywords/processor.rs +275 -275
- data/vendor/kreuzberg/src/keywords/rake.rs +293 -293
- data/vendor/kreuzberg/src/keywords/types.rs +68 -68
- data/vendor/kreuzberg/src/keywords/yake.rs +163 -163
- data/vendor/kreuzberg/src/language_detection/mod.rs +985 -985
- data/vendor/kreuzberg/src/language_detection/processor.rs +219 -219
- data/vendor/kreuzberg/src/lib.rs +113 -113
- data/vendor/kreuzberg/src/mcp/mod.rs +35 -35
- data/vendor/kreuzberg/src/mcp/server.rs +2076 -2076
- data/vendor/kreuzberg/src/ocr/cache.rs +469 -469
- data/vendor/kreuzberg/src/ocr/error.rs +37 -37
- data/vendor/kreuzberg/src/ocr/hocr.rs +216 -216
- data/vendor/kreuzberg/src/ocr/mod.rs +58 -58
- data/vendor/kreuzberg/src/ocr/processor.rs +863 -863
- data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -4
- data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -144
- data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +452 -452
- data/vendor/kreuzberg/src/ocr/types.rs +393 -393
- data/vendor/kreuzberg/src/ocr/utils.rs +47 -47
- data/vendor/kreuzberg/src/ocr/validation.rs +206 -206
- data/vendor/kreuzberg/src/panic_context.rs +154 -154
- data/vendor/kreuzberg/src/pdf/bindings.rs +44 -44
- data/vendor/kreuzberg/src/pdf/bundled.rs +452 -346
- data/vendor/kreuzberg/src/pdf/error.rs +130 -130
- data/vendor/kreuzberg/src/pdf/images.rs +139 -139
- data/vendor/kreuzberg/src/pdf/metadata.rs +489 -489
- data/vendor/kreuzberg/src/pdf/mod.rs +68 -68
- data/vendor/kreuzberg/src/pdf/rendering.rs +368 -368
- data/vendor/kreuzberg/src/pdf/table.rs +420 -420
- data/vendor/kreuzberg/src/pdf/text.rs +240 -240
- data/vendor/kreuzberg/src/plugins/extractor.rs +1044 -1044
- data/vendor/kreuzberg/src/plugins/mod.rs +212 -212
- data/vendor/kreuzberg/src/plugins/ocr.rs +639 -639
- data/vendor/kreuzberg/src/plugins/processor.rs +650 -650
- data/vendor/kreuzberg/src/plugins/registry.rs +1339 -1339
- data/vendor/kreuzberg/src/plugins/traits.rs +258 -258
- data/vendor/kreuzberg/src/plugins/validator.rs +967 -967
- data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -1470
- data/vendor/kreuzberg/src/text/mod.rs +25 -25
- data/vendor/kreuzberg/src/text/quality.rs +697 -697
- data/vendor/kreuzberg/src/text/quality_processor.rs +219 -219
- data/vendor/kreuzberg/src/text/string_utils.rs +217 -217
- data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -164
- data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -100
- data/vendor/kreuzberg/src/text/token_reduction/core.rs +796 -796
- data/vendor/kreuzberg/src/text/token_reduction/filters.rs +902 -902
- data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -160
- data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -619
- data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +147 -147
- data/vendor/kreuzberg/src/types.rs +1055 -1055
- data/vendor/kreuzberg/src/utils/mod.rs +17 -17
- data/vendor/kreuzberg/src/utils/quality.rs +959 -959
- data/vendor/kreuzberg/src/utils/string_utils.rs +381 -381
- data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -53
- data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -482
- data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -261
- data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -400
- data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -1205
- data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -280
- data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -425
- data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -172
- data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -622
- data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -849
- data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -1300
- data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -175
- data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -734
- data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -37
- data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -100
- data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -801
- data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -849
- data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -693
- data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -111
- data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -162
- data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -226
- data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -41
- data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -196
- data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -227
- data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -181
- data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -791
- data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -47
- data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -760
- data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -634
- data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -136
- data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -84
- data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -681
- data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -64
- data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -51
- data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -476
- data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -163
- data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -1
- data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -101
- data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -477
- data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -490
- data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -415
- data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -223
- data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -331
- data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -562
- data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -436
- data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -561
- data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -193
- data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -420
- data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -448
- data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -32
- data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -33
- data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -420
- data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -76
- data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -129
- data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -54
- data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -118
- data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -149
- data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -506
- data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -75
- data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -519
- data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -647
- data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -62
- data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -796
- data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -31
- data/vendor/kreuzberg/tests/api_extract_multipart.rs +52 -52
- data/vendor/kreuzberg/tests/api_tests.rs +966 -966
- data/vendor/kreuzberg/tests/archive_integration.rs +545 -545
- data/vendor/kreuzberg/tests/batch_orchestration.rs +556 -556
- data/vendor/kreuzberg/tests/batch_processing.rs +318 -318
- data/vendor/kreuzberg/tests/bibtex_parity_test.rs +421 -421
- data/vendor/kreuzberg/tests/concurrency_stress.rs +533 -533
- data/vendor/kreuzberg/tests/config_features.rs +612 -612
- data/vendor/kreuzberg/tests/config_loading_tests.rs +416 -416
- data/vendor/kreuzberg/tests/core_integration.rs +510 -510
- data/vendor/kreuzberg/tests/csv_integration.rs +414 -414
- data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +500 -500
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +122 -122
- data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +370 -370
- data/vendor/kreuzberg/tests/email_integration.rs +327 -327
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +275 -275
- data/vendor/kreuzberg/tests/error_handling.rs +402 -402
- data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +228 -228
- data/vendor/kreuzberg/tests/format_integration.rs +165 -164
- data/vendor/kreuzberg/tests/helpers/mod.rs +142 -142
- data/vendor/kreuzberg/tests/html_table_test.rs +551 -551
- data/vendor/kreuzberg/tests/image_integration.rs +255 -255
- data/vendor/kreuzberg/tests/instrumentation_test.rs +139 -139
- data/vendor/kreuzberg/tests/jats_extractor_tests.rs +639 -639
- data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +704 -704
- data/vendor/kreuzberg/tests/keywords_integration.rs +479 -479
- data/vendor/kreuzberg/tests/keywords_quality.rs +509 -509
- data/vendor/kreuzberg/tests/latex_extractor_tests.rs +496 -496
- data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +490 -490
- data/vendor/kreuzberg/tests/mime_detection.rs +429 -429
- data/vendor/kreuzberg/tests/ocr_configuration.rs +514 -514
- data/vendor/kreuzberg/tests/ocr_errors.rs +698 -698
- data/vendor/kreuzberg/tests/ocr_quality.rs +629 -629
- data/vendor/kreuzberg/tests/ocr_stress.rs +469 -469
- data/vendor/kreuzberg/tests/odt_extractor_tests.rs +674 -674
- data/vendor/kreuzberg/tests/opml_extractor_tests.rs +616 -616
- data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +822 -822
- data/vendor/kreuzberg/tests/pdf_integration.rs +45 -45
- data/vendor/kreuzberg/tests/pdfium_linking.rs +374 -374
- data/vendor/kreuzberg/tests/pipeline_integration.rs +1436 -1436
- data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +776 -776
- data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +560 -560
- data/vendor/kreuzberg/tests/plugin_system.rs +927 -927
- data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -783
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +587 -587
- data/vendor/kreuzberg/tests/rst_extractor_tests.rs +694 -694
- data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +775 -775
- data/vendor/kreuzberg/tests/security_validation.rs +416 -416
- data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -888
- data/vendor/kreuzberg/tests/test_fastembed.rs +631 -631
- data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +1260 -1260
- data/vendor/kreuzberg/tests/typst_extractor_tests.rs +648 -648
- data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -87
- data/vendor/kreuzberg-tesseract/.commitlintrc.json +13 -13
- data/vendor/kreuzberg-tesseract/.crate-ignore +2 -2
- data/vendor/kreuzberg-tesseract/Cargo.lock +2933 -2933
- data/vendor/kreuzberg-tesseract/Cargo.toml +2 -2
- data/vendor/kreuzberg-tesseract/LICENSE +22 -22
- data/vendor/kreuzberg-tesseract/README.md +399 -399
- data/vendor/kreuzberg-tesseract/build.rs +1354 -1354
- data/vendor/kreuzberg-tesseract/patches/README.md +71 -71
- data/vendor/kreuzberg-tesseract/patches/tesseract.diff +199 -199
- data/vendor/kreuzberg-tesseract/src/api.rs +1371 -1371
- data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +77 -77
- data/vendor/kreuzberg-tesseract/src/enums.rs +297 -297
- data/vendor/kreuzberg-tesseract/src/error.rs +81 -81
- data/vendor/kreuzberg-tesseract/src/lib.rs +145 -145
- data/vendor/kreuzberg-tesseract/src/monitor.rs +57 -57
- data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +197 -197
- data/vendor/kreuzberg-tesseract/src/page_iterator.rs +253 -253
- data/vendor/kreuzberg-tesseract/src/result_iterator.rs +286 -286
- data/vendor/kreuzberg-tesseract/src/result_renderer.rs +183 -183
- data/vendor/kreuzberg-tesseract/tests/integration_test.rs +211 -211
- data/vendor/rb-sys/.cargo_vcs_info.json +5 -5
- data/vendor/rb-sys/Cargo.lock +393 -393
- data/vendor/rb-sys/Cargo.toml +70 -70
- data/vendor/rb-sys/Cargo.toml.orig +57 -57
- data/vendor/rb-sys/LICENSE-APACHE +190 -190
- data/vendor/rb-sys/LICENSE-MIT +21 -21
- data/vendor/rb-sys/build/features.rs +111 -111
- data/vendor/rb-sys/build/main.rs +286 -286
- data/vendor/rb-sys/build/stable_api_config.rs +155 -155
- data/vendor/rb-sys/build/version.rs +50 -50
- data/vendor/rb-sys/readme.md +36 -36
- data/vendor/rb-sys/src/bindings.rs +21 -21
- data/vendor/rb-sys/src/hidden.rs +11 -11
- data/vendor/rb-sys/src/lib.rs +35 -35
- data/vendor/rb-sys/src/macros.rs +371 -371
- data/vendor/rb-sys/src/memory.rs +53 -53
- data/vendor/rb-sys/src/ruby_abi_version.rs +38 -38
- data/vendor/rb-sys/src/special_consts.rs +31 -31
- data/vendor/rb-sys/src/stable_api/compiled.c +179 -179
- data/vendor/rb-sys/src/stable_api/compiled.rs +257 -257
- data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +324 -324
- data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +332 -332
- data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +325 -325
- data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +323 -323
- data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +339 -339
- data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +339 -339
- data/vendor/rb-sys/src/stable_api.rs +260 -260
- data/vendor/rb-sys/src/symbol.rs +31 -31
- data/vendor/rb-sys/src/tracking_allocator.rs +330 -330
- data/vendor/rb-sys/src/utils.rs +89 -89
- data/vendor/rb-sys/src/value_type.rs +7 -7
- metadata +81 -22
- data/vendor/kreuzberg-ffi/Cargo.toml +0 -63
- data/vendor/kreuzberg-ffi/README.md +0 -851
- data/vendor/kreuzberg-ffi/build.rs +0 -176
- data/vendor/kreuzberg-ffi/cbindgen.toml +0 -27
- data/vendor/kreuzberg-ffi/kreuzberg-ffi-install.pc +0 -12
- data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +0 -12
- data/vendor/kreuzberg-ffi/kreuzberg.h +0 -1087
- data/vendor/kreuzberg-ffi/src/lib.rs +0 -3616
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +0 -247
- data/vendor/kreuzberg-ffi/tests.disabled/README.md +0 -48
- data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +0 -299
- data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +0 -346
- data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +0 -232
- data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +0 -470
|
@@ -1,651 +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
|
-
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.date = 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: ®ex::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.date.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
|
-
}
|
|
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.date = 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: ®ex::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.date.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
|
+
}
|