forked from yyle88/yyle88
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyyle88_test.go
322 lines (272 loc) · 8.19 KB
/
yyle88_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package yyle88_test
import (
"fmt"
"math/rand/v2"
"net/url"
"os"
"slices"
"strings"
"sync"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/yyle88/done"
"github.com/yyle88/must"
"github.com/yyle88/osexistpath/osmustexist"
"github.com/yyle88/osexistpath/osomitexist"
"github.com/yyle88/runpath"
"github.com/yyle88/yyle88"
"github.com/yyle88/yyle88/internal/utils"
)
const username = "yyle88"
var mutexRewriteFp sync.Mutex //write file one by one
type DocGenParam struct {
shortName string
titleLine string
repoTitle string
}
func TestGenMarkdown(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.md",
titleLine: "| **RepoName** | **Description** |",
repoTitle: "repo",
})
}
func TestGenMarkdownZhHans(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.zh.md",
titleLine: "| 项目名称 | 项目描述 |",
repoTitle: "项目",
})
}
func TestGenMarkdownJapanese(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.ja.md",
titleLine: "| **リポ名** | **説明** |",
repoTitle: "リポ",
})
}
func TestGenMarkdownZhHant(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.zh-Hant.md",
titleLine: "| 倉庫名稱 | 描述 |",
repoTitle: "倉庫",
})
}
func TestGenMarkdownRussian(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.ru.md",
titleLine: "| Название проекта | Описание проекта |",
repoTitle: "Проект",
})
}
func TestGenMarkdownDeutsch(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.de.md",
titleLine: "| **RepoName** | **Beschreibung** |",
repoTitle: "Repo",
})
}
func TestGenMarkdownFrançais(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.fr.md",
titleLine: "| **Nom du dépôt** | **Description** |",
repoTitle: "Dépôt",
})
}
func TestGenMarkdownEspañol(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.es.md",
titleLine: "| **Nombre del repositorio** | **Descripción** |",
repoTitle: "Repositorio",
})
}
func TestGenMarkdownPortuguês(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.pt.md",
titleLine: "| **Nome do repositório** | **Descrição** |",
repoTitle: "Repositório",
})
}
func TestGenMarkdownKhmer(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.kh.md",
titleLine: "| **ឈ្មោះរ៉េបូ** | **ការពិពណ៌នា** |",
repoTitle: "រ៉េបូ",
})
}
func TestGenMarkdownVietnamese(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.vi.md",
titleLine: "| **Tên Repo** | **Mô tả** |",
repoTitle: "Repo",
})
}
func TestGenMarkdownKorean(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.ko.md",
titleLine: "| **레포 이름** | **설명** |",
repoTitle: "레포",
})
}
func TestGenMarkdownTurkish(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.tr.md",
titleLine: "| **Repo Adı** | **Açıklama** |",
repoTitle: "Repo",
})
}
func TestGenMarkdownPolish(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.pl.md",
titleLine: "| **Nazwa repozytorium** | **Opis** |",
repoTitle: "Repozytorium",
})
}
func TestGenMarkdownItalian(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.it.md",
titleLine: "| **Nome del repository** | **Descrizione** |",
repoTitle: "Repository",
})
}
func TestGenMarkdownArabic(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.ar.md",
titleLine: "| **اسم المستودع** | **الوصف** |",
repoTitle: "مستودع",
})
}
func TestGenMarkdownPersian(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.fa.md",
titleLine: "| **نام مخزن** | **توضیحات** |",
repoTitle: "مخزن",
})
}
func TestGenMarkdownCzech(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.cs.md",
titleLine: "| **Název repozitáře** | **Popis** |",
repoTitle: "Repozitář",
})
}
func TestGenMarkdownUkrainian(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.uk.md",
titleLine: "| **Назва репозиторію** | **Опис** |",
repoTitle: "Репозиторій",
})
}
func TestGenMarkdownDutch(t *testing.T) {
GenMarkdownTable(t, &DocGenParam{
shortName: "README.nl.md",
titleLine: "| **RepoNaam** | **Beschrijving** |",
repoTitle: "Repo",
})
}
func GenMarkdownTable(t *testing.T, arg *DocGenParam) {
mutexRewriteFp.Lock()
defer mutexRewriteFp.Unlock()
repos := fetchRepos()
require.NotEmpty(t, repos)
ptx := utils.NewPTX()
cardThemes := utils.GetRepoCardThemes()
require.NotEmpty(t, cardThemes)
subRepos, repos := splitRepos(repos, 10)
ptx.Println(`<div align="left">`)
ptx.Println()
for _, repo := range subRepos {
cardLine := makeCardLine(repo, cardThemes[rand.IntN(len(cardThemes))])
ptx.Println(cardLine)
ptx.Println()
}
ptx.Println(`</div>`)
ptx.Println()
colors := utils.GetBadgeColors()
require.NotEmpty(t, colors)
rand.Shuffle(len(colors), func(i, j int) {
colors[i], colors[j] = colors[j], colors[i]
})
if len(repos) > 0 {
ptx.Println()
ptx.Println(`<div align="left">`)
ptx.Println()
const stepLimit = 4
ptx.Println("|" + repeatString(" "+arg.repoTitle+" |", stepLimit))
ptx.Println("|" + repeatString(" :--: |", stepLimit))
for start := 0; start < len(repos); start += stepLimit {
ptx.Print("|")
for num := 0; num < stepLimit; num++ {
if idx := start + num; idx < len(repos) {
repo := repos[idx]
ptx.Print(makeCustomHeightBadge(repo.Name, repo.Link, colors[idx%len(colors)], 24), " | ")
} else {
ptx.Print("-", " | ")
}
}
ptx.Println()
}
ptx.Println()
ptx.Println(`</div>`)
ptx.Println()
}
stb := ptx.String()
t.Log(stb)
path := runpath.PARENT.Join(arg.shortName)
if !osomitexist.IsFile(path) {
path = runpath.PARENT.Join("locales", arg.shortName)
}
t.Log(osmustexist.PATH(path))
text := string(done.VAE(os.ReadFile(path)).Nice())
t.Log(text)
contentLines := strings.Split(text, "\n")
sIdx := slices.Index(contentLines, "<!-- 这是一个注释,它不会在渲染时显示出来,这是项目列表的起始位置 -->")
require.Positive(t, sIdx)
eIdx := slices.Index(contentLines, "<!-- 这是一个注释,它不会在渲染时显示出来,这是项目列表的终止位置 -->")
require.Positive(t, eIdx)
require.Less(t, sIdx, eIdx)
content := strings.Join(contentLines[:sIdx+1], "\n") + "\n" + "\n" +
stb + "\n" +
strings.Join(contentLines[eIdx:], "\n")
t.Log(content)
must.Done(os.WriteFile(path, []byte(content), 0666))
t.Log("success")
}
var reposSingleton []*yyle88.Repo
var onceFetchRepos sync.Once
func fetchRepos() []*yyle88.Repo {
onceFetchRepos.Do(func() {
reposSingleton = done.VAE(yyle88.GetGithubRepos(username)).Nice()
})
return reposSingleton
}
func splitRepos(repos []*yyle88.Repo, subSize int) ([]*yyle88.Repo, []*yyle88.Repo) {
idx := min(subSize, len(repos))
return repos[:idx], repos[idx:]
}
//func makeBadge(repo *yyle88.Repo, colorString string) string {
// return fmt.Sprintf("[![%s](https://img.shields.io/badge/%s-%s.svg?style=flat&logoColor=white)](%s)", repo.Name, repo.Name, url.QueryEscape(colorString), repo.Link)
//}
func makeCustomHeightBadge(name string, link string, colorString string, height int) string {
return fmt.Sprintf(`<a href="%s"><img src="https://img.shields.io/badge/%s-%s.svg?style=flat&logoColor=white" height="%d"></a>`, link, strings.ReplaceAll(name, "-", "+"), url.QueryEscape(colorString), height)
}
func repeatString(s string, n int) string {
var res string
for i := 0; i < n; i++ {
res += s
}
return res
}
func makeCardLine(repo *yyle88.Repo, cardTheme string) string {
const templateLine = "[![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username={{ username }}&repo={{ repo_name }}&theme={{ card_theme }}&unique={{ unique_uuid }})]({{ repo_link }})"
rep := strings.NewReplacer(
"{{ username }}", username,
"{{ repo_name }}", repo.Name,
"{{ card_theme }}", cardTheme,
"{{ unique_uuid }}", uuid.New().String(),
"{{ repo_link }}", repo.Link,
)
cardLine := rep.Replace(templateLine)
return cardLine
}