Skip to content

Commit

Permalink
update doc, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed May 8, 2024
1 parent d941875 commit fdfd440
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/content/administration/customizing-gitea.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
gitea-theme-meta-info {
Expand Down
16 changes: 12 additions & 4 deletions services/webtheme/webtheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
(
\s*(--[-\w]+)
\s*:
\s*("(\\"|[^"])*")
\s*(
("(\\"|[^"])*")
|('(\\'|[^'])*')
|([^'";]+)
)
\s*;
\s*
)
Expand All @@ -66,9 +70,13 @@ func parseThemeMetaInfoToMap(cssContent string) map[string]string {
m := map[string]string{}
for _, item := range matchedItems {
v := item[3]
v = strings.TrimPrefix(v, "\"")
v = strings.TrimSuffix(v, "\"")
v = strings.ReplaceAll(v, `\"`, `"`)
if strings.HasPrefix(v, `"`) {
v = strings.TrimSuffix(strings.TrimPrefix(v, `"`), `"`)
v = strings.ReplaceAll(v, `\"`, `"`)
} else if strings.HasPrefix(v, `'`) {
v = strings.TrimSuffix(strings.TrimPrefix(v, `'`), `'`)
v = strings.ReplaceAll(v, `\'`, `'`)
}
m[item[2]] = v
}
return m
Expand Down
26 changes: 24 additions & 2 deletions services/webtheme/webtheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import (
)

func TestParseThemeMetaInfo(t *testing.T) {
m := parseThemeMetaInfoToMap(`gitea-theme-meta-info { --k1: "v1"; --k2: "a\"b"; }`)
assert.Equal(t, map[string]string{"--k1": "v1", "--k2": `a"b`}, m)
m := parseThemeMetaInfoToMap(`gitea-theme-meta-info {
--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)
}

0 comments on commit fdfd440

Please sign in to comment.