summaryrefslogtreecommitdiffhomepage
path: root/hugolib/pages_map.go
blob: b5b47ccafe4d90dde5f5ee11a2f184d04a133234 (plain)
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
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
	"fmt"
	"path"
	"path/filepath"
	"strings"
	"sync"

	"github.com/gohugoio/hugo/common/maps"

	radix "github.com/armon/go-radix"
	"github.com/spf13/cast"

	"github.com/gohugoio/hugo/resources/page"
)

func newPagesMap(s *Site) *pagesMap {
	return &pagesMap{
		r: radix.New(),
		s: s,
	}
}

type pagesMap struct {
	r *radix.Tree
	s *Site
}

func (m *pagesMap) Get(key string) *pagesMapBucket {
	key = m.cleanKey(key)
	v, found := m.r.Get(key)
	if !found {
		return nil
	}

	return v.(*pagesMapBucket)
}

func (m *pagesMap) getKey(p *pageState) string {
	if !p.File().IsZero() {
		return m.cleanKey(p.File().Dir())
	}
	return m.cleanKey(p.SectionsPath())
}

func (m *pagesMap) getOrCreateHome() *pageState {
	var home *pageState
	b, found := m.r.Get("/")
	if !found {
		home = m.s.newPage(page.KindHome)
		m.addBucketFor("/", home, nil)
	} else {
		home = b.(*pagesMapBucket).owner
	}

	return home
}

func (m *pagesMap) initPageMeta(p *pageState, bucket *pagesMapBucket) error {
	var err error
	p.metaInit.Do(func() {
		if p.metaInitFn != nil {
			err = p.metaInitFn(bucket)
		}
	})
	return err
}

func (m *pagesMap) initPageMetaFor(prefix string, bucket *pagesMapBucket) error {
	parentBucket := m.parentBucket(prefix)

	m.mergeCascades(bucket, parentBucket)

	if err := m.initPageMeta(bucket.owner, bucket); err != nil {
		return err
	}

	if !bucket.view {
		for _, p := range bucket.pages {
			ps := p.(*pageState)
			if err := m.initPageMeta(ps, bucket); err != nil {
				return err
			}

			for _, p := range ps.resources.ByType(pageResourceType) {
				if err := m.initPageMeta(p.(*pageState), bucket); err != nil {
					return err
				}
			}
		}

		// Now that the metadata is initialized (with dates, draft set etc.)
		// we can remove the pages that we for some reason should not include
		// in this build.
		tmp := bucket.pages[:0]
		for _, x := range bucket.pages {
			if m.s.shouldBuild(x) {
				if x.(*pageState).m.headless {
					bucket.headlessPages = append(bucket.headlessPages, x)
				} else {
					tmp = append(tmp, x)
				}

			}
		}
		bucket.pages = tmp
	}

	return nil
}

func (m *pagesMap) createSectionIfNotExists(section string) {
	key := m.cleanKey(section)
	_, found := m.r.Get(key)
	if !found {
		kind := m.s.kindFromSectionPath(section)
		p := m.s.newPage(kind, section)
		m.addBucketFor(key, p, nil)
	}
}

func (m *pagesMap) addBucket(p *pageState) {
	key := m.getKey(p)

	m.addBucketFor(key, p, nil)
}

func (m *pagesMap) addBucketFor(key string, p *pageState, meta map[string]interface{}) *pagesMapBucket {
	var isView bool
	switch p.Kind() {
	case page.KindTaxonomy, page.KindTaxonomyTerm:
		isView = true
	}

	disabled := !m.s.isEnabled(p.Kind())

	bucket := &pagesMapBucket{owner: p, view: isView, meta: meta, disabled: disabled}
	p.bucket = bucket

	m.r.Insert(key, bucket)

	return bucket
}

