""" Rooting Future - Content Formatter v1.0 Converts markdown content to styled HTML for webapp viewer """ import re import markdown from typing import Dict, Any import logging logger = logging.getLogger(__name__) class ContentFormatter: """Formatta contenuti piano strategico per webapp""" @staticmethod def format_section_content(content: str) -> str: """ Converte markdown in HTML formattato con: - Headers con anchor links - Bullet points stilizzati - Callout boxes per info importanti - Code blocks con syntax highlighting - Keyword badges """ if not content: return '
Sezione non disponibile
' # Strip whitespace content = content.strip() if len(content) < 20: return 'Sezione non disponibile
' try: # Convert markdown to HTML with extensions html = markdown.markdown( content, extensions=[ 'extra', # Tables, fenced code, etc. 'codehilite', # Syntax highlighting 'toc', # Table of contents 'tables', # GitHub-style tables 'nl2br', # Newline to{content}
' @staticmethod def _enhance_html(html: str) -> str: """Aggiunge classi CSS custom per styling avanzato""" # 1. Highlight priority headers (### PRIORITÀ, ### OBIETTIVI, etc.) html = re.sub( r'(NOTA|IMPORTANTE|ATTENZIONE|NOTA BENE|NB):\s*(.*?)
', r'({keyword}):', rf'
{emoji} \1:', html, flags=re.IGNORECASE ) return html @staticmethod def extract_key_metrics(content: str) -> Dict[str, Any]: """Estrae metriche chiave dal contenuto per dashboard""" if not content: return {} metrics = {} try: # Estrai percentuali (es. "97.5%", "20%") percentages = re.findall(r'(\d+(?:\.\d+)?%)', content) if percentages: metrics['percentages'] = percentages[:5] # Estrai valori monetari (es. "€50K", "€1.2M") money = re.findall(r'€\s?(\d+(?:\.\d+)?[KM]?)', content, re.IGNORECASE) if money: metrics['financial'] = money[:5] # Conta priorità/obiettivi priorities = len(re.findall(r'MACRO \d+|PRIORITÀ \d+|OBIETTIVO \d+', content, re.IGNORECASE)) if priorities: metrics['priorities_count'] = priorities # Estrai numeri importanti (es. "15 progetti", "3 anni") numbers = re.findall(r'\b(\d+)\s+(progetti|obiettivi|anni|mesi|settimane)', content, re.IGNORECASE) if numbers: metrics['key_numbers'] = [(num, unit) for num, unit in numbers[:5]] except Exception as e: logger.error(f"[FORMATTER] Error extracting metrics: {e}") return metrics # Utility function for quick formatting def format_plan_section(content: str) -> str: """Quick utility to format a single section""" formatter = ContentFormatter() return formatter.format_section_content(content)