This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
blogs.go
89 lines (77 loc) · 2.48 KB
/
blogs.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
package leansite
import (
"path/filepath"
"sort"
"strings"
"github.com/metaleap/go-util/fs"
)
// A collection of blog entries
type BlogNavItems []BlogNavItem
// Implements sort.Interface.Len()
func (me BlogNavItems) Len() int { return len(me) }
// Implements sort.Interface.Less()
func (me BlogNavItems) Less(i, j int) bool { return me[j].Href < me[i].Href }
// Implements sort.Interface.Swap()
func (me BlogNavItems) Swap(i, j int) { me[i], me[j] = me[j], me[i] }
// Represents a blog entry
type BlogNavItem struct {
// Embedded navigation info (Href, Caption)
NavItem
// Date posted
Year, Month, Day string
}
// Returns true if year is different than the value passed when this method was last called
func (me *BlogNav) ShowYear(year string) (dif bool) {
if dif = (year != me.lastYear); dif {
me.lastYear = year
}
return
}
// A chronological listing of blog entries
type BlogNav struct {
// A chronological listing of blog entries
Nav BlogNavItems
lastYear string
}
// Returns a BlogNav for the specified path.
// For example, GetBlogArchive("blog") maps to "contents/blog/"
func (me *PageContext) GetBlogArchive(path string) *BlogNav {
if _, ok := SiteData.Blogs[path]; !ok {
dirPath := dir("contents", path)
handler := func(_ string) {
items := BlogNavItems{}
ufs.WalkAllFiles(dirPath, func(fullPath string) bool {
if filepath.Dir(fullPath) != dirPath {
vpath := fullPath[:len(fullPath)-len(filepath.Ext(fullPath))]
vpath = vpath[len(dirPath):]
navItem := BlogNavItem{}
navItem.Href, navItem.Caption = filepath.ToSlash(vpath), filepath.Base(vpath)
if src := ufs.ReadTextFile(fullPath, false, ""); len(src) > 0 {
if pos1, pos2 := strings.Index(src, "<h2>"), strings.Index(src, "</h2>"); pos1 >= 0 && pos2 > pos1 {
src = src[:pos2]
navItem.Caption = src[pos1+4:]
}
}
if pathItems := strings.Split(navItem.Href, "/"); len(pathItems) > 0 {
if navItem.Year = pathItems[1]; len(pathItems) > 1 {
if navItem.Month = pathItems[2]; len(pathItems) > 2 {
navItem.Day = pathItems[3]
}
}
}
items = append(items, navItem)
}
return true
})
sort.Sort(items)
SiteData.Blogs[path] = BlogNav{Nav: items}
}
ufs.WalkAllDirs(dirPath, func(fullPath string) bool {
DirWatch.WatchIn(fullPath, "*", false, handler)
return true
})
handler("")
}
copy := SiteData.Blogs[path]
return ©
}