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,220 @@
|
|
|
1
|
+
"""DOCX renderer 使用的 LaTeX 与 OMML 转换。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from latex2mathml.converter import convert as latex_to_mathml
|
|
6
|
+
from lxml import etree
|
|
7
|
+
from mathml2omml import convert as mathml_to_omml
|
|
8
|
+
import re
|
|
9
|
+
|
|
10
|
+
_OFFICE_MATH_NAMESPACE = "http://schemas.openxmlformats.org/officeDocument/2006/math"
|
|
11
|
+
_EMPTY_SCRIPT_RE = re.compile(r"(?:_\s*\{\s*\}|\^\s*\{\s*\})")
|
|
12
|
+
_GENFRAC_COMMAND = r"\genfrac"
|
|
13
|
+
_GENFRAC_ARGUMENT_COUNT = 6
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class DocxFormulaError(ValueError):
|
|
17
|
+
"""表示 LaTeX 无法转换为可插入 DOCX 的 OMML。"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def split_formula_tag(content: str) -> tuple[str, str | None]:
|
|
21
|
+
"""剥离公式末尾括号平衡的 ``\\tag{...}``,并返回正文与编号。"""
|
|
22
|
+
stripped_end = len(content.rstrip())
|
|
23
|
+
if stripped_end == 0 or content[stripped_end - 1] != "}":
|
|
24
|
+
return content, None
|
|
25
|
+
|
|
26
|
+
search_end = stripped_end
|
|
27
|
+
while (tag_start := content.rfind(r"\tag", 0, search_end)) >= 0:
|
|
28
|
+
if not _is_escaped_command(content, tag_start):
|
|
29
|
+
opening_brace = _find_tag_opening_brace(content, tag_start, stripped_end)
|
|
30
|
+
if opening_brace is not None:
|
|
31
|
+
closing_brace = _find_balanced_closing_brace(content, opening_brace, stripped_end)
|
|
32
|
+
if closing_brace == stripped_end - 1:
|
|
33
|
+
formula = content[:tag_start].rstrip()
|
|
34
|
+
tag = content[opening_brace + 1 : closing_brace].strip()
|
|
35
|
+
return formula, tag
|
|
36
|
+
search_end = tag_start
|
|
37
|
+
|
|
38
|
+
return content, None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def latex_to_omml(latex: str, *, display: bool) -> etree._Element:
|
|
42
|
+
"""把 LaTeX 转换为带命名空间、可直接插入 DOCX 的 OMML 节点。"""
|
|
43
|
+
try:
|
|
44
|
+
normalized_latex = _normalize_supported_genfrac(latex)
|
|
45
|
+
normalized_latex = _EMPTY_SCRIPT_RE.sub("", normalized_latex)
|
|
46
|
+
mathml = latex_to_mathml(normalized_latex, display="block" if display else "inline")
|
|
47
|
+
omml_xml = _repair_mathml2omml_xml(mathml_to_omml(mathml))
|
|
48
|
+
equation = _parse_omml(omml_xml)
|
|
49
|
+
_normalize_omml(equation)
|
|
50
|
+
if not display:
|
|
51
|
+
return equation
|
|
52
|
+
|
|
53
|
+
paragraph = etree.Element(
|
|
54
|
+
etree.QName(_OFFICE_MATH_NAMESPACE, "oMathPara"),
|
|
55
|
+
nsmap={"m": _OFFICE_MATH_NAMESPACE},
|
|
56
|
+
)
|
|
57
|
+
paragraph.append(equation)
|
|
58
|
+
return paragraph
|
|
59
|
+
except Exception as exc:
|
|
60
|
+
raise DocxFormulaError(f"LaTeX 公式无法转换为 OMML: {latex!r}") from exc
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _normalize_supported_genfrac(content: str) -> str:
|
|
64
|
+
"""把规范无横线 genfrac 转为 latex2mathml 支持的双行 matrix。"""
|
|
65
|
+
chunks: list[str] = []
|
|
66
|
+
preserved_start = 0
|
|
67
|
+
search_start = 0
|
|
68
|
+
while (command_start := content.find(_GENFRAC_COMMAND, search_start)) >= 0:
|
|
69
|
+
command_end = command_start + len(_GENFRAC_COMMAND)
|
|
70
|
+
if _is_escaped_command(content, command_start):
|
|
71
|
+
search_start = command_end
|
|
72
|
+
continue
|
|
73
|
+
|
|
74
|
+
parsed = _parse_braced_arguments(content, command_end, _GENFRAC_ARGUMENT_COUNT)
|
|
75
|
+
if parsed is None:
|
|
76
|
+
search_start = command_end
|
|
77
|
+
continue
|
|
78
|
+
arguments, expression_end = parsed
|
|
79
|
+
left_delimiter, right_delimiter, thickness, math_style, numerator, denominator = arguments
|
|
80
|
+
if left_delimiter.strip() or right_delimiter.strip() or thickness.strip() != "0pt" or math_style.strip():
|
|
81
|
+
search_start = expression_end
|
|
82
|
+
continue
|
|
83
|
+
|
|
84
|
+
normalized_numerator = _normalize_supported_genfrac(numerator)
|
|
85
|
+
normalized_denominator = _normalize_supported_genfrac(denominator)
|
|
86
|
+
replacement = (
|
|
87
|
+
rf"\begin{{matrix}}{{{normalized_numerator}}}"
|
|
88
|
+
r"\\"
|
|
89
|
+
rf"{{{normalized_denominator}}}\end{{matrix}}"
|
|
90
|
+
)
|
|
91
|
+
chunks.append(content[preserved_start:command_start])
|
|
92
|
+
chunks.append(replacement)
|
|
93
|
+
preserved_start = expression_end
|
|
94
|
+
search_start = expression_end
|
|
95
|
+
|
|
96
|
+
if not chunks:
|
|
97
|
+
return content
|
|
98
|
+
chunks.append(content[preserved_start:])
|
|
99
|
+
return "".join(chunks)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _parse_braced_arguments(
|
|
103
|
+
content: str,
|
|
104
|
+
cursor: int,
|
|
105
|
+
count: int,
|
|
106
|
+
) -> tuple[tuple[str, ...], int] | None:
|
|
107
|
+
"""从指定位置读取固定数量的平衡花括号参数,并返回参数与结束位置。"""
|
|
108
|
+
arguments: list[str] = []
|
|
109
|
+
content_end = len(content)
|
|
110
|
+
for _ in range(count):
|
|
111
|
+
while cursor < content_end and content[cursor].isspace():
|
|
112
|
+
cursor += 1
|
|
113
|
+
if cursor >= content_end or content[cursor] != "{":
|
|
114
|
+
return None
|
|
115
|
+
closing_brace = _find_balanced_closing_brace(content, cursor, content_end)
|
|
116
|
+
if closing_brace is None:
|
|
117
|
+
return None
|
|
118
|
+
arguments.append(content[cursor + 1 : closing_brace])
|
|
119
|
+
cursor = closing_brace + 1
|
|
120
|
+
return tuple(arguments), cursor
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _repair_mathml2omml_xml(omml_xml: str) -> str:
|
|
124
|
+
"""修复 mathml2omml 0.0.2 对 groupChrPr 写出的错误闭合标签。"""
|
|
125
|
+
return omml_xml.replace(
|
|
126
|
+
"</m:groupChr><m:e>",
|
|
127
|
+
"</m:groupChrPr><m:e>",
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _normalize_omml(equation: etree._Element) -> None:
|
|
132
|
+
"""补齐 Word 要求的根号属性,并隐藏无显式底数的脚本占位框。"""
|
|
133
|
+
namespace = f"{{{_OFFICE_MATH_NAMESPACE}}}"
|
|
134
|
+
for radical in equation.findall(f".//{namespace}rad"):
|
|
135
|
+
if radical.find(f"{namespace}deg") is not None:
|
|
136
|
+
continue
|
|
137
|
+
properties = etree.Element(etree.QName(_OFFICE_MATH_NAMESPACE, "radPr"))
|
|
138
|
+
degree_hidden = etree.SubElement(
|
|
139
|
+
properties,
|
|
140
|
+
etree.QName(_OFFICE_MATH_NAMESPACE, "degHide"),
|
|
141
|
+
)
|
|
142
|
+
degree_hidden.set(etree.QName(_OFFICE_MATH_NAMESPACE, "val"), "1")
|
|
143
|
+
degree = etree.Element(etree.QName(_OFFICE_MATH_NAMESPACE, "deg"))
|
|
144
|
+
radical.insert(0, properties)
|
|
145
|
+
radical.insert(1, degree)
|
|
146
|
+
|
|
147
|
+
for script_name in ("sSup", "sSub", "sSubSup"):
|
|
148
|
+
for script in equation.findall(f".//{namespace}{script_name}"):
|
|
149
|
+
base = script.find(f"{namespace}e")
|
|
150
|
+
if base is None or "".join(base.itertext()).strip():
|
|
151
|
+
continue
|
|
152
|
+
text = base.find(f".//{namespace}t")
|
|
153
|
+
if text is None:
|
|
154
|
+
run = etree.SubElement(base, etree.QName(_OFFICE_MATH_NAMESPACE, "r"))
|
|
155
|
+
text = etree.SubElement(run, etree.QName(_OFFICE_MATH_NAMESPACE, "t"))
|
|
156
|
+
text.text = "\u200b"
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _is_escaped_command(content: str, command_start: int) -> bool:
|
|
160
|
+
"""判断命令起始反斜杠是否被前一个反斜杠转义。"""
|
|
161
|
+
preceding_backslashes = 0
|
|
162
|
+
cursor = command_start - 1
|
|
163
|
+
while cursor >= 0 and content[cursor] == "\\":
|
|
164
|
+
preceding_backslashes += 1
|
|
165
|
+
cursor -= 1
|
|
166
|
+
return preceding_backslashes % 2 == 1
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _find_tag_opening_brace(content: str, tag_start: int, content_end: int) -> int | None:
|
|
170
|
+
"""查找 tag 命令允许空白后的左花括号。"""
|
|
171
|
+
cursor = tag_start + len(r"\tag")
|
|
172
|
+
while cursor < content_end and content[cursor].isspace():
|
|
173
|
+
cursor += 1
|
|
174
|
+
if cursor >= content_end or content[cursor] != "{":
|
|
175
|
+
return None
|
|
176
|
+
return cursor
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _find_balanced_closing_brace(content: str, opening_brace: int, content_end: int) -> int | None:
|
|
180
|
+
"""查找与 tag 左花括号配对的右花括号,并忽略转义花括号。"""
|
|
181
|
+
depth = 0
|
|
182
|
+
for cursor in range(opening_brace, content_end):
|
|
183
|
+
character = content[cursor]
|
|
184
|
+
if character not in "{}" or _is_escaped_character(content, cursor):
|
|
185
|
+
continue
|
|
186
|
+
depth += 1 if character == "{" else -1
|
|
187
|
+
if depth == 0:
|
|
188
|
+
return cursor
|
|
189
|
+
if depth < 0:
|
|
190
|
+
return None
|
|
191
|
+
return None
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _is_escaped_character(content: str, position: int) -> bool:
|
|
195
|
+
"""判断指定字符前是否存在奇数个连续反斜杠。"""
|
|
196
|
+
preceding_backslashes = 0
|
|
197
|
+
cursor = position - 1
|
|
198
|
+
while cursor >= 0 and content[cursor] == "\\":
|
|
199
|
+
preceding_backslashes += 1
|
|
200
|
+
cursor -= 1
|
|
201
|
+
return preceding_backslashes % 2 == 1
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _parse_omml(omml_xml: str) -> etree._Element:
|
|
205
|
+
"""为第三方库返回的未绑定 ``m`` 前缀补充命名空间并解析节点。"""
|
|
206
|
+
wrapper_xml = f'<docx-math-root xmlns:m="{_OFFICE_MATH_NAMESPACE}">{omml_xml}</docx-math-root>'
|
|
207
|
+
parser = etree.XMLParser(resolve_entities=False, no_network=True, recover=False)
|
|
208
|
+
wrapper = etree.fromstring(wrapper_xml.encode("utf-8"), parser=parser)
|
|
209
|
+
if len(wrapper) != 1:
|
|
210
|
+
raise ValueError("OMML 转换结果必须只包含一个根节点")
|
|
211
|
+
|
|
212
|
+
equation = wrapper[0]
|
|
213
|
+
expected_tag = etree.QName(_OFFICE_MATH_NAMESPACE, "oMath")
|
|
214
|
+
if equation.tag != expected_tag:
|
|
215
|
+
raise ValueError("OMML 转换结果的根节点必须是 m:oMath")
|
|
216
|
+
wrapper.remove(equation)
|
|
217
|
+
return equation
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
__all__ = ["DocxFormulaError", "latex_to_omml", "split_formula_tag"]
|