This repository has been archived by the owner on Sep 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
170 lines (158 loc) · 4.49 KB
/
template.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
package main
import (
"bufio"
"bytes"
"strings"
"text/template"
"gopkg.in/russross/blackfriday.v2"
)
// Helper function to capitalize the first character of a string
func capitalizeFirst(s string) string {
if len(s) > 1 {
return strings.ToUpper(string(s[0])) + s[1:]
} else if len(s) == 1 {
return strings.ToUpper(string(s[0]))
} else {
return ""
}
}
// Helper function to split a string on an old delimeter and re-join the string
// slice with a new one.
func rejoin(input, old, new string) string {
input = strings.TrimRight(input, old)
tmp := strings.Split(input, old)
return strings.Join(tmp, new)
}
func funcIndent(count int, text string) string {
var buf bytes.Buffer
prefix := strings.Repeat(" ", count)
scan := bufio.NewScanner(strings.NewReader(text))
for scan.Scan() {
buf.WriteString(prefix + scan.Text() + "\n")
}
return strings.TrimRight(buf.String(), "\n")
}
// Send functions to the template rendering engine
var funcMap = template.FuncMap{
"inc": func(i int) int {
return i + 1
},
"indent": funcIndent,
"join": func(arr []string, sep string) string {
return strings.Join(arr, sep)
},
"blackfriday": func(input string) string {
return string(blackfriday.Run([]byte(input)))
},
"rejoin": rejoin,
}
// Templates is the global templates used to render API results.
var Templates = map[string]*template.Template{
"definition": template.Must(template.New("definition").Funcs(funcMap).Parse(`
{{- (index .Results 0).RenderTitle }}
{{ range .Results -}}
{{ range .LexicalEntries -}}
{{ .RenderLexicalCategory }}:
{{ if len .Pronunciations }}{{ range .Pronunciations }}- {{ .String }}
{{ end }}
{{ end -}}
{{ range .Entries -}}
{{ range $i, $sense := .Senses -}}
{{inc $i}}. {{if .Tags }}{{ .RenderTags }} {{end}}{{ .RenderDefinitions }}
{{ range .Examples }} - {{ .Render }}
{{ end }}
{{- if .Subsenses }}
{{ range $j, $subsense := .Subsenses }} {{inc $j}}. {{if .Tags }}{{ .RenderTags }} {{end}}{{ .RenderDefinitions }}
{{ range .Examples }} - {{ .Render }}
{{ end }}
{{ end -}}{{ else }}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"definition-simple": template.Must(template.New("definition-simple").Funcs(funcMap).Parse(`
{{- range .LexicalEntries -}}
{{ range .Entries -}}
{{ range .Senses -}}
{{ .RenderDefinitions }}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"definition-short": template.Must(template.New("definition-short").Funcs(funcMap).Parse(`
{{- .Word }}{{ with index .LexicalEntries 0 -}}
{{ if .Pronunciations }} · {{ index .Pronunciations 0 }}{{else -}}{{end}}
{{ with index .Entries 0 }}{{ with index .Senses 0 }}{{.RenderDefinitions}}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"synonyms": template.Must(template.New("synonym").Funcs(funcMap).Parse(`
{{- .RenderTitle }}
{{ range .LexicalEntries -}}
{{ .RenderLexicalCategory }}:
{{ range .Entries }}
{{ range $i, $sense := .Senses }}{{inc $i}}. {{ rejoin .RenderExamples "\n" ", " }}
{{- if .HasSynonyms }}
- {{ .RenderTags "informal" }}{{ .RenderSynonyms }}
{{- end -}}
{{- if .Subsenses }}
{{ range .Subsenses -}}
{{- if .HasSynonyms }} - {{if .Tags "informal"}}{{ .RenderTags "informal" }} {{end}}{{ .RenderSynonyms }}
{{ end -}}
{{ end -}}{{ else }}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"synonyms-simple": template.Must(template.New("synonyms-simple").Funcs(funcMap).Parse(`
{{- range .LexicalEntries -}}
{{ range .Entries -}}
{{ range .Senses -}}
{{- if .HasSynonyms }}{{ .RenderSynonyms }}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"antonyms": template.Must(template.New("antonym").Funcs(funcMap).Parse(`
{{- .RenderTitle }}
{{ range .LexicalEntries -}}
{{ .RenderLexicalCategory }}:
{{ range .Entries }}
{{ range $i, $sense := .Senses }}{{inc $i}}. {{ rejoin .RenderExamples "\n" ", " }}
{{- if .HasAntonyms }}
- {{ .RenderTags "informal" }}{{ .RenderAntonyms }}
{{- end -}}
{{- if .Subsenses }}
{{ range .Subsenses -}}
{{- if .HasAntonyms }} - {{if .Tags "informal"}}{{ .RenderTags "informal" }} {{end}}{{ .RenderAntonyms }}
{{ end -}}
{{ end -}}{{ else }}
{{ end }}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"antonyms-simple": template.Must(template.New("antonyms-simple").Funcs(funcMap).Parse(`
{{- range .LexicalEntries -}}
{{ range .Entries -}}
{{ range .Senses -}}
{{- if .HasAntonyms }}{{ .RenderAntonyms }}
{{ end -}}
{{ end -}}
{{ end -}}
{{ end -}}
`)),
"webpage": template.Must(template.New("webpage").Funcs(funcMap).Parse(`<!DOCTYPE html>
<html lang="en">
{{ if .Title}}<title>{{.Title}}</title>
{{ end -}}
<style>html{font-family:sans-serif}</style>
{{ blackfriday .Content -}}
</html>
`)),
}