"""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 # Tokens that should render uppercase instead of title-cased (e.g. "ml-engineer" # → "ML Engineer", not "Ml Engineer"). _TAG_ACRONYMS = {"ml", "ai", "ui", "ux", "api", "cli", "sql", "css", "html", "js", "ios"} # Human-readable names for the breakdown factors `compute_grade` emits. Used in # the subtitle's "Strong in ..." callout to surface the two highest-scoring # pillars of the grade — real info instead of repeating the letter + score. _BREAKDOWN_LABELS = { "commits": "Commits", "consistency": "Consistency", "repos": "Repos", "stars": "Stars", "forks": "Forks", "activity": "Activity", "followers": "Followers", } def _top_strengths(breakdown: dict | None, n: int = 2) -> list[str]: if not breakdown: return [] ranked = sorted(breakdown.items(), key=lambda kv: kv[1], reverse=True) return [_BREAKDOWN_LABELS.get(k, k.capitalize()) for k, _ in ranked[:n]] def _format_tag_label(tag: str) -> str: # Custom tags may define an explicit label in src/tag_rules.py::TAG_DEFS. try: from .. import tag_rules meta = tag_rules.TAG_DEFS.get(tag) if meta and meta.get("label"): return meta["label"] except Exception: pass return " ".join( w.upper() if w.lower() in _TAG_ACRONYMS else w.capitalize() for w in tag.split("-") ) 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 # S-tier grades get a fully closed ring regardless of exact score — the # letter "S" itself carries the "top of the scale" meaning, so the visual # should match. S+, S++ share the full circle. offset = 0 if base == "S" else 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''' {icon_svg(key, t["text_secondary"])} {val_str} {stat_labels.get(key, key)} ''' # 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 = getattr(tag, "label", None) or _format_tag_label(tag.tag) 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''' {escape(label)} ''' 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 # Reserve space at the bottom for the "Generated with gh-stats" # attribution. Matches the composite widget's footer convention so # the standalone grade widget feels visually consistent when it's # embedded on its own (the /api//grade.svg path). footer_h = 16 card_h = (tags_y + tags_h + tag_padding) if data.tags else (stats_y + 54) card_h += footer_h strengths = _top_strengths(data.breakdown) strengths_line = f"Strong in {' · '.join(strengths)}" if strengths else "" inner = f''' {escape(data.grade)} Developer Profile {strengths_line} Score {score:.0f} / 100 {stats_svg} ''' if data.tags: inner += f''' {tags_svg} ''' # Footer attribution. Marked with data-gh-attribution so composite.py # can strip it (and reclaim the 16px) when this widget gets inlined # — the composite has its own footer at the bottom of the whole # card, so showing both would double-tag the same image. # Positioned in the inner-svg coordinate space (card_wrapper passes # title="" here, so inner is not vertically offset). inner += f''' Generated with gh-stats''' return card_wrapper(inner, 380, card_h, t, "")