-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
310 lines (259 loc) · 5.41 KB
/
types.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"cloud.google.com/go/civil"
"fmt"
"regexp"
"strings"
"time"
)
type appConfig struct {
siteName string
theme string
homePage string
pageSize int
useThumbs bool
thumbSizes []int
thumbThreshold float64
serveHost string
servePort int
}
type appCommandDescriptor struct {
command string
description string
usage string
reqConfig bool
reqArgCnt int
optArgCnt int
}
type appCommand func(config appConfig, commandArgs ...string)
type templateIncludeType int
const (
UndefinedTemplateIncludeType templateIncludeType = iota
Template
Content
)
func (t templateIncludeType) String() string {
switch t {
case Template:
return "Template"
case Content:
return "Content"
}
panic("Undefined Template Include Type")
}
type templateIncludeLevel int
const (
UndefinedTemplateIncludeLevel templateIncludeLevel = iota
Global
Theme
)
var templateIncludeLevels = /* const */ []templateIncludeLevel{Global, Theme}
func (t templateIncludeLevel) String() string {
switch t {
case Global:
return "global"
case Theme:
return "theme"
}
panic("Unknown Template Include Level")
}
type templateInclude struct {
includeType templateIncludeType
placeholder string
fileName string
}
type contentEntityType int
const (
UndefinedContentEntityType contentEntityType = iota
Page
Post
)
func (c contentEntityType) Page() bool {
return c == Page
}
func (c contentEntityType) Post() bool {
return c == Post
}
func (c contentEntityType) String() string {
switch c {
case Page:
return "Page"
case Post:
return "Post"
}
panic("Undefined Content Entity Type")
}
type templateContent struct {
EntityType contentEntityType
Title string
FileName string
Content any
}
type contentDirectiveData struct {
Text string
Media []media
Embed []embeddedMedia
Props map[string]string
}
func (c contentDirectiveData) Images() []media {
return c.filterMediaByType(Image)
}
func (c contentDirectiveData) Videos() []media {
return c.filterMediaByType(Video)
}
func (c contentDirectiveData) filterMediaByType(mType mediaType) []media {
var mList []media
for _, m := range c.Media {
if m.Type == mType {
mList = append(mList, m)
}
}
return mList
}
type pagerData struct {
CurrPageNum int
TotalPageCnt int
PageUriPrefix string
IndexPageUri string
}
type embeddedMediaType int
const (
UndefinedEmbeddedMediaType embeddedMediaType = iota
YouTube
Vimeo
)
var embeddedMediaTypes = /* const */ []embeddedMediaType{YouTube, Vimeo}
var embeddedMediaTypeRegexp = /* const */ map[embeddedMediaType]*regexp.Regexp{
YouTube: regexp.MustCompile(`(?i)(youtu\.be/|youtube\.com/watch\?v=)([\w_-]+)`),
Vimeo: regexp.MustCompile(`(?i)vimeo\.com/([\w_-]+)`),
}
func (c embeddedMediaType) String() string {
switch c {
case YouTube:
return "youtube"
case Vimeo:
return "vimeo"
}
panic("Undefined Embedded Media Type")
}
func (c embeddedMediaType) getCode(url string) string {
var codeGroup int
switch c {
case YouTube:
codeGroup = 2
case Vimeo:
codeGroup = 1
default:
panic("Undefined Embedded Media Type")
}
emm := embeddedMediaTypeRegexp[c].FindStringSubmatch(url)
if emm != nil {
return emm[codeGroup]
}
return ""
}
type embeddedMedia struct {
MediaType embeddedMediaType
Code string
}
type mediaType int
const (
UndefinedMediaType mediaType = iota
Image
Video
)
func (m mediaType) Image() bool {
return m == Image
}
func (m mediaType) Video() bool {
return m == Video
}
type thumb struct {
Uri string
Size int
}
type media struct {
Type mediaType
Uri string
thumbs []thumb
}
func (m media) SrcSet() string {
minThumbSize := defaultThumbSizes[0]
maxThumbSize := defaultThumbSizes[len(defaultThumbSizes)-1]
var srcSet []string
for _, thumb := range m.thumbs {
srcSet = append(srcSet, fmt.Sprintf("%s %dw", thumb.Uri, thumb.Size))
if thumb.Size < minThumbSize {
minThumbSize = thumb.Size
}
if thumb.Size > maxThumbSize {
maxThumbSize = thumb.Size
}
}
srcSet = append(srcSet, fmt.Sprintf("%s %dw", m.Uri, maxThumbSize+(minThumbSize+maxThumbSize)/2))
return strings.Join(srcSet, ", ")
}
func (m media) ThumbUri(sizeIdx int) string {
return m.thumbs[sizeIdx-1].Uri
}
type page struct {
id string
Title string
Body string
Media []media
}
type post struct {
id string
Date civil.Date
Time civil.Time
Title string
Body string
Tags []string
}
func (p post) HasDateOrTime() bool {
return !p.Date.IsZero() || !p.Time.IsZero()
}
func (p post) FmtDate() string {
if !p.Date.IsZero() {
return p.Date.String()
}
return ""
}
func (p post) FmtTime() string {
if !p.Time.IsZero() {
time := p.Time.String()
if strings.HasSuffix(time, ":00") {
time, _ = strings.CutSuffix(time, ":00")
}
return time
}
return ""
}
type archiveIndexData struct {
YearData []archiveYearData
}
type archiveYearData struct {
Year int
MonthData []archiveMonthData
}
type archiveMonthData struct {
Month time.Month
PostCnt int
}
type tuple2[T1, T2 any] struct {
V1 T1
V2 T2
}
type resourceLoader struct {
config appConfig
loadTemplate func(templateFileName string) ([]byte, error)
loadInclude func(includeFileName string, level templateIncludeLevel) ([]byte, error)
}
type stats struct {
pageCnt int
postCnt int
tagCnt int
genCnt int
}
type processorOutputHandler func(outputFilePath string, data []byte) bool
type imageThumbnailHandler func(imgDirPath string, config appConfig)