/
templates.go
206 lines (184 loc) · 5.02 KB
/
templates.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
package main
import (
"fmt"
"html/template"
"reflect"
"strings"
. "github.com/Deleplace/programming-idioms/idioms"
)
var templates, _ = initTemplates()
func initTemplates() (*template.Template, error) {
var err error
t := template.New("programming-idioms")
funcMap := template.FuncMap{
"markup2CSS": markup2CSS,
"mainStreamLanguages": MainStreamLanguages,
"moreLanguages": MoreLanguages,
"allToggleNames": allToggleNames,
"printNiceLang": PrintNiceLang,
"printNiceLangs": PrintNiceLangs,
"prettifyCSSClass": prettifyCSSClass,
"hljsCSSClass": hljsCSSClass,
"prettifyExtension": prettifyExtension,
"normLang": NormLang,
"langBadgeClass": langBadgeClass,
"isInStringList": isInStringList,
"idEqual": idEqual,
"decorate": decorate,
"langCoverageClass": langCoverageClass,
"niceIdiomURL": NiceIdiomURL,
"niceIdiomIDTitleURL": NiceIdiomIDTitleURL,
"niceImplURL": NiceImplURL,
"themeDir": themeDirectory,
"hostPrefix": hostPrefix,
"host": host,
"filterOut": FilterOut,
"toggled": isToggled,
"join": strings.Join,
"dict": dict,
"hiddenizeExtraColumns": hiddenizeExtraColumns,
"empty": empty,
"notEmpty": notEmpty,
"blank": isBlank,
"implementationsFor": implementationsFor,
"shorten": Shorten,
"atom2string": atom2string,
"atom2int": atom2int,
"trim": strings.TrimSpace,
"ifval": ifval,
"diffClass": diffClass,
"plus": plus,
"minus": minus,
"hasSuffix": strings.HasSuffix,
"replace": strings.Replace,
"logo": languageLogo,
}
t = t.Funcs(funcMap)
folders := []string{
"template",
"template/page",
"template/page/about",
"template/page/admin",
"template/page/hybrid",
"template/ajax-block",
"template/content/widget",
"template/content/block",
"template/header",
"template/footer",
}
for _, f := range folders {
t, err = t.ParseGlob(f + "/*.html")
if err != nil {
return nil, err
}
}
return t, nil
}
func isInStringList(lang string, favlangs []string) bool {
return StringSliceContains(favlangs, lang)
}
func idEqual(a int, b int) bool {
return a == b
}
func empty(x interface{}) bool {
v := reflect.ValueOf(x)
return v.Len() == 0
}
func notEmpty(x interface{}) bool {
v := reflect.ValueOf(x)
return v.Len() > 0
}
func isBlank(s string) bool {
return strings.TrimSpace(s) == ""
}
// Directory containing CSS, JS, and pictures.
// Warning : contains a leading slash
// Warning : does not contain a trailing slash
func themeDirectory() string {
if toggles["themeVirtualVersioning"] {
return "/" + ThemeVersion + "_" + ThemeDate
}
return "/" + ThemeVersion
}
func hostPrefix() string {
if toggles["useAbsoluteUrls"] {
return env.Host
}
return ""
}
func host() string {
return env.Host
}
// Poor workaround for proper PageMeta.Toggles
//
// Note this is reading global toggles, it doesn't
// work at all with page custom toggles.
func isToggled(name string) bool {
return toggles[name]
}
// If no favorites, all badges are blue
// If favorite, badge is green
// If there are favorites and this one is not, badge is grey
func langBadgeClass(lang string, favlangs []string) string {
if len(favlangs) > 0 {
if StringSliceContains(favlangs, lang) {
return "badge-fav-lang"
} else {
return "badge-non-fav-lang"
}
} else {
return "badge-lang"
}
}
// If favorite, circle is green
func langCoverageClass(lang string, favlangs []string) string {
if StringSliceContains(favlangs, lang) {
return "coverage-fav-lang"
}
return ""
}
// A "functional if": depending of first (bool) argument,
// return second or third argument.
// Note that all arguments are always evaluated, even if discarded.
func ifval(cond bool, value1, value2 interface{}) interface{} {
if cond {
return value1
}
return value2
}
// diffClass compare arguments to determine some CSS class to apply:
// - empty when equals
// - "touched" when both values are different and non-empty
// - "created" when left is empty and right is non-empty
// - "deleted" when left is non-empty and right is empty
func diffClass(leftArg, rightArg interface{}) string {
switch left := leftArg.(type) {
case string:
right, ok := rightArg.(string)
if !ok {
panic(fmt.Errorf("unexpected right type %T\n", rightArg))
}
// \r\n are pita
left = strings.Replace(left, "\r\n", "\n", -1)
right = strings.Replace(right, "\r\n", "\n", -1)
if left == right {
return ""
}
if left == "" {
return "created"
}
if right == "" {
return "deleted"
}
return "touched"
// TODO other useful types?
default:
panic(fmt.Errorf("unexpected left type %T\n", leftArg))
}
}
func plus(a, b int) int {
return a + b
}
func minus(a, b int) int {
return a - b
}