def extract_boxed_content()

in src/latex2sympy2_extended/math_normalization.py [0:0]


def extract_boxed_content(text: str, mode: Literal["last", "all"] = "last") -> str:
    """
    Find and extract all \\boxed{...} or \\fbox{...} elements from a string, searching from right to left.
    If mode is "last", return content up to the last valid separator.
    If mode is "all", return all boxed contents joined by commas.
    """
    
    def find_content_boundaries(text: str, opening_brace_pos: int, max_pos: int) -> tuple[int, int] | None:
        # Start searching for closing brace from the opening brace position
        i = opening_brace_pos
        num_left_braces_open = 1  # We start after the opening brace
        
        while i + 1 < max_pos:  # Check if next position is within bounds and max_pos
            i += 1
            if text[i] == "{":
                num_left_braces_open += 1
            elif text[i] == "}":
                num_left_braces_open -= 1
                if num_left_braces_open == 0:
                    return opening_brace_pos, i
        return None
    
    def has_valid_separator(text: str, content_end: int, next_boxed_start: int) -> bool:
        between_text = text[content_end + 1:next_boxed_start]
        # Making regex for it not worth it so this works
        return len(between_text) < 70 and bool(VALID_SEPARATOR_PATTERN.search(between_text))
    
    results = []
    current_pos = len(text)
    last_boxed_start = None
    
    max_pos = len(text)
    while True:
        boxed_idx = text.rfind("\\boxed", 0, current_pos)
        fbox_idx = text.rfind("\\fbox", 0, current_pos)
        
        if boxed_idx < 0 and fbox_idx < 0:
            break
            
        start_idx = max(boxed_idx, fbox_idx)
        command_end = start_idx + (6 if boxed_idx > fbox_idx else 5)
        
        # Find opening brace
        next_char_pos = command_end
        while next_char_pos < max_pos and text[next_char_pos].isspace():
            next_char_pos += 1
            
        if next_char_pos >= max_pos:
            break
            
        if text[next_char_pos] == "{":
            boundaries = find_content_boundaries(text, next_char_pos, max_pos)
            if not boundaries:
                # This is our last box
                if len(results) == 0:
                    results.append(text[next_char_pos:])
                break
            content_start, content_end = boundaries
            content = text[content_start + 1:content_end].strip()
            
            if mode == "last" and last_boxed_start is not None:
                if not has_valid_separator(text, content_end, last_boxed_start):
                    break
            
            results.append(content)
            last_boxed_start = start_idx
            max_pos = start_idx
        else:
            # This is our last box
            if len(results) == 0:
                results.append(text[next_char_pos:])
            # Otherwise we just ignore it
            break
            
        
        current_pos = start_idx
    
    if not results:
        return text
        
    return ",".join(reversed(results))