"""Render the shared credit data as source text for each stack. Every emitter returns the full file. Long data (the SVG, the polygon points) is wrapped into short lines so each stack's linter accepts the file as is. """ from __future__ import annotations import json import shlex import textwrap from dataclasses import dataclass from typing import TYPE_CHECKING if TYPE_CHECKING: from generator.svg import Logo from generator.tokens import Palette, Tokens MARK = "generated by kuhy_credit/generator/generate.py; DO NOT EDIT" @dataclass(frozen=True) class Credit: """Everything a stack needs: the words, the link, the art, the tokens.""" url: str text: str svg: str logo: Logo tokens: Tokens def _num(v: float) -> str: return f"{v:g}" if v != int(v) else f"{int(v)}.0" def _wrap(items: list[str], indent: str, width: int = 76) -> str: joined = ", ".join(items) + "," return textwrap.fill(joined, width, initial_indent=indent, subsequent_indent=indent) def _points(points: tuple[float, ...], indent: int) -> str: return _wrap([_num(p) for p in points], " " * indent) def _chunks(text: str, size: int = 60) -> list[str]: return [json.dumps(text[i : i + size]) for i in range(0, len(text), size)] def _gd_str(chunk: str) -> str: """GDScript literal the way gdformat writes it: single quotes around '"'.""" text = json.dumps(chunk) if '"' in chunk and "'" not in chunk: return "'" + text[1:-1].replace('\\"', '"') + "'" return text def _argb(hex_colour: str) -> str: return f"0xFF{hex_colour.lstrip('#').upper()}" def python(c: Credit) -> str: """kuhy_credit/_data.py.""" shapes = "".join( f' (\n "{s.fill}",\n (\n{_points(s.points, 12)}\n ),\n ),\n' for s in c.logo.shapes ) def pal(p: Palette) -> str: return ( f'{{"bg": "{p.bg}", "surface": "{p.surface}", "border": "{p.border}", ' f'"text": "{p.text}"}}' ) t = c.tokens return ( f'"""The shared credit data ({MARK})."""\n\n' f"URL = {json.dumps(c.url)}\nTEXT = {json.dumps(c.text)}\n" f"LOGO_SIZE = {c.logo.size}\nLOGO_FIELD = {json.dumps(c.logo.field)}\n" f"LOGO_SHAPES: tuple[tuple[str, tuple[float, ...]], ...] = (\n{shapes})\n" f"DARK = {pal(t.dark)}\nLIGHT = {pal(t.light)}\n" f'FOCUS = "{t.focus}"\nRADIUS = {t.radius}\nSPACE_SM = {t.space_sm}\n' f"SPACE_MD = {t.space_md}\nTEXT_BODY = {t.text_body}\n" ) def web(c: Credit) -> str: """web/src/credit-data.js.""" svg = "\n + ".join(_chunks(c.svg)) t = c.tokens def pal(p: Palette) -> str: return f'{{ bg: "{p.bg}", surface: "{p.surface}", border: "{p.border}", text: "{p.text}" }}' return ( f"// The shared credit data ({MARK}).\n" f"export const CREDIT_URL = {json.dumps(c.url)};\n" f"export const CREDIT_TEXT = {json.dumps(c.text)};\n" f"export const LOGO_SVG =\n {svg};\n" f"export const TOKENS = {{\n dark: {pal(t.dark)},\n light: {pal(t.light)},\n" f' focus: "{t.focus}",\n radius: {t.radius},\n spaceSm: {t.space_sm},\n' f" spaceMd: {t.space_md},\n textBody: {t.text_body},\n}};\n" ) def flutter(c: Credit) -> str: """flutter/lib/src/credit_data.g.dart.""" shapes = "".join( f" (\n {_argb(s.fill)},\n [\n{_points(s.points, 6)}\n ],\n ),\n" for s in c.logo.shapes ) t = c.tokens return ( f"// GENERATED CODE - DO NOT MODIFY BY HAND ({MARK}).\n" "// ignore_for_file: public_member_api_docs\n\n" f"const String kCreditUrl = {json.dumps(c.url)};\n" f"const String kCreditText = {json.dumps(c.text)};\n" f"const double kLogoSize = {c.logo.size};\n" f"const int kLogoField = {_argb(c.logo.field)};\n" f"const List<(int, List)> kLogoShapes = [\n{shapes}];\n" f"const int kDarkBg = {_argb(t.dark.bg)};\n" f"const int kLightBg = {_argb(t.light.bg)};\n" f"const int kDarkSurface = {_argb(t.dark.surface)};\n" f"const int kDarkBorder = {_argb(t.dark.border)};\n" f"const int kDarkText = {_argb(t.dark.text)};\n" f"const int kLightSurface = {_argb(t.light.surface)};\n" f"const int kLightBorder = {_argb(t.light.border)};\n" f"const int kLightText = {_argb(t.light.text)};\n" f"const int kFocus = {_argb(t.focus)};\n" f"const double kRadius = {t.radius};\n" f"const double kSpaceSm = {t.space_sm};\n" f"const double kSpaceMd = {t.space_md};\n" f"const double kTextBody = {t.text_body};\n" ) def godot(c: Credit) -> str: """godot/addons/kuhy_credit/credit_data.gd.""" svg = "\n\t+ ".join(_gd_str(c.svg[i : i + 60]) for i in range(0, len(c.svg), 60)) t = c.tokens def pal(p: Palette) -> str: return ( f'{{\n\t"bg": Color("{p.bg}"),\n\t"surface": Color("{p.surface}"),' f'\n\t"border": Color("{p.border}"),\n\t"text": Color("{p.text}"),\n}}' ) return ( f"## The shared credit data ({MARK}).\n" "extends RefCounted\n\n" f"const URL := {json.dumps(c.url)}\n" f"const TEXT := {json.dumps(c.text)}\n" f"const LOGO_SIZE := {c.logo.size}\n" f"const LOGO_SVG := (\n\t{svg}\n)\n" f"const DARK := {pal(t.dark)}\n" f"const LIGHT := {pal(t.light)}\n" f'const FOCUS := Color("{t.focus}")\n' f"const RADIUS := {t.radius}\nconst SPACE_SM := {t.space_sm}\n" f"const SPACE_MD := {t.space_md}\nconst TEXT_BODY := {t.text_body}\n" ) def terminal(c: Credit) -> str: """terminal/kuhy-credit.sh.""" return ( "#!/bin/bash\n" f"# Prints the one-line kuhy credit and opens nothing ({MARK}).\n" "set -euo pipefail\n" f"printf '%s %s\\n' {shlex.quote(c.text)} {shlex.quote(c.url)}\n" )