-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
514 lines (490 loc) · 16.5 KB
/
parser.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package main
import (
"bytes"
"cloud.google.com/go/civil"
"fmt"
"github.com/google/uuid"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"os"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
)
var mainTemplateMarkup /* const */ string
var markdown = /* const */ goldmark.New(
goldmark.WithExtensions(
meta.Meta,
extension.Strikethrough,
extension.DefinitionList,
extension.Table,
extension.Linkify,
// TODO: support an option to include extension.CJK (?)
),
goldmark.WithRendererOptions(
html.WithHardWraps(),
),
)
func parsePages(config appConfig, resLoader resourceLoader, thumbHandler imageThumbnailHandler, useCache bool) []page {
if !dirExists(markdownPagesDirName) {
return nil
}
markdownPageDirEntries, err := os.ReadDir(markdownPagesDirName)
check(err)
if len(markdownPageDirEntries) == 0 {
return nil
}
sprintln(" - parsing pages ...")
var pages []page
for _, pageEntry := range markdownPageDirEntries {
pageEntryInfo, err := pageEntry.Info()
check(err)
if !pageEntryInfo.IsDir() {
pageEntryFileName := pageEntryInfo.Name()
pageEntryModTime := pageEntryInfo.ModTime()
if useCache {
ce := getContentEntityFromCache(Page, pageEntryFileName, pageEntryModTime)
if ce != nil {
page := ce.(page)
page.skipProcessing = true
pages = append(pages, page)
continue
}
}
pageEntryPath := fmt.Sprintf("%s%c%s", markdownPagesDirName, os.PathSeparator, pageEntryFileName)
content, err := os.ReadFile(pageEntryPath)
check(err)
pageId := pageEntryFileName[:len(pageEntryFileName)-len(filepath.Ext(pageEntryFileName))]
pageMediaDirPath := fmt.Sprintf("%s%c%s%c%s%c%s", deployDirName, os.PathSeparator, mediaDirName, os.PathSeparator, deployPageDirName, os.PathSeparator, pageId)
handleThumbnails(pageMediaDirPath, config, thumbHandler)
page := parsePage(pageId, string(content), config, resLoader)
if useCache {
addContentEntityToCache(pageEntryFileName, pageEntryModTime, page)
}
pages = append(pages, page)
}
}
return pages
}
func parsePosts(config appConfig, resLoader resourceLoader, thumbHandler imageThumbnailHandler, useCache bool) []post {
if !dirExists(markdownPostsDirName) {
return nil
}
markdownPostDirEntries, err := os.ReadDir(markdownPostsDirName)
check(err)
if len(markdownPostDirEntries) == 0 {
return nil
}
sort.Slice(markdownPostDirEntries, func(i, j int) bool {
return markdownPostDirEntries[i].Name() > markdownPostDirEntries[j].Name()
})
sprintln(" - parsing posts ...")
var posts []post
for _, postEntry := range markdownPostDirEntries {
postEntryInfo, err := postEntry.Info()
check(err)
if !postEntryInfo.IsDir() {
postEntryFileName := postEntryInfo.Name()
postEntryModTime := postEntryInfo.ModTime()
if useCache {
ce := getContentEntityFromCache(Post, postEntryFileName, postEntryModTime)
if ce != nil {
post := ce.(post)
post.skipProcessing = true
posts = append(posts, post)
continue
}
}
postEntryPath := fmt.Sprintf("%s%c%s", markdownPostsDirName, os.PathSeparator, postEntryFileName)
content, err := os.ReadFile(postEntryPath)
check(err)
postId := postEntryFileName[:len(postEntryFileName)-len(filepath.Ext(postEntryFileName))]
postMediaDirPath := fmt.Sprintf("%s%c%s%c%s%c%s", deployDirName, os.PathSeparator, mediaDirName, os.PathSeparator, deployPostDirName, os.PathSeparator, postId)
handleThumbnails(postMediaDirPath, config, thumbHandler)
post := parsePost(postId, string(content), config, resLoader)
if useCache {
addContentEntityToCache(postEntryFileName, postEntryModTime, post)
}
posts = append(posts, post)
}
}
return posts
}
func parsePage(pageId string, content string, config appConfig, resLoader resourceLoader) page {
page := page{Id: pageId}
content, rawBodyContent, cdPhReps, _ := parseContentDirectives(Page, pageId, content, config, resLoader)
var buf bytes.Buffer
context := parser.NewContext()
err := markdown.Convert([]byte(content), &buf, parser.WithContext(context))
check(err)
page.Body = strings.TrimSpace(buf.String())
page.Body = handleContentDirectivePlaceholderReplacements(page.Body, cdPhReps)
metaData := meta.Get(context)
rawTitle := ""
if title, ok := metaData[metaDataKeyTitle].(string); ok {
rawTitle = strings.ToLower(title)
if strings.Contains(title, "\n") {
title = strings.Replace(title, "\n", "<br>", -1)
}
page.Title = title
}
page.SearchData = searchData{
TypeId: "page/" + page.Id,
Content: rawTitle + " " + rawBodyContent,
}
return page
}
func parsePost(postId string, content string, config appConfig, resLoader resourceLoader) post {
post := post{Id: postId}
// ================================================================================
// replace metadata tabs with two spaces to avoid markdown parsing issues
// ================================================================================
metadataContent := metaDataPlaceholderRegexp.FindString(content)
if metadataContent != "" {
metadataContent = strings.Replace(metadataContent, "\t", " ", -1)
content = metaDataPlaceholderRegexp.ReplaceAllString(content, metadataContent)
}
// ================================================================================
content, rawBodyContent, cdPhReps, hashTags := parseContentDirectives(Post, postId, content, config, resLoader)
var buf bytes.Buffer
context := parser.NewContext()
err := markdown.Convert([]byte(content), &buf, parser.WithContext(context))
check(err)
post.Body = strings.TrimSpace(buf.String())
post.Body = handleContentDirectivePlaceholderReplacements(post.Body, cdPhReps)
metaData := meta.Get(context)
if date, ok := metaData[metaDataKeyDate].(string); ok {
d, err := civil.ParseDate(date)
check(err)
post.Date = d
}
if time, ok := metaData[metaDataKeyTime].(string); ok {
if len(time) == 5 {
time += ":00"
}
t, err := civil.ParseTime(time)
check(err)
post.Time = t
}
rawTitle := ""
if title, ok := metaData[metaDataKeyTitle].(string); ok {
rawTitle = strings.ToLower(title)
if strings.Contains(title, "\n") {
title = strings.Replace(title, "\n", "<br>", -1)
}
post.Title = title
}
tags := metaData[metaDataKeyTags]
if tags != nil {
ti := tags.([]interface{})
for _, v := range ti {
tag := v.(string)
if !slices.Contains(post.Tags, tag) {
post.Tags = append(post.Tags, tag)
}
}
}
if hashTags != nil {
for _, tag := range hashTags {
if !slices.Contains(post.Tags, tag) {
post.Tags = append(post.Tags, tag)
}
}
}
post.SearchData = searchData{
TypeId: "post/" + post.Id,
Content: rawTitle + " " + rawBodyContent + " " + strings.ToLower(strings.Join(post.Tags[:], " ")),
}
return post
}
func handleThumbnails(mediaDirPath string, config appConfig, thumbHandler imageThumbnailHandler) {
if thumbHandler != nil {
thumbHandler(mediaDirPath, config)
}
}
func parseContentDirectives(ceType contentEntityType, ceId string, content string, config appConfig, resLoader resourceLoader) (string, string, map[string]string, []string) {
rawBodyContent := metaDataPlaceholderRegexp.ReplaceAllString(content, "")
rawBodyContent = contentDirectivePlaceholderRegexp.ReplaceAllString(rawBodyContent, "")
rawBodyContent = whitespacePlaceholderRegexp.ReplaceAllString(rawBodyContent, " ")
rawBodyContent = strings.ToLower(strings.TrimSpace(rawBodyContent))
var phReps map[string]string
var tags []string
hashTagPlaceholders := hashTagRegex.FindAllStringSubmatch(content, -1)
if hashTagPlaceholders != nil {
for _, htp := range hashTagPlaceholders {
placeholder := htp[0]
tag := htp[1]
replacement := fmt.Sprintf(hashTagMarkdownReplacementFormat, tag, strings.ToLower(tag))
content = strings.Replace(content, placeholder, replacement, 1)
tags = append(tags, tag)
}
}
contentLinkPlaceholders := contentLinkPlaceholderRegexp.FindAllStringSubmatch(content, -1)
if contentLinkPlaceholders != nil {
for _, clp := range contentLinkPlaceholders {
placeholder := clp[0]
entityType := strings.ToLower(clp[1])
entryId := clp[2]
var ceType contentEntityType
switch entityType {
case "page":
ceType = Page
case "post":
ceType = Post
}
var link string
if ceType != UndefinedContentEntityType {
link = "/" + strings.ToLower(ceType.String()) + "/" + entryId + contentFileExtension
}
content = strings.Replace(content, placeholder, link, 1)
}
}
var expListMedia []string
wrapPlaceholders := wrapPlaceholderRegexp.FindAllStringSubmatch(content, -1)
if wrapPlaceholders != nil {
sortContentDirectivePlaceholders(wrapPlaceholders)
for _, wp := range wrapPlaceholders {
mediaArg := wp[4]
if mediaArg != "" {
for _, a := range strings.Split(mediaArg, ",") {
m := strings.TrimSpace(a)
expListMedia = append(expListMedia, m)
}
}
}
}
mediaPlaceholders := mediaPlaceholderRegexp.FindAllStringSubmatch(content, -1)
if mediaPlaceholders != nil {
sortContentDirectivePlaceholders(mediaPlaceholders)
for _, mp := range mediaPlaceholders {
mediaArg := mp[3]
if mediaArg != "" {
for _, a := range strings.Split(mediaArg, ",") {
m := strings.TrimSpace(a)
expListMedia = append(expListMedia, m)
}
}
}
}
if wrapPlaceholders != nil {
for _, wp := range wrapPlaceholders {
placeholder := wp[0]
directive := wp[1]
propStr := strings.Trim(wp[2], "()")
mediaArg := wp[4]
text := strings.TrimSpace(wp[5])
var buf bytes.Buffer
err := markdown.Convert([]byte(text), &buf)
check(err)
text = strings.TrimSpace(buf.String())
props := make(map[string]string)
if propStr != "" {
for _, pStr := range strings.Split(propStr, ",") {
prop := strings.Split(strings.TrimSpace(pStr), "=")
key := strings.TrimSpace(prop[0])
val := strings.TrimSpace(prop[1])
props[key] = val
}
}
var mediaFileNames []string
if mediaArg == "" {
mediaFileNames = listAllMedia(ceType, ceId, expListMedia)
} else {
for _, a := range strings.Split(mediaArg, ",") {
m := strings.TrimSpace(a)
mediaFileNames = append(mediaFileNames, m)
}
}
allMedia := parseMediaFileNames(mediaFileNames, ceType, ceId, config)
contentDirectiveTemplate, err := compileContentDirectiveTemplate(directive, resLoader)
if err != nil {
exitWithError(" - failed to process " + directive + " directive for " + ceId + ": " + err.Error())
}
var contentDirectiveMarkupBuffer bytes.Buffer
err = contentDirectiveTemplate.Execute(&contentDirectiveMarkupBuffer, contentDirectiveData{
Text: text,
Media: allMedia,
Props: props,
})
check(err)
ph := fmt.Sprintf(directivePlaceholderReplacementFormat, uuid.New().String())
if phReps == nil {
phReps = make(map[string]string)
}
phReps[ph] = strings.TrimSpace(contentDirectiveMarkupBuffer.String())
content = strings.Replace(content, placeholder, ph, 1)
}
}
if mediaPlaceholders != nil {
for _, mp := range mediaPlaceholders {
placeholder := mp[0]
propStr := strings.Trim(mp[1], "()")
mediaArg := mp[3]
props := make(map[string]string)
if propStr != "" {
for _, pStr := range strings.Split(propStr, ",") {
prop := strings.Split(strings.TrimSpace(pStr), "=")
key := strings.TrimSpace(prop[0])
val := strings.TrimSpace(prop[1])
props[key] = val
}
}
var mediaFileNames []string
if mediaArg == "" {
mediaFileNames = listAllMedia(ceType, ceId, expListMedia)
} else {
for _, a := range strings.Split(mediaArg, ",") {
m := strings.TrimSpace(a)
mediaFileNames = append(mediaFileNames, m)
}
}
allMedia := parseMediaFileNames(mediaFileNames, ceType, ceId, config)
if allMedia != nil {
inlineMediaTemplate := compileMediaTemplate(resLoader)
var inlineMediaMarkupBuffer bytes.Buffer
err := inlineMediaTemplate.Execute(&inlineMediaMarkupBuffer, contentDirectiveData{
Media: allMedia,
Props: props,
})
check(err)
ph := fmt.Sprintf(directivePlaceholderReplacementFormat, uuid.New().String())
if phReps == nil {
phReps = make(map[string]string)
}
phReps[ph] = strings.TrimSpace(inlineMediaMarkupBuffer.String())
content = strings.Replace(content, placeholder, ph, 1)
} else {
content = strings.Replace(content, placeholder, "", 1)
}
}
}
embedMediaPlaceholders := embedMediaPlaceholderRegexp.FindAllStringSubmatch(content, -1)
if embedMediaPlaceholders != nil {
var em []embeddedMedia
for _, emp := range embedMediaPlaceholders {
placeholder := emp[0]
url := emp[1]
for _, emt := range embeddedMediaTypes {
code := emt.getCode(url)
if code != "" {
em = append(em, embeddedMedia{
MediaType: emt,
Code: code,
})
break
}
}
if len(em) > 0 {
inlineMediaTemplate := compileMediaTemplate(resLoader)
var inlineMediaMarkupBuffer bytes.Buffer
err := inlineMediaTemplate.Execute(&inlineMediaMarkupBuffer, contentDirectiveData{
Embed: em,
})
check(err)
ph := fmt.Sprintf(directivePlaceholderReplacementFormat, uuid.New().String())
if phReps == nil {
phReps = make(map[string]string)
}
phReps[ph] = strings.TrimSpace(inlineMediaMarkupBuffer.String())
content = strings.Replace(content, placeholder, ph, 1)
} else {
content = strings.Replace(content, placeholder, "", 1)
}
}
}
return content, rawBodyContent, phReps, tags
}
func sortContentDirectivePlaceholders(cdPlaceholders [][]string) {
// ==================================================
// directives with explicitly listed media
// should be processed before the ones
// that do not explicitly list any media
// ==================================================
sort.Slice(cdPlaceholders, func(i, j int) bool {
return strings.Compare(
cdPlaceholders[i][0],
cdPlaceholders[j][0]) == -1
})
// ==================================================
}
func handleContentDirectivePlaceholderReplacements(content string, phReps map[string]string) string {
if phReps != nil {
for placeholder, replacement := range phReps {
content = strings.Replace(content, placeholder, replacement, 1)
}
}
return content
}
func listAllMedia(contentEntityType contentEntityType, contentEntityId string, skipFiles []string) []string {
ceType := strings.ToLower(contentEntityType.String())
var allMedia []string
mediaDirPath := fmt.Sprintf("%s%c%s%c%s%c%s", deployDirName, os.PathSeparator, mediaDirName, os.PathSeparator, ceType, os.PathSeparator, contentEntityId)
if dirExists(mediaDirPath) {
videoFiles, err := listFilesByExt(mediaDirPath, videoFileExtensions...)
check(err)
for _, video := range videoFiles {
if !slices.Contains(skipFiles, video) {
allMedia = append(allMedia, video)
}
}
imageFiles, err := listFilesByExt(mediaDirPath, imageFileExtensions...)
check(err)
for _, image := range imageFiles {
if !slices.Contains(skipFiles, image) && !strings.Contains(image, thumbImgFileSuffix) {
allMedia = append(allMedia, image)
}
}
}
return allMedia
}
func parseMediaFileNames(mediaFileNames []string, contentEntityType contentEntityType, contentEntityId string, config appConfig) []media {
var allMedia []media
ceType := strings.ToLower(contentEntityType.String())
for _, mediaFileName := range mediaFileNames {
if strings.Contains(mediaFileName, thumbImgFileSuffix) {
continue
}
mediaUri := "/" + mediaDirName + "/" + ceType + "/" + contentEntityId + "/" + mediaFileName
mediaFileExt := filepath.Ext(mediaFileName)
var mType mediaType
if slices.Contains(imageFileExtensions, mediaFileExt) {
mType = Image
imgFileExt := filepath.Ext(mediaFileName)
var thumbs []thumb
for _, thSize := range config.thumbSizes {
thFileSuffix := "_" + strconv.Itoa(thSize) + thumbImgFileSuffix + imgFileExt
thumbFile := mediaFileName + thFileSuffix
thumbFilePath := fmt.Sprintf("%s%c%s%c%s%c%s", deployDirName, os.PathSeparator, mediaDirName, os.PathSeparator, contentEntityId, os.PathSeparator, thumbFile)
if fileExists(thumbFilePath) {
thumbUri := "/" + mediaDirName + "/" + ceType + "/" + contentEntityId + "/" + thumbFile
thumbs = append(thumbs, thumb{
Uri: thumbUri,
Size: thSize,
})
} else {
thumbs = append(thumbs, thumb{
Uri: mediaUri,
Size: thSize,
})
}
}
allMedia = append(allMedia, media{
Type: mType,
Uri: mediaUri,
thumbs: thumbs,
})
} else if slices.Contains(videoFileExtensions, mediaFileExt) {
mType = Video
allMedia = append(allMedia, media{
Type: mType,
Uri: mediaUri,
})
}
}
return allMedia
}