theme v2
This commit is contained in:
@@ -80,19 +80,12 @@ func TestGenPreview(t *testing.T) {
|
||||
themeBox["PageUser"].(map[string]interface{})["Background"] = bgURI
|
||||
themeBox["PageUser"].(map[string]interface{})["Usernamecolor"] = "#3a2b30"
|
||||
themeBox["PageUser"].(map[string]interface{})["Introcolor"] = "#6b545c"
|
||||
themeCSS, err := os.ReadFile(filepath.Join("..", "static", "css", "themes", "sakura.css"))
|
||||
if err != nil {
|
||||
t.Fatalf("read theme css: %v", err)
|
||||
}
|
||||
for _, theme := range []string{"light", "dark"} {
|
||||
page, err := renderToString(t, "question/list", themeBox)
|
||||
if err != nil {
|
||||
t.Fatalf("render themed list: %v", err)
|
||||
}
|
||||
// 预览页走 file://,把主题 css 内联进去(真实页面是一次带缓存的静态请求)。
|
||||
// 用正则匹配:预览数据里没有 StaticVersion,v 参数为空串,不能按固定串替换。
|
||||
link := regexp.MustCompile(`<link rel="stylesheet" href="/static/css/themes/sakura\.css\?v=[^"]*"/>`)
|
||||
page = link.ReplaceAllString(page, "<style>"+string(themeCSS)+"</style>")
|
||||
page = inlineThemeCSS(t, page, "sakura")
|
||||
writePreview(t, outDir, "preview-theme-sakura-"+theme+".html", page, theme, "../static")
|
||||
}
|
||||
|
||||
@@ -159,6 +152,40 @@ func TestGenPreview(t *testing.T) {
|
||||
}
|
||||
writePreview(t, outDir, "preview-profile-account-"+theme+".html", page, theme, "../static")
|
||||
}
|
||||
|
||||
// 设置页在箱主主题下的样子:/user 后台现在也会跟随本人主题,
|
||||
// 需要确认「粉色页面 + 主题选择器」不会互相吃掉。
|
||||
profileSakura := profileBaseData()
|
||||
profileSakura["Themes"] = profile["Themes"]
|
||||
profileSakura["CurrentTheme"] = "sakura"
|
||||
profileSakura["ActiveProfileTab"] = "account"
|
||||
profileSakura["ThemeSlug"] = "sakura"
|
||||
profileSakura["FeatureQuestion"] = nil
|
||||
profileSakura["FeatureChat"] = nil
|
||||
profileSakura["FeatureVote"] = nil
|
||||
for _, theme := range []string{"light", "dark"} {
|
||||
page, err := renderToString(t, "user/profile", profileSakura)
|
||||
if err != nil {
|
||||
t.Fatalf("render themed profile: %v", err)
|
||||
}
|
||||
page = inlineThemeCSS(t, page, "sakura")
|
||||
writePreview(t, outDir, "preview-profile-sakura-"+theme+".html", page, theme, "../static")
|
||||
}
|
||||
}
|
||||
|
||||
// inlineThemeCSS 把主题 <link> 换成内联 style:预览页走 file://,相对路径取不到。
|
||||
// 预览数据里没有 StaticVersion,v 参数为空串,所以按正则匹配而不是固定串替换。
|
||||
func inlineThemeCSS(t *testing.T, html, slug string) string {
|
||||
t.Helper()
|
||||
css, err := os.ReadFile(filepath.Join("..", "static", "css", "themes", slug+".css"))
|
||||
if err != nil {
|
||||
t.Fatalf("read theme css: %v", err)
|
||||
}
|
||||
link := regexp.MustCompile(`<link rel="stylesheet" href="/static/css/themes/` + slug + `\.css\?v=[^"]*"/>`)
|
||||
if !link.MatchString(html) {
|
||||
t.Fatalf("主题 %s 的 <link> 未出现在页面里(ThemeSlug 没传到模板)", slug)
|
||||
}
|
||||
return link.ReplaceAllString(html, "<style>"+string(css)+"</style>")
|
||||
}
|
||||
|
||||
// writePreview 把 CDN 换成可直连的镜像、钉死主题,仅用于本地预览。
|
||||
|
||||
Reference in New Issue
Block a user