2016-11-03 19:34:25 -04:00
|
|
|
// Copyright 2016 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
|
|
|
|
|
2017-05-25 05:32:02 -04:00
|
|
|
import (
|
|
|
|
"path"
|
2017-05-25 14:13:03 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/cache"
|
2017-05-25 05:32:02 -04:00
|
|
|
)
|
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
// PageCollections contains the page collections for a site.
|
2016-11-03 19:34:25 -04:00
|
|
|
type PageCollections struct {
|
2016-11-11 03:01:47 -05:00
|
|
|
// Includes only pages of all types, and only pages in the current language.
|
2016-11-03 19:34:25 -04:00
|
|
|
Pages Pages
|
|
|
|
|
|
|
|
// Includes all pages in all languages, including the current one.
|
2017-02-02 16:25:42 -05:00
|
|
|
// Includes pages of all types.
|
2016-11-03 19:34:25 -04:00
|
|
|
AllPages Pages
|
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
// A convenience cache for the traditional index types, taxonomies, home page etc.
|
|
|
|
// This is for the current language only.
|
|
|
|
indexPages Pages
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2016-11-11 03:01:47 -05:00
|
|
|
// A convenience cache for the regular pages.
|
2016-11-03 19:34:25 -04:00
|
|
|
// This is for the current language only.
|
2016-11-13 08:27:10 -05:00
|
|
|
RegularPages Pages
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2016-11-16 15:06:10 -05:00
|
|
|
// A convenience cache for the all the regular pages.
|
|
|
|
AllRegularPages Pages
|
|
|
|
|
2016-11-03 19:34:25 -04:00
|
|
|
// Includes absolute all pages (of all types), including drafts etc.
|
|
|
|
rawAllPages Pages
|
2017-05-25 14:13:03 -04:00
|
|
|
|
|
|
|
pageCache *cache.PartitionedLazyCache
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) refreshPageCaches() {
|
2016-11-16 11:52:03 -05:00
|
|
|
c.indexPages = c.findPagesByKindNotIn(KindPage, c.Pages)
|
|
|
|
c.RegularPages = c.findPagesByKindIn(KindPage, c.Pages)
|
2016-11-16 15:06:10 -05:00
|
|
|
c.AllRegularPages = c.findPagesByKindIn(KindPage, c.AllPages)
|
2016-11-07 14:24:37 -05:00
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
cacheLoader := func(kind string) func() (map[string]interface{}, error) {
|
|
|
|
return func() (map[string]interface{}, error) {
|
|
|
|
cache := make(map[string]interface{})
|
|
|
|
switch kind {
|
|
|
|
case KindPage:
|
|
|
|
// Note that we deliberately use the pages from all sites
|
|
|
|
// in this cache, as we intend to use this in the ref and relref
|
|
|
|
// shortcodes. If the user says "sect/doc1.en.md", he/she knows
|
|
|
|
// what he/she is looking for.
|
|
|
|
for _, p := range c.AllRegularPages {
|
|
|
|
cache[filepath.ToSlash(p.Source.Path())] = p
|
2017-05-26 03:12:19 -04:00
|
|
|
// Ref/Relref supports this potentially ambiguous lookup.
|
|
|
|
cache[p.Source.LogicalName()] = p
|
2017-05-25 14:13:03 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
for _, p := range c.indexPages {
|
|
|
|
key := path.Join(p.sections...)
|
|
|
|
cache[key] = p
|
|
|
|
}
|
2017-05-25 05:32:02 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
return cache, nil
|
2016-11-07 14:24:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 03:39:43 -05:00
|
|
|
partitions := make([]cache.Partition, len(allKindsInPages))
|
2017-05-22 18:20:31 -04:00
|
|
|
|
2017-11-11 03:39:43 -05:00
|
|
|
for i, kind := range allKindsInPages {
|
|
|
|
partitions[i] = cache.Partition{Key: kind, Load: cacheLoader(kind)}
|
2017-05-25 14:13:03 -04:00
|
|
|
}
|
2017-05-22 18:20:31 -04:00
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
c.pageCache = cache.NewPartitionedLazyCache(partitions...)
|
|
|
|
}
|
2017-05-25 05:32:02 -04:00
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
func newPageCollections() *PageCollections {
|
|
|
|
return &PageCollections{}
|
2017-05-25 05:32:02 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
func newPageCollectionsFromPages(pages Pages) *PageCollections {
|
|
|
|
return &PageCollections{rawAllPages: pages}
|
|
|
|
}
|
2017-05-22 18:20:31 -04:00
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
func (c *PageCollections) getPage(typ string, sections ...string) *Page {
|
|
|
|
var key string
|
|
|
|
if len(sections) == 1 {
|
|
|
|
key = filepath.ToSlash(sections[0])
|
2017-05-22 18:20:31 -04:00
|
|
|
} else {
|
2017-05-25 14:13:03 -04:00
|
|
|
key = path.Join(sections...)
|
2017-05-22 18:20:31 -04:00
|
|
|
}
|
|
|
|
|
2017-05-25 14:13:03 -04:00
|
|
|
p, _ := c.pageCache.Get(typ, key)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p.(*Page)
|
2017-05-22 18:20:31 -04:00
|
|
|
|
2016-11-07 14:24:37 -05:00
|
|
|
}
|
|
|
|
|
2016-11-16 11:52:03 -05:00
|
|
|
func (*PageCollections) findPagesByKindIn(kind string, inPages Pages) Pages {
|
2016-11-03 19:34:25 -04:00
|
|
|
var pages Pages
|
|
|
|
for _, p := range inPages {
|
2016-11-16 11:52:03 -05:00
|
|
|
if p.Kind == kind {
|
2016-11-03 19:34:25 -04:00
|
|
|
pages = append(pages, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
2016-11-16 11:52:03 -05:00
|
|
|
func (*PageCollections) findPagesByKindNotIn(kind string, inPages Pages) Pages {
|
2016-11-03 19:34:25 -04:00
|
|
|
var pages Pages
|
|
|
|
for _, p := range inPages {
|
2016-11-16 11:52:03 -05:00
|
|
|
if p.Kind != kind {
|
2016-11-03 19:34:25 -04:00
|
|
|
pages = append(pages, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
2016-11-16 11:52:03 -05:00
|
|
|
func (c *PageCollections) findPagesByKind(kind string) Pages {
|
|
|
|
return c.findPagesByKindIn(kind, c.Pages)
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) addPage(page *Page) {
|
|
|
|
c.rawAllPages = append(c.rawAllPages, page)
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:00:05 -04:00
|
|
|
// When we get a REMOVE event we're not always getting all the individual files,
|
|
|
|
// so we need to remove all below a given path.
|
|
|
|
func (c *PageCollections) removePageByPathPrefix(path string) {
|
|
|
|
for {
|
2017-06-08 16:32:01 -04:00
|
|
|
i := c.rawAllPages.findFirstPagePosByFilePathPrefix(path)
|
2017-06-08 14:00:05 -04:00
|
|
|
if i == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:34:25 -04:00
|
|
|
func (c *PageCollections) removePageByPath(path string) {
|
2017-06-08 16:32:01 -04:00
|
|
|
if i := c.rawAllPages.findPagePosByFilePath(path); i >= 0 {
|
2016-11-03 19:34:25 -04:00
|
|
|
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) removePage(page *Page) {
|
2017-06-08 16:32:01 -04:00
|
|
|
if i := c.rawAllPages.findPagePos(page); i >= 0 {
|
2016-11-03 19:34:25 -04:00
|
|
|
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 14:54:50 -05:00
|
|
|
func (c *PageCollections) findPagesByShortcode(shortcode string) Pages {
|
|
|
|
var pages Pages
|
|
|
|
|
|
|
|
for _, p := range c.rawAllPages {
|
|
|
|
if p.shortcodeState != nil {
|
|
|
|
if _, ok := p.shortcodeState.nameSet[shortcode]; ok {
|
|
|
|
pages = append(pages, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
2016-11-03 19:34:25 -04:00
|
|
|
func (c *PageCollections) replacePage(page *Page) {
|
|
|
|
// will find existing page that matches filepath and remove it
|
|
|
|
c.removePage(page)
|
|
|
|
c.addPage(page)
|
|
|
|
}
|