/
res.go
234 lines (197 loc) · 7.17 KB
/
res.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package server
import (
"time"
"golang.org/x/text/language"
"go101.org/gold/code"
theme "go101.org/gold/internal/server/themes"
translation "go101.org/gold/internal/server/translations"
)
type Theme interface {
Name() string
CSS() string
}
type Translation interface {
Name() string
LangTag() string
// common
Text_Space() string
Text_Comma() string
Text_Colon(tailSpace bool) string
Text_Period(paragraphEnd bool) string
Text_EnclosedInOarentheses(text string) string
Text_PreferredFontList() string
Text_BlankList() string
// server
Text_Server_Started() string
// analyzing
Text_Analyzing() string
Text_AnalyzingRefresh(currentPageURL string) string // also used in other pages
Text_Analyzing_Start() string
Text_Analyzing_Done(d time.Duration, memoryUse string) string
Text_Analyzing_PreparationDone(d time.Duration) string // ToDo: merge these into one?
Text_Analyzing_NFilesParsed(numFiles int, d time.Duration) string
Text_Analyzing_ParsePackagesDone(numFiles int, d time.Duration) string
Text_Analyzing_CollectPackages(numPkgs int, d time.Duration) string
Text_Analyzing_SortPackagesByDependencies(d time.Duration) string
Text_Analyzing_CollectDeclarations(d time.Duration) string
Text_Analyzing_CollectRuntimeFunctionPositions(d time.Duration) string
Text_Analyzing_FindTypeSources(d time.Duration) string
Text_Analyzing_CollectSelectors(d time.Duration) string
Text_Analyzing_FindImplementations(d time.Duration) string
Text_Analyzing_RegisterInterfaceMethodsForTypes(d time.Duration) string
Text_Analyzing_MakeStatistics(d time.Duration) string
Text_Analyzing_CollectSourceFiles(d time.Duration) string
Text_Analyzing_CollectObjectReferences(d time.Duration) string
Text_Analyzing_CacheSourceFiles(d time.Duration) string
// overview page
Text_Overview() string
Text_PackageList() string
Text_StatisticsWithMoreLink(detailedStatsLink string) string
Text_SimpleStats(stats *code.Stats) string
Text_Modules() string // to use
Text_BelongingModule() string // to use
Text_RequireStat(numRequires, numRequiredBys int) string // to use
Text_UpdateTip(tipName string) string // tip names: "ToUpdate", "Updating", "Updated"
Text_SortBy() string // also used in other pages
Text_Filter() string // also used in other pages
Text_SortByItem(by string) string // also used in other pages
Text_FilterItem(fltr string) string // also used in other pages
// package details page
Text_Package(pkgPath string) string
Text_BelongingPackage() string // also used in source code page
Text_PackageDocsLinksOnOtherWebsites(pkgPath string, isStdPkg bool) string
Text_ImportPath() string
Text_ImportStat(numImports, numImportedBys int, depPageURL string) string
Text_InvolvedFiles(num int) string
Text_ExportedValues(num int) string
Text_ExportedTypeNames(num int) string
Text_AllPackageLevelTypeNames(num int) string
Text_TypeNameListShowOption(exportedsOnly bool) string
Text_Fields(num int, exportedsOnly bool) string // ToDo: merge these into one?
Text_Methods(num int, exportedsOnly bool) string
Text_ImplementedBy(num int) string
Text_Implements(num int) string
Text_AsOutputsOf(num int) string
Text_AsInputsOf(num int) string
Text_AsTypesOf(num int) string
Text_References(num int) string
// package dependencies page
Text_DependencyRelations(pkgPath string) string // also used in package details page with a blank argument.
Text_Imports() string
Text_ImportedBy() string
// method implementation page
Text_MethodImplementations() string
Text_NumMethodsImplementingNothing(count int) string
// object references(uses) page
Text_ReferenceList() string
Text_ObjectKind(kind string) string
Text_ObjectUses(num int) string // also used in other pages
// source code page
Text_SourceCode(pkgPath, bareFilename string) string
Text_SourceFilePath() string
Text_GeneratedFrom() string
// statistics
Text_Statistics() string
Text_ChartTitle(chartName string) string
Text_StatisticsTitle(titleName string) string
Text_PackageStatistics(values map[string]interface{}) string
Text_TypeStatistics(values map[string]interface{}) string
Text_ValueStatistics(values map[string]interface{}) string
Text_Othertatistics(values map[string]interface{}) string
// Footer
Text_GeneratedPageFooter(goldVersion, qrCodeLink, goOS, goArch string) string
}
func (ds *docServer) currentSettings() (Theme, Translation) {
ds.mutex.Lock()
defer ds.mutex.Unlock()
return ds.currentTheme, ds.currentTranslation
}
func (ds *docServer) changeSettings(themeName string, langTags ...string) {
ds.mutex.Lock()
defer ds.mutex.Unlock()
if themeName != "" {
ds.currentTheme = ds.themeByName(themeName)
}
if len(langTags) > 0 {
ds.currentTranslation = ds.translationByLangs(langTags...)
}
}
func (ds *docServer) changeTranslationByAcceptLanguage(acceptedLanguage string) {
langTags, _, _ := language.ParseAcceptLanguage(acceptedLanguage)
ds.mutex.Lock()
defer ds.mutex.Unlock()
ds.currentTranslation = ds.translationByLangTags(langTags...)
}
// All themes and translations must be registered at init phase,
// so that no syncrhomization is needed.
func (ds *docServer) initSettings(lang string) {
var (
themes = make([]Theme, 0, 2)
translations = make([]Translation, 0, 6)
langTags = make([]language.Tag, 0, len(translations)*2)
translations2 = make([]Translation, 0, len(translations)*2)
)
registerTheme := func(theme Theme) {
themes = append(themes, theme)
}
registerTranslation := func(tr Translation) {
translations = append(translations, tr)
tag := language.Make(tr.LangTag())
langTags = append(langTags, tag)
translations2 = append(translations2, tr)
}
registerTheme(&theme.Light{})
registerTranslation(&translation.English{})
registerTranslation(&translation.Chinese{})
ds.mutex.Lock()
defer ds.mutex.Unlock()
ds.allThemes = themes
ds.allTranslations = translations
ds.langMatcher = language.NewMatcher(langTags)
ds.translationsByLangTagIndex = translations2
ds.currentTheme = ds.allThemes[0]
ds.currentTranslation = ds.allTranslations[0]
ds.currentTranslation = ds.translationByLangs(lang)
}
func (ds *docServer) currentTranslationSafely() Translation {
return ds.currentTranslation
}
func (ds *docServer) themeByName(name string) Theme {
theme := ds.allThemes[0]
for _, t := range ds.allThemes[1:] {
if t.Name() == name {
theme = t
break
}
}
return theme
}
func (ds *docServer) translationByName(name string) Translation {
trans := ds.allTranslations[0]
for _, tr := range ds.allTranslations[1:] {
if tr.Name() == name {
trans = tr
break
}
}
return trans
}
func (ds *docServer) translationByLangs(langs ...string) Translation {
userPrefs := make([]language.Tag, 0, len(langs))
for _, l := range langs {
if l != "" {
userPrefs = append(userPrefs, language.Make(l))
}
}
return ds.translationByLangTags(userPrefs...)
}
func (ds *docServer) translationByLangTags(userPrefs ...language.Tag) Translation {
if len(userPrefs) == 0 {
return ds.currentTranslation
}
_, index, confidence := ds.langMatcher.Match(userPrefs...)
if confidence == language.No {
return ds.allTranslations[0]
}
return ds.translationsByLangTagIndex[index]
}