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,215 @@
|
|
|
1
|
+
"""解析 Word STSH/STD 样式表并解析继承链。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from loguru import logger
|
|
9
|
+
|
|
10
|
+
from ..legacy.binary import bounded_slice, get_u16
|
|
11
|
+
from .models import DocCharStyle
|
|
12
|
+
from .records import DocBudget
|
|
13
|
+
from .sprm import PapDelta, apply_character_sprms, apply_paragraph_sprms
|
|
14
|
+
|
|
15
|
+
ISTD_NIL = 0x0FFF
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class ResolvedStyle:
|
|
20
|
+
"""一个样式继承完成后的可见属性。"""
|
|
21
|
+
|
|
22
|
+
name: str = ""
|
|
23
|
+
character: DocCharStyle = DocCharStyle()
|
|
24
|
+
paragraph: PapDelta = PapDelta()
|
|
25
|
+
heading_level: int | None = None
|
|
26
|
+
is_title: bool = False
|
|
27
|
+
toc_level: int | None = None
|
|
28
|
+
is_code: bool = False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class _RawStyle:
|
|
33
|
+
"""STSH 内尚未解析继承的 STD。"""
|
|
34
|
+
|
|
35
|
+
sti: int
|
|
36
|
+
name: str
|
|
37
|
+
base: int
|
|
38
|
+
paragraph_grpprl: bytes
|
|
39
|
+
character_grpprl: bytes
|
|
40
|
+
paragraph_style: bool
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Stylesheet:
|
|
44
|
+
"""按 istd 提供解析后样式的只读集合。"""
|
|
45
|
+
|
|
46
|
+
def __init__(self, styles: dict[int, ResolvedStyle] | None = None) -> None:
|
|
47
|
+
"""保存已解析样式并准备默认样式。"""
|
|
48
|
+
|
|
49
|
+
self._styles = styles or {}
|
|
50
|
+
self._default = ResolvedStyle()
|
|
51
|
+
|
|
52
|
+
def get(self, style_id: int) -> ResolvedStyle:
|
|
53
|
+
"""返回指定样式,不存在时使用空默认值。"""
|
|
54
|
+
|
|
55
|
+
return self._styles.get(style_id, self._default)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _style_semantics(sti: int, name: str) -> tuple[int | None, bool, int | None, bool]:
|
|
59
|
+
"""从内建 ID 与样式名识别标题、目录和代码语义。"""
|
|
60
|
+
|
|
61
|
+
normalized = re.sub(r"\s+", " ", name.strip()).casefold()
|
|
62
|
+
heading_level = sti if 1 <= sti <= 9 else None
|
|
63
|
+
if heading_level is None:
|
|
64
|
+
match = re.match(r"^(?:heading|标题)\s*([1-9])$", normalized)
|
|
65
|
+
if match:
|
|
66
|
+
heading_level = int(match.group(1))
|
|
67
|
+
is_title = normalized in {"title", "标题", "文档标题"} or sti == 15
|
|
68
|
+
toc_level: int | None = None
|
|
69
|
+
match = re.match(r"^(?:toc|目录)\s*([1-9])$", normalized)
|
|
70
|
+
if match:
|
|
71
|
+
toc_level = int(match.group(1)) - 1
|
|
72
|
+
is_code = any(token in normalized for token in ("code", "preformatted", "source code", "代码"))
|
|
73
|
+
return heading_level, is_title, toc_level, is_code
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _parse_std(record: bytes, base_size: int) -> _RawStyle | None:
|
|
77
|
+
"""解析一条 STD 的名称、继承和 UPX 内容。"""
|
|
78
|
+
|
|
79
|
+
if len(record) < max(base_size, 10):
|
|
80
|
+
return None
|
|
81
|
+
first = get_u16(record, 0)
|
|
82
|
+
second = get_u16(record, 2)
|
|
83
|
+
third = get_u16(record, 4)
|
|
84
|
+
if first is None or second is None or third is None:
|
|
85
|
+
return None
|
|
86
|
+
sti = first & 0x0FFF
|
|
87
|
+
kind = second & 0x000F
|
|
88
|
+
base = (second >> 4) & 0x0FFF
|
|
89
|
+
upx_count = third & 0x000F
|
|
90
|
+
name_offset = max(base_size, 10)
|
|
91
|
+
name_length = get_u16(record, name_offset)
|
|
92
|
+
if name_length is None:
|
|
93
|
+
return None
|
|
94
|
+
name_bytes = name_length * 2
|
|
95
|
+
raw_name = bounded_slice(record, name_offset + 2, name_bytes)
|
|
96
|
+
if raw_name is None:
|
|
97
|
+
return None
|
|
98
|
+
name = raw_name.decode("utf-16le", errors="replace")
|
|
99
|
+
cursor = name_offset + 2 + name_bytes + 2
|
|
100
|
+
upx: list[bytes] = []
|
|
101
|
+
for _ in range(upx_count):
|
|
102
|
+
if cursor % 2:
|
|
103
|
+
cursor += 1
|
|
104
|
+
length = get_u16(record, cursor)
|
|
105
|
+
if length is None:
|
|
106
|
+
break
|
|
107
|
+
payload = bounded_slice(record, cursor + 2, length)
|
|
108
|
+
if payload is None:
|
|
109
|
+
break
|
|
110
|
+
upx.append(payload)
|
|
111
|
+
cursor += 2 + length
|
|
112
|
+
paragraph_style = kind == 1
|
|
113
|
+
if paragraph_style:
|
|
114
|
+
paragraph_grpprl = upx[0][2:] if upx and len(upx[0]) >= 2 else b""
|
|
115
|
+
character_grpprl = upx[1] if len(upx) > 1 else b""
|
|
116
|
+
elif kind == 2:
|
|
117
|
+
paragraph_grpprl = b""
|
|
118
|
+
character_grpprl = upx[0] if upx else b""
|
|
119
|
+
else:
|
|
120
|
+
paragraph_grpprl = b""
|
|
121
|
+
character_grpprl = b""
|
|
122
|
+
return _RawStyle(
|
|
123
|
+
sti=sti,
|
|
124
|
+
name=name,
|
|
125
|
+
base=base,
|
|
126
|
+
paragraph_grpprl=paragraph_grpprl,
|
|
127
|
+
character_grpprl=character_grpprl,
|
|
128
|
+
paragraph_style=paragraph_style,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def parse_stylesheet(
|
|
133
|
+
table_stream: bytes,
|
|
134
|
+
*,
|
|
135
|
+
offset: int,
|
|
136
|
+
size: int,
|
|
137
|
+
budget: DocBudget,
|
|
138
|
+
) -> Stylesheet:
|
|
139
|
+
"""解析 STSH,并在循环样式链处安全截断继承。"""
|
|
140
|
+
|
|
141
|
+
payload = bounded_slice(table_stream, offset, size)
|
|
142
|
+
if payload is None or len(payload) < 8:
|
|
143
|
+
return Stylesheet()
|
|
144
|
+
header_size = get_u16(payload, 0)
|
|
145
|
+
style_count = get_u16(payload, 2)
|
|
146
|
+
base_size = get_u16(payload, 4)
|
|
147
|
+
if header_size is None or style_count is None or base_size is None:
|
|
148
|
+
return Stylesheet()
|
|
149
|
+
cursor = 2 + header_size
|
|
150
|
+
raw_styles: dict[int, _RawStyle] = {}
|
|
151
|
+
for style_id in range(style_count):
|
|
152
|
+
length = get_u16(payload, cursor)
|
|
153
|
+
if length is None:
|
|
154
|
+
break
|
|
155
|
+
cursor += 2
|
|
156
|
+
if length == 0:
|
|
157
|
+
continue
|
|
158
|
+
record = bounded_slice(payload, cursor, length)
|
|
159
|
+
if record is None:
|
|
160
|
+
break
|
|
161
|
+
cursor += length
|
|
162
|
+
budget.charge()
|
|
163
|
+
parsed = _parse_std(record, base_size)
|
|
164
|
+
if parsed is not None:
|
|
165
|
+
raw_styles[style_id] = parsed
|
|
166
|
+
|
|
167
|
+
memo: dict[int, ResolvedStyle] = {}
|
|
168
|
+
|
|
169
|
+
def resolve(style_id: int) -> ResolvedStyle:
|
|
170
|
+
"""迭代解析一个样式的 based-on 链。"""
|
|
171
|
+
|
|
172
|
+
if style_id in memo:
|
|
173
|
+
return memo[style_id]
|
|
174
|
+
chain: list[int] = []
|
|
175
|
+
visiting: set[int] = set()
|
|
176
|
+
cursor_id: int | None = style_id
|
|
177
|
+
base = ResolvedStyle()
|
|
178
|
+
while cursor_id is not None:
|
|
179
|
+
if cursor_id in memo:
|
|
180
|
+
base = memo[cursor_id]
|
|
181
|
+
break
|
|
182
|
+
if cursor_id in visiting:
|
|
183
|
+
logger.warning(f"DOC style inheritance cycle at istd={cursor_id}")
|
|
184
|
+
break
|
|
185
|
+
raw = raw_styles.get(cursor_id)
|
|
186
|
+
if raw is None:
|
|
187
|
+
break
|
|
188
|
+
visiting.add(cursor_id)
|
|
189
|
+
chain.append(cursor_id)
|
|
190
|
+
cursor_id = raw.base if raw.base not in {ISTD_NIL, cursor_id} else None
|
|
191
|
+
for current_id in reversed(chain):
|
|
192
|
+
raw = raw_styles[current_id]
|
|
193
|
+
paragraph = apply_paragraph_sprms(raw.paragraph_grpprl, b"", base.paragraph, budget=budget)
|
|
194
|
+
character = apply_character_sprms(
|
|
195
|
+
raw.character_grpprl,
|
|
196
|
+
base.character,
|
|
197
|
+
base.character,
|
|
198
|
+
budget=budget,
|
|
199
|
+
)
|
|
200
|
+
heading, is_title, toc_level, is_code = _style_semantics(raw.sti, raw.name)
|
|
201
|
+
base = ResolvedStyle(
|
|
202
|
+
name=raw.name or base.name,
|
|
203
|
+
character=character,
|
|
204
|
+
paragraph=paragraph,
|
|
205
|
+
heading_level=heading if heading is not None else base.heading_level,
|
|
206
|
+
is_title=is_title or base.is_title,
|
|
207
|
+
toc_level=toc_level if toc_level is not None else base.toc_level,
|
|
208
|
+
is_code=is_code or base.is_code,
|
|
209
|
+
)
|
|
210
|
+
memo[current_id] = base
|
|
211
|
+
return base
|
|
212
|
+
|
|
213
|
+
for style_id in raw_styles:
|
|
214
|
+
resolve(style_id)
|
|
215
|
+
return Stylesheet(memo)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""DOCX 转换共享的 XML 常量和字段栈契约。"""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Final, Optional, TypeAlias, Union
|
|
6
|
+
from pydantic import AnyUrl
|
|
7
|
+
from .formatting_types import Formatting
|
|
8
|
+
from docvortex.foundation.type_identity import preserve_type_module
|
|
9
|
+
|
|
10
|
+
_ParagraphHyperlink: TypeAlias = Optional[Union[AnyUrl, Path, str]]
|
|
11
|
+
_ParagraphElement: TypeAlias = tuple[str, Optional[Formatting], _ParagraphHyperlink]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(slots=True)
|
|
15
|
+
class _DocxComplexFieldFrame:
|
|
16
|
+
"""保存一个 DOCX 复杂字段的指令、阶段与已解析结果元素。"""
|
|
17
|
+
|
|
18
|
+
instruction_parts: list[str] = field(default_factory=list)
|
|
19
|
+
phase: str = "instr"
|
|
20
|
+
result_elements: list[_ParagraphElement] = field(default_factory=list)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class _DocxConstants:
|
|
24
|
+
"""保存跨职责使用的固定命名空间与公式载体优先级。"""
|
|
25
|
+
|
|
26
|
+
_BLIP_NAMESPACES: Final = {
|
|
27
|
+
"a": "http://schemas.openxmlformats.org/drawingml/2006/main",
|
|
28
|
+
"r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
|
29
|
+
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
|
30
|
+
"wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
|
31
|
+
"mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
|
32
|
+
"o": "urn:schemas-microsoft-com:office:office",
|
|
33
|
+
"v": "urn:schemas-microsoft-com:vml",
|
|
34
|
+
"wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
|
|
35
|
+
"w10": "urn:schemas-microsoft-com:office:word",
|
|
36
|
+
"a14": "http://schemas.microsoft.com/office/drawing/2010/main",
|
|
37
|
+
"c": "http://schemas.openxmlformats.org/drawingml/2006/chart",
|
|
38
|
+
}
|
|
39
|
+
_PARAGRAPH_TRANSPARENT_INLINE_CONTAINERS: Final = {
|
|
40
|
+
"bdo",
|
|
41
|
+
"customXml",
|
|
42
|
+
"dir",
|
|
43
|
+
"fldSimple",
|
|
44
|
+
"ins",
|
|
45
|
+
"moveTo",
|
|
46
|
+
"smartTag",
|
|
47
|
+
}
|
|
48
|
+
_FORMULA_TOKEN_KINDS: Final = frozenset({"omml", "equationxml", "mtef", "image_mtef"})
|
|
49
|
+
_FORMULA_SOURCE_PRIORITY: Final = (
|
|
50
|
+
"omml",
|
|
51
|
+
"equationxml",
|
|
52
|
+
"mtef",
|
|
53
|
+
"image_mtef",
|
|
54
|
+
)
|
|
55
|
+
"""
|
|
56
|
+
Word 文档中使用的 XML 命名空间映射。
|
|
57
|
+
|
|
58
|
+
这些命名空间用于解析 DOCX 文件中的各种元素,包括:
|
|
59
|
+
- a: DrawingML 主命名空间
|
|
60
|
+
- r: Office 文档关系命名空间
|
|
61
|
+
- w: WordprocessingML 主命名空间
|
|
62
|
+
- wp: Wordprocessing Drawing 命名空间
|
|
63
|
+
- mc: 标记兼容性命名空间
|
|
64
|
+
- v: VML (Vector Markup Language) 命名空间
|
|
65
|
+
- wps: Wordprocessing Shape 命名空间
|
|
66
|
+
- w10: Office Word 命名空间
|
|
67
|
+
- a14: Office 2010 Drawing 命名空间
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
preserve_type_module(_DocxComplexFieldFrame, "docvortex.analyzers.native.office.docx.docx_converter")
|