"""
Batch JSON converter for omml2tex with key preservation.
Handles conversion of multiple equations in JSON format while preserving
unique identifiers. Supports both sequential and parallel processing.
"""
from typing import Dict, Any
import sys
from .compat import LegacyMathMLToTeXConverter
from .parsers.omml_parser import OmmlParser
class BatchJSONConverter:
"""
Batch converter for JSON format with key preservation.
Converts a dictionary of equations (key -> XML) to LaTeX while preserving
the original keys. Supports both OMML and MathML input formats with
automatic detection.
Example:
>>> converter = BatchJSONConverter(subject_mode="chemistry")
>>> equations = {"eq1": "", "eq2": "..."}
>>> results = converter.convert_batch(equations)
>>> print(results["eq1"]) # LaTeX output
"""
def __init__(
self,
subject_mode: str = "math",
wrap_identifiers: bool = True,
prefer_xlongequal: bool = True,
universal_delimiter_scaling: bool = True,
verbose: bool = False
):
"""
Initialize batch converter.
Args:
subject_mode: Subject mode for conversion (math, chemistry, physics, statistics)
wrap_identifiers: Whether to wrap identifiers in \\mathrm{}
prefer_xlongequal: Whether to prefer \\xlongequal for overset equations
universal_delimiter_scaling: Whether to add \\left\\right to all delimiters
verbose: Enable verbose output
"""
self.subject_mode = subject_mode
self.wrap_identifiers = wrap_identifiers
self.prefer_xlongequal = prefer_xlongequal
self.universal_delimiter_scaling = universal_delimiter_scaling
self.verbose = verbose
# Initialize converter
self.converter = LegacyMathMLToTeXConverter(
wrap_identifiers_in_mathrm=wrap_identifiers,
prefer_xlongequal=prefer_xlongequal,
subject_mode=subject_mode,
universal_delimiter_scaling=universal_delimiter_scaling,
)
# OMML parser for OMML input
self.omml_parser = OmmlParser()
def detect_input_type(self, content: str) -> str:
"""
Detect if input is OMML or MathML.
Args:
content: XML content string
Returns:
"omml" or "mathml"
"""
content_stripped = content.strip()
# Check for OMML namespace
if 'm:oMath' in content_stripped or 'm:oMathPara' in content_stripped:
return "omml"
# Check for MathML namespace
if '