Files
Quentin 3271fee5cc init: WordFileParse docx→HTML 解析服务(含 Word EQ 域公式修复)
服务:Spring Boot 3.2 + docx4j 8.3.9,POST /api/v3/server_office_file/file/parse
      接受 .docx,返回 HTML(公式 → LaTeX,图片 → RustFS 外链)

公式支持三类来源:
- OMML(Word 2007+ 公式)      → Python omml_mathml2latex
- MathType OLE 对象            → Ruby mathtype2mathml → MathML → LaTeX
- Word EQ 域(老教辅主力)      → EqToLatexUtil(本次新增)

本次改动:
1. 新增 java-svc/.../core/EqToLatexUtil.java:
   移植自 jby backend-services office-core,EQ 语法 → LaTeX
   (\f→\dfrac、\r→\sqrt、\i→\int/\sum/\prod、\s→^{}/_{}、\b→\left\right、
     \a→array、\x→\boxed/\overline、\o\al 堆叠、圈码、化学符号、极限、连等式)
   品牌标记 JBYU* → WPU*,与采集端一致
2. CtoMathFinder:接上 EQ 分支(原为 not-ported 桩),域代码经
   EqToLatexUtil.convert 转 LaTeX 写入 eqLatexMap(WPEQNUM 占位符)
3. WordParseUtil.dealPlaceholder:EQ 的 LaTeX 同样加 \( \) 包裹
   (原先因 WPEQNUM 例外不加包裹,前端/MathJax 不渲染)

修前症状:教辅 Word 里分式/方程组/上下标整段丢失(如 {−1/3, 1} 变成 {-,1})
修后实测:46 份教辅 2908 处 EQ 域全部还原,公式总数 2954 处、零未包裹残留

安全:application.yml(含 RustFS 密钥)不入库,提供 application.yml.example;
      OssService 的 @Value 默认密钥已清空
2026-09-29 09:43:13 +08:00

89 lines
2.9 KiB
Python

r"""
Visitor for <maligngroup> (alignment group marker) MathML elements.
The <maligngroup/> element is an empty element used within table cells (<mtd>)
to mark alignment groups. It's used to control horizontal alignment across
table columns for complex multi-line expressions.
In LaTeX conversion, alignment groups are handled implicitly by the array/matrix
environment's column specifications (e.g., {lll} for left-aligned columns).
Therefore, maligngroup markers are treated as transparent (no output).
"""
from lxml import etree as ET
from ..core.interfaces import Visitor, Strategy
from ..utils import local_name as _local
class MaligngroupVisitor(Visitor):
r"""
Handles <maligngroup/> (alignment group marker) MathML elements.
Treats maligngroup as an invisible marker since LaTeX handles alignment
through column specifications in array/matrix environments. The element
is completely ignored in conversion.
Examples:
Alignment group in cell:
<mtd>
<maligngroup/>
<mi>x</mi>
</mtd>
→ x (maligngroup ignored)
Multiple alignment groups:
<mtd>
<maligngroup groupalign="left"/>
<mi>a</mi>
<maligngroup groupalign="right"/>
<mi>b</mi>
</mtd>
→ a b (both maligngroup markers ignored)
Group with alignment attribute:
<maligngroup groupalign="center"/>
→ (empty - no output)
Note:
LaTeX doesn't support MathML's alignment group concept. Alignment
is controlled by column specifications in the array/matrix preamble
(e.g., {ccc} for center-aligned, {lll} for left-aligned).
Therefore, maligngroup elements produce no output.
"""
def __init__(self, strategy: Strategy):
"""Initialize the Maligngroup visitor."""
super().__init__(strategy)
def visit(self, node: ET.Element) -> str:
r"""
Convert <maligngroup/> element (returns empty string).
Args:
node: The <maligngroup/> element to convert
Returns:
Empty string (maligngroup is invisible in LaTeX)
Raises:
ValueError: If node is not a <maligngroup/> element
"""
if not self.can_handle(node):
raise ValueError(f"MaligngroupVisitor cannot handle element '{node.tag}'")
# Maligngroup is an invisible alignment marker - no output
return ""
def can_handle(self, node: ET.Element) -> bool:
"""Check if this visitor can handle the given node."""
return _local(node.tag) == 'maligngroup'
@property
def element_name(self) -> str:
"""Get the element name this visitor handles."""
return 'maligngroup'
def __repr__(self) -> str:
"""String representation for debugging."""
return f"MaligngroupVisitor(strategy={self.strategy.name})"