|
4 | 4 | package webtheme
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "regexp" |
7 | 8 | "sort"
|
8 | 9 | "strings"
|
9 | 10 | "sync"
|
|
20 | 21 | themeOnce sync.Once
|
21 | 22 | )
|
22 | 23 |
|
| 24 | +type ThemeMetaInfo struct { |
| 25 | + ThemeFileName string |
| 26 | + DisplayName string |
| 27 | + PreferColorSchemes []string |
| 28 | +} |
| 29 | + |
| 30 | +func parseThemeMetaInfoToMap(cssContent string) map[string]string { |
| 31 | + reMetaInfoItem := ` |
| 32 | +( |
| 33 | +\s*(--[-\w]+) |
| 34 | +\s*: |
| 35 | +\s*("(\\"|[^"])*") |
| 36 | +\s*; |
| 37 | +\s* |
| 38 | +) |
| 39 | +` |
| 40 | + reMetaInfoItem = strings.ReplaceAll(reMetaInfoItem, "\n", "") |
| 41 | + reMetaInfoBlock := `\bgitea-theme-meta-info\s*\{(` + reMetaInfoItem + `+)\}` |
| 42 | + re := regexp.MustCompile(reMetaInfoBlock) |
| 43 | + matchedMetaInfoBlock := re.FindAllStringSubmatch(cssContent, -1) |
| 44 | + if len(matchedMetaInfoBlock) == 0 { |
| 45 | + return nil |
| 46 | + } |
| 47 | + re = regexp.MustCompile(strings.ReplaceAll(reMetaInfoItem, "\n", "")) |
| 48 | + matchedItems := re.FindAllStringSubmatch(matchedMetaInfoBlock[0][1], -1) |
| 49 | + m := map[string]string{} |
| 50 | + for _, item := range matchedItems { |
| 51 | + v := item[3] |
| 52 | + v = strings.TrimPrefix(v, "\"") |
| 53 | + v = strings.TrimSuffix(v, "\"") |
| 54 | + v = strings.ReplaceAll(v, `\"`, `"`) |
| 55 | + m[item[2]] = v |
| 56 | + } |
| 57 | + return m |
| 58 | +} |
| 59 | + |
| 60 | +func parseThemeMetaInfo(fileName, cssContent string) *ThemeMetaInfo { |
| 61 | + themeInfo := &ThemeMetaInfo{ |
| 62 | + ThemeFileName: fileName, |
| 63 | + DisplayName: strings.TrimSuffix(strings.TrimPrefix(fileName, "theme-"), ".css"), |
| 64 | + } |
| 65 | + m := parseThemeMetaInfoToMap(cssContent) |
| 66 | + if m == nil { |
| 67 | + return themeInfo |
| 68 | + } |
| 69 | + themeInfo.DisplayName = m["--theme-display-name"] |
| 70 | + return themeInfo |
| 71 | +} |
| 72 | + |
23 | 73 | func initThemes() {
|
24 | 74 | availableThemes = nil
|
25 | 75 | defer func() {
|
|
0 commit comments