"""
Knowledge Universe — HTML Format Adapter
Rich HTML cards for UI rendering.
Each format gets its own card layout:
video → thumbnail + play button
jupyter → code preview + Colab link
dataset → stats + download button
podcast → artwork + episode count
pdf → page count + download button
github → stars + clone button
"""
from typing import Any
from src.api.models import Source, SourceFormat
from src.format_adapters.base_adapter import BaseFormatAdapter
class HTMLFormatAdapter(BaseFormatAdapter):
"""
Produces rich HTML cards per source type.
Used by the Streamlit demo and any HTML-consuming client.
"""
FORMAT_ICONS = {
"pdf": "📄",
"video": "▶️",
"jupyter": "📓",
"github": "🐙",
"dataset": "📊",
"podcast": "🎧",
"audio": "🔊",
"html": "🌐",
"epub": "📚",
"markdown": "📝",
"sandbox": "🎮",
"simulation": "🔬",
"knowledge_graph": "🕸️",
"problem_set":"📐",
"flashcards": "🃏",
"3d_model": "🧊",
"map": "🗺️",
"timeseries": "📈",
"stackoverflow": "💬",
"colab": "⚡",
}
def transform(self, source: Source) -> str:
fmt = source.formats[0].value if source.formats else "html"
icon = self.FORMAT_ICONS.get(fmt, "🔗")
stars = "⭐" * source.difficulty
access_links = "".join([
f''
f'{link.type.upper()}'
for link in source.links[:2]
])
format_badges = "".join([
f'{f.value}'
for f in source.formats[:4]
])
# Platform-specific extras
extras = self._platform_extras(source)
quality_color = (
"#22c55e" if source.quality_score >= 7.5 else
"#f59e0b" if source.quality_score >= 5.0 else
"#ef4444"
)
thumbnail_html = (
f''
if source.thumbnail_url else ""
)
html = f"""