-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentmap.go
206 lines (163 loc) · 4.36 KB
/
contentmap.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
package entity
import (
"fmt"
fsVO "github.com/gohugonet/hugoverse/internal/domain/fs/valueobject"
"path"
"path/filepath"
"strings"
)
type ContentMap struct {
// View of regular pages, sections, and taxonomies.
PageTrees ContentTrees
// View of pages, sections, taxonomies, and resources.
BundleTrees ContentTrees
// Stores page bundles keyed by its path's directory or the base filename,
// e.g. "blog/post.md" => "/blog/post", "blog/post/index.md" => "/blog/post"
// These are the "regular pages" and all of them are bundles.
Pages *ContentTree
// Section nodes.
Sections *ContentTree
// Resources stored per bundle below a common prefix, e.g. "/blog/post__hb_".
//Resources *ContentTree
}
func (m *ContentMap) AddFilesBundle(header fsVO.FileMetaInfo) error {
var (
meta = header.Meta()
bundlePath = m.getBundleDir(meta)
n = m.newContentNodeFromFi(header)
b = m.newKeyBuilder()
section string
)
// A regular page. Attach it to its section.
section, _ = m.getOrCreateSection(n, bundlePath) // /abc/
b = b.WithSection(section).ForPage(bundlePath).Insert(n)
fmt.Println("<<< AddFilesBundle section: ", section, bundlePath, n.p)
return nil
}
func (m *ContentMap) getBundleDir(meta *fsVO.FileMeta) string {
dir := cleanTreeKey(filepath.Dir(meta.Path))
fmt.Println(">>> getBundleDir dir: ", dir)
switch meta.Classifier {
case fsVO.ContentClassContent:
meta.TranslationBaseName = "post"
fmt.Println(">>> getBundleDir111 dir: ", path.Join(dir, meta.TranslationBaseName))
return path.Join(dir, meta.TranslationBaseName)
default:
return dir
}
}
func (m *ContentMap) newContentNodeFromFi(fi fsVO.FileMetaInfo) *contentNode {
return &contentNode{
fi: fi,
path: strings.TrimPrefix(filepath.ToSlash(fi.Meta().Path), "/"),
}
}
func (m *ContentMap) newKeyBuilder() *cmInsertKeyBuilder {
return &cmInsertKeyBuilder{m: m}
}
func (m *ContentMap) getOrCreateSection(n *contentNode, s string) (string, *contentNode) {
k, b := m.getSection(s)
k = cleanSectionTreeKey(s[:strings.Index(s[1:], "/")+1])
b = &contentNode{
path: n.rootSection(),
}
m.Sections.Insert(k, b)
return k, b
}
func (m *ContentMap) getSection(s string) (string, *contentNode) {
s = AddTrailingSlash(path.Dir(strings.TrimSuffix(s, "/")))
v, found := m.Sections.Get(s)
if found {
return s, v.(*contentNode)
}
return "", nil
}
// AddTrailingSlash adds a trailing Unix styled slash (/) if not already
// there.
func AddTrailingSlash(path string) string {
if !strings.HasSuffix(path, "/") {
path += "/"
}
return path
}
func cleanSectionTreeKey(k string) string {
k = cleanTreeKey(k)
if k != "/" {
k += "/"
}
return k
}
func cleanTreeKey(k string) string {
k = "/" + strings.ToLower(strings.Trim(path.Clean(filepath.ToSlash(k)), "./"))
return k
}
type cmInsertKeyBuilder struct {
m *ContentMap
err error
// Builder state
tree *ContentTree
baseKey string // Section or page key
key string
}
func (b *cmInsertKeyBuilder) WithSection(s string) *cmInsertKeyBuilder {
s = cleanSectionTreeKey(s)
b.newTopLevel()
b.tree = b.m.Sections
b.baseKey = s
b.key = s
return b
}
func (b *cmInsertKeyBuilder) newTopLevel() {
b.key = ""
}
const (
cmBranchSeparator = "__hb_"
cmLeafSeparator = "__hl_"
)
func (b *cmInsertKeyBuilder) ForPage(s string) *cmInsertKeyBuilder {
baseKey := b.baseKey
b.baseKey = s
if baseKey != "/" {
// Don't repeat the section path in the key.
s = strings.TrimPrefix(s, baseKey)
}
s = strings.TrimPrefix(s, "/")
switch b.tree {
case b.m.Sections:
b.tree = b.m.Pages
b.key = baseKey + cmBranchSeparator + s + cmLeafSeparator
default:
panic("invalid state")
}
return b
}
func (b *cmInsertKeyBuilder) Insert(n *contentNode) *cmInsertKeyBuilder {
if b.err == nil {
b.tree.Insert(b.Key(), n)
}
return b
}
func (b *cmInsertKeyBuilder) Key() string {
switch b.tree {
case b.m.Sections:
return cleanSectionTreeKey(b.key)
default:
return cleanTreeKey(b.key)
}
}
// Assemble
func (m *ContentMap) CreateMissingNodes() error {
// Create missing home and root sections
rootSections := make(map[string]any)
rootSections["/"] = true // not found in both sections and pages
for sect := range rootSections {
var sectionPath string
sect = cleanSectionTreeKey(sect)
_, found := m.Sections.Get(sect)
if !found {
mm := &contentNode{path: sectionPath} // ""
_, _ = m.Sections.Insert(sect, mm) // "/"
}
}
return nil
}