-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagecollections.go
228 lines (185 loc) · 4.62 KB
/
pagecollections.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
package entity
import (
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/contenthub"
"github.com/gohugonet/hugoverse/internal/domain/contenthub/valueobject"
fsVO "github.com/gohugonet/hugoverse/internal/domain/fs/valueobject"
"github.com/gohugonet/hugoverse/pkg/io"
"github.com/gohugonet/hugoverse/pkg/parser/pageparser"
"github.com/gohugonet/hugoverse/pkg/paths"
"path"
"strings"
)
// PageCollections contains the page collections for a site.
type PageCollections struct {
PageMap *PageMap
}
type PageMap struct {
*ContentMap
*ContentSpec
}
func (m *PageMap) Assemble() error {
if err := m.CreateMissingNodes(); err != nil {
return err
}
if err := m.AssemblePages(); err != nil {
return err
}
// Handle any new sections created in the step above.
if err := m.AssembleSections(); err != nil {
return err
}
return nil
}
func (m *PageMap) AssemblePages() error {
var err error
if err = m.AssembleSections(); err != nil {
return err
}
m.Pages.Walk(func(k string, v any) bool {
n := v.(*contentNode)
if n.p != nil {
return false
}
_, parent := m.getSection(k)
if parent == nil {
panic(fmt.Sprintf("BUG: parent not set for %q", k))
}
n.p, err = m.newPageFromContentNode(n)
fmt.Printf(">>> AssemblePages 000 %+v\n", n.p)
if err != nil {
return true
}
return false
})
return err
}
func (m *PageMap) AssembleSections() error {
m.Sections.Walk(func(k string, v any) bool {
n := v.(*contentNode)
sections := m.splitKey(k)
kind := contenthub.KindSection
if k == "/" {
kind = contenthub.KindHome
}
if n.fi != nil {
panic("assembleSections newPageFromContentNode not ready")
} else {
n.p = newPage(m.ContentSpec, n, kind, sections...)
fmt.Println(">>> assembleSections new page 999", sections, k, n.p)
}
return false
})
return nil
}
func (m *PageMap) newPageFromContentNode(n *contentNode) (*pageState, error) {
if n.fi == nil {
panic("FileInfo must (currently) be set")
}
f, err := valueobject.NewFileInfo(n.fi)
if err != nil {
return nil, err
}
meta := n.fi.Meta()
content := func() (io.ReadSeekCloser, error) {
return meta.Open()
}
sections := m.sectionsFromFile(f)
kind := m.kindFromFileInfoOrSections(f, sections)
metaProvider := &pageMeta{
kind: kind, sections: sections, bundled: false, f: f,
contentCovertProvider: m.ContentSpec,
}
ps, err := newPageBase(metaProvider)
if err != nil {
return nil, err
}
n.p = ps
r, err := content()
if err != nil {
return nil, err
}
defer r.Close()
// .md parseResult
// TODO: parser works way
parseResult, err := pageparser.Parse(
r,
pageparser.Config{EnableEmoji: false},
)
if err != nil {
return nil, err
}
ps.pageContent = pageContent{
source: rawPageContent{
parsed: parseResult,
posMainContent: -1,
posSummaryEnd: -1,
posBodyStart: -1,
},
}
if err := ps.mapContent(metaProvider); err != nil {
return nil, err
}
metaProvider.applyDefaultValues()
ps.init.Add(func() (any, error) {
fmt.Printf("TODO init page start : %+v\n", ps)
po := newPageOutput()
ps.pageOutput = po
contentProvider, err := newPageContentOutput(ps)
if err != nil {
return nil, err
}
po.initContentProvider(contentProvider)
return nil, nil
})
return ps, nil
}
func (m *PageMap) sectionsFromFile(fi contenthub.File) []string {
dirname := fi.Dir()
dirname = strings.Trim(dirname, paths.FilePathSeparator)
if dirname == "" {
return nil
}
parts := strings.Split(dirname, paths.FilePathSeparator)
if fii, ok := fi.(*valueobject.File); ok {
if len(parts) > 0 && fii.FileInfo().Meta().Classifier == fsVO.ContentClassLeaf {
// my-section/mybundle/index.md => my-section
return parts[:len(parts)-1]
}
}
return parts
}
func (m *PageMap) kindFromFileInfoOrSections(fi *valueobject.File, sections []string) string {
if fi.TranslationBaseName() == "_index" {
if fi.Dir() == "" {
return contenthub.KindHome
}
return m.kindFromSections(sections)
}
return contenthub.KindPage
}
func (m *PageMap) kindFromSections(sections []string) string {
if len(sections) == 0 {
return contenthub.KindHome
}
return m.kindFromSectionPath(path.Join(sections...))
}
func (m *PageMap) kindFromSectionPath(sectionPath string) string {
return contenthub.KindSection
}
func (m *PageMap) splitKey(k string) []string {
if k == "" || k == "/" {
return nil
}
return strings.Split(k, "/")[1:]
}
// withEveryBundlePage applies fn to every Page, including those bundled inside
// leaf bundles.
func (m *PageMap) withEveryBundlePage(fn func(p *pageState) bool) {
m.BundleTrees.Walk(func(s string, n *contentNode) bool {
if n.p != nil {
return fn(n.p)
}
return false
})
}