-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
459 lines (386 loc) · 8.68 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"bytes"
"cloud.google.com/go/civil"
"encoding/json"
"fmt"
"image/png"
"regexp"
"strings"
"time"
)
type pngCompressionLevel string
const (
DefaultCompression pngCompressionLevel = "DefaultCompression"
NoCompression pngCompressionLevel = "NoCompression"
BestSpeed pngCompressionLevel = "BestSpeed"
BestCompression pngCompressionLevel = "BestCompression"
)
func (p pngCompressionLevel) String() string {
return string(p)
}
func pngCompressionLevelFromString(level string) pngCompressionLevel {
switch strings.ToLower(level) {
case strings.ToLower(NoCompression.String()):
return NoCompression
case strings.ToLower(BestSpeed.String()):
return BestSpeed
case strings.ToLower(BestCompression.String()):
return BestCompression
case strings.ToLower(DefaultCompression.String()):
return BestCompression
}
return ""
}
func (p pngCompressionLevel) Value() png.CompressionLevel {
switch p {
case NoCompression:
return png.NoCompression
case BestSpeed:
return png.BestSpeed
case BestCompression:
return png.BestCompression
}
return png.DefaultCompression
}
func pngCompressionLevelStringValues() []string {
return []string{NoCompression.String(), BestSpeed.String(), BestCompression.String(), DefaultCompression.String()}
}
type appConfig struct {
siteName string
theme string
homePage string
generateArchive bool
generateTagIndex bool
enableSearch bool
pageSize int
resizeOrigImages bool
maxImgSize int
useThumbs bool
thumbSizes []int
thumbThreshold float64
jpegQuality int
pngCompressionLevel pngCompressionLevel
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
)
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) MarshalJSON() ([]byte, error) {
return json.Marshal(strings.ToLower(c.String()))
}
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")
}
func contentEntityTypeFromString(typeName string) contentEntityType {
switch strings.ToLower(typeName) {
case strings.ToLower(Page.String()):
return Page
case strings.ToLower(Post.String()):
return Post
}
return UndefinedContentEntityType
}
type templateContent struct {
EntityType contentEntityType
Title string
FileName string
Content any
Config map[string]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 searchData struct {
TypeId string
Content string
}
type contentEntity interface {
ContentEntityType() contentEntityType
EntityId() string
}
type page struct {
Id string
Title string
Body string
Media []media
SearchData searchData
skipProcessing bool
}
func (p page) ContentEntityType() contentEntityType {
return Page
}
func (p page) EntityId() string {
return p.Id
}
type post struct {
Id string
Date civil.Date
Time civil.Time
Title string
Body string
Tags []string
SearchData searchData
skipProcessing bool
}
func (p post) ContentEntityType() contentEntityType {
return Post
}
func (p post) EntityId() string {
return p.Id
}
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() {
t := p.Time.String()
if strings.HasSuffix(t, ":00") {
t, _ = strings.CutSuffix(t, ":00")
}
return t
}
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 mapItem struct {
Key, Value interface{}
}
type mapSlice []mapItem
func (ms mapSlice) MarshalJSON() ([]byte, error) {
buf := &bytes.Buffer{}
buf.Write([]byte{'{'})
for i, mi := range ms {
b, err := json.Marshal(&mi.Value)
if err != nil {
return nil, err
}
buf.WriteString(fmt.Sprintf("%q:", fmt.Sprintf("%v", mi.Key)))
buf.Write(b)
if i < len(ms)-1 {
buf.Write([]byte{','})
}
}
buf.Write([]byte{'}'})
return buf.Bytes(), nil
}
type resourceLoader struct {
config appConfig
loadTemplate func(templateFileName string) ([]byte, error)
loadInclude func(includeFileName string, level templateIncludeLevel) ([]byte, error)
}
type tagData struct {
Title string
Count int
Ratio float64
}
type stats struct {
pageCnt int
postCnt int
tagCnt int
genCnt int
}
type watchReloadData struct {
Type contentEntityType `json:"type"`
Id string `json:"id"`
Op dirWatchOp `json:"op"`
}
type processorOutputHandler func(outputFilePath string, data []byte) bool
type imageThumbnailHandler func(mediaDirPath string, config appConfig)
type dirWatchOp string
const (
dirWatchOpCreate dirWatchOp = "create"
dirWatchOpUpdate dirWatchOp = "update"
dirWatchOpRename dirWatchOp = "rename"
dirWatchOpDelete dirWatchOp = "delete"
)
type dirWatchEvent struct {
filePath string
originalFilePath *string
op dirWatchOp
}
type dirWatchHandler func(dwEvent dirWatchEvent)