func (m *pagesMap) addPage(p *pageState) {
	if !p.IsPage() {
		m.addBucket(p)
		return
	}

	if !m.s.isEnabled(page.KindPage) {
		return
	}

	key := m.getKey(p)

	var bucket *pagesMapBucket

	_, v, found := m.r.LongestPrefix(key)
	if !found {
		panic(fmt.Sprintf("[BUG] bucket with key %q not found", key))
	}

	bucket = v.(*pagesMapBucket)
	p.bucket = bucket

	bucket.pages = append(bucket.pages, p)
}

func (m *pagesMap) assemblePageMeta() error {
	var walkErr error
	m.r.Walk(func(s string, v interface{}) bool {
		bucket := v.(*pagesMapBucket)

		if err := m.initPageMetaFor(s, bucket); err != nil {
			walkErr = err
			return true
		}
		return false
	})

	return walkErr
}

func (m *pagesMap) assembleTaxonomies(s *Site) error {
	s.Taxonomies = make(TaxonomyList)

	type bucketKey struct {
		plural  string
		termKey string
	}

	// Temporary cache.
	taxonomyBuckets := make(map[bucketKey]*pagesMapBucket)

	for singular, plural := range s.siteCfg.taxonomiesConfig {
		s.Taxonomies[plural] = make(Taxonomy)
		bkey := bucketKey{
			plural: plural,
		}

		bucket := m.Get(plural)

		if bucket == nil {
			// Create the page and bucket
			n := s.newPage(page.KindTaxonomyTerm, plural)

			key := m.cleanKey(plural)
			bucket = m.addBucketFor(key, n, nil)
			if err := m.initPageMetaFor(key, bucket); err != nil {
				return err
			}
		}

		if bucket.meta == nil {
			bucket.meta = map[string]interface{}{
				"singular": singular,
				"plural":   plural,
			}
		}

		// Add it to the temporary cache.
		taxonomyBuckets[bkey] = bucket

		// Taxonomy entries used in page front matter will be picked up later,
		// but there may be some yet to be used.
		pluralPrefix := m.cleanKey(plural) + "/"
		m.r.WalkPrefix(pluralPrefix, func(k string, v interface{}) bool {
			tb := v.(*pagesMapBucket)
			termKey := strings.TrimPrefix(k, pluralPrefix)
			if tb.meta == nil {
				tb.meta = map[string]interface{}{
					"singular": singular,
					"plural":   plural,
					"term":     tb.owner.Title(),
					"termKey":  termKey,
				}
			}

			bucket.pages = append(bucket.pages, tb.owner)
			bkey.termKey = termKey
			taxonomyBuckets[bkey] = tb

			return false
		})

	}

	addTaxonomy := func(singular, plural, term string, weight int, p page.Page) error {
		bkey := bucketKey{
			plural: plural,
		}

		termKey := s.getTaxonomyKey(term)

		b1 := taxonomyBuckets[bkey]

		var b2 *pagesMapBucket
		bkey.termKey = termKey
		b, found := taxonomyBuckets[bkey]
		if found {
			b2 = b
		} else {

			// Create the page and bucket
			n := s.newTaxonomyPage(term, plural, termKey)
			meta := map[string]interface{}{
				"singular": singular,
				"plural":   plural,
				"term":     term,
				"termKey":  termKey,
			}

			key := m.cleanKey(path.Join(plural, termKey))
			b2 = m.addBucketFor(key, n, meta)
			if err := m.initPageMetaFor(key, b2); err != nil {
				return err
			}
			b1.pages = append(b1.pages, b2.owner)
			taxonomyBuckets[bkey] = b2

		}

		w := page.NewWeightedPage(weight, p, b2.owner)

		s.Taxonomies[plural].add(termKey, w)

		b1.owner.m.Dates.UpdateDateAndLastmodIfAfter(p)
		b2.owner.m.Dates.UpdateDateAndLastmodIfAfter(p)

		return nil
	}

	m.r.Walk(func(k string, v interface{}) bool {
		b := v.(*pagesMapBucket)
		if b.view {
			return false
		}

		for singular, plural := range s.siteCfg.taxonomiesConfig {
			for _, p := range b.pages {

				vals := getParam(p, plural, false)

				w := getParamToLower(p, plural+"_weight")
				weight, err := cast.ToIntE(w)
				if err != nil {
					m.s.Log.ERROR.Printf("Unable to convert taxonomy weight %#v to int for %q", w, p.Path())
					// weight will equal zero, so let the flow continue
				}

				if vals != nil {
					if v, ok := vals.([]string); ok {
						for _, idx := range v {
							if err := addTaxonomy(singular, plural, idx, weight, p); err != nil {
								m.s.Log.ERROR.Printf("Failed to add taxonomy %q for %q: %s", plural, p.Path(), err)
							}
						}
					} else if v, ok := vals.(string); ok {
						if err := addTaxonomy(singular, plural, v, weight, p); err != nil {
							m.s.Log.ERROR.Printf("Failed to add taxonomy %q for %q: %s", plural, p.Path(), err)
						}
					} else {
						m.s.Log.ERROR.Printf("Invalid %s in %q\n", plural, p.Path())
					}
				}

			}
		}
		return false
	})

	for _, plural := range s.siteCfg.taxonomiesConfig {
		for k := range s.Taxonomies[plural] {
			s.Taxonomies[plural][k].Sort()
		}
	}

	return nil
}

