feat(grade): add data attributes for footer attribution in widgets

This commit is contained in:
shay
2026-05-06 16:38:59 -04:00
parent 8f7b27f806
commit 9d15dd6c94
3 changed files with 45 additions and 5 deletions
+22
View File
@@ -5,9 +5,23 @@ from ..themes import THEMES
from ..utils import escape
_ATTRIBUTION_RE = re.compile(
r'<text\b[^>]*\bdata-gh-attribution="1"[^>]*>[^<]*</text>',
re.DOTALL,
)
_ATTRIBUTION_H_RE = re.compile(r'\bdata-gh-h="(\d+)"')
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.
Also strips any per-widget "Generated with gh-stats" attribution text
(marked with data-gh-attribution="1") and shrinks the reported height
by data-gh-h so the composite doesn't reserve empty space for the
removed footer. The composite has its own attribution at the bottom
of the whole card; per-widget attributions are for the standalone
/api/<u>/<widget>.svg embed paths.
Returns (inner_svg_content, height).
"""
# Parse height
@@ -31,6 +45,14 @@ def _extract_inner(svg: str, widget_key: str) -> tuple[str, int]:
inner = re.sub(r'<svg[^>]*>', '', inner, count=1)
inner = re.sub(r'</svg>\s*$', '', inner)
# Strip any embedded attribution and reclaim its reserved height.
attr_match = _ATTRIBUTION_RE.search(inner)
if attr_match:
inner = inner.replace(attr_match.group(0), '', 1)
h_match = _ATTRIBUTION_H_RE.search(attr_match.group(0))
if h_match:
h -= int(h_match.group(1))
return inner, h
+8 -4
View File
@@ -172,11 +172,15 @@ def render_grade_widget(data: GradeData, theme_name: str = "dark", settings: dic
{tags_svg}
</g>'''
# Footer attribution. Positioned in the inner-svg coordinate space
# (card_wrapper passes title="" here, so inner is not vertically
# offset). 8px above the bottom edge mirrors the composite widget.
# 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'''
<text x="190" y="{card_h - 8}" text-anchor="middle"
<text data-gh-attribution="1" data-gh-h="{footer_h}"
x="190" y="{card_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 gh-stats</text>'''
+15 -1
View File
@@ -95,5 +95,19 @@ def render_languages_widget(languages: list[LanguageData], theme_name: str = "da
{legend}
</g>'''
# Footer attribution. Marked with data-gh-attribution so composite.py
# can strip it (and reclaim the 16px) when inlining the widget into
# the larger composite — the composite has its own footer, showing
# both would be visually redundant. card_wrapper here passes
# title="Languages", which adds a 36px translate to inner. The 36 is
# already baked into the card height (rows_h + 36); the attribution
# adds another 16 on top, positioned 8px from the bottom of the card.
rows_h = max(len(langs) * 22 + 40, 120)
return card_wrapper(inner, 380, rows_h + 36, t, "Languages")
footer_h = 16
card_h = rows_h + 36 + footer_h
inner += f'''
<text data-gh-attribution="1" data-gh-h="{footer_h}"
x="190" y="{card_h - 36 - 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 gh-stats</text>'''
return card_wrapper(inner, 380, card_h, t, "Languages")