2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2016-11-03 19:34:25 -04:00
|
|
|
//
|
|
|
|
// 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 (
|
2018-05-29 21:35:27 -04:00
|
|
|
"fmt"
|
2017-05-25 05:32:02 -04:00
|
|
|
"path"
|
2018-07-19 12:26:10 -04:00
|
|
|
"path/filepath"
|
2018-01-24 03:47:30 -05:00
|
|
|
"strings"
|
2019-01-02 06:33:26 -05:00
|
|
|
"sync"
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2021-06-18 04:27:27 -04:00
|
|
|
"github.com/gohugoio/hugo/common/paths"
|
|
|
|
|
2020-02-28 08:22:05 -05:00
|
|
|
"github.com/gohugoio/hugo/hugofs/files"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2017-05-25 14:13:03 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
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 {
|
2019-09-10 05:26:34 -04:00
|
|
|
pageMap *pageMap
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// Lazy initialized page collections
|
|
|
|
pages *lazyPagesFactory
|
|
|
|
regularPages *lazyPagesFactory
|
|
|
|
allPages *lazyPagesFactory
|
|
|
|
allRegularPages *lazyPagesFactory
|
|
|
|
}
|
2017-05-25 14:13:03 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// Pages returns all pages.
|
|
|
|
// This is for the current language only.
|
|
|
|
func (c *PageCollections) Pages() page.Pages {
|
|
|
|
return c.pages.get()
|
|
|
|
}
|
2018-01-23 08:02:54 -05:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
// RegularPages returns all the regular pages.
|
|
|
|
// This is for the current language only.
|
|
|
|
func (c *PageCollections) RegularPages() page.Pages {
|
|
|
|
return c.regularPages.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllPages returns all pages for all languages.
|
|
|
|
func (c *PageCollections) AllPages() page.Pages {
|
|
|
|
return c.allPages.get()
|
|
|
|
}
|
|
|
|
|
2023-05-18 05:05:56 -04:00
|
|
|
// AllRegularPages returns all regular pages for all languages.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (c *PageCollections) AllRegularPages() page.Pages {
|
|
|
|
return c.allRegularPages.get()
|
2018-05-29 21:35:27 -04:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
type lazyPagesFactory struct {
|
|
|
|
pages page.Pages
|
2018-05-29 21:35:27 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
init sync.Once
|
|
|
|
factory page.PagesFactory
|
|
|
|
}
|
2016-11-07 14:24:37 -05:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func (l *lazyPagesFactory) get() page.Pages {
|
|
|
|
l.init.Do(func() {
|
|
|
|
l.pages = l.factory()
|
|
|
|
})
|
|
|
|
return l.pages
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLazyPagesFactory(factory page.PagesFactory) *lazyPagesFactory {
|
|
|
|
return &lazyPagesFactory{factory: factory}
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
func newPageCollections(m *pageMap) *PageCollections {
|
|
|
|
if m == nil {
|
|
|
|
panic("must provide a pageMap")
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
c := &PageCollections{pageMap: m}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
c.pages = newLazyPagesFactory(func() page.Pages {
|
2019-09-10 05:26:34 -04:00
|
|
|
return m.createListAllPages()
|
2019-01-02 06:33:26 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
c.regularPages = newLazyPagesFactory(func() page.Pages {
|
2019-09-10 05:26:34 -04:00
|
|
|
return c.findPagesByKindIn(page.KindPage, c.pages.get())
|
2019-01-02 06:33:26 -05:00
|
|
|
})
|
2018-07-17 05:18:29 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
return c
|
2017-05-25 14:13:03 -04:00
|
|
|
}
|
2017-05-22 18:20:31 -04:00
|
|
|
|
2018-07-19 12:26:10 -04:00
|
|
|
// This is an adapter func for the old API with Kind as first argument.
|
|
|
|
// This is invoked when you do .Site.GetPage. We drop the Kind and fails
|
2020-12-16 06:11:32 -05:00
|
|
|
// if there are more than 2 arguments, which would be ambiguous.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (c *PageCollections) getPageOldVersion(ref ...string) (page.Page, error) {
|
2018-07-19 12:26:10 -04:00
|
|
|
var refs []string
|
|
|
|
for _, r := range ref {
|
|
|
|
// A common construct in the wild is
|
|
|
|
// .Site.GetPage "home" "" or
|
|
|
|
// .Site.GetPage "home" "/"
|
|
|
|
if r != "" && r != "/" {
|
|
|
|
refs = append(refs, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var key string
|
|
|
|
|
|
|
|
if len(refs) > 2 {
|
|
|
|
// This was allowed in Hugo <= 0.44, but we cannot support this with the
|
|
|
|
// new API. This should be the most unusual case.
|
|
|
|
return nil, fmt.Errorf(`too many arguments to .Site.GetPage: %v. Use lookups on the form {{ .Site.GetPage "/posts/mypage-md" }}`, ref)
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
if len(refs) == 0 || refs[0] == page.KindHome {
|
2018-07-19 12:26:10 -04:00
|
|
|
key = "/"
|
|
|
|
} else if len(refs) == 1 {
|
2019-01-02 06:33:26 -05:00
|
|
|
if len(ref) == 2 && refs[0] == page.KindSection {
|
2018-07-24 04:10:51 -04:00
|
|
|
// This is an old style reference to the "Home Page section".
|
|
|
|
// Typically fetched via {{ .Site.GetPage "section" .Section }}
|
|
|
|
// See https://github.com/gohugoio/hugo/issues/4989
|
|
|
|
key = "/"
|
|
|
|
} else {
|
|
|
|
key = refs[0]
|
|
|
|
}
|
2018-07-19 12:26:10 -04:00
|
|
|
} else {
|
|
|
|
key = refs[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
key = filepath.ToSlash(key)
|
|
|
|
if !strings.HasPrefix(key, "/") {
|
|
|
|
key = "/" + key
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.getPageNew(nil, key)
|
|
|
|
}
|
|
|
|
|
2023-03-10 14:18:41 -05:00
|
|
|
// Only used in tests.
|
2019-01-02 06:33:26 -05:00
|
|
|
func (c *PageCollections) getPage(typ string, sections ...string) page.Page {
|
2018-07-19 12:26:10 -04:00
|
|
|
refs := append([]string{typ}, path.Join(sections...))
|
|
|
|
p, _ := c.getPageOldVersion(refs...)
|
2018-07-17 05:18:29 -04:00
|
|
|
return p
|
|
|
|
}
|
2018-05-29 21:35:27 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// getPageRef resolves a Page from ref/relRef, with a slightly more comprehensive
|
|
|
|
// search path than getPageNew.
|
|
|
|
func (c *PageCollections) getPageRef(context page.Page, ref string) (page.Page, error) {
|
|
|
|
n, err := c.getContentNode(context, true, ref)
|
|
|
|
if err != nil || n == nil || n.p == nil {
|
|
|
|
return nil, err
|
2017-05-25 14:13:03 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
return n.p, nil
|
2016-11-07 14:24:37 -05:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
func (c *PageCollections) getPageNew(context page.Page, ref string) (page.Page, error) {
|
|
|
|
n, err := c.getContentNode(context, false, ref)
|
|
|
|
if err != nil || n == nil || n.p == nil {
|
|
|
|
return nil, err
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
return n.p, nil
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
func (c *PageCollections) getSectionOrPage(ref string) (*contentNode, string) {
|
|
|
|
var n *contentNode
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-05-21 05:25:00 -04:00
|
|
|
pref := helpers.AddTrailingSlash(ref)
|
|
|
|
s, v, found := c.pageMap.sections.LongestPrefix(pref)
|
2018-02-25 04:50:44 -05:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if found {
|
|
|
|
n = v.(*contentNode)
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
|
|
|
|
2020-05-21 05:25:00 -04:00
|
|
|
if found && s == pref {
|
2019-09-10 05:26:34 -04:00
|
|
|
// A section
|
|
|
|
return n, ""
|
2016-11-03 19:34:25 -04:00
|
|
|
}
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
m := c.pageMap
|
2020-05-21 05:25:00 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
filename := strings.TrimPrefix(strings.TrimPrefix(ref, s), "/")
|
|
|
|
langSuffix := "." + m.s.Lang()
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// Trim both extension and any language code.
|
2021-06-18 04:27:27 -04:00
|
|
|
name := paths.PathNoExt(filename)
|
2019-09-10 05:26:34 -04:00
|
|
|
name = strings.TrimSuffix(name, langSuffix)
|
2016-11-03 19:34:25 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// These are reserved bundle names and will always be stored by their owning
|
|
|
|
// folder name.
|
|
|
|
name = strings.TrimSuffix(name, "/index")
|
|
|
|
name = strings.TrimSuffix(name, "/_index")
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if !found {
|
|
|
|
return nil, name
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// Check if it's a section with filename provided.
|
|
|
|
if !n.p.File().IsZero() && n.p.File().LogicalName() == filename {
|
|
|
|
return n, name
|
|
|
|
}
|
2019-08-09 04:05:22 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
return m.getPage(s, name), name
|
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2023-02-18 15:47:35 -05:00
|
|
|
// For Ref/Reflink and .Site.GetPage do simple name lookups for the potentially ambiguous myarticle.md and /myarticle.md,
|
2020-03-09 09:01:28 -04:00
|
|
|
// but not when we get ./myarticle*, section/myarticle.
|
|
|
|
func shouldDoSimpleLookup(ref string) bool {
|
|
|
|
if ref[0] == '.' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
slashCount := strings.Count(ref, "/")
|
|
|
|
|
|
|
|
if slashCount > 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return slashCount == 0 || ref[0] == '/'
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
func (c *PageCollections) getContentNode(context page.Page, isReflink bool, ref string) (*contentNode, error) {
|
|
|
|
ref = filepath.ToSlash(strings.ToLower(strings.TrimSpace(ref)))
|
2020-05-21 05:25:00 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if ref == "" {
|
|
|
|
ref = "/"
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2020-05-21 05:25:00 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
inRef := ref
|
2020-02-28 08:22:05 -05:00
|
|
|
navUp := strings.HasPrefix(ref, "..")
|
2019-09-10 05:26:34 -04:00
|
|
|
var doSimpleLookup bool
|
|
|
|
if isReflink || context == nil {
|
2020-03-09 09:01:28 -04:00
|
|
|
doSimpleLookup = shouldDoSimpleLookup(ref)
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if context != nil && !strings.HasPrefix(ref, "/") {
|
|
|
|
// Try the page-relative path.
|
|
|
|
var base string
|
|
|
|
if context.File().IsZero() {
|
|
|
|
base = context.SectionsPath()
|
|
|
|
} else {
|
2020-02-28 08:22:05 -05:00
|
|
|
meta := context.File().FileInfo().Meta()
|
2021-07-13 05:41:02 -04:00
|
|
|
base = filepath.ToSlash(filepath.Dir(meta.Path))
|
|
|
|
if meta.Classifier == files.ContentClassLeaf {
|
2020-02-28 08:22:05 -05:00
|
|
|
// Bundles are stored in subfolders e.g. blog/mybundle/index.md,
|
|
|
|
// so if the user has not explicitly asked to go up,
|
|
|
|
// look on the "blog" level.
|
|
|
|
if !navUp {
|
|
|
|
base = path.Dir(base)
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
ref = path.Join("/", strings.ToLower(base), ref)
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if !strings.HasPrefix(ref, "/") {
|
|
|
|
ref = "/" + ref
|
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
m := c.pageMap
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// It's either a section, a page in a section or a taxonomy node.
|
|
|
|
// Start with the most likely:
|
|
|
|
n, name := c.getSectionOrPage(ref)
|
|
|
|
if n != nil {
|
|
|
|
return n, nil
|
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if !strings.HasPrefix(inRef, "/") {
|
|
|
|
// Many people will have "post/foo.md" in their content files.
|
|
|
|
if n, _ := c.getSectionOrPage("/" + inRef); n != nil {
|
|
|
|
return n, nil
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
// Check if it's a taxonomy node
|
2020-05-21 05:25:00 -04:00
|
|
|
pref := helpers.AddTrailingSlash(ref)
|
|
|
|
s, v, found := m.taxonomies.LongestPrefix(pref)
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if found {
|
2020-05-21 05:25:00 -04:00
|
|
|
if !m.onSameLevel(pref, s) {
|
2019-09-10 05:26:34 -04:00
|
|
|
return nil, nil
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
return v.(*contentNode), nil
|
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
getByName := func(s string) (*contentNode, error) {
|
|
|
|
n := m.pageReverseIndex.Get(s)
|
|
|
|
if n != nil {
|
2020-12-16 06:11:32 -05:00
|
|
|
if n == ambiguousContentNode {
|
2019-09-10 05:26:34 -04:00
|
|
|
return nil, fmt.Errorf("page reference %q is ambiguous", ref)
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
return n, nil
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
return nil, nil
|
2019-08-09 04:05:22 -04:00
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
var module string
|
|
|
|
if context != nil && !context.File().IsZero() {
|
2021-07-13 05:41:02 -04:00
|
|
|
module = context.File().FileInfo().Meta().Module
|
2019-09-10 05:26:34 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if module == "" && !c.pageMap.s.home.File().IsZero() {
|
2021-07-13 05:41:02 -04:00
|
|
|
module = c.pageMap.s.home.File().FileInfo().Meta().Module
|
2019-09-10 05:26:34 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if module != "" {
|
|
|
|
n, err := getByName(module + ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2019-09-10 05:26:34 -04:00
|
|
|
if n != nil {
|
|
|
|
return n, nil
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
if !doSimpleLookup {
|
|
|
|
return nil, nil
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
|
2023-02-18 15:47:35 -05:00
|
|
|
// Ref/relref supports this potentially ambiguous lookup.
|
2020-03-02 14:06:58 -05:00
|
|
|
return getByName(path.Base(name))
|
2019-09-10 05:26:34 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-09-10 05:26:34 -04:00
|
|
|
func (*PageCollections) findPagesByKindIn(kind string, inPages page.Pages) page.Pages {
|
|
|
|
var pages page.Pages
|
|
|
|
for _, p := range inPages {
|
|
|
|
if p.Kind() == kind {
|
|
|
|
pages = append(pages, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pages
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|