Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 24, 2024
1 parent 1a2ae64 commit 247084f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
50 changes: 50 additions & 0 deletions services/webtheme/webtheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package webtheme

import (
"regexp"
"sort"
"strings"
"sync"
Expand All @@ -20,6 +21,55 @@ var (
themeOnce sync.Once
)

type ThemeMetaInfo struct {
ThemeFileName string
DisplayName string
PreferColorSchemes []string
}

func parseThemeMetaInfoToMap(cssContent string) map[string]string {
reMetaInfoItem := `
(
\s*(--[-\w]+)
\s*:
\s*("(\\"|[^"])*")
\s*;
\s*
)
`
reMetaInfoItem = strings.ReplaceAll(reMetaInfoItem, "\n", "")
reMetaInfoBlock := `\bgitea-theme-meta-info\s*\{(` + reMetaInfoItem + `+)\}`
re := regexp.MustCompile(reMetaInfoBlock)
matchedMetaInfoBlock := re.FindAllStringSubmatch(cssContent, -1)
if len(matchedMetaInfoBlock) == 0 {
return nil
}
re = regexp.MustCompile(strings.ReplaceAll(reMetaInfoItem, "\n", ""))
matchedItems := re.FindAllStringSubmatch(matchedMetaInfoBlock[0][1], -1)
m := map[string]string{}
for _, item := range matchedItems {
v := item[3]
v = strings.TrimPrefix(v, "\"")
v = strings.TrimSuffix(v, "\"")
v = strings.ReplaceAll(v, `\"`, `"`)
m[item[2]] = v
}
return m
}

func parseThemeMetaInfo(fileName, cssContent string) *ThemeMetaInfo {

Check failure on line 60 in services/webtheme/webtheme.go

View workflow job for this annotation

GitHub Actions / lint-backend

func `parseThemeMetaInfo` is unused (unused)

Check failure on line 60 in services/webtheme/webtheme.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

func `parseThemeMetaInfo` is unused (unused)

Check failure on line 60 in services/webtheme/webtheme.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

func `parseThemeMetaInfo` is unused (unused)
themeInfo := &ThemeMetaInfo{
ThemeFileName: fileName,
DisplayName: strings.TrimSuffix(strings.TrimPrefix(fileName, "theme-"), ".css"),
}
m := parseThemeMetaInfoToMap(cssContent)
if m == nil {
return themeInfo
}
themeInfo.DisplayName = m["--theme-display-name"]
return themeInfo
}

func initThemes() {
availableThemes = nil
defer func() {
Expand Down
12 changes: 12 additions & 0 deletions services/webtheme/webtheme_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package webtheme

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseThemeMetaInfoToMap(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)
}
4 changes: 4 additions & 0 deletions web_src/css/themes/theme-gitea-auto.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
@import "./theme-gitea-light.css" (prefers-color-scheme: light);
@import "./theme-gitea-dark.css" (prefers-color-scheme: dark);

gitea-theme-meta-info {
--theme-display-name: 'Auto';
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@import "./theme-gitea-dark.css";

gitea-theme-meta-info {
--theme-display-name: "Dark (Red/Green Colorblind-Friendly)";
}

/* red/green colorblind-friendly colors */
/* from GitHub: --diffBlob-addition-*, --diffBlob-deletion-*, etc */
:root {
Expand Down
4 changes: 4 additions & 0 deletions web_src/css/themes/theme-gitea-dark.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@import "../chroma/dark.css";
@import "../codemirror/dark.css";

gitea-theme-meta-info {
--theme-display-name: "Dark";
}

:root {
--is-dark-theme: true;
--color-primary: #4183c4;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@import "./theme-gitea-light.css";

gitea-theme-meta-info {
--theme-display-name: "Light (Red/Green Colorblind-Friendly)";
}

/* red/green colorblind-friendly colors */
/* from GitHub: --diffBlob-addition-*, --diffBlob-deletion-*, etc */
:root {
Expand Down
4 changes: 4 additions & 0 deletions web_src/css/themes/theme-gitea-light.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@import "../chroma/light.css";
@import "../codemirror/light.css";

gitea-theme-meta-info {
--theme-display-name: "Light";
}

:root {
--is-dark-theme: false;
--color-primary: #4183c4;
Expand Down

0 comments on commit 247084f

Please sign in to comment.