gitannals 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,518 @@
1
+ """SVG 图表生成。
2
+
3
+ 全部在 Python 侧算好坐标再输出 SVG,理由有三:
4
+ 1. 零第三方图表库,单文件才能压到几百 KB;
5
+ 2. 断网、内网、老浏览器都能看,不依赖 CDN;
6
+ 3. 截图和导出 PNG 时是矢量重绘,不会糊。
7
+
8
+ 所有来自仓库的字符串(作者名、路径、commit message)一律 html.escape ——
9
+ commit message 是不可信输入,`<script>` 直接进 SVG 会破图甚至执行。
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import html
15
+ import math
16
+
17
+ from .. import TOOL_NAME
18
+ from ..i18n import t, weekdays
19
+
20
+ __all__ = [
21
+ "svg_timeline",
22
+ "svg_commit_bars",
23
+ "svg_heatmap",
24
+ "svg_share_card",
25
+ "svg_share_card_author",
26
+ ]
27
+
28
+ _ADD = "#1D9E75"
29
+ _DEL = "#D85A30"
30
+ _INK = "#16151A"
31
+ _MUTED = "#6E6A63"
32
+ _LINE = "#E4E0D8"
33
+ _FONT = "ui-sans-serif,system-ui,'PingFang SC','Microsoft YaHei',sans-serif"
34
+
35
+
36
+ def _e(value: object) -> str:
37
+ return html.escape(str(value), quote=True)
38
+
39
+
40
+ def _fmt(value: float) -> str:
41
+ return f"{int(value):,}"
42
+
43
+
44
+ def _compact(value: float) -> str:
45
+ for limit, suffix in ((1_000_000, "M"), (1_000, "k")):
46
+ if value >= limit:
47
+ text = f"{value / limit:.1f}"
48
+ return text.removesuffix(".0") + suffix
49
+ return str(int(value))
50
+
51
+
52
+ def _signed(value: float, sign: str) -> str:
53
+ return f"{sign}{int(value):,}" if value else "0"
54
+
55
+
56
+ def _nice_max(value: float) -> float:
57
+ if value <= 0:
58
+ return 1
59
+ magnitude = 10 ** math.floor(math.log10(value))
60
+ for factor in (1, 2, 2.5, 5, 10):
61
+ candidate = magnitude * factor
62
+ if candidate >= value:
63
+ return candidate
64
+ return magnitude * 10
65
+
66
+
67
+ def svg_timeline(
68
+ points: list[dict], *, width: int = 880, height: int = 260, lang: str = "zh"
69
+ ) -> str:
70
+ """增减行的镜像面积图:新增在上,删除在下,零线居中。"""
71
+ if not points:
72
+ return f'<p class="empty">{_e(t("chart.empty", lang))}</p>'
73
+
74
+ n = len(points)
75
+ pad_l, pad_r, pad_t, pad_b = 46, 14, 16, 26
76
+ plot_w = width - pad_l - pad_r
77
+ plot_h = height - pad_t - pad_b
78
+ step = plot_w / n
79
+ zero = pad_t + plot_h * 0.5
80
+
81
+ max_up = max((p["insertions"] for p in points), default=0) or 1
82
+ max_down = max((p["deletions"] for p in points), default=0) or 1
83
+ top = _nice_max(max_up)
84
+ bottom = _nice_max(max_down)
85
+ scale_up = (plot_h * 0.5 - 6) / top
86
+ scale_down = (plot_h * 0.5 - 6) / bottom
87
+
88
+ def x_of(index: int) -> float:
89
+ return pad_l + step * (index + 0.5)
90
+
91
+ up_pts = [(x_of(i), zero - p["insertions"] * scale_up) for i, p in enumerate(points)]
92
+ down_pts = [(x_of(i), zero + p["deletions"] * scale_down) for i, p in enumerate(points)]
93
+
94
+ up_area = f"M{x_of(0) - step / 2:.1f},{zero:.1f} " + " ".join(
95
+ f"L{x:.1f},{y:.1f}" for x, y in up_pts
96
+ ) + f" L{x_of(n - 1) + step / 2:.1f},{zero:.1f} Z"
97
+ down_area = f"M{x_of(0) - step / 2:.1f},{zero:.1f} " + " ".join(
98
+ f"L{x:.1f},{y:.1f}" for x, y in down_pts
99
+ ) + f" L{x_of(n - 1) + step / 2:.1f},{zero:.1f} Z"
100
+
101
+ up_line = " ".join(f"{x:.1f},{y:.1f}" for x, y in up_pts)
102
+ down_line = " ".join(f"{x:.1f},{y:.1f}" for x, y in down_pts)
103
+
104
+ parts = [
105
+ f'<svg viewBox="0 0 {width} {height}" width="100%" role="img" '
106
+ f'xmlns="http://www.w3.org/2000/svg" font-family="{_FONT}">',
107
+ f'<title>{_e(t("chart.timeline.title", lang))}</title>',
108
+ # 网格
109
+ f'<line x1="{pad_l}" y1="{pad_t}" x2="{width - pad_r}" y2="{pad_t}" stroke="{_LINE}" stroke-width="0.5"/>',
110
+ f'<line x1="{pad_l}" y1="{zero:.1f}" x2="{width - pad_r}" y2="{zero:.1f}" stroke="{_INK}" stroke-width="0.8" opacity="0.25"/>',
111
+ f'<line x1="{pad_l}" y1="{height - pad_b}" x2="{width - pad_r}" y2="{height - pad_b}" stroke="{_LINE}" stroke-width="0.5"/>',
112
+ # y 轴刻度
113
+ f'<text x="{pad_l - 8}" y="{pad_t + 4}" text-anchor="end" font-size="11" fill="{_MUTED}">+{_compact(top)}</text>',
114
+ f'<text x="{pad_l - 8}" y="{height - pad_b}" text-anchor="end" font-size="11" fill="{_MUTED}">−{_compact(bottom)}</text>',
115
+ f'<text x="{pad_l - 8}" y="{zero + 4:.1f}" text-anchor="end" font-size="11" fill="{_MUTED}">0</text>',
116
+ # 面积
117
+ f'<path d="{up_area}" fill="{_ADD}" opacity="0.14"/>',
118
+ f'<path d="{down_area}" fill="{_DEL}" opacity="0.14"/>',
119
+ f'<polyline points="{up_line}" fill="none" stroke="{_ADD}" stroke-width="1.6" stroke-linejoin="round"/>',
120
+ f'<polyline points="{down_line}" fill="none" stroke="{_DEL}" stroke-width="1.6" stroke-linejoin="round"/>',
121
+ ]
122
+
123
+ # x 轴标签:最多 6 个,避免重叠
124
+ label_step = max(1, n // 6)
125
+ for index in range(0, n, label_step):
126
+ point = points[index]
127
+ parts.append(
128
+ f'<text x="{x_of(index):.1f}" y="{height - 8}" text-anchor="middle" '
129
+ f'font-size="10.5" fill="{_MUTED}">{_e(point["start"][5:10])}</text>'
130
+ )
131
+
132
+ # 悬停提示用原生 title,不写 JS
133
+ for index, point in enumerate(points):
134
+ parts.append(
135
+ f'<g><title>{_e(t("chart.tooltip.point", lang, label=point["label"], n=point["commits"], i=_signed(point["insertions"], "+"), d=_signed(point["deletions"], "−")))}</title>'
136
+ f'<rect x="{x_of(index) - step / 2:.1f}" y="{pad_t}" width="{step:.1f}" '
137
+ f'height="{plot_h}" fill="transparent"/></g>'
138
+ )
139
+
140
+ parts.append("</svg>")
141
+ return "".join(parts)
142
+
143
+
144
+ def svg_commit_bars(
145
+ points: list[dict], *, width: int = 880, height: int = 78, lang: str = "zh"
146
+ ) -> str:
147
+ """提交频次柱状图。单独成图,避免和增删量挤在一个坐标系里互相干扰。"""
148
+ if not points:
149
+ return ""
150
+ n = len(points)
151
+ pad_l, pad_r, pad_t, pad_b = 46, 14, 8, 18
152
+ plot_w = width - pad_l - pad_r
153
+ plot_h = height - pad_t - pad_b
154
+ step = plot_w / n
155
+ peak = max((p["commits"] for p in points), default=0) or 1
156
+ bar_w = max(1.5, step * 0.62)
157
+
158
+ parts = [
159
+ f'<svg viewBox="0 0 {width} {height}" width="100%" role="img" '
160
+ f'xmlns="http://www.w3.org/2000/svg" font-family="{_FONT}">',
161
+ f'<title>{_e(t("chart.bars.title", lang))}</title>',
162
+ f'<text x="{pad_l - 8}" y="{pad_t + 9}" text-anchor="end" font-size="11" fill="{_MUTED}">{peak}</text>',
163
+ f'<text x="{pad_l - 8}" y="{height - pad_b}" text-anchor="end" font-size="11" fill="{_MUTED}">0</text>',
164
+ f'<line x1="{pad_l}" y1="{height - pad_b}" x2="{width - pad_r}" y2="{height - pad_b}" stroke="{_LINE}" stroke-width="0.5"/>',
165
+ ]
166
+ for index, point in enumerate(points):
167
+ # 空周不画:一条 2px 的矮条会让整行看起像虚线,反而掩盖了「这周他不在」
168
+ if not point["commits"]:
169
+ continue
170
+ bar_h = plot_h * (point["commits"] / peak)
171
+ x = pad_l + step * index + (step - bar_w) / 2
172
+ y = height - pad_b - bar_h
173
+ opacity = 0.28 + 0.62 * (point["commits"] / peak)
174
+ parts.append(
175
+ f'<g><title>{_e(t("chart.tooltip.bar", lang, label=point["label"], n=point["commits"]))}</title>'
176
+ f'<rect x="{x:.1f}" y="{y:.1f}" width="{bar_w:.1f}" height="{max(bar_h, 2.5):.1f}" '
177
+ f'rx="1" fill="{_INK}" opacity="{opacity:.2f}"/></g>'
178
+ )
179
+ parts.append("</svg>")
180
+ return "".join(parts)
181
+
182
+
183
+ def svg_heatmap(rhythm: dict, *, cell: int = 15, gap: int = 3, lang: str = "zh") -> str:
184
+ """7×24 作息热力图。周一在最上,凌晨在最左。"""
185
+ matrix = rhythm.get("matrix") or [[0] * 24 for _ in range(7)]
186
+ labels = rhythm.get("weekday_labels") or list(weekdays(lang))
187
+ peak = max((max(row) for row in matrix), default=0) or 1
188
+
189
+ left, top_pad = 38, 18
190
+ width = left + 24 * (cell + gap)
191
+ height = top_pad + 7 * (cell + gap) + 4
192
+
193
+ parts = [
194
+ f'<svg viewBox="0 0 {width} {height}" width="100%" role="img" '
195
+ f'xmlns="http://www.w3.org/2000/svg" font-family="{_FONT}">',
196
+ f'<title>{_e(t("chart.heatmap.title", lang))}</title>'
197
+ f'<desc>{_e(t("chart.heatmap.desc", lang))}</desc>',
198
+ ]
199
+
200
+ for hour in (0, 6, 12, 18, 23):
201
+ x = left + hour * (cell + gap) + cell / 2
202
+ parts.append(
203
+ f'<text x="{x:.1f}" y="{top_pad - 6}" text-anchor="middle" '
204
+ f'font-size="10" fill="{_MUTED}">{hour:02d}</text>'
205
+ )
206
+
207
+ for dow in range(7):
208
+ y = top_pad + dow * (cell + gap)
209
+ parts.append(
210
+ f'<text x="{left - 8}" y="{y + cell * 0.72:.1f}" text-anchor="end" '
211
+ f'font-size="10.5" fill="{_MUTED}">{_e(labels[dow])}</text>'
212
+ )
213
+ for hour in range(24):
214
+ value = matrix[dow][hour]
215
+ x = left + hour * (cell + gap)
216
+ if value:
217
+ ratio = (value / peak) ** 0.55 # 幂次压缩,否则零星提交全看不见
218
+ opacity = 0.16 + 0.84 * ratio
219
+ fill = _INK
220
+ else:
221
+ opacity = 0.035
222
+ fill = _INK
223
+ parts.append(
224
+ f'<g><title>{_e(t("chart.tooltip.cell", lang, dow=labels[dow], h=f"{hour:02d}", n=value))}</title>'
225
+ f'<rect x="{x}" y="{y}" width="{cell}" height="{cell}" rx="2.5" '
226
+ f'fill="{fill}" opacity="{opacity:.3f}"/></g>'
227
+ )
228
+
229
+ parts.append("</svg>")
230
+ return "".join(parts)
231
+
232
+
233
+ _BREAK_CHARS = ",。、;:!?,.;:!?))」』”"
234
+
235
+
236
+ def _wrap(text: str, limit: int, max_lines: int = 3) -> list[str]:
237
+ """按字符宽度折行。中文一字一格,英文按 0.55 折算。
238
+
239
+ 溢出时优先回退到最近的标点处断开 —— 否则中文会被切成「一段仍在生 / 长的历史」
240
+ 这种断在词中间的难看位置。
241
+ """
242
+ lines: list[str] = []
243
+ current = ""
244
+ width = 0.0
245
+ for char in text:
246
+ weight = 1.0 if ord(char) > 0x2E80 else 0.55
247
+ if width + weight > limit and current:
248
+ cut = -1
249
+ for back in range(len(current) - 1, max(len(current) - 10, 0), -1):
250
+ if current[back] in _BREAK_CHARS:
251
+ cut = back + 1
252
+ break
253
+ if cut > 0:
254
+ lines.append(current[:cut])
255
+ current = current[cut:] + char
256
+ width = sum(1.0 if ord(c) > 0x2E80 else 0.55 for c in current)
257
+ else:
258
+ lines.append(current)
259
+ current, width = char, weight
260
+ if len(lines) == max_lines:
261
+ current = ""
262
+ break
263
+ continue
264
+ current += char
265
+ width += weight
266
+ if current and len(lines) < max_lines:
267
+ lines.append(current)
268
+ if len(lines) == max_lines and len("".join(lines)) < len(text):
269
+ lines[-1] = lines[-1][:-1] + "…"
270
+ return lines
271
+
272
+
273
+ def _mini_churn(
274
+ points: list[dict], x0: float, y0: float, width: float, height: float
275
+ ) -> str:
276
+ """分享卡里的迷你增删曲线。画在深色底上,所以用浅一档的配色。"""
277
+ if len(points) < 2:
278
+ return ""
279
+ n = len(points)
280
+ step = width / n
281
+ mid = y0 + height * 0.5
282
+ max_up = max((p["insertions"] for p in points), default=0) or 1
283
+ max_dn = max((p["deletions"] for p in points), default=0) or 1
284
+ amp = height * 0.44
285
+
286
+ up = [(x0 + step * (i + 0.5), mid - p["insertions"] / max_up * amp) for i, p in enumerate(points)]
287
+ dn = [(x0 + step * (i + 0.5), mid + p["deletions"] / max_dn * amp) for i, p in enumerate(points)]
288
+
289
+ up_area = f"M{x0:.1f},{mid:.1f} " + " ".join(f"L{x:.1f},{y:.1f}" for x, y in up) + f" L{x0 + width:.1f},{mid:.1f} Z"
290
+ dn_area = f"M{x0:.1f},{mid:.1f} " + " ".join(f"L{x:.1f},{y:.1f}" for x, y in dn) + f" L{x0 + width:.1f},{mid:.1f} Z"
291
+ up_line = " ".join(f"{x:.1f},{y:.1f}" for x, y in up)
292
+ dn_line = " ".join(f"{x:.1f},{y:.1f}" for x, y in dn)
293
+
294
+ return "".join(
295
+ [
296
+ f'<path d="{up_area}" fill="#5DCAA5" opacity="0.16"/>',
297
+ f'<path d="{dn_area}" fill="#F0997B" opacity="0.16"/>',
298
+ f'<polyline points="{up_line}" fill="none" stroke="#5DCAA5" stroke-width="1.4" opacity="0.85"/>',
299
+ f'<polyline points="{dn_line}" fill="none" stroke="#F0997B" stroke-width="1.4" opacity="0.85"/>',
300
+ f'<line x1="{x0}" y1="{mid:.1f}" x2="{x0 + width}" y2="{mid:.1f}" stroke="#F5F2EA" stroke-width="0.5" opacity="0.12"/>',
301
+ ]
302
+ )
303
+
304
+
305
+ def svg_share_card(report: dict, *, width: int = 720, height: int = 1080) -> str:
306
+ """竖版分享卡,可直接在浏览器里转成 PNG。
307
+
308
+ 版式固定四段:标识 / 仓库名 + 金句 / 迷你曲线 / 四宫格数字。
309
+ 中间那条曲线不是装饰 —— 没有它,卡中间会空掉三分之一。
310
+ """
311
+ meta = report.get("meta", {})
312
+ overview = report.get("overview", {})
313
+ points = (report.get("timeline") or {}).get("points") or []
314
+ repo_name = (meta.get("repo") or {}).get("name") or "repository"
315
+ first = ((meta.get("range") or {}).get("first_commit") or "")[:10]
316
+ last = ((meta.get("range") or {}).get("last_commit") or "")[:10]
317
+ quote = report.get("quote") or ""
318
+ card_lang = meta.get("lang", "zh")
319
+
320
+ pad = 64
321
+ inner = width - pad * 2
322
+ parts = [
323
+ f'<svg id="share-card" viewBox="0 0 {width} {height}" width="{width}" height="{height}" '
324
+ f'xmlns="http://www.w3.org/2000/svg" font-family="{_FONT}">',
325
+ f'<title>{_e(t("chart.card.title", meta.get("lang", "zh")))}</title>',
326
+ f'<rect width="{width}" height="{height}" fill="#16151A"/>',
327
+ f'<text x="{pad}" y="72" font-size="12.5" fill="#8C877E" letter-spacing="3">GIT CHRONICLE</text>',
328
+ f'<text x="{width - pad}" y="72" text-anchor="end" font-size="12.5" fill="#8C877E">'
329
+ f"{_e(first)} — {_e(last)}</text>",
330
+ ]
331
+
332
+ y = 168
333
+ for line in _wrap(repo_name, 17, max_lines=2):
334
+ parts.append(
335
+ f'<text x="{pad}" y="{y}" font-size="56" font-weight="600" fill="#F5F2EA" '
336
+ f'letter-spacing="-1.5">{_e(line)}</text>'
337
+ )
338
+ y += 64
339
+
340
+ quote_y = 300
341
+ for index, line in enumerate(_wrap(quote, 24, max_lines=2)):
342
+ parts.append(
343
+ f'<text x="{pad}" y="{quote_y + index * 32}" font-size="18" fill="#C9C3B6">{_e(line)}</text>'
344
+ )
345
+
346
+ parts.append(
347
+ f'<text x="{pad}" y="386" font-size="10.5" fill="#6B6660" letter-spacing="1.5">'
348
+ f'{_e(t("chart.card.weekly", card_lang))}</text>'
349
+ )
350
+ parts.append(_mini_churn(points, pad, 400, inner, 150))
351
+
352
+ stats = [
353
+ (t("ui.stat.commits", card_lang), _fmt(overview.get("commits", 0))),
354
+ (t("ui.stat.added_lines", card_lang), _fmt(overview.get("insertions", 0))),
355
+ (t("ui.stat.authors", card_lang), str(overview.get("authors", 0))),
356
+ (t("ui.stat.active_days", card_lang), str(overview.get("active_days", 0))),
357
+ ]
358
+ for index, (label, value) in enumerate(stats):
359
+ col, row = index % 2, index // 2
360
+ x = pad + col * (inner / 2)
361
+ sy = 690 + row * 122
362
+ parts.append(
363
+ f'<text x="{x}" y="{sy}" font-size="11.5" fill="#8C877E" letter-spacing="1.5">{_e(label)}</text>'
364
+ )
365
+ parts.append(
366
+ f'<text x="{x}" y="{sy + 50}" font-size="40" font-weight="600" fill="#F5F2EA">{_e(value)}</text>'
367
+ )
368
+
369
+ parts.append(
370
+ f'<line x1="{pad}" y1="{height - 96}" x2="{width - pad}" y2="{height - 96}" '
371
+ f'stroke="#2E2B27" stroke-width="1"/>'
372
+ )
373
+ parts.append(
374
+ f'<text x="{pad}" y="{height - 60}" font-size="12" fill="#6B6660">'
375
+ f'{_e(TOOL_NAME)} · {_e(meta.get("version", "0.1.0"))}</text>'
376
+ )
377
+ zh = card_lang == "zh"
378
+ tail = (
379
+ f"{overview.get('span_days', 0)} 天 · {overview.get('files_touched', 0)} 个文件"
380
+ if zh
381
+ else f"{overview.get('span_days', 0)} days · {overview.get('files_touched', 0)} files"
382
+ )
383
+ parts.append(
384
+ f'<text x="{width - pad}" y="{height - 60}" text-anchor="end" font-size="12" fill="#6B6660">'
385
+ f"{_e(tail)}</text>"
386
+ )
387
+ parts.append("</svg>")
388
+ return "".join(parts)
389
+
390
+
391
+ def _mini_heatmap(rhythm: dict, x0: float, y0: float, width: float, height: float) -> str:
392
+ """分享卡里的迷你作息点阵。7 行 24 列,格子在卡片上小到只能看形状 —— 这正是重点。"""
393
+ matrix = rhythm.get("matrix") or [[0] * 24 for _ in range(7)]
394
+ peak = max((max(row) for row in matrix), default=0) or 1
395
+ gap = 3.0
396
+ cell_w = (width - 23 * gap) / 24
397
+ cell_h = (height - 6 * gap) / 7
398
+
399
+ parts = []
400
+ for dow in range(7):
401
+ for hour in range(24):
402
+ value = matrix[dow][hour]
403
+ x = x0 + hour * (cell_w + gap)
404
+ y = y0 + dow * (cell_h + gap)
405
+ if value:
406
+ opacity = 0.14 + 0.86 * (value / peak) ** 0.55
407
+ fill = "#5DCAA5"
408
+ else:
409
+ opacity = 0.06
410
+ fill = "#F5F2EA"
411
+ parts.append(
412
+ f'<rect x="{x:.1f}" y="{y:.1f}" width="{cell_w:.1f}" height="{cell_h:.1f}" '
413
+ f'rx="2" fill="{fill}" opacity="{opacity:.3f}"/>'
414
+ )
415
+ return "".join(parts)
416
+
417
+
418
+ def svg_share_card_author(report: dict, *, width: int = 720, height: int = 1080) -> str:
419
+ """竖版个人分享卡。和项目卡同一套骨架,但把曲线换成作息点阵 ——
420
+ 那张图是「这个人几点写代码」的指纹,比任何数字都好认。"""
421
+ meta = report.get("meta", {})
422
+ persona = report.get("persona", {})
423
+ rhythm = report.get("rhythm", {})
424
+ quote = report.get("quote") or ""
425
+ name = persona.get("name") or "developer"
426
+ tags = [tag["label"] for tag in persona.get("tags", [])][:3]
427
+ card_lang = meta.get("lang", "zh")
428
+
429
+ pad = 64
430
+ inner = width - pad * 2
431
+ first = (persona.get("span_start") or "")[:10]
432
+ last = (persona.get("span_end") or "")[:10]
433
+
434
+ parts = [
435
+ f'<svg id="share-card" viewBox="0 0 {width} {height}" width="{width}" height="{height}" '
436
+ f'xmlns="http://www.w3.org/2000/svg" font-family="{_FONT}">',
437
+ f'<title>{_e(t("chart.card.title", meta.get("lang", "zh")))}</title>',
438
+ f'<rect width="{width}" height="{height}" fill="#16151A"/>',
439
+ f'<text x="{pad}" y="72" font-size="12.5" fill="#8C877E" letter-spacing="3">DEVELOPER PROFILE</text>',
440
+ f'<text x="{width - pad}" y="72" text-anchor="end" font-size="12.5" fill="#8C877E">'
441
+ f"{_e(first)} — {_e(last)}</text>",
442
+ ]
443
+
444
+ y = 168
445
+ for line in _wrap(name, 16, max_lines=2):
446
+ parts.append(
447
+ f'<text x="{pad}" y="{y}" font-size="54" font-weight="600" fill="#F5F2EA" '
448
+ f'letter-spacing="-1.5">{_e(line)}</text>'
449
+ )
450
+ y += 62
451
+
452
+ # 标签药丸
453
+ pill_x = pad
454
+ pill_y = y + 6
455
+ for label in tags:
456
+ pill_w = len(label) * 13 + 26
457
+ if pill_x + pill_w > width - pad:
458
+ break
459
+ parts.append(
460
+ f'<rect x="{pill_x}" y="{pill_y}" width="{pill_w}" height="30" rx="15" '
461
+ f'fill="#1D9E75" opacity="0.14"/>'
462
+ f'<text x="{pill_x + pill_w / 2}" y="{pill_y + 20}" text-anchor="middle" '
463
+ f'font-size="13" fill="#5DCAA5">{_e(label)}</text>'
464
+ )
465
+ pill_x += pill_w + 8
466
+
467
+ quote_y = pill_y + 78
468
+ for index, line in enumerate(_wrap(quote, 25, max_lines=2)):
469
+ parts.append(
470
+ f'<text x="{pad}" y="{quote_y + index * 32}" font-size="18" fill="#C9C3B6">{_e(line)}</text>'
471
+ )
472
+
473
+ band = quote_y + 84
474
+ parts.append(
475
+ f'<text x="{pad}" y="{band}" font-size="10.5" fill="#6B6660" letter-spacing="1.5">'
476
+ f'{_e(t("chart.card.rhythm", meta.get("lang", "zh")))}</text>'
477
+ )
478
+ parts.append(_mini_heatmap(rhythm, pad, band + 14, inner, 146))
479
+
480
+ stats = [
481
+ (t("ui.stat.commits", card_lang), _fmt(persona.get("commits", 0))),
482
+ (t("ui.stat.share_of_repo", card_lang), f"{persona.get('share', 0)}%"),
483
+ (t("ui.stat.added_lines", card_lang), _fmt(persona.get("insertions", 0))),
484
+ (t("ui.stat.active_days", card_lang), str(persona.get("active_days", 0))),
485
+ ]
486
+ grid_top = band + 232
487
+ for index, (label, value) in enumerate(stats):
488
+ col, row = index % 2, index // 2
489
+ x = pad + col * (inner / 2)
490
+ sy = grid_top + row * 116
491
+ parts.append(
492
+ f'<text x="{x}" y="{sy}" font-size="11.5" fill="#8C877E" letter-spacing="1.5">{_e(label)}</text>'
493
+ )
494
+ parts.append(
495
+ f'<text x="{x}" y="{sy + 48}" font-size="38" font-weight="600" fill="#F5F2EA">{_e(value)}</text>'
496
+ )
497
+
498
+ parts.append(
499
+ f'<line x1="{pad}" y1="{height - 96}" x2="{width - pad}" y2="{height - 96}" '
500
+ f'stroke="#2E2B27" stroke-width="1"/>'
501
+ )
502
+ repo_name = (meta.get("repo") or {}).get("name") or ""
503
+ parts.append(
504
+ f'<text x="{pad}" y="{height - 60}" font-size="12" fill="#6B6660">'
505
+ f"{_e(repo_name)} · {_e(TOOL_NAME)} {_e(meta.get('version', '0.1.0'))}</text>"
506
+ )
507
+ zh = card_lang == "zh"
508
+ tail = (
509
+ f"{persona.get('span_days', 0)} 天"
510
+ if zh
511
+ else f"{persona.get('span_days', 0)} days"
512
+ )
513
+ parts.append(
514
+ f'<text x="{width - pad}" y="{height - 60}" text-anchor="end" font-size="12" fill="#6B6660">'
515
+ f"{_e(tail)}</text>"
516
+ )
517
+ parts.append("</svg>")
518
+ return "".join(parts)
@@ -0,0 +1,80 @@
1
+ /* 两个视角共用的设计令牌与基础组件。改这里,两套报告一起变。 */
2
+ :root{
3
+ --ink:#16151A; --muted:#6E6A63; --faint:#9C978E;
4
+ --line:#E6E2D9; --paper:#FBFAF6; --card:#FFFFFF;
5
+ --add:#1D9E75; --del:#D85A30; --warm:#B4552A;
6
+ --serif:Georgia,"Songti SC","Source Han Serif SC",serif;
7
+ --sans:ui-sans-serif,system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;
8
+ --mono:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;
9
+ }
10
+ *{box-sizing:border-box}
11
+ html{-webkit-text-size-adjust:100%}
12
+ body{
13
+ margin:0; background:var(--paper); color:var(--ink);
14
+ font-family:var(--sans); font-size:15px; line-height:1.75;
15
+ -webkit-font-smoothing:antialiased;
16
+ }
17
+ .wrap{max-width:900px;margin:0 auto;padding:0 28px}
18
+ h1,h2,h3{font-family:var(--serif);font-weight:600;letter-spacing:-.01em;margin:0}
19
+ h1{font-size:clamp(28px,5vw,46px);line-height:1.15}
20
+ h2{font-size:24px;margin-bottom:6px}
21
+ h3{font-size:17px}
22
+ p{margin:0}
23
+ a{color:inherit}
24
+ .num{font-family:var(--mono);font-variant-numeric:tabular-nums}
25
+ .section{padding:64px 0 0}
26
+ .eyebrow{
27
+ font-size:11.5px;letter-spacing:.16em;text-transform:uppercase;
28
+ color:var(--faint);font-weight:500
29
+ }
30
+ .lead{color:var(--muted);font-size:14.5px;max-width:62ch}
31
+
32
+ /* hero */
33
+ .hero{background:var(--ink);color:#F5F2EA;padding:72px 0 56px;position:relative;overflow:hidden}
34
+ .hero::after{
35
+ content:"";position:absolute;right:-120px;top:-120px;width:420px;height:420px;
36
+ border:1px solid rgba(245,242,234,.07);border-radius:50%;
37
+ }
38
+ .hero .eyebrow{color:#8C877E}
39
+ .hero h1{margin:14px 0 0;color:#F5F2EA}
40
+ .hero .quote{
41
+ font-family:var(--serif);font-size:clamp(16px,2.2vw,20px);line-height:1.65;
42
+ color:#C9C3B6;margin-top:22px;max-width:52ch;font-style:italic
43
+ }
44
+ .hero .meta{margin-top:16px;font-size:12.5px;color:#8C877E;display:flex;gap:14px;flex-wrap:wrap}
45
+ .hero .meta span{display:inline-flex;align-items:center;gap:6px}
46
+ .dot{width:4px;height:4px;border-radius:50%;background:#57534C}
47
+ .stats{display:grid;grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:2px;margin-top:40px;
48
+ background:rgba(245,242,234,.09);border-radius:10px;overflow:hidden}
49
+ .stat{background:var(--ink);padding:18px 16px}
50
+ .stat dt{font-size:11px;letter-spacing:.1em;color:#8C877E;text-transform:uppercase}
51
+ .stat dd{margin:6px 0 0;font-size:23px;font-family:var(--mono);font-variant-numeric:tabular-nums;color:#F5F2EA}
52
+ .stat dd small{font-size:12px;color:#8C877E;margin-left:1px;font-family:var(--sans)}
53
+ .hero-actions{margin-top:28px;display:flex;gap:12px;align-items:center;flex-wrap:wrap}
54
+ .btn{
55
+ appearance:none;border:1px solid rgba(245,242,234,.24);background:transparent;color:#F5F2EA;
56
+ font:inherit;font-size:13px;padding:9px 18px;border-radius:999px;cursor:pointer;
57
+ transition:background .18s,border-color .18s
58
+ }
59
+ .btn:hover{background:rgba(245,242,234,.1);border-color:rgba(245,242,234,.4)}
60
+ .btn-note{font-size:12px;color:#7A766F}
61
+
62
+ /* 卡片与提示 */
63
+ .block{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:26px 24px}
64
+ .legend{display:flex;gap:18px;font-size:12px;color:var(--muted);margin:14px 0 4px}
65
+ .legend i{display:inline-block;width:9px;height:9px;border-radius:2px;margin-right:6px;vertical-align:middle}
66
+ .caption{font-size:12px;color:var(--faint);margin-top:12px;line-height:1.6}
67
+ .notice{margin-top:24px;padding:14px 16px;border-left:2px solid var(--warm);
68
+ background:rgba(180,85,42,.04);font-size:12.5px;color:var(--muted);border-radius:0 6px 6px 0}
69
+ .empty{color:var(--faint);font-size:13px;padding:8px 0}
70
+ footer{margin-top:72px;padding:28px 0 60px;border-top:1px solid var(--line);
71
+ font-size:12px;color:var(--faint);display:flex;justify-content:space-between;gap:16px;flex-wrap:wrap}
72
+ .dirs{display:flex;gap:8px;flex-wrap:wrap;margin-top:20px;padding-top:18px;border-top:1px solid var(--line)}
73
+ .dchip{font-size:11.5px;font-family:var(--mono);color:var(--muted);
74
+ background:rgba(22,21,26,.04);padding:3px 9px;border-radius:4px}
75
+
76
+ @media (max-width:720px){
77
+ .wrap{padding:0 20px}
78
+ .hero{padding:52px 0 44px}
79
+ .section{padding:48px 0 0}
80
+ }