2014-10-18 14:25:10 -04:00
|
|
|
// Copyright © 2013-14 Steve Francia <spf@spf13.com>.
|
2013-07-04 11:32:55 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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 (
|
2015-03-18 06:30:23 -04:00
|
|
|
"github.com/spf13/hugo/helpers"
|
2014-01-29 17:50:31 -05:00
|
|
|
"html/template"
|
2014-12-27 08:11:19 -05:00
|
|
|
"sync"
|
2014-01-29 17:50:31 -05:00
|
|
|
"time"
|
2015-03-31 01:44:08 -04:00
|
|
|
"strings"
|
2013-07-04 11:32:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
2014-01-29 17:50:31 -05:00
|
|
|
RSSLink template.HTML
|
2014-05-28 19:11:54 -04:00
|
|
|
Site *SiteInfo
|
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
|
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
|
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() {
|
2015-03-18 01:16:54 -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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func (n *Node) IsMenuCurrent(menuID string, inme *MenuEntry) bool {
|
2015-03-31 01:44:08 -04:00
|
|
|
s := n.Site
|
2014-10-18 14:25:10 -04:00
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
me := MenuEntry{Name: n.Title, URL: n.URL}
|
2015-03-31 01:44:08 -04:00
|
|
|
|
|
|
|
if strings.HasPrefix(me.URL, "/") {
|
|
|
|
// make it match the nodes
|
|
|
|
menuEntryURL := me.URL
|
|
|
|
menuEntryURL = helpers.URLizeAndPrep(menuEntryURL)
|
|
|
|
if !s.canonifyURLs {
|
|
|
|
menuEntryURL = helpers.AddContextRoot(string(s.BaseURL), menuEntryURL)
|
|
|
|
}
|
|
|
|
me.URL = menuEntryURL
|
|
|
|
}
|
|
|
|
|
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-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 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) IsPage() bool {
|
|
|
|
return !n.IsNode()
|
|
|
|
}
|
|
|
|
|
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
|
2014-01-29 17:50:31 -05:00
|
|
|
Permalink template.HTML
|
|
|
|
Slug string
|
|
|
|
Section string
|
2013-07-04 11:32:55 -04:00
|
|
|
}
|
2014-12-26 15:18:26 -05:00
|
|
|
|
2015-03-18 01:16:54 -04:00
|
|
|
// Url is deprecated. Will be removed in 0.15.
|
|
|
|
func (n *Node) Url() string {
|
|
|
|
helpers.Deprecated("Node", ".Url", ".URL")
|
|
|
|
return n.URL
|
|
|
|
}
|
|
|
|
|
2015-03-18 06:30:23 -04:00
|
|
|
// UrlPath is deprecated. Will be removed in 0.15.
|
|
|
|
func (n *Node) UrlPath() URLPath {
|
|
|
|
helpers.Deprecated("Node", ".UrlPath", ".URLPath")
|
|
|
|
return n.URLPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Url is deprecated. Will be removed in 0.15.
|
|
|
|
func (up URLPath) Url() string {
|
|
|
|
helpers.Deprecated("URLPath", ".Url", ".URL")
|
|
|
|
return up.URL
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|