update doc, add tests

This commit is contained in:
wxiaoguang 2024-05-08 09:36:47 +08:00
parent d941875c17
commit fdfd440061
3 changed files with 37 additions and 6 deletions

View File

@ -388,6 +388,7 @@ It could add theme meta information into the custom theme CSS file to provide mo
If a custom theme is a dark theme, please set the global css variable `--is-dark-theme: true` in the `:root` block. If a custom theme is a dark theme, please set the global css variable `--is-dark-theme: true` in the `:root` block.
This allows Gitea to adjust the Monaco code editor's theme accordingly. This allows Gitea to adjust the Monaco code editor's theme accordingly.
An "auto" theme could be implemented by using "theme-gitea-auto.css" as a reference.
```css ```css
gitea-theme-meta-info { gitea-theme-meta-info {

View File

@ -49,7 +49,11 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
( (
\s*(--[-\w]+) \s*(--[-\w]+)
\s*: \s*:
\s*("(\\"|[^"])*") \s*(
("(\\"|[^"])*")
|('(\\'|[^'])*')
|([^'";]+)
)
\s*; \s*;
\s* \s*
) )
@ -66,9 +70,13 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
m := map[string]string{} m := map[string]string{}
for _, item := range matchedItems { for _, item := range matchedItems {
v := item[3] v := item[3]
v = strings.TrimPrefix(v, "\"") if strings.HasPrefix(v, `"`) {
v = strings.TrimSuffix(v, "\"") v = strings.TrimSuffix(strings.TrimPrefix(v, `"`), `"`)
v = strings.ReplaceAll(v, `\"`, `"`) v = strings.ReplaceAll(v, `\"`, `"`)
} else if strings.HasPrefix(v, `'`) {
v = strings.TrimSuffix(strings.TrimPrefix(v, `'`), `'`)
v = strings.ReplaceAll(v, `\'`, `'`)
}
m[item[2]] = v m[item[2]] = v
} }
return m return m

View File

@ -10,6 +10,28 @@ import (
) )
func TestParseThemeMetaInfo(t *testing.T) { func TestParseThemeMetaInfo(t *testing.T) {
m := parseThemeMetaInfoToMap(`gitea-theme-meta-info { --k1: "v1"; --k2: "a\"b"; }`) m := parseThemeMetaInfoToMap(`gitea-theme-meta-info {
assert.Equal(t, map[string]string{"--k1": "v1", "--k2": `a"b`}, m) --k1: "v1";
--k2: "v\"2";
--k3: 'v3';
--k4: 'v\'4';
--k5: v5;
}`)
assert.Equal(t, map[string]string{
"--k1": "v1",
"--k2": `v"2`,
"--k3": "v3",
"--k4": "v'4",
"--k5": "v5",
}, m)
// if an auto theme imports others, the meta info should be extracted from the last one
// the meta in imported themes should be ignored to avoid incorrect overriding
m = parseThemeMetaInfoToMap(`
@media (prefers-color-scheme: dark) { gitea-theme-meta-info { --k1: foo; } }
@media (prefers-color-scheme: dark) { gitea-theme-meta-info { --k1: bar; } }
gitea-theme-meta-info {
--k2: real;
}`)
assert.Equal(t, map[string]string{"--k2": "real"}, m)
} }