2016-11-03 23:34:25 +00: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
|
|
|
|
|
2016-11-11 08:01:47 +00:00
|
|
|
// PageCollections contains the page collections for a site.
|
2016-11-03 23:34:25 +00:00
|
|
|
type PageCollections struct {
|
2016-11-11 08:01:47 +00:00
|
|
|
// Includes only pages of all types, and only pages in the current language.
|
2016-11-03 23:34:25 +00:00
|
|
|
Pages Pages
|
|
|
|
|
|
|
|
// Includes all pages in all languages, including the current one.
|
2016-11-11 08:01:47 +00:00
|
|
|
// Inlcudes pages of all types.
|
2016-11-03 23:34:25 +00:00
|
|
|
AllPages Pages
|
|
|
|
|
2016-11-11 08:01:47 +00: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 23:34:25 +00:00
|
|
|
|
2016-11-11 08:01:47 +00:00
|
|
|
// A convenience cache for the regular pages.
|
2016-11-03 23:34:25 +00:00
|
|
|
// This is for the current language only.
|
2016-11-13 13:27:10 +00:00
|
|
|
RegularPages Pages
|
2016-11-03 23:34:25 +00:00
|
|
|
|
2016-11-16 20:06:10 +00:00
|
|
|
// A convenience cache for the all the regular pages.
|
|
|
|
AllRegularPages Pages
|
|
|
|
|
2016-11-03 23:34:25 +00:00
|
|
|
// Includes absolute all pages (of all types), including drafts etc.
|
|
|
|
rawAllPages Pages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) refreshPageCaches() {
|
2016-11-16 16:52:03 +00:00
|
|
|
c.indexPages = c.findPagesByKindNotIn(KindPage, c.Pages)
|
|
|
|
c.RegularPages = c.findPagesByKindIn(KindPage, c.Pages)
|
2016-11-16 20:06:10 +00:00
|
|
|
c.AllRegularPages = c.findPagesByKindIn(KindPage, c.AllPages)
|
2016-11-03 23:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPageCollections() *PageCollections {
|
|
|
|
return &PageCollections{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPageCollectionsFromPages(pages Pages) *PageCollections {
|
|
|
|
return &PageCollections{rawAllPages: pages}
|
|
|
|
}
|
|
|
|
|
2016-11-13 11:33:11 +00:00
|
|
|
func (c *PageCollections) getPage(typ string, path ...string) *Page {
|
2016-11-16 16:52:03 +00:00
|
|
|
pages := c.findPagesByKindIn(typ, c.Pages)
|
2016-11-07 19:24:37 +00:00
|
|
|
|
|
|
|
if len(pages) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) == 0 && len(pages) == 1 {
|
|
|
|
return pages[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range pages {
|
|
|
|
match := false
|
|
|
|
for i := 0; i < len(path); i++ {
|
|
|
|
if len(p.sections) > i && path[i] == p.sections[i] {
|
|
|
|
match = true
|
|
|
|
} else {
|
|
|
|
match = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-16 16:52:03 +00:00
|
|
|
func (*PageCollections) findPagesByKindIn(kind string, inPages Pages) Pages {
|
2016-11-03 23:34:25 +00:00
|
|
|
var pages Pages
|
|
|
|
for _, p := range inPages {
|
2016-11-16 16:52:03 +00:00
|
|
|
if p.Kind == kind {
|
2016-11-03 23:34:25 +00:00
|
|
|
pages = append(pages, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
2016-11-16 16:52:03 +00:00
|
|
|
func (*PageCollections) findPagesByKindNotIn(kind string, inPages Pages) Pages {
|
2016-11-03 23:34:25 +00:00
|
|
|
var pages Pages
|
|
|
|
for _, p := range inPages {
|
2016-11-16 16:52:03 +00:00
|
|
|
if p.Kind != kind {
|
2016-11-03 23:34:25 +00:00
|
|
|
pages = append(pages, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
2016-11-16 16:52:03 +00:00
|
|
|
func (c *PageCollections) findPagesByKind(kind string) Pages {
|
|
|
|
return c.findPagesByKindIn(kind, c.Pages)
|
2016-11-03 23:34:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) addPage(page *Page) {
|
|
|
|
c.rawAllPages = append(c.rawAllPages, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) removePageByPath(path string) {
|
|
|
|
if i := c.rawAllPages.FindPagePosByFilePath(path); i >= 0 {
|
|
|
|
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) removePage(page *Page) {
|
|
|
|
if i := c.rawAllPages.FindPagePos(page); i >= 0 {
|
|
|
|
c.rawAllPages = append(c.rawAllPages[:i], c.rawAllPages[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *PageCollections) replacePage(page *Page) {
|
|
|
|
// will find existing page that matches filepath and remove it
|
|
|
|
c.removePage(page)
|
|
|
|
c.addPage(page)
|
|
|
|
}
|