docvortex 0.2.1__py3-none-any.whl
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.
- docvortex/__init__.py +21 -0
- docvortex/analyzers/__init__.py +3 -0
- docvortex/analyzers/native/__init__.py +37 -0
- docvortex/analyzers/native/_shared/__init__.py +3 -0
- docvortex/analyzers/native/_shared/hyperlink.py +16 -0
- docvortex/analyzers/native/_shared/image.py +8 -0
- docvortex/analyzers/native/_shared/markup/__init__.py +47 -0
- docvortex/analyzers/native/_shared/markup/anchors.py +30 -0
- docvortex/analyzers/native/_shared/markup/formula.py +37 -0
- docvortex/analyzers/native/_shared/markup/projector.py +57 -0
- docvortex/analyzers/native/_shared/markup/styles.py +19 -0
- docvortex/analyzers/native/_shared/mathml.py +17 -0
- docvortex/analyzers/native/_shared/names.py +7 -0
- docvortex/analyzers/native/_shared/xycut.py +414 -0
- docvortex/analyzers/native/contracts.py +44 -0
- docvortex/analyzers/native/csv.py +351 -0
- docvortex/analyzers/native/epub/__init__.py +18 -0
- docvortex/analyzers/native/epub/constants.py +45 -0
- docvortex/analyzers/native/epub/converter.py +80 -0
- docvortex/analyzers/native/epub/errors.py +20 -0
- docvortex/analyzers/native/epub/metadata.py +31 -0
- docvortex/analyzers/native/epub/package.py +649 -0
- docvortex/analyzers/native/epub/xhtml.py +306 -0
- docvortex/analyzers/native/html/__init__.py +6 -0
- docvortex/analyzers/native/html/anchors.py +271 -0
- docvortex/analyzers/native/html/constants.py +25 -0
- docvortex/analyzers/native/html/contracts.py +7 -0
- docvortex/analyzers/native/html/converter.py +117 -0
- docvortex/analyzers/native/html/document.py +389 -0
- docvortex/analyzers/native/html/errors.py +12 -0
- docvortex/analyzers/native/html/resources.py +365 -0
- docvortex/analyzers/native/html/selector.py +421 -0
- docvortex/analyzers/native/models.py +209 -0
- docvortex/analyzers/native/ofd/__init__.py +14 -0
- docvortex/analyzers/native/ofd/constants.py +59 -0
- docvortex/analyzers/native/ofd/converter.py +31 -0
- docvortex/analyzers/native/ofd/errors.py +16 -0
- docvortex/analyzers/native/ofd/geometry.py +215 -0
- docvortex/analyzers/native/ofd/images.py +124 -0
- docvortex/analyzers/native/ofd/metadata.py +53 -0
- docvortex/analyzers/native/ofd/models.py +170 -0
- docvortex/analyzers/native/ofd/package.py +346 -0
- docvortex/analyzers/native/ofd/path.py +201 -0
- docvortex/analyzers/native/ofd/reading_order.py +293 -0
- docvortex/analyzers/native/ofd/resources.py +122 -0
- docvortex/analyzers/native/ofd/scene.py +347 -0
- docvortex/analyzers/native/ofd/table.py +240 -0
- docvortex/analyzers/native/ofd/text.py +521 -0
- docvortex/analyzers/native/office/__init__.py +3 -0
- docvortex/analyzers/native/office/doc/__init__.py +3 -0
- docvortex/analyzers/native/office/doc/bookmarks.py +81 -0
- docvortex/analyzers/native/office/doc/doc_converter.py +573 -0
- docvortex/analyzers/native/office/doc/fib.py +261 -0
- docvortex/analyzers/native/office/doc/fields.py +101 -0
- docvortex/analyzers/native/office/doc/formatting.py +171 -0
- docvortex/analyzers/native/office/doc/images.py +162 -0
- docvortex/analyzers/native/office/doc/lists.py +356 -0
- docvortex/analyzers/native/office/doc/models.py +164 -0
- docvortex/analyzers/native/office/doc/parser.py +851 -0
- docvortex/analyzers/native/office/doc/pieces.py +256 -0
- docvortex/analyzers/native/office/doc/records.py +40 -0
- docvortex/analyzers/native/office/doc/sprm.py +269 -0
- docvortex/analyzers/native/office/doc/styles.py +215 -0
- docvortex/analyzers/native/office/docx/__init__.py +3 -0
- docvortex/analyzers/native/office/docx/context.py +71 -0
- docvortex/analyzers/native/office/docx/docx_converter.py +614 -0
- docvortex/analyzers/native/office/docx/equationxml.py +119 -0
- docvortex/analyzers/native/office/docx/fields.py +816 -0
- docvortex/analyzers/native/office/docx/formatting_types.py +23 -0
- docvortex/analyzers/native/office/docx/main.py +50 -0
- docvortex/analyzers/native/office/docx/numbering.py +491 -0
- docvortex/analyzers/native/office/docx/office_xml.py +57 -0
- docvortex/analyzers/native/office/docx/package_normalizer.py +248 -0
- docvortex/analyzers/native/office/docx/resources.py +526 -0
- docvortex/analyzers/native/office/docx/styles.py +693 -0
- docvortex/analyzers/native/office/docx/tables.py +515 -0
- docvortex/analyzers/native/office/equation/__init__.py +3 -0
- docvortex/analyzers/native/office/equation/image.py +470 -0
- docvortex/analyzers/native/office/equation/latex_dict.py +324 -0
- docvortex/analyzers/native/office/equation/mtef.py +885 -0
- docvortex/analyzers/native/office/equation/mtef_v5.py +941 -0
- docvortex/analyzers/native/office/equation/omml.py +561 -0
- docvortex/analyzers/native/office/equation/ooxml.py +62 -0
- docvortex/analyzers/native/office/errors.py +33 -0
- docvortex/analyzers/native/office/image.py +307 -0
- docvortex/analyzers/native/office/legacy/__init__.py +3 -0
- docvortex/analyzers/native/office/legacy/binary.py +43 -0
- docvortex/analyzers/native/office/legacy/officeart.py +362 -0
- docvortex/analyzers/native/office/legacy/ole.py +110 -0
- docvortex/analyzers/native/office/limits.py +12 -0
- docvortex/analyzers/native/office/odf/__init__.py +3 -0
- docvortex/analyzers/native/office/odf/chart.py +76 -0
- docvortex/analyzers/native/office/odf/constants.py +70 -0
- docvortex/analyzers/native/office/odf/converters.py +403 -0
- docvortex/analyzers/native/office/odf/errors.py +18 -0
- docvortex/analyzers/native/office/odf/metadata.py +91 -0
- docvortex/analyzers/native/office/odf/models.py +176 -0
- docvortex/analyzers/native/office/odf/package.py +270 -0
- docvortex/analyzers/native/office/odf/styles.py +329 -0
- docvortex/analyzers/native/office/odf/table.py +469 -0
- docvortex/analyzers/native/office/odf/text.py +1002 -0
- docvortex/analyzers/native/office/ooxml_chart.py +1016 -0
- docvortex/analyzers/native/office/opc.py +38 -0
- docvortex/analyzers/native/office/ppt/__init__.py +3 -0
- docvortex/analyzers/native/office/ppt/models.py +118 -0
- docvortex/analyzers/native/office/ppt/parser.py +1895 -0
- docvortex/analyzers/native/office/ppt/ppt_converter.py +292 -0
- docvortex/analyzers/native/office/ppt/records.py +131 -0
- docvortex/analyzers/native/office/ppt/style_text.py +247 -0
- docvortex/analyzers/native/office/pptx/__init__.py +3 -0
- docvortex/analyzers/native/office/pptx/context.py +113 -0
- docvortex/analyzers/native/office/pptx/lists.py +558 -0
- docvortex/analyzers/native/office/pptx/main.py +20 -0
- docvortex/analyzers/native/office/pptx/package_normalizer.py +321 -0
- docvortex/analyzers/native/office/pptx/pptx_converter.py +323 -0
- docvortex/analyzers/native/office/pptx/resources.py +329 -0
- docvortex/analyzers/native/office/pptx/shapes.py +393 -0
- docvortex/analyzers/native/office/pptx/text_styles.py +625 -0
- docvortex/analyzers/native/office/pptx/titles.py +178 -0
- docvortex/analyzers/native/office/rich_text.py +420 -0
- docvortex/analyzers/native/office/rtf/__init__.py +3 -0
- docvortex/analyzers/native/office/rtf/converter.py +708 -0
- docvortex/analyzers/native/office/rtf/lexer.py +213 -0
- docvortex/analyzers/native/office/rtf/math.py +339 -0
- docvortex/analyzers/native/office/rtf/models.py +185 -0
- docvortex/analyzers/native/office/rtf/parser.py +1553 -0
- docvortex/analyzers/native/office/spreadsheet/__init__.py +3 -0
- docvortex/analyzers/native/office/spreadsheet/html.py +81 -0
- docvortex/analyzers/native/office/spreadsheet/models.py +79 -0
- docvortex/analyzers/native/office/spreadsheet/projector.py +928 -0
- docvortex/analyzers/native/office/streams.py +18 -0
- docvortex/analyzers/native/office/xls/__init__.py +3 -0
- docvortex/analyzers/native/office/xls/chart.py +132 -0
- docvortex/analyzers/native/office/xls/embedded_chart.py +299 -0
- docvortex/analyzers/native/office/xls/models.py +109 -0
- docvortex/analyzers/native/office/xls/number_format.py +521 -0
- docvortex/analyzers/native/office/xls/parser.py +1145 -0
- docvortex/analyzers/native/office/xls/records.py +201 -0
- docvortex/analyzers/native/office/xls/strings.py +205 -0
- docvortex/analyzers/native/office/xls/xls_converter.py +354 -0
- docvortex/analyzers/native/office/xlsx/__init__.py +3 -0
- docvortex/analyzers/native/office/xlsx/main.py +20 -0
- docvortex/analyzers/native/office/xlsx/ooxml_ole.py +522 -0
- docvortex/analyzers/native/office/xlsx/package_normalizer.py +310 -0
- docvortex/analyzers/native/office/xlsx/xlsx_converter.py +716 -0
- docvortex/analyzers/native/pdf/__init__.py +3 -0
- docvortex/analyzers/native/pdf/auxiliary_text.py +1671 -0
- docvortex/analyzers/native/pdf/char_geometry.py +1662 -0
- docvortex/analyzers/native/pdf/code_blocks.py +535 -0
- docvortex/analyzers/native/pdf/formulas.py +1985 -0
- docvortex/analyzers/native/pdf/geometry.py +281 -0
- docvortex/analyzers/native/pdf/graphics.py +1501 -0
- docvortex/analyzers/native/pdf/index_blocks.py +268 -0
- docvortex/analyzers/native/pdf/inline/__init__.py +3 -0
- docvortex/analyzers/native/pdf/inline/common.py +112 -0
- docvortex/analyzers/native/pdf/inline/detection.py +590 -0
- docvortex/analyzers/native/pdf/inline/matching.py +1185 -0
- docvortex/analyzers/native/pdf/inline/materialize.py +457 -0
- docvortex/analyzers/native/pdf/inline/scripts.py +975 -0
- docvortex/analyzers/native/pdf/inline/types.py +385 -0
- docvortex/analyzers/native/pdf/line_layout.py +1106 -0
- docvortex/analyzers/native/pdf/line_merging.py +1223 -0
- docvortex/analyzers/native/pdf/models.py +246 -0
- docvortex/analyzers/native/pdf/native_text.py +1004 -0
- docvortex/analyzers/native/pdf/pipeline.py +1390 -0
- docvortex/analyzers/native/pdf/script_geometry.py +636 -0
- docvortex/analyzers/native/pdf/shared.py +30 -0
- docvortex/analyzers/native/pdf/spatial_text.py +383 -0
- docvortex/analyzers/native/pdf/table_annotations.py +446 -0
- docvortex/analyzers/native/pdf/table_constants.py +48 -0
- docvortex/analyzers/native/pdf/table_detection.py +227 -0
- docvortex/analyzers/native/pdf/table_filled_grid.py +218 -0
- docvortex/analyzers/native/pdf/table_geometry.py +40 -0
- docvortex/analyzers/native/pdf/table_materialization.py +444 -0
- docvortex/analyzers/native/pdf/table_recovery/__init__.py +15 -0
- docvortex/analyzers/native/pdf/table_recovery/candidate.py +356 -0
- docvortex/analyzers/native/pdf/table_recovery/contracts.py +154 -0
- docvortex/analyzers/native/pdf/table_recovery/engine.py +609 -0
- docvortex/analyzers/native/pdf/table_recovery/geometry.py +164 -0
- docvortex/analyzers/native/pdf/table_recovery/sparse_common.py +85 -0
- docvortex/analyzers/native/pdf/table_recovery/sparse_hybrid.py +804 -0
- docvortex/analyzers/native/pdf/table_recovery/sparse_multiline.py +1122 -0
- docvortex/analyzers/native/pdf/table_recovery/text.py +414 -0
- docvortex/analyzers/native/pdf/table_recovery/text_grid.py +618 -0
- docvortex/analyzers/native/pdf/table_recovery/vector.py +1931 -0
- docvortex/analyzers/native/pdf/table_rows.py +34 -0
- docvortex/analyzers/native/pdf/table_rules.py +1129 -0
- docvortex/analyzers/native/pdf/table_text_styles.py +283 -0
- docvortex/analyzers/native/pdf/tables.py +147 -0
- docvortex/analyzers/native/pdf/text_assembly/__init__.py +3 -0
- docvortex/analyzers/native/pdf/text_assembly/annotations.py +581 -0
- docvortex/analyzers/native/pdf/text_assembly/assembly.py +292 -0
- docvortex/analyzers/native/pdf/text_assembly/common.py +477 -0
- docvortex/analyzers/native/pdf/text_assembly/footnotes.py +394 -0
- docvortex/analyzers/native/pdf/text_assembly/merging.py +1274 -0
- docvortex/analyzers/native/pdf/text_assembly/rows.py +692 -0
- docvortex/analyzers/native/pdf/text_blocks.py +82 -0
- docvortex/analyzers/native/pdf/text_styles.py +55 -0
- docvortex/analyzers/native/pdf/title_analysis/__init__.py +3 -0
- docvortex/analyzers/native/pdf/title_analysis/body_profile.py +215 -0
- docvortex/analyzers/native/pdf/title_analysis/common.py +117 -0
- docvortex/analyzers/native/pdf/title_analysis/document_profile.py +164 -0
- docvortex/analyzers/native/pdf/title_analysis/lane_titles.py +758 -0
- docvortex/analyzers/native/pdf/title_analysis/page_titles.py +1024 -0
- docvortex/analyzers/native/pdf/title_analysis/prototype.py +194 -0
- docvortex/analyzers/native/pdf/title_analysis/structural.py +1081 -0
- docvortex/analyzers/native/pdf/titles.py +75 -0
- docvortex/analyzers/native/pdf/typography.py +19 -0
- docvortex/analyzers/native/pdf/visual_annotations.py +1262 -0
- docvortex/api.py +180 -0
- docvortex/assets/__init__.py +5 -0
- docvortex/assets/store.py +51 -0
- docvortex/cli.py +54 -0
- docvortex/codecs/__init__.py +3 -0
- docvortex/codecs/html/__init__.py +22 -0
- docvortex/codecs/html/contracts.py +236 -0
- docvortex/codecs/html/materializer.py +331 -0
- docvortex/codecs/html/parser.py +763 -0
- docvortex/codecs/html/resources.py +34 -0
- docvortex/codecs/json.py +17 -0
- docvortex/content/__init__.py +5 -0
- docvortex/content/inline.py +248 -0
- docvortex/content/markup/__init__.py +44 -0
- docvortex/content/markup/anchors.py +188 -0
- docvortex/content/markup/formula.py +280 -0
- docvortex/content/markup/projector.py +1237 -0
- docvortex/content/markup/styles.py +327 -0
- docvortex/content/mathml.py +167 -0
- docvortex/content/normalization.py +188 -0
- docvortex/content/spans.py +183 -0
- docvortex/content/table/__init__.py +18 -0
- docvortex/content/table/blocks.py +152 -0
- docvortex/content/table/content.py +425 -0
- docvortex/content/table/document.py +104 -0
- docvortex/content/table/html.py +399 -0
- docvortex/content/table/models.py +76 -0
- docvortex/content/table/rules.py +42 -0
- docvortex/content/table/structure.py +221 -0
- docvortex/content/tree.py +5 -0
- docvortex/document/__init__.py +3 -0
- docvortex/document/contracts.py +22 -0
- docvortex/document/detection.py +389 -0
- docvortex/document/filetypes.py +175 -0
- docvortex/document/page_range.py +167 -0
- docvortex/document/pdf/__init__.py +19 -0
- docvortex/document/pdf/classify.py +1138 -0
- docvortex/document/pdf/constants.py +132 -0
- docvortex/document/pdf/diagnostics.py +350 -0
- docvortex/document/pdf/document.py +582 -0
- docvortex/document/pdf/font_runtime.py +335 -0
- docvortex/document/pdf/geometry.py +31 -0
- docvortex/document/pdf/images.py +654 -0
- docvortex/document/pdf/native_annotations.py +367 -0
- docvortex/document/pdf/native_contracts.py +169 -0
- docvortex/document/pdf/native_coordinates.py +216 -0
- docvortex/document/pdf/native_lifecycle.py +16 -0
- docvortex/document/pdf/native_objects.py +902 -0
- docvortex/document/pdf/native_text_geometry.py +315 -0
- docvortex/document/pdf/pdfium.py +325 -0
- docvortex/document/pdf/raster.py +46 -0
- docvortex/document/pdf/text/__init__.py +62 -0
- docvortex/document/pdf/text/contracts.py +211 -0
- docvortex/document/pdf/text/extract.py +165 -0
- docvortex/document/pdf/text/geometry.py +16 -0
- docvortex/document/pdf/text/groups.py +162 -0
- docvortex/document/pdf/visual_geometry.py +201 -0
- docvortex/document/pdf/visuals.py +343 -0
- docvortex/document/source.py +92 -0
- docvortex/errors.py +21 -0
- docvortex/export/__init__.py +3 -0
- docvortex/export/bundle.py +98 -0
- docvortex/export/files.py +66 -0
- docvortex/export/middle.py +208 -0
- docvortex/foundation/__init__.py +3 -0
- docvortex/foundation/geometry.py +125 -0
- docvortex/foundation/hyperlink.py +65 -0
- docvortex/foundation/image.py +48 -0
- docvortex/foundation/image_encoding.py +30 -0
- docvortex/foundation/image_payload.py +280 -0
- docvortex/foundation/language.py +92 -0
- docvortex/foundation/platform.py +38 -0
- docvortex/foundation/text.py +153 -0
- docvortex/foundation/type_identity.py +20 -0
- docvortex/foundation/xml_names.py +20 -0
- docvortex/options.py +30 -0
- docvortex/postprocess/__init__.py +3 -0
- docvortex/postprocess/content.py +53 -0
- docvortex/postprocess/document.py +19 -0
- docvortex/postprocess/lists.py +236 -0
- docvortex/postprocess/page_blocks.py +214 -0
- docvortex/postprocess/pages.py +95 -0
- docvortex/postprocess/paragraphs.py +580 -0
- docvortex/postprocess/visual.py +715 -0
- docvortex/render/__init__.py +48 -0
- docvortex/render/_internal/__init__.py +3 -0
- docvortex/render/_internal/common/__init__.py +3 -0
- docvortex/render/_internal/common/context.py +43 -0
- docvortex/render/_internal/common/html_table.py +178 -0
- docvortex/render/_internal/common/index.py +33 -0
- docvortex/render/_internal/common/list_items.py +158 -0
- docvortex/render/_internal/common/planner.py +140 -0
- docvortex/render/_internal/docx/__init__.py +3 -0
- docvortex/render/_internal/docx/assets.py +202 -0
- docvortex/render/_internal/docx/inline.py +434 -0
- docvortex/render/_internal/docx/math.py +220 -0
- docvortex/render/_internal/docx/renderer.py +905 -0
- docvortex/render/_internal/docx/styles.py +195 -0
- docvortex/render/_internal/docx/table.py +442 -0
- docvortex/render/_internal/epub/__init__.py +5 -0
- docvortex/render/_internal/epub/assets.py +173 -0
- docvortex/render/_internal/epub/package.py +249 -0
- docvortex/render/_internal/epub/renderer.py +1156 -0
- docvortex/render/_internal/html/__init__.py +3 -0
- docvortex/render/_internal/html/inline.py +346 -0
- docvortex/render/_internal/html/renderer.py +1041 -0
- docvortex/render/_internal/html/sanitizer.py +478 -0
- docvortex/render/_internal/html/table.py +121 -0
- docvortex/render/_internal/latex/__init__.py +1 -0
- docvortex/render/_internal/latex/assets.py +85 -0
- docvortex/render/_internal/latex/inline.py +145 -0
- docvortex/render/_internal/latex/renderer.py +506 -0
- docvortex/render/_internal/latex/table.py +347 -0
- docvortex/render/_internal/markdown/__init__.py +3 -0
- docvortex/render/_internal/markdown/assets.py +78 -0
- docvortex/render/_internal/markdown/blocks.py +635 -0
- docvortex/render/_internal/markdown/escaping.py +51 -0
- docvortex/render/_internal/markdown/inline.py +260 -0
- docvortex/render/_internal/markdown/renderer.py +93 -0
- docvortex/render/_internal/markdown/table.py +281 -0
- docvortex/render/_internal/pdf/__init__.py +3 -0
- docvortex/render/_internal/pdf/assets.py +197 -0
- docvortex/render/_internal/pdf/formula.py +417 -0
- docvortex/render/_internal/pdf/inline.py +343 -0
- docvortex/render/_internal/pdf/renderer.py +734 -0
- docvortex/render/_internal/pdf/styles.py +206 -0
- docvortex/render/_internal/pdf/table.py +272 -0
- docvortex/render/_internal/structured_content/__init__.py +3 -0
- docvortex/render/_internal/structured_content/renderer.py +193 -0
- docvortex/render/api.py +199 -0
- docvortex/render/contracts.py +205 -0
- docvortex/render/docx.py +38 -0
- docvortex/render/epub.py +35 -0
- docvortex/render/fragments.py +70 -0
- docvortex/render/html.py +29 -0
- docvortex/render/latex.py +24 -0
- docvortex/render/markdown.py +50 -0
- docvortex/render/pdf.py +25 -0
- docvortex/render/structured_content.py +23 -0
- docvortex/resources/epub/docvortex.css +91 -0
- docvortex/resources/fasttext-langdetect/lid.176.ftz +0 -0
- docvortex/resources/fonts/DroidSansFallbackFull.ttf +0 -0
- docvortex/resources/fonts/NOTICE +190 -0
- docvortex/resources/fonts/manifest.json +9 -0
- docvortex/resources/html/docvortex.css +601 -0
- docvortex/resources/html/docvortex.min.css +1 -0
- docvortex/result.py +91 -0
- docvortex/schema.py +1141 -0
- docvortex/version.py +3 -0
- docvortex-0.2.1.dist-info/METADATA +193 -0
- docvortex-0.2.1.dist-info/RECORD +364 -0
- docvortex-0.2.1.dist-info/WHEEL +5 -0
- docvortex-0.2.1.dist-info/entry_points.txt +2 -0
- docvortex-0.2.1.dist-info/licenses/LICENSE.md +21 -0
- docvortex-0.2.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"""集中识别静态 HTML/XHTML 公式并归一化为裸 LaTeX。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import re
|
|
7
|
+
from typing import Literal, TypeAlias
|
|
8
|
+
|
|
9
|
+
from lxml import etree # type: ignore[reportMissingImports]
|
|
10
|
+
|
|
11
|
+
from docvortex.content.mathml import mathml_to_latex
|
|
12
|
+
from docvortex.foundation.xml_names import local_name
|
|
13
|
+
from docvortex.foundation.type_identity import preserve_type_module
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
FormulaDisplay: TypeAlias = Literal["inline", "block"]
|
|
17
|
+
FormulaSourceKind: TypeAlias = Literal[
|
|
18
|
+
"docvortex_latex",
|
|
19
|
+
"mathml_tex_annotation",
|
|
20
|
+
"tex_script",
|
|
21
|
+
"data_tex",
|
|
22
|
+
"docvortex_text",
|
|
23
|
+
"mathml_alttext",
|
|
24
|
+
"embedded_mathml",
|
|
25
|
+
"presentation_mathml",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
_MATH_SCRIPT_TYPE_RE = re.compile(r"^math/(?:tex|latex)(?:\s*;\s*mode\s*=\s*display)?$", re.IGNORECASE)
|
|
29
|
+
_DISPLAY_FORMULA_TOKENS = frozenset({"display", "katex-display", "math-display", "mathjax-display", "docvortex-math--block"})
|
|
30
|
+
_TEX_ANNOTATION_ENCODINGS = frozenset(
|
|
31
|
+
{
|
|
32
|
+
"application/tex",
|
|
33
|
+
"application/x-latex",
|
|
34
|
+
"application/x-tex",
|
|
35
|
+
"text/latex",
|
|
36
|
+
"text/tex",
|
|
37
|
+
}
|
|
38
|
+
)
|
|
39
|
+
_PRESENTATION_MATHML_TAGS = frozenset(
|
|
40
|
+
{
|
|
41
|
+
"annotation",
|
|
42
|
+
"annotation-xml",
|
|
43
|
+
"math",
|
|
44
|
+
"mfenced",
|
|
45
|
+
"mfrac",
|
|
46
|
+
"mi",
|
|
47
|
+
"mlabeledtr",
|
|
48
|
+
"mn",
|
|
49
|
+
"mo",
|
|
50
|
+
"mover",
|
|
51
|
+
"mpadded",
|
|
52
|
+
"mphantom",
|
|
53
|
+
"mroot",
|
|
54
|
+
"mrow",
|
|
55
|
+
"mspace",
|
|
56
|
+
"msqrt",
|
|
57
|
+
"mstyle",
|
|
58
|
+
"msub",
|
|
59
|
+
"msubsup",
|
|
60
|
+
"msup",
|
|
61
|
+
"mtable",
|
|
62
|
+
"mtd",
|
|
63
|
+
"mtext",
|
|
64
|
+
"mtr",
|
|
65
|
+
"munder",
|
|
66
|
+
"munderover",
|
|
67
|
+
"semantics",
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
_PRESENTATION_ARITIES: dict[str, int] = {
|
|
71
|
+
"mfrac": 2,
|
|
72
|
+
"mover": 2,
|
|
73
|
+
"mroot": 2,
|
|
74
|
+
"msub": 2,
|
|
75
|
+
"msubsup": 3,
|
|
76
|
+
"msup": 2,
|
|
77
|
+
"munder": 2,
|
|
78
|
+
"munderover": 3,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True, slots=True)
|
|
83
|
+
class FormulaExtraction:
|
|
84
|
+
"""保存一次成功公式识别的裸 LaTeX、显示方式和来源。"""
|
|
85
|
+
|
|
86
|
+
latex: str
|
|
87
|
+
display: FormulaDisplay
|
|
88
|
+
source_kind: FormulaSourceKind
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def extract_formula(element: etree._Element) -> FormulaExtraction | None:
|
|
92
|
+
"""按固定优先级从公式节点或常见公式包装器中提取裸 LaTeX。"""
|
|
93
|
+
display = _formula_display(element)
|
|
94
|
+
|
|
95
|
+
if latex := _subtree_attribute(element, "data-docvortex-latex"):
|
|
96
|
+
return FormulaExtraction(latex, display, "docvortex_latex")
|
|
97
|
+
|
|
98
|
+
if latex := _tex_annotation(element):
|
|
99
|
+
return FormulaExtraction(latex, display, "mathml_tex_annotation")
|
|
100
|
+
|
|
101
|
+
if latex := _tex_script(element):
|
|
102
|
+
return FormulaExtraction(latex, _script_display(element, display), "tex_script")
|
|
103
|
+
|
|
104
|
+
if latex := _data_formula(element):
|
|
105
|
+
return FormulaExtraction(latex, display, "data_tex")
|
|
106
|
+
|
|
107
|
+
if "docvortex-math" in _class_tokens(element):
|
|
108
|
+
latex = strip_formula_delimiters("".join(element.itertext()))
|
|
109
|
+
if latex:
|
|
110
|
+
return FormulaExtraction(latex, display, "docvortex_text")
|
|
111
|
+
|
|
112
|
+
if latex := _mathml_alttext(element):
|
|
113
|
+
return FormulaExtraction(latex, display, "mathml_alttext")
|
|
114
|
+
|
|
115
|
+
name = local_name(element)
|
|
116
|
+
if name != "math":
|
|
117
|
+
math_element = next(
|
|
118
|
+
(child for child in element.iterdescendants() if isinstance(child.tag, str) and local_name(child) == "math"),
|
|
119
|
+
None,
|
|
120
|
+
)
|
|
121
|
+
if math_element is not None and (latex := _presentation_mathml(math_element)):
|
|
122
|
+
return FormulaExtraction(latex, _formula_display(math_element, display), "embedded_mathml")
|
|
123
|
+
return None
|
|
124
|
+
|
|
125
|
+
if latex := _presentation_mathml(element):
|
|
126
|
+
return FormulaExtraction(latex, display, "presentation_mathml")
|
|
127
|
+
return None
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def strip_formula_delimiters(value: str) -> str:
|
|
131
|
+
"""重复移除常见外层 TeX 定界符,不改动公式内部结构。"""
|
|
132
|
+
normalized = value.strip()
|
|
133
|
+
pairs = (("$$", "$$"), (r"\[", r"\]"), (r"\(", r"\)"), ("$", "$"))
|
|
134
|
+
for _ in range(4):
|
|
135
|
+
stripped = normalized
|
|
136
|
+
for opening, closing in pairs:
|
|
137
|
+
has_outer_pair = normalized.startswith(opening) and normalized.endswith(closing)
|
|
138
|
+
if has_outer_pair and len(normalized) > len(opening) + len(closing):
|
|
139
|
+
stripped = normalized[len(opening) : -len(closing)].strip()
|
|
140
|
+
break
|
|
141
|
+
if stripped == normalized:
|
|
142
|
+
break
|
|
143
|
+
normalized = stripped
|
|
144
|
+
return normalized
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def is_tex_script(element: etree._Element) -> bool:
|
|
148
|
+
"""判断元素是否是受支持的静态 TeX/LaTeX script carrier。"""
|
|
149
|
+
return local_name(element) == "script" and _MATH_SCRIPT_TYPE_RE.fullmatch((element.get("type") or "").strip()) is not None
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _normalized_attribute(element: etree._Element, name: str) -> str | None:
|
|
153
|
+
"""读取并去除外层定界符,空值统一返回 None。"""
|
|
154
|
+
value = strip_formula_delimiters(element.get(name) or "")
|
|
155
|
+
return value or None
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _subtree_attribute(element: etree._Element, name: str) -> str | None:
|
|
159
|
+
"""按文档顺序返回自身或后代首个非空规范公式属性。"""
|
|
160
|
+
for candidate in [element, *element.iterdescendants()]:
|
|
161
|
+
if isinstance(candidate.tag, str) and (value := _normalized_attribute(candidate, name)):
|
|
162
|
+
return value
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _tex_annotation(element: etree._Element) -> str | None:
|
|
167
|
+
"""返回节点子树中首个受支持 TeX annotation。"""
|
|
168
|
+
candidates = [element, *element.iterdescendants()]
|
|
169
|
+
for candidate in candidates:
|
|
170
|
+
if not isinstance(candidate.tag, str) or local_name(candidate) != "annotation":
|
|
171
|
+
continue
|
|
172
|
+
encoding = (candidate.get("encoding") or "").strip().casefold()
|
|
173
|
+
if encoding not in _TEX_ANNOTATION_ENCODINGS:
|
|
174
|
+
continue
|
|
175
|
+
latex = strip_formula_delimiters("".join(candidate.itertext()))
|
|
176
|
+
if latex:
|
|
177
|
+
return latex
|
|
178
|
+
return None
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _tex_script(element: etree._Element) -> str | None:
|
|
182
|
+
"""返回当前节点或后代中首个受支持 TeX script 内容。"""
|
|
183
|
+
candidates = [element, *element.iterdescendants()]
|
|
184
|
+
for candidate in candidates:
|
|
185
|
+
if not isinstance(candidate.tag, str) or not is_tex_script(candidate):
|
|
186
|
+
continue
|
|
187
|
+
latex = strip_formula_delimiters("".join(candidate.itertext()))
|
|
188
|
+
if latex:
|
|
189
|
+
return latex
|
|
190
|
+
return None
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _data_formula(element: etree._Element) -> str | None:
|
|
194
|
+
"""按 data-tex、data-expr 顺序读取节点及其后代。"""
|
|
195
|
+
for candidate in [element, *element.iterdescendants()]:
|
|
196
|
+
if not isinstance(candidate.tag, str):
|
|
197
|
+
continue
|
|
198
|
+
for attribute in ("data-tex", "data-expr"):
|
|
199
|
+
if latex := _normalized_attribute(candidate, attribute):
|
|
200
|
+
return latex
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _mathml_alttext(element: etree._Element) -> str | None:
|
|
205
|
+
"""返回首个 MathML alttext 中声明的 LaTeX。"""
|
|
206
|
+
for candidate in [element, *element.iterdescendants()]:
|
|
207
|
+
if not isinstance(candidate.tag, str) or local_name(candidate) != "math":
|
|
208
|
+
continue
|
|
209
|
+
if latex := _normalized_attribute(candidate, "alttext"):
|
|
210
|
+
return latex
|
|
211
|
+
return None
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _presentation_mathml(element: etree._Element) -> str | None:
|
|
215
|
+
"""尽力把受支持 Presentation MathML 转换为裸 LaTeX。"""
|
|
216
|
+
if not _is_supported_presentation_mathml(element):
|
|
217
|
+
return None
|
|
218
|
+
latex = mathml_to_latex(element)
|
|
219
|
+
normalized = strip_formula_delimiters(latex or "")
|
|
220
|
+
return normalized or None
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _is_supported_presentation_mathml(element: etree._Element) -> bool:
|
|
224
|
+
"""拒绝未知节点和畸形固定元数结构,避免把普通 XML 文本冒充为 LaTeX。"""
|
|
225
|
+
if local_name(element) != "math":
|
|
226
|
+
return False
|
|
227
|
+
for candidate in [element, *element.iterdescendants()]:
|
|
228
|
+
if not isinstance(candidate.tag, str):
|
|
229
|
+
continue
|
|
230
|
+
name = local_name(candidate)
|
|
231
|
+
if name not in _PRESENTATION_MATHML_TAGS:
|
|
232
|
+
return False
|
|
233
|
+
children = [child for child in candidate if isinstance(child.tag, str)]
|
|
234
|
+
if name in _PRESENTATION_ARITIES and len(children) != _PRESENTATION_ARITIES[name]:
|
|
235
|
+
return False
|
|
236
|
+
if name in {"mi", "mn", "mo", "mtext", "mspace"} and children:
|
|
237
|
+
return False
|
|
238
|
+
if name == "semantics" and (not children or local_name(children[0]) in {"annotation", "annotation-xml"}):
|
|
239
|
+
return False
|
|
240
|
+
return True
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _formula_display(element: etree._Element, default: FormulaDisplay = "inline") -> FormulaDisplay:
|
|
244
|
+
"""从显式属性、MathML display 和完整 class token 推断显示方式。"""
|
|
245
|
+
declared = (element.get("data-formula-display") or "").strip().casefold()
|
|
246
|
+
if declared in {"inline", "block"}:
|
|
247
|
+
return declared # type: ignore[return-value]
|
|
248
|
+
if (element.get("display") or "").strip().casefold() == "block":
|
|
249
|
+
return "block"
|
|
250
|
+
classes = frozenset((element.get("class") or "").casefold().split())
|
|
251
|
+
return "block" if classes & _DISPLAY_FORMULA_TOKENS else default
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _script_display(element: etree._Element, default: FormulaDisplay) -> FormulaDisplay:
|
|
255
|
+
"""在脚本来源中识别 mode=display,并保留外层显式显示方式。"""
|
|
256
|
+
for candidate in [element, *element.iterdescendants()]:
|
|
257
|
+
if not isinstance(candidate.tag, str) or not is_tex_script(candidate):
|
|
258
|
+
continue
|
|
259
|
+
if "mode=display" in re.sub(r"\s+", "", (candidate.get("type") or "").casefold()):
|
|
260
|
+
return "block"
|
|
261
|
+
break
|
|
262
|
+
return default
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _class_tokens(element: etree._Element) -> frozenset[str]:
|
|
266
|
+
"""返回不做任意 substring 匹配的完整 class token。"""
|
|
267
|
+
return frozenset((element.get("class") or "").casefold().split())
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
__all__ = [
|
|
271
|
+
"FormulaDisplay",
|
|
272
|
+
"FormulaExtraction",
|
|
273
|
+
"FormulaSourceKind",
|
|
274
|
+
"extract_formula",
|
|
275
|
+
"is_tex_script",
|
|
276
|
+
"strip_formula_delimiters",
|
|
277
|
+
]
|
|
278
|
+
|
|
279
|
+
# 保持既有公开类型的 pickle 路径,所有旧、新入口指向同一个类。
|
|
280
|
+
preserve_type_module(FormulaExtraction, "docvortex.analyzers.native._shared.markup.formula")
|