"""
Table Integration Module
Table + frontmatter styling integration and slide data assembly.
Handles extracting tables from content and applying frontmatter styling configurations.
This module bridges table parsing with slide presentation requirements.
"""
from typing import Optional
from .table_parser import is_table_content # Removed complex markdown parsing
def extract_table_from_content(content: str, slide_data: dict) -> Optional[dict]:
"""
Extract table data from content and combine with frontmatter styling properties.
SIMPLIFIED: Now uses plain text processing only for 50%+ performance improvement.
Args:
content: The content string which may contain markdown tables
slide_data: The slide data containing frontmatter properties
Returns:
Dictionary with table data and styling, or None if no table found
"""
if not is_table_content(content):
return None
# SIMPLIFIED: Parse table as plain text only using TableHandler
from ..core.table_handler import TableHandler
table_handler = TableHandler()
plain_text_rows = table_handler.parse_table_structure(content)
# Convert to expected format (plain text cells)
table_data = {"rows": plain_text_rows, "data": plain_text_rows}
if not table_data.get("rows"):
return None
# Extract styling properties from slide data
table_config = {
# Core table data
"data": table_data["rows"],
"headers": table_data.get("headers", []),
"row_count": table_data.get("row_count", 0),
"column_count": table_data.get("column_count", 0),
# Styling from frontmatter
"style": slide_data.get("style", "default_style"),
"header_style": slide_data.get("style", "dark_blue_white_text"),
"row_style": slide_data.get("row_style", "alternating_light_gray"),
"border_style": slide_data.get("border_style", "thin_gray"),
# Dimensions and sizing
"row_height": slide_data.get("row_height", 0.6),
"table_width": slide_data.get("table_width"),
"column_widths": slide_data.get("column_widths", []),
# Font sizing
"header_font_size": slide_data.get("header_font_size"),
"data_font_size": slide_data.get("data_font_size"),
# Custom colors
"custom_colors": slide_data.get("custom_colors", {}),
}
return table_config
def apply_table_styling_to_slide(slide_data: dict, table_data: dict) -> dict:
"""
Apply table styling properties to slide data.
Args:
slide_data: Original slide data
table_data: Table data with styling information
Returns:
Updated slide data with table styling applied
"""
# Create a copy to avoid modifying the original
updated_slide_data = slide_data.copy()
# Apply table styling properties to slide level
style_fields = ["style", "header_style", "row_style", "border_style", "row_height", "table_width", "column_widths", "header_font_size", "data_font_size", "custom_colors"]
for field in style_fields:
if field in table_data:
updated_slide_data[field] = table_data[field]
return updated_slide_data
def validate_table_styling(table_data: dict) -> dict:
"""
Validate and normalize table styling configuration.
Args:
table_data: Table data with styling
Returns:
Validated and normalized table data
"""
validated_data = table_data.copy()
# Validate row height
if "row_height" in validated_data:
try:
row_height = float(validated_data["row_height"])
if row_height <= 0:
validated_data["row_height"] = 0.6 # Default
except (ValueError, TypeError):
validated_data["row_height"] = 0.6 # Default
# Validate column widths
if "column_widths" in validated_data:
widths = validated_data["column_widths"]
if not isinstance(widths, list):
validated_data["column_widths"] = []
else:
# Ensure all widths are numeric
validated_widths = []
for width in widths:
try:
validated_widths.append(float(width))
except (ValueError, TypeError):
validated_widths.append(5.0) # Default width
validated_data["column_widths"] = validated_widths
# Validate table width
if "table_width" in validated_data and validated_data["table_width"] is not None:
try:
table_width = float(validated_data["table_width"])
if table_width <= 0:
validated_data["table_width"] = None # Use default
except (ValueError, TypeError):
validated_data["table_width"] = None # Use default
# Validate font sizes
for font_field in ["header_font_size", "data_font_size"]:
if font_field in validated_data and validated_data[font_field] is not None:
try:
font_size = int(validated_data[font_field])
if font_size < 8 or font_size > 72:
validated_data[font_field] = None # Use default
except (ValueError, TypeError):
validated_data[font_field] = None # Use default
return validated_data
def apply_frontmatter_styling_to_table(table_data: dict, frontmatter_properties: dict) -> dict:
"""
Apply frontmatter styling properties to table data.
Args:
table_data: Table data dictionary
frontmatter_properties: Frontmatter properties from slide data
Returns:
Table data with styling applied
"""
# This function applies styling from frontmatter to table data
# It's essentially the same as what extract_table_from_content does for styling
styled_table = table_data.copy()
# Apply styling properties
if "style" in frontmatter_properties:
styled_table["header_style"] = frontmatter_properties["style"]
if "row_style" in frontmatter_properties:
styled_table["row_style"] = frontmatter_properties["row_style"]
if "border_style" in frontmatter_properties:
styled_table["border_style"] = frontmatter_properties["border_style"]
# Apply dimension properties
if "row_height" in frontmatter_properties:
styled_table["row_height"] = frontmatter_properties["row_height"]
if "table_width" in frontmatter_properties:
styled_table["table_width"] = frontmatter_properties["table_width"]
if "column_widths" in frontmatter_properties:
styled_table["column_widths"] = frontmatter_properties["column_widths"]
# Apply font sizing
if "header_font_size" in frontmatter_properties:
styled_table["header_font_size"] = frontmatter_properties["header_font_size"]
if "data_font_size" in frontmatter_properties:
styled_table["data_font_size"] = frontmatter_properties["data_font_size"]
# Apply custom colors
if "custom_colors" in frontmatter_properties:
styled_table["custom_colors"] = frontmatter_properties["custom_colors"]
return styled_table
def process_markdown_content(content: str):
"""Process markdown content to convert dash bullets to bullet symbols and detect tables"""
if not isinstance(content, str):
return content
# First check if this content contains a table
if is_table_content(content):
# SIMPLIFIED: Use TableHandler for plain text processing only
from ..core.table_handler import TableHandler
table_handler = TableHandler()
plain_text_rows = table_handler.parse_table_structure(content)
# Convert to legacy format expected by converter - plain text only
table_data = {
"type": "table",
"data": plain_text_rows, # Plain text cells only
"header_style": "dark_blue_white_text",
"row_style": "alternating_light_gray",
"border_style": "thin_gray",
"custom_colors": {},
}
return table_data
# Convert dash bullets to bullet symbols
# Match lines that start with "- " (dash followed by space)
import re
processed = re.sub(r"^- ", "• ", content, flags=re.MULTILINE)
return processed