2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2013-07-04 11:32:55 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2013-07-04 11:32:55 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-04 11:32:55 -04:00
|
|
|
//
|
|
|
|
// 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 (
|
2016-11-01 11:47:15 -04:00
|
|
|
"fmt"
|
2014-01-29 17:50:31 -05:00
|
|
|
"html/template"
|
2016-07-26 13:04:10 -04:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2016-07-28 03:30:58 -04:00
|
|
|
"strings"
|
2014-12-27 08:11:19 -05:00
|
|
|
"sync"
|
2014-01-29 17:50:31 -05:00
|
|
|
"time"
|
2016-07-25 16:22:09 -04:00
|
|
|
|
2016-09-15 22:28:13 -04:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2016-09-18 13:10:11 -04:00
|
|
|
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
2016-10-30 12:59:24 -04:00
|
|
|
// TODO(bep) np add String()
|
|
|
|
type NodeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NodePage NodeType = iota
|
|
|
|
|
2016-10-31 14:53:33 -04:00
|
|
|
// Temporary state.
|
|
|
|
NodeUnknown
|
|
|
|
|
2016-10-30 12:59:24 -04:00
|
|
|
// The rest are node types; home page, sections etc.
|
|
|
|
NodeHome
|
2016-10-31 13:03:02 -04:00
|
|
|
NodeSection
|
2016-10-31 14:53:33 -04:00
|
|
|
NodeTaxonomy
|
2016-11-01 11:47:15 -04:00
|
|
|
NodeTaxonomyTerms
|
2016-10-30 12:59:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p NodeType) IsNode() bool {
|
|
|
|
return p >= NodeHome
|
|
|
|
}
|
|
|
|
|
2013-07-04 11:32:55 -04:00
|
|
|
type Node struct {
|
2016-10-30 12:59:24 -04:00
|
|
|
NodeType NodeType
|
|
|
|
|
2016-08-09 14:06:15 -04:00
|
|
|
// a natural key that should be unique for this site
|
|
|
|
// for the home page this will typically be "home", but it can anything
|
|
|
|
// as long as it is the same for repeated builds.
|
|
|
|
nodeID string
|
|
|
|
|
2014-01-29 17:50:31 -05:00
|
|
|
RSSLink template.HTML
|
2015-05-23 06:28:01 -04:00
|
|
|
Site *SiteInfo `json:"-"`
|
2014-01-29 17:50:31 -05:00
|
|
|
// layout string
|
|
|
|
Data map[string]interface{}
|
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Keywords []string
|
2014-04-08 21:40:38 -04:00
|
|
|
Params map[string]interface{}
|
2014-01-29 17:50:31 -05:00
|
|
|
Date time.Time
|
2015-05-14 16:06:36 -04:00
|
|
|
Lastmod time.Time
|
2014-05-06 06:50:23 -04:00
|
|
|
Sitemap Sitemap
|
2015-03-18 01:16:54 -04:00
|
|
|
URLPath
|
2015-04-21 15:25:42 -04:00
|
|
|
paginator *Pager
|
2014-12-27 08:11:19 -05:00
|
|
|
paginatorInit sync.Once
|
2014-12-26 15:18:26 -05:00
|
|
|
scratch *Scratch
|
2016-07-26 13:04:10 -04:00
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
language *helpers.Language
|
2016-08-07 08:03:03 -04:00
|
|
|
languageInit sync.Once
|
2016-08-07 18:12:06 -04:00
|
|
|
lang string
|
2016-07-26 13:04:10 -04:00
|
|
|
|
|
|
|
translations Nodes
|
|
|
|
translationsInit sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Nodes type is temporary until we get https://github.com/spf13/hugo/issues/2297 fixed.
|
|
|
|
type Nodes []*Node
|
|
|
|
|
|
|
|
func (n Nodes) Len() int {
|
|
|
|
return len(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Nodes) Less(i, j int) bool {
|
|
|
|
return n[i].language.Weight < n[j].language.Weight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Nodes) Swap(i, j int) {
|
|
|
|
n[i], n[j] = n[j], n[i]
|
2014-01-18 22:16:19 -05:00
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
func (n *Node) Now() time.Time {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (n *Node) HasMenuCurrent(menuID string, inme *MenuEntry) bool {
|
2014-10-18 14:25:10 -04:00
|
|
|
if inme.HasChildren() {
|
2016-07-26 13:04:10 -04:00
|
|
|
me := MenuEntry{Name: n.Title, URL: n.URL()}
|
2014-10-18 14:25:10 -04:00
|
|
|
|
|
|
|
for _, child := range inme.Children {
|
|
|
|
if me.IsSameResource(child) {
|
|
|
|
return true
|
|
|
|
}
|
2015-06-25 06:26:48 -04:00
|
|
|
if n.HasMenuCurrent(menuID, child) {
|
|
|
|
return true
|
|
|
|
}
|
2014-10-18 14:25:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (n *Node) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2016-07-26 13:04:10 -04:00
|
|
|
me := MenuEntry{Name: n.Title, URL: n.Site.createNodeMenuEntryURL(n.URL())}
|
2015-03-31 01:44:08 -04:00
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
if !me.IsSameResource(inme) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// this resource may be included in several menus
|
|
|
|
// search for it to make sure that it is in the menu with the given menuId
|
2015-03-11 13:34:57 -04:00
|
|
|
if menu, ok := (*n.Site.Menus)[menuID]; ok {
|
2014-10-18 14:25:10 -04:00
|
|
|
for _, menuEntry := range *menu {
|
|
|
|
if menuEntry.IsSameResource(inme) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
descendantFound := n.isSameAsDescendantMenu(inme, menuEntry)
|
|
|
|
if descendantFound {
|
|
|
|
return descendantFound
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 02:59:19 -04:00
|
|
|
return false
|
|
|
|
}
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2015-10-11 23:51:04 -04:00
|
|
|
// Param is a convenience method to do lookups in Site's Params map.
|
|
|
|
//
|
2016-10-17 13:30:21 -04:00
|
|
|
// This method is also implemented on Page and SiteInfo.
|
2015-10-11 23:51:04 -04:00
|
|
|
func (n *Node) Param(key interface{}) (interface{}, error) {
|
2016-10-17 13:30:21 -04:00
|
|
|
return n.Site.Param(key)
|
2015-10-11 23:51:04 -04:00
|
|
|
}
|
|
|
|
|
2015-01-18 20:40:34 -05:00
|
|
|
func (n *Node) Hugo() *HugoInfo {
|
2015-01-18 21:06:07 -05:00
|
|
|
return hugoInfo
|
2015-01-18 20:40:34 -05:00
|
|
|
}
|
|
|
|
|
2014-10-18 14:25:10 -04:00
|
|
|
func (n *Node) isSameAsDescendantMenu(inme *MenuEntry, parent *MenuEntry) bool {
|
|
|
|
if parent.HasChildren() {
|
|
|
|
for _, child := range parent.Children {
|
|
|
|
if child.IsSameResource(inme) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
descendantFound := n.isSameAsDescendantMenu(inme, child)
|
|
|
|
if descendantFound {
|
|
|
|
return descendantFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 02:59:19 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) RSSlink() template.HTML {
|
2014-01-29 17:50:31 -05:00
|
|
|
return n.RSSLink
|
2013-08-13 19:39:24 -04:00
|
|
|
}
|
|
|
|
|
2014-08-19 21:27:13 -04:00
|
|
|
func (n *Node) IsNode() bool {
|
2016-10-30 12:59:24 -04:00
|
|
|
return n.NodeType.IsNode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) IsHome() bool {
|
|
|
|
return n.NodeType == NodeHome
|
2014-08-19 21:27:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) IsPage() bool {
|
2016-10-30 12:59:24 -04:00
|
|
|
return n.NodeType == NodePage
|
2014-08-19 21:27:13 -04:00
|
|
|
}
|
|
|
|
|
Provide (relative) reference funcs & shortcodes.
- `.Ref` and `.RelRef` take a reference (the logical filename for a
page, including extension and/or a document fragment ID) and return
a permalink (or relative permalink) to the referenced document.
- If the reference is a page name (such as `about.md`), the page
will be discovered and the permalink will be returned: `/about/`
- If the reference is a page name with a fragment (such as
`about.md#who`), the page will be discovered and used to add the
`page.UniqueID()` to the resulting fragment and permalink:
`/about/#who:deadbeef`.
- If the reference is a fragment and `.*Ref` has been called from
a `Node` or `SiteInfo`, it will be returned as is: `#who`.
- If the reference is a fragment and `.*Ref` has been called from
a `Page`, it will be returned with the page’s unique ID:
`#who:deadbeef`.
- `.*Ref` can be called from either `Node`, `SiteInfo` (e.g.,
`Node.Site`), `Page` objects, or `ShortcodeWithPage` objects in
templates.
- `.*Ref` cannot be used in content, so two shortcodes have been
created to provide the functionality to content: `ref` and `relref`.
These are intended to be used within markup, like `[Who]({{% ref
about.md#who %}})` or `<a href="{{% ref about.md#who %}}">Who</a>`.
- There are also `ref` and `relref` template functions (used to create
the shortcodes) that expect a `Page` or `Node` object and the
reference string (e.g., `{{ relref . "about.md" }}` or `{{
"about.md" | ref . }}`). It actually looks for `.*Ref` as defined on
`Node` or `Page` objects.
- Shortcode handling had to use a *differently unique* wrapper in
`createShortcodePlaceholder` because of the way that the `ref` and
`relref` are intended to be used in content.
2014-11-24 01:15:34 -05:00
|
|
|
func (n *Node) Ref(ref string) (string, error) {
|
|
|
|
return n.Site.Ref(ref, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) RelRef(ref string) (string, error) {
|
|
|
|
return n.Site.RelRef(ref, nil)
|
|
|
|
}
|
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
type URLPath struct {
|
|
|
|
URL string
|
2015-10-16 23:45:49 -04:00
|
|
|
Permalink string
|
2014-01-29 17:50:31 -05:00
|
|
|
Slug string
|
|
|
|
Section string
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2014-12-26 15:18:26 -05:00
|
|
|
|
2016-07-26 13:04:10 -04:00
|
|
|
func (n *Node) URL() string {
|
2016-08-08 07:55:18 -04:00
|
|
|
return n.addLangPathPrefix(n.URLPath.URL)
|
2016-07-26 13:04:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Permalink() string {
|
2016-10-24 07:45:30 -04:00
|
|
|
return n.Site.permalink(n.URL())
|
2016-07-26 13:04:10 -04:00
|
|
|
}
|
|
|
|
|
2014-12-26 15:18:26 -05:00
|
|
|
// Scratch returns the writable context associated with this Node.
|
|
|
|
func (n *Node) Scratch() *Scratch {
|
|
|
|
if n.scratch == nil {
|
|
|
|
n.scratch = newScratch()
|
|
|
|
}
|
|
|
|
return n.scratch
|
|
|
|
}
|
2016-07-26 13:04:10 -04:00
|
|
|
|
2016-08-07 16:01:55 -04:00
|
|
|
func (n *Node) Language() *helpers.Language {
|
2016-08-07 08:03:03 -04:00
|
|
|
n.initLanguage()
|
2016-07-26 13:04:10 -04:00
|
|
|
return n.language
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Lang() string {
|
2016-08-06 08:51:50 -04:00
|
|
|
// When set, Language can be different from lang in the case where there is a
|
|
|
|
// content file (doc.sv.md) with language indicator, but there is no language
|
|
|
|
// config for that language. Then the language will fall back on the site default.
|
2016-07-26 13:04:10 -04:00
|
|
|
if n.Language() != nil {
|
|
|
|
return n.Language().Lang
|
|
|
|
}
|
|
|
|
return n.lang
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func (n *Node) shouldAddLanguagePrefix() bool {
|
|
|
|
if !n.Site.IsMultiLingual() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.Lang() == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !n.Site.defaultContentLanguageInSubdir && n.Lang() == n.Site.multilingual.DefaultLang.Lang {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-08-07 08:03:03 -04:00
|
|
|
func (n *Node) initLanguage() {
|
|
|
|
n.languageInit.Do(func() {
|
2016-08-08 07:55:18 -04:00
|
|
|
if n.language != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-08-07 08:03:03 -04:00
|
|
|
pageLang := n.lang
|
|
|
|
ml := n.Site.multilingual
|
|
|
|
if ml == nil {
|
|
|
|
panic("Multilanguage not set")
|
|
|
|
}
|
|
|
|
if pageLang == "" {
|
|
|
|
n.language = ml.DefaultLang
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
language := ml.Language(pageLang)
|
|
|
|
|
|
|
|
if language == nil {
|
2016-08-07 18:12:06 -04:00
|
|
|
// It can be a file named stefano.chiodino.md.
|
2016-08-07 08:03:03 -04:00
|
|
|
jww.WARN.Printf("Page language (if it is that) not found in multilang setup: %s.", pageLang)
|
|
|
|
language = ml.DefaultLang
|
|
|
|
}
|
|
|
|
|
|
|
|
n.language = language
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-04 06:06:29 -04:00
|
|
|
func (n *Node) LanguagePrefix() string {
|
|
|
|
return n.Site.LanguagePrefix
|
|
|
|
}
|
|
|
|
|
2016-07-26 13:04:10 -04:00
|
|
|
// AllTranslations returns all translations, including the current Node.
|
|
|
|
// Note that this and the one below is kind of a temporary hack before #2297 is solved.
|
|
|
|
func (n *Node) AllTranslations() Nodes {
|
|
|
|
n.initTranslations()
|
|
|
|
return n.translations
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translations returns the translations excluding the current Node.
|
|
|
|
func (n *Node) Translations() Nodes {
|
|
|
|
n.initTranslations()
|
|
|
|
translations := make(Nodes, 0)
|
|
|
|
|
|
|
|
for _, t := range n.translations {
|
|
|
|
|
|
|
|
if t != n {
|
|
|
|
translations = append(translations, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return translations
|
|
|
|
}
|
|
|
|
|
2016-08-09 08:26:55 -04:00
|
|
|
// IsTranslated returns whether this node is translated to
|
|
|
|
// other language(s).
|
|
|
|
func (n *Node) IsTranslated() bool {
|
|
|
|
n.initTranslations()
|
|
|
|
return len(n.translations) > 1
|
|
|
|
}
|
|
|
|
|
2016-07-26 13:04:10 -04:00
|
|
|
func (n *Node) initTranslations() {
|
|
|
|
n.translationsInit.Do(func() {
|
2016-08-09 14:06:15 -04:00
|
|
|
n.translations = n.Site.owner.getNodes(n.nodeID)
|
2016-07-26 13:04:10 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func (n *Node) addLangPathPrefix(outfile string) string {
|
|
|
|
return n.addLangPathPrefixIfFlagSet(outfile, n.shouldAddLanguagePrefix())
|
|
|
|
}
|
2016-07-28 03:30:58 -04:00
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func (n *Node) addLangPathPrefixIfFlagSet(outfile string, should bool) string {
|
2016-07-28 03:30:58 -04:00
|
|
|
if helpers.IsAbsURL(outfile) {
|
|
|
|
return outfile
|
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
if !should {
|
2016-07-26 13:04:10 -04:00
|
|
|
return outfile
|
|
|
|
}
|
2016-08-08 07:55:18 -04:00
|
|
|
|
|
|
|
hadSlashSuffix := strings.HasSuffix(outfile, "/")
|
|
|
|
|
|
|
|
outfile = "/" + path.Join(n.Lang(), outfile)
|
2016-07-28 03:30:58 -04:00
|
|
|
if hadSlashSuffix {
|
|
|
|
outfile += "/"
|
|
|
|
}
|
|
|
|
return outfile
|
2016-07-26 13:04:10 -04:00
|
|
|
}
|
|
|
|
|
2016-08-08 07:55:18 -04:00
|
|
|
func (n *Node) addLangFilepathPrefix(outfile string) string {
|
|
|
|
if outfile == "" {
|
|
|
|
outfile = helpers.FilePathSeparator
|
|
|
|
}
|
|
|
|
if !n.shouldAddLanguagePrefix() {
|
2016-07-26 13:04:10 -04:00
|
|
|
return outfile
|
|
|
|
}
|
2016-08-08 07:55:18 -04:00
|
|
|
return helpers.FilePathSeparator + filepath.Join(n.Lang(), outfile)
|
2016-07-26 13:04:10 -04:00
|
|
|
}
|
2016-10-30 12:59:24 -04:00
|
|
|
|
2016-10-31 14:53:33 -04:00
|
|
|
func sectionsFromFilename(filename string) []string {
|
|
|
|
dir, _ := filepath.Split(filename)
|
2016-11-01 11:47:15 -04:00
|
|
|
dir = strings.TrimSuffix(dir, helpers.FilePathSeparator)
|
|
|
|
sections := strings.Split(dir, helpers.FilePathSeparator)
|
|
|
|
return sections
|
2016-10-31 14:53:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bep) np node identificator
|
2016-10-30 12:59:24 -04:00
|
|
|
func nodeTypeFromFilename(filename string) NodeType {
|
2016-10-31 14:53:33 -04:00
|
|
|
|
2016-10-31 13:03:02 -04:00
|
|
|
if !strings.Contains(filename, "_node") {
|
2016-10-30 12:59:24 -04:00
|
|
|
return NodePage
|
|
|
|
}
|
2016-10-31 13:03:02 -04:00
|
|
|
|
|
|
|
if strings.HasPrefix(filename, "_node") {
|
|
|
|
return NodeHome
|
|
|
|
}
|
|
|
|
|
2016-10-31 14:53:33 -04:00
|
|
|
// We don't know enough yet to determine the type.
|
|
|
|
return NodeUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) setNodeTypeVars(s *Site) {
|
|
|
|
// TODO(bep) np taxonomies etc.
|
|
|
|
if p.NodeType == NodeUnknown {
|
2016-11-01 11:47:15 -04:00
|
|
|
// This is either a taxonomy list, taxonomy term or a section
|
|
|
|
nodeType := s.nodeTypeFromSections(p.sections)
|
|
|
|
|
|
|
|
if nodeType == NodeUnknown {
|
|
|
|
panic(fmt.Sprintf("Unable to determine node type from %q", p.sections))
|
2016-10-31 14:53:33 -04:00
|
|
|
}
|
|
|
|
|
2016-11-01 11:47:15 -04:00
|
|
|
p.NodeType = nodeType
|
|
|
|
|
2016-10-31 14:53:33 -04:00
|
|
|
}
|
|
|
|
// TODO(bep) np node URL
|
|
|
|
// Set Node URL
|
|
|
|
switch p.NodeType {
|
|
|
|
case NodeHome:
|
|
|
|
p.URLPath.URL = ""
|
|
|
|
case NodeSection:
|
|
|
|
p.URLPath.URL = p.Section()
|
|
|
|
case NodeTaxonomy:
|
|
|
|
p.URLPath.URL = path.Join(p.sections...)
|
2016-11-01 11:47:15 -04:00
|
|
|
case NodeTaxonomyTerms:
|
|
|
|
p.URLPath.URL = path.Join(p.sections...)
|
2016-10-31 14:53:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.site = s
|
|
|
|
|
2016-10-30 12:59:24 -04:00
|
|
|
}
|