feat(generator): scaffold + move widgets/themes/models/utils/processor/config

- Add generator/requirements.txt and generator/pytest.ini
- git mv src/widgets, themes, models, utils, data/processor.py, config.py into generator/src/
- Fix relative imports in processor.py (moved up one level: .. → .)
- Append service-specific env vars (PORT, DB paths, FETCHER_URL, etc.) to generator/src/config.py
- git rm retired src/db, src/data/__init__.py, src/__init__.py

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
shay
2026-04-17 16:39:29 -04:00
co-authored by Claude Sonnet 4.6
parent cb3ace8dcd
commit 3525144aeb
23 changed files with 12 additions and 301 deletions
+19
View File
@@ -0,0 +1,19 @@
"""Widget rendering functions."""
from .grade import render_grade_widget
from .impact import render_impact_widget
from .collaborators import render_collaborators_widget
from .focus import render_focus_widget
from .languages import render_languages_widget
from .achievements import render_achievements_widget
from .composite import compose_widget
__all__ = [
"render_grade_widget",
"render_impact_widget",
"render_collaborators_widget",
"render_focus_widget",
"render_languages_widget",
"render_achievements_widget",
"compose_widget",
]
+72
View File
@@ -0,0 +1,72 @@
"""Achievements widget rendering."""
from ..models import AchievementData
from ..themes import THEMES
from ..utils import escape, card_wrapper
def _achievement_icon_svg(icon_type: str, color: str) -> str:
"""Returns SVG for achievement icons (24x24)."""
icons = {
"trophy": f'''<svg viewBox="0 0 24 24" width="24" height="24">
<path d="M7 2h10v2h2.5c.8 0 1.5.7 1.5 1.5V8c0 1.7-1.3 3-3 3h-.5c-.5 1.5-1.8 2.7-3.5 3v2.5h3c.6 0 1 .4 1 1s-.4 1-1 1H7c-.6 0-1-.4-1-1s.4-1 1-1h3V14c-1.7-.3-3-1.5-3.5-3H6c-1.7 0-3-1.3-3-3V5.5C3 4.7 3.7 4 4.5 4H7V2zm0 4H4.5v2c0 .8.7 1.5 1.5 1.5h1V6zm12.5 0H17v3.5h1c.8 0 1.5-.7 1.5-1.5V6z" fill="{color}" opacity="0.85"/>
</svg>''',
"medal": f'''<svg viewBox="0 0 24 24" width="24" height="24">
<circle cx="12" cy="14" r="5" fill="{color}" opacity="0.2"/>
<circle cx="12" cy="14" r="4" fill="none" stroke="{color}" stroke-width="1.5"/>
<path d="M12 11.5l1 2 2.2.3-1.6 1.5.4 2.2-2-1-2 1 .4-2.2-1.6-1.5 2.2-.3z" fill="{color}"/>
<path d="M9 3l3 8m0-8l3 8m-6-8h6" stroke="{color}" stroke-width="1.5" fill="none" stroke-linecap="round"/>
</svg>''',
"star": f'''<svg viewBox="0 0 24 24" width="24" height="24">
<path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z" fill="{color}" opacity="0.85"/>
</svg>''',
"hackathon": f'''<svg viewBox="0 0 24 24" width="24" height="24">
<rect x="4" y="5" width="16" height="11" rx="1" fill="none" stroke="{color}" stroke-width="1.5"/>
<rect x="3" y="16" width="18" height="1.5" rx="0.5" fill="{color}" opacity="0.85"/>
<path d="M9 9l-2 3 2 3m6-6l2 3-2 3" stroke="{color}" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
</svg>''',
}
return icons.get(icon_type, icons["trophy"])
def render_achievements_widget(
achievements: list[AchievementData],
theme_name: str = "dark",
max_items: int = 5,
settings: dict | None = None,
) -> str:
"""Renders the achievements widget with icons and descriptions.
Settings:
max_items (int): Max achievements to show (default 5)
"""
t = THEMES[theme_name]
s = settings or {}
max_items = min(max(int(s.get("max_items", max_items)), 1), 10)
items = ""
accent_colors = [t["orange"], t["green"], t["accent"], t["purple"], t["pink"]]
shown = achievements[:max_items]
if not shown:
return ""
for i, ach in enumerate(shown):
y = i * 56
color = accent_colors[i % len(accent_colors)]
icon_svg = _achievement_icon_svg(ach.icon, color)
items += f'''
<g transform="translate(16, {y + 8})">
<rect width="348" height="48" rx="8" fill="{color}" opacity="0.06"/>
<rect width="348" height="48" rx="8" fill="none" stroke="{color}" stroke-width="0.5" opacity="0.3"/>
<g transform="translate(12, 12)">
{icon_svg}
</g>
<text x="48" y="20" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="13" font-weight="600" fill="{t["text"]}">{escape(ach.title)}</text>
<text x="48" y="36" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="10" fill="{t["text_secondary"]}">{escape(ach.subtitle)}{(" · " + ach.event_date) if ach.event_date else ""}</text>
</g>'''
total_h = len(shown) * 56 + 50
return card_wrapper(items, 380, total_h, t, "Achievements")
+52
View File
@@ -0,0 +1,52 @@
"""Collaborators widget rendering."""
from ..models import CollaboratorData
from ..themes import THEMES
from ..utils import escape, card_wrapper
def render_collaborators_widget(collabs: list[CollaboratorData], theme_name: str = "dark", settings: dict | None = None) -> str:
"""Renders the top collaborators widget with avatars and contribution bars.
Settings:
max_count (int): Number of collaborators to show (default 5)
bar_color (str): Hex color for the contribution bars (default theme purple)
"""
t = THEMES[theme_name]
s = settings or {}
max_count = min(max(int(s.get("max_count", 5)), 1), 10)
bar_color = s.get("bar_color") or t["purple"]
items = ""
for i, c in enumerate(collabs[:max_count]):
y = i * 50
avatar_el = ""
if c.avatar_b64:
avatar_el = f'''<image x="-18" y="-18" width="36" height="36" href="data:image/png;base64,{c.avatar_b64}" clip-path="url(#avatarClip)"/>'''
else:
hue = hash(c.username) % 360
avatar_el = f'''
<circle cx="0" cy="0" r="18" fill="hsl({hue}, 50%, 40%)"/>
<text x="0" y="1" text-anchor="middle" dominant-baseline="middle"
font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="14" font-weight="600" fill="white">{escape(c.username[0].upper())}</text>'''
bar_max = max((x.shared_commits for x in collabs[:max_count]), default=1) or 1
bar_w = c.shared_commits / bar_max * 120
items += f'''
<g transform="translate(36, {y + 20})">
{avatar_el}
<text x="28" y="-2" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="13" font-weight="600" fill="{t["text"]}">{escape(c.username)}</text>
<text x="28" y="14" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="10" fill="{t["text_secondary"]}">{c.shared_repos} repos · {c.shared_commits} commits</text>
<rect x="200" y="-6" width="130" height="8" rx="4" fill="{t["grid"]}"/>
<rect x="200" y="-6" width="{bar_w}" height="8" rx="4" fill="{bar_color}" opacity="0.8">
<animate attributeName="width" from="0" to="{bar_w}" dur="0.6s" fill="freeze"/>
</rect>
</g>'''
total_h = len(collabs[:max_count]) * 50 + 48
return card_wrapper(items, 380, total_h, t, "Top Collaborators")
+100
View File
@@ -0,0 +1,100 @@
"""Composite widget builder for combining multiple widgets."""
import re
from ..themes import THEMES
from ..utils import escape
def _extract_inner(svg: str, widget_key: str) -> tuple[str, int]:
"""Extract the inner content from a widget SVG and rewrite IDs to be unique.
Returns (inner_svg_content, height).
"""
# Parse height
hm = re.search(r'height="(\d+)"', svg)
h = int(hm.group(1)) if hm else 160
# Rewrite IDs to avoid conflicts: shadow, avatarClip, areaGrad, etc.
# Prefix all id="..." and url(#...) and href="#..." references
prefix = f"{widget_key}_"
# Find all IDs in the SVG
ids = re.findall(r'id="([^"]+)"', svg)
inner = svg
for old_id in ids:
new_id = f"{prefix}{old_id}"
inner = inner.replace(f'id="{old_id}"', f'id="{new_id}"')
inner = inner.replace(f'url(#{old_id})', f'url(#{new_id})')
inner = inner.replace(f'href="#{old_id}"', f'href="#{new_id}"')
# Strip the outer <svg> and </svg> tags to get just the content
inner = re.sub(r'<svg[^>]*>', '', inner, count=1)
inner = re.sub(r'</svg>\s*$', '', inner)
return inner, h
def compose_widget(
widgets: dict[str, str],
enabled: list[str],
theme_name: str = "dark",
username: str = "",
avatar_b64: str = "",
) -> str:
"""
Takes rendered SVG strings for each widget type and composes them
into a single unified profile widget by inlining widget SVGs directly.
"""
t = THEMES[theme_name]
total_w = 420
header_h = 60
padding = 20
gap = 16
# Extract inner content from each widget SVG
parts = []
for key in enabled:
if key in widgets and widgets[key]:
inner, h = _extract_inner(widgets[key], key)
parts.append((key, inner, h))
content_h = sum(h for _, _, h in parts) + gap * max(len(parts) - 1, 0)
total_h = header_h + content_h + padding * 2 + 10
# Header
avatar_svg = ""
if avatar_b64:
avatar_svg = f'''
<defs><clipPath id="mainAvatarClip"><circle cx="30" cy="30" r="16"/></clipPath></defs>
<image x="14" y="14" width="32" height="32" href="data:image/png;base64,{avatar_b64}" clip-path="url(#mainAvatarClip)"/>'''
else:
avatar_svg = f'<circle cx="30" cy="30" r="16" fill="{t["accent"]}" opacity="0.3"/>'
header = f'''
{avatar_svg}
<text x="54" y="38" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="16" font-weight="700" fill="{t["text"]}">{escape(username or "Developer")}</text>
<line x1="{padding}" y1="{header_h}" x2="{total_w - padding}" y2="{header_h}"
stroke="{t["card_border"]}" stroke-width="0.5"/>'''
# Inline widgets using <svg> sub-documents positioned with x/y
y_offset = header_h + padding
embedded = ""
for key, inner, h in parts:
embedded += f'''
<svg x="{padding}" y="{y_offset}" width="380" height="{h}" viewBox="0 0 380 {h}">
{inner}
</svg>'''
y_offset += h + gap
return f'''<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="{total_w}" height="{total_h}" viewBox="0 0 {total_w} {total_h}">
<rect width="{total_w}" height="{total_h}" rx="16" fill="{t["bg"]}"/>
<rect x="0.5" y="0.5" width="{total_w-1}" height="{total_h-1}" rx="16"
fill="none" stroke="{t["card_border"]}" stroke-width="1"/>
{header}
{embedded}
<text x="{total_w // 2}" y="{total_h - 8}" text-anchor="middle"
font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="9" fill="{t["text_secondary"]}" opacity="0.5">Generated with ♥</text>
</svg>'''
+63
View File
@@ -0,0 +1,63 @@
"""Focus areas widget rendering."""
from ..models import FocusCategory
from ..themes import THEMES, FOCUS_COLORS
from ..utils import escape, card_wrapper
def render_focus_widget(categories: list[FocusCategory], theme_name: str = "dark", period: str = "1y", settings: dict | None = None) -> str:
"""Renders the focus areas widget with horizontal bars.
Settings:
max_categories (int): Max categories to show (default 6)
"""
t = THEMES[theme_name]
s = settings or {}
max_cats = min(max(int(s.get("max_categories", 6)), 1), 10)
cats = sorted(categories, key=lambda c: -c.percentage)[:max_cats]
if not cats:
return ""
# Format period for display
period_map = {
"1w": "last week",
"1m": "last month",
"3m": "last 3 months",
"6m": "last 6 months",
"1y": "last year",
"all": "all time"
}
period_display = period_map.get(period, period)
max_pct = cats[0].percentage if cats else 100
max_pct = max(max_pct, 1) # Avoid division by zero
bar_max_w = 210
items = ""
for i, cat in enumerate(cats):
color = FOCUS_COLORS[i % len(FOCUS_COLORS)]
y = i * 36
bar_w = (cat.percentage / max_pct) * bar_max_w
items += f'''
<g transform="translate(16, {y})">
<text x="0" y="14" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="12" font-weight="600" fill="{t["text"]}">{escape(cat.category)}</text>
<rect x="90" y="4" width="{bar_max_w}" height="14" rx="7" fill="{t["grid"]}"/>
<rect x="90" y="4" width="{bar_w}" height="14" rx="7" fill="{color}" opacity="0.8">
<animate attributeName="width" from="0" to="{bar_w}" dur="0.7s" fill="freeze"/>
</rect>
<text x="90" y="32" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="9" fill="{t["text_secondary"]}">{cat.commit_count} commits</text>
<text x="{90 + bar_max_w + 8}" y="15" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="12" font-weight="700" fill="{color}">{cat.percentage:.0f}%</text>
</g>'''
# Add period subtitle
subtitle_y = len(cats) * 36 + 8
items += f'''
<text x="16" y="{subtitle_y}" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="9" fill="{t["text_secondary"]}" opacity="0.7">Recent activity · {period_display}</text>'''
card_h = len(cats) * 36 + 54
return card_wrapper(items, 380, card_h, t, "Recent Focus")
+124
View File
@@ -0,0 +1,124 @@
"""Grade widget rendering."""
import math
from ..models import GradeData
from ..themes import THEMES, TAG_COLORS, GRADE_COLORS
from ..utils import escape, card_wrapper, icon_svg
def render_grade_widget(data: GradeData, theme_name: str = "dark", settings: dict | None = None) -> str:
"""Renders the developer grade widget with stats and tags.
Settings:
max_tags (int): Max tags to display (default all)
"""
t = THEMES[theme_name]
s = settings or {}
max_tags = s.get("max_tags")
if max_tags is not None:
max_tags = min(max(int(max_tags), 1), 20)
data = GradeData(
grade=data.grade, score=data.score, stats=data.stats,
tags=data.tags[:max_tags] if data.tags else [],
breakdown=data.breakdown,
)
base = data.grade[0]
color = GRADE_COLORS.get(base, t["accent"])
score = data.score
radius = 36
circumference = 2 * math.pi * radius
offset = circumference * (1 - score / 100)
grade_font = 30 if len(data.grade) <= 2 else 22
# Stats row with icons
stat_labels = {
"commits": "Commits",
"prs": "PRs",
"stars": "Stars",
"repos": "Repos",
"followers": "Followers"
}
stat_keys = [k for k in ["commits", "prs", "stars", "repos", "followers"] if k in data.stats]
stats_svg = ""
stat_w = 340 / max(len(stat_keys), 1)
for i, key in enumerate(stat_keys):
val = data.stats[key]
sx = i * stat_w + stat_w / 2
val_str = f"{val:,}" if val < 100000 else f"{val/1000:.0f}k"
stats_svg += f'''
<g transform="translate({sx}, 0)">
<g transform="translate(-7, 0)">{icon_svg(key, t["text_secondary"])}</g>
<text x="0" y="28" text-anchor="middle" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="14" font-weight="700" fill="{t["text"]}">{val_str}</text>
<text x="0" y="41" text-anchor="middle" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="9" fill="{t["text_secondary"]}">{stat_labels.get(key, key)}</text>
</g>'''
# Tag pills
tags_svg = ""
tx, ty = 0, 0
max_tw = 340
for tag in (data.tags or []):
tag_color = TAG_COLORS.get(tag.tag, t["accent"])
label = tag.tag.replace("-", " ").title()
tw = int(len(label) * 6.6 + 18)
pill_opacity = 0.9 if tag.source == "earned" else 0.55
if tx + tw > max_tw:
tx = 0
ty += 30
tags_svg += f'''
<g transform="translate({tx}, {ty})" opacity="{pill_opacity}">
<rect width="{tw}" height="24" rx="12" fill="{tag_color}" opacity="0.12"/>
<rect width="{tw}" height="24" rx="12" fill="none" stroke="{tag_color}" stroke-width="1" opacity="0.3"/>
<text x="{tw // 2}" y="16" text-anchor="middle" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="10" font-weight="500" fill="{tag_color}">{escape(label)}</text>
</g>'''
tx += tw + 6
tags_h = ty + 24 if data.tags else 0
# Layout
stats_y = 100
tags_y = stats_y + 54
tag_padding = 18 if tags_h > 24 else 14
card_h = (tags_y + tags_h + tag_padding) if data.tags else (stats_y + 54)
inner = f'''
<g transform="translate(52, 48)">
<circle cx="0" cy="0" r="{radius}" fill="none" stroke="{t["grid"]}" stroke-width="5"/>
<circle cx="0" cy="0" r="{radius}" fill="none" stroke="{color}" stroke-width="5"
stroke-dasharray="{circumference}" stroke-dashoffset="{offset}"
stroke-linecap="round" transform="rotate(-90)">
<animate attributeName="stroke-dashoffset" from="{circumference}" to="{offset}" dur="1s" fill="freeze"/>
</circle>
<text x="0" y="-4" text-anchor="middle" dominant-baseline="middle"
font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="{grade_font}" font-weight="800" fill="{color}">{escape(data.grade)}</text>
<text x="0" y="18" text-anchor="middle" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="11" fill="{t["text_secondary"]}">{score:.0f} / 100</text>
</g>
<line x1="100" y1="16" x2="100" y2="80" stroke="{t["grid"]}" stroke-width="1"/>
<g transform="translate(114, 26)">
<text x="0" y="10" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="16" font-weight="700" fill="{t["text"]}">Developer Profile</text>
<text x="0" y="28" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="11" fill="{t["text_secondary"]}">Grade <tspan fill="{color}" font-weight="700">{escape(data.grade)}</tspan> · {score:.0f}/100</text>
</g>
<line x1="20" y1="{stats_y - 4}" x2="360" y2="{stats_y - 4}" stroke="{t["grid"]}" stroke-width="0.5"/>
<g transform="translate(20, {stats_y})">
{stats_svg}
</g>'''
if data.tags:
inner += f'''
<line x1="20" y1="{tags_y - 6}" x2="360" y2="{tags_y - 6}" stroke="{t["grid"]}" stroke-width="0.5"/>
<g transform="translate(20, {tags_y})">
{tags_svg}
</g>'''
return card_wrapper(inner, 380, card_h, t, "")
+95
View File
@@ -0,0 +1,95 @@
"""Impact timeline widget rendering."""
from ..models import ImpactWeek
from ..themes import THEMES
from ..utils import card_wrapper
def render_impact_widget(weeks: list[ImpactWeek], theme_name: str = "dark", period: str = "6mo", settings: dict | None = None) -> str:
"""Renders the impact timeline chart widget.
Settings:
line_color (str): Hex color for the chart line/area (default theme accent)
"""
t = THEMES[theme_name]
s = settings or {}
line_color = s.get("line_color") or t["accent"]
w, h = 380, 200
chart_x, chart_y = 40, 8
chart_w, chart_h = 316, 82
if not weeks:
# Render an empty chart so the widget still appears
inner = f'''
<line x1="{chart_x}" y1="{chart_y + chart_h}" x2="{chart_x + chart_w}" y2="{chart_y + chart_h}" stroke="{t["grid"]}" stroke-width="0.5"/>
<text x="{chart_x + chart_w / 2}" y="{chart_y + chart_h / 2 + 4}" text-anchor="middle"
font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="11" fill="{t["text_secondary"]}">No contribution data yet</text>
<g transform="translate(20, {chart_y + chart_h + 46})">
<text x="0" y="0" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="13" font-weight="700" fill="{t["text"]}">0</text>
<text x="0" y="14" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="9" fill="{t["text_secondary"]}">commits over {period}</text>
</g>'''
return card_wrapper(inner, w, h, t, "Impact Timeline")
max_commits = max((wk.commits for wk in weeks), default=1) or 1
n = len(weeks)
# Build area path
points = []
for i, wk in enumerate(weeks):
px = chart_x + (i / max(n - 1, 1)) * chart_w
py = chart_y + chart_h - (wk.commits / max_commits) * chart_h
points.append((px, py))
if not points:
return ""
# Smooth curve using cardinal spline
path_d = f"M {points[0][0]:.1f} {points[0][1]:.1f}"
for i in range(1, len(points)):
cx = (points[i-1][0] + points[i][0]) / 2
path_d += f" C {cx:.1f} {points[i-1][1]:.1f}, {cx:.1f} {points[i][1]:.1f}, {points[i][0]:.1f} {points[i][1]:.1f}"
area_d = path_d + f" L {points[-1][0]:.1f} {chart_y + chart_h} L {points[0][0]:.1f} {chart_y + chart_h} Z"
# Y-axis labels
y_labels = ""
for i in range(3):
val = int(max_commits * (1 - i / 2))
yy = chart_y + (i / 2) * chart_h
y_labels += f'<text x="{chart_x - 6}" y="{yy + 4}" text-anchor="end" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif" font-size="9" fill="{t["text_secondary"]}">{val}</text>'
y_labels += f'<line x1="{chart_x}" y1="{yy}" x2="{chart_x + chart_w}" y2="{yy}" stroke="{t["grid"]}" stroke-width="0.5" stroke-dasharray="3,3"/>'
# X-axis labels
x_labels = ""
label_indices = [0, n // 2, n - 1] if n > 2 else list(range(n))
for idx in label_indices:
if idx < len(weeks):
px = chart_x + (idx / max(n - 1, 1)) * chart_w
x_labels += f'<text x="{px:.1f}" y="{chart_y + chart_h + 14}" text-anchor="middle" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif" font-size="9" fill="{t["text_secondary"]}">{weeks[idx].week_start[:7]}</text>'
# Summary
total = sum(wk.commits for wk in weeks)
summary_y = chart_y + chart_h + 46
inner = f'''
<defs>
<linearGradient id="areaGrad" x1="0" y1="{chart_y}" x2="0" y2="{chart_y + chart_h}" gradientUnits="userSpaceOnUse">
<stop offset="0%" stop-color="{line_color}" stop-opacity="0.35"/>
<stop offset="100%" stop-color="{line_color}" stop-opacity="0.02"/>
</linearGradient>
</defs>
{y_labels}
{x_labels}
<path d="{area_d}" fill="url(#areaGrad)"/>
<path d="{path_d}" fill="none" stroke="{line_color}" stroke-width="2" stroke-linecap="round"/>
<g transform="translate(20, {summary_y})">
<text x="0" y="0" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="13" font-weight="700" fill="{t["text"]}">{total:,}</text>
<text x="0" y="14" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="9" fill="{t["text_secondary"]}">commits over {period}</text>
</g>'''
return card_wrapper(inner, w, h, t, "Impact Timeline")
+99
View File
@@ -0,0 +1,99 @@
"""Languages widget rendering."""
import math
from ..models import LanguageData
from ..themes import THEMES, LANG_COLORS, FOCUS_COLORS
from ..utils import escape, card_wrapper
def render_languages_widget(languages: list[LanguageData], theme_name: str = "dark", settings: dict | None = None) -> str:
"""Renders the languages donut chart widget.
Settings:
max_languages (int): Max languages to show (default 5)
"""
t = THEMES[theme_name]
s = settings or {}
max_langs = min(max(int(s.get("max_languages", 5)), 1), 10)
top_langs = sorted(languages, key=lambda l: -l.percentage)[:max_langs]
if not top_langs:
return ""
# Ensure percentages always sum to exactly 100% for a complete circle
total_percentage = sum(lang.percentage for lang in top_langs)
langs = list(top_langs)
# Add "Other" category if there are remaining languages
if total_percentage < 99.9 and len(languages) > max_langs:
other_percentage = 100 - total_percentage
from ..models import LanguageData
langs.append(LanguageData(
language="Other",
percentage=round(other_percentage, 1),
loc=0
))
elif total_percentage < 99.9:
# If we have <= max_langs languages total but they don't sum to 100%,
# adjust the percentages proportionally to sum to 100%
if total_percentage > 0:
factor = 100 / total_percentage
for lang in langs:
lang.percentage = round(lang.percentage * factor, 1)
# Calculate center position to align with legend
legend_start = 20
legend_height = len(langs) * 22
cy = legend_start + legend_height / 2
cx, r = 60, 44
inner_r = 28
circumference = 2 * math.pi * r
arcs = ""
legend = ""
offset = 0
for i, lang in enumerate(langs):
# Use gray for "Other" category
if lang.language == "Other":
color = t["text_secondary"]
else:
color = LANG_COLORS.get(lang.language, FOCUS_COLORS[i % len(FOCUS_COLORS)])
dash = circumference * lang.percentage / 100
gap = circumference - dash
arcs += f'''
<circle cx="{cx}" cy="{cy}" r="{r}" fill="none" stroke="{color}" stroke-width="16"
stroke-dasharray="{dash:.1f} {gap:.1f}" stroke-dashoffset="{-offset:.1f}"
transform="rotate(-90 {cx} {cy})" opacity="0.85">
<animate attributeName="stroke-dashoffset" from="{circumference - offset:.1f}" to="{-offset:.1f}" dur="0.8s" fill="freeze"/>
</circle>'''
offset += dash
ly = i * 22
legend += f'''
<g transform="translate(140, {ly + legend_start})">
<rect width="10" height="10" rx="2" fill="{color}"/>
<text x="16" y="9" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="12" fill="{t["text"]}">{escape(lang.language)}</text>
<text x="210" y="9" text-anchor="end" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="11" font-weight="600" fill="{t["text_secondary"]}">{lang.percentage:.0f}%</text>
</g>'''
# Center circle with top language
top_lang = langs[0].language if langs else ""
center = f'''
<circle cx="{cx}" cy="{cy}" r="{inner_r}" fill="{t["card_bg"]}"/>
<text x="{cx}" y="{cy + 1}" text-anchor="middle" dominant-baseline="middle"
font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="11" font-weight="700" fill="{t["text"]}">{escape(top_lang)}</text>'''
inner = f'''
<g transform="translate(16, 0)">
{arcs}
{center}
{legend}
</g>'''
rows_h = max(len(langs) * 22 + 40, 120)
return card_wrapper(inner, 380, rows_h + 36, t, "Languages")