-
Notifications
You must be signed in to change notification settings - Fork 4
/
web_themes.go
207 lines (171 loc) · 4.97 KB
/
web_themes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2021-2023 Leonid Maslakov.
// This file is part of Lenpaste.
// Lenpaste is free software: you can redistribute it
// and/or modify it under the terms of the
// GNU Affero Public License as published by the
// Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// Lenpaste is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero Public License for more details.
// You should have received a copy of the GNU Affero Public License along with Lenpaste.
// If not, see <https://www.gnu.org/licenses/>.
package web
import (
"bytes"
"errors"
"fmt"
"html/template"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
)
const baseTheme = "dark"
const embThemesDir = "data/theme"
type Theme map[string]string
type Themes map[string]Theme
type ThemesListPart map[string]string
type ThemesList map[string]ThemesListPart
func loadThemes(hostThemeDir string, localesList LocalesList, defaultTheme string) (Themes, ThemesList, error) {
themes := make(Themes)
themesList := make(ThemesList)
for localeCode, _ := range localesList {
themesList[localeCode] = make(ThemesListPart)
}
// Prepare load FS function
loadThemesFromFS := func(f fs.FS, themeDir string) error {
// Get theme files list
files, err := fs.ReadDir(f, themeDir)
if err != nil {
return errors.New("web: failed read dir '" + themeDir + "': " + err.Error())
}
for _, fileInfo := range files {
// Check file
if fileInfo.IsDir() {
continue
}
fileName := fileInfo.Name()
if strings.HasSuffix(fileName, ".theme") == false {
continue
}
themeCode := fileName[:len(fileName)-6]
// Read file
filePath := filepath.Join(themeDir, fileName)
fileByte, err := fs.ReadFile(f, filePath)
if err != nil {
return errors.New("web: failed open file '" + filePath + "': " + err.Error())
}
fileStr := bytes.NewBuffer(fileByte).String()
// Load theme
theme, err := readKVCfg(fileStr)
if err != nil {
return errors.New("web: failed read file '" + filePath + "': " + err.Error())
}
_, themeExist := themes[themeCode]
if themeExist {
return errors.New("web: theme alredy loaded: " + filePath)
}
themes[themeCode] = Theme(theme)
}
return nil
}
// Load embed themes
err := loadThemesFromFS(embFS, embThemesDir)
if err != nil {
return nil, nil, err
}
// Load external themes
if hostThemeDir != "" {
err = loadThemesFromFS(os.DirFS(hostThemeDir), ".")
if err != nil {
return nil, nil, err
}
}
// Prepare themes list
for key, val := range themes {
// Get theme name
themeName := val["theme.Name."+baseLocale]
if themeName == "" {
return nil, nil, errors.New("web: empty theme.Name." + baseLocale + " parameter in '" + key + "' theme")
}
// Append to the translation, if it is not complete
defTheme := themes[baseTheme]
defTotal := len(defTheme)
curTotal := 0
for defKey, defVal := range defTheme {
_, isExist := val[defKey]
if isExist {
curTotal = curTotal + 1
} else {
if strings.HasPrefix(defKey, "theme.Name.") {
val[defKey] = val["theme.Name."+baseLocale]
} else {
val[defKey] = defVal
}
}
}
if curTotal == 0 {
return nil, nil, errors.New("web: theme '" + key + "' is empty")
}
// Add theme to themes list
themeNameSuffix := ""
if curTotal != defTotal {
themeNameSuffix = fmt.Sprintf(" (%.2f%%)", (float32(curTotal)/float32(defTotal))*100)
}
themesList[baseLocale][key] = themeName + themeNameSuffix
for localeCode, _ := range localesList {
result, ok := val["theme.Name."+localeCode]
if ok {
themesList[localeCode][key] = result + themeNameSuffix
} else {
themesList[localeCode][key] = themeName + themeNameSuffix
}
}
}
// Check default theme exist
_, ok := themes[defaultTheme]
if ok == false {
return nil, nil, errors.New("web: default theme '" + defaultTheme + "' not found")
}
return themes, themesList, nil
}
func (themesList ThemesList) getForLocale(req *http.Request) ThemesListPart {
// Get theme by cookie
langCookie := getCookie(req, "lang")
if langCookie != "" {
theme, ok := themesList[langCookie]
if ok == true {
return theme
}
}
// Load default part theme
theme, _ := themesList[baseLocale]
return theme
}
func (themes Themes) findTheme(req *http.Request, defaultTheme string) Theme {
// Get theme by cookie
themeCookie := getCookie(req, "theme")
if themeCookie != "" {
theme, ok := themes[themeCookie]
if ok == true {
return theme
}
}
// Load default theme
theme, _ := themes[defaultTheme]
return theme
}
func (theme Theme) theme(s string) string {
for key, val := range theme {
if key == s {
return val
}
}
panic(errors.New("web: theme: unknown theme key: " + s))
}
func (theme Theme) tryHighlight(source string, lexer string) template.HTML {
return tryHighlight(source, lexer, theme.theme("highlight.Theme"))
}