func (m *pagesMap) cleanKey(key string) string {
	key = filepath.ToSlash(strings.ToLower(key))
	key = strings.Trim(key, "/")
	return "/" + key
}

func (m *pagesMap) mergeCascades(b1, b2 *pagesMapBucket) {
	if b1.cascade == nil {
		b1.cascade = make(maps.Params)
	}
	if b2 != nil && b2.cascade != nil {
		for k, v := range b2.cascade {
			if _, found := b1.cascade[k]; !found {
				b1.cascade[k] = v
			}
		}
	}
}

func (m *pagesMap) parentBucket(prefix string) *pagesMapBucket {
	if prefix == "/" {
		return nil
	}
	_, parentv, found := m.r.LongestPrefix(path.Dir(prefix))
	if !found {
		panic(fmt.Sprintf("[BUG] parent bucket not found for %q", prefix))
	}
	return parentv.(*pagesMapBucket)

}

func (m *pagesMap) withEveryPage(f func(p *pageState)) {
	m.r.Walk(func(k string, v interface{}) bool {
		b := v.(*pagesMapBucket)
		f(b.owner)
		if !b.view {
			for _, p := range b.pages {
				f(p.(*pageState))
			}
		}

		return false
	})
}

type pagesMapBucket struct {
	// Set if the pages in this bucket is also present in another bucket.
	view bool

	// Some additional metatadata attached to this node.
	meta map[string]interface{}

	// Cascading front matter.
	cascade map[string]interface{}

	owner *pageState // The branch node

	// When disableKinds is enabled for this node.
	disabled bool

	// Used to navigate the sections tree
	parent         *pagesMapBucket
	bucketSections []*pagesMapBucket

	pagesInit     sync.Once
	pages         page.Pages
	headlessPages page.Pages

	pagesAndSectionsInit sync.Once
	pagesAndSections     page.Pages

	sectionsInit sync.Once
	sections     page.Pages
}

func (b *pagesMapBucket) isEmpty() bool {
	return len(b.pages) == 0 && len(b.bucketSections) == 0
}

func (b *pagesMapBucket) getPages() page.Pages {
	b.pagesInit.Do(func() {
		page.SortByDefault(b.pages)
	})
	return b.pages
}

func (b *pagesMapBucket) getPagesAndSections() page.Pages {
	b.pagesAndSectionsInit.Do(func() {
		var pas page.Pages
		pas = append(pas, b.getPages()...)
		for _, p := range b.bucketSections {
			pas = append(pas, p.owner)
		}
		b.pagesAndSections = pas
		page.SortByDefault(b.pagesAndSections)
	})
	return b.pagesAndSections
}

func (b *pagesMapBucket) getSections() page.Pages {
	b.sectionsInit.Do(func() {
		for _, p := range b.bucketSections {
			b.sections = append(b.sections, p.owner)
		}
		page.SortByDefault(b.sections)
	})

	return b.sections
}