Skip to content

Commit

Permalink
Add Translations and AllTranslations to Node
Browse files Browse the repository at this point in the history
This commit also consolidates URLs on Node vs Page, so now .Permalink should be interoperable.

Note that this implementations should be fairly short-livded, waiting for #2297, but the API should be stable.
  • Loading branch information
bep committed Jul 30, 2016
1 parent 6346ca5 commit 1489121
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 74 deletions.
2 changes: 1 addition & 1 deletion hugolib/menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func TestHomeNodeMenu(t *testing.T) {
s := setupMenuTests(t, menuPageSources)

home := s.newHomeNode()
homeMenuEntry := &MenuEntry{Name: home.Title, URL: home.URL}
homeMenuEntry := &MenuEntry{Name: home.Title, URL: home.URL()}

for i, this := range []struct {
menu string
Expand Down
109 changes: 107 additions & 2 deletions hugolib/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ package hugolib

import (
"html/template"
"path"
"path/filepath"
"sort"
"sync"
"time"

Expand All @@ -38,6 +41,27 @@ type Node struct {
paginator *Pager
paginatorInit sync.Once
scratch *Scratch

language *Language
lang string // TODO(bep) multilingo

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]
}

func (n *Node) Now() time.Time {
Expand All @@ -46,7 +70,7 @@ func (n *Node) Now() time.Time {

func (n *Node) HasMenuCurrent(menuID string, inme *MenuEntry) bool {
if inme.HasChildren() {
me := MenuEntry{Name: n.Title, URL: n.URL}
me := MenuEntry{Name: n.Title, URL: n.URL()}

for _, child := range inme.Children {
if me.IsSameResource(child) {
Expand All @@ -63,7 +87,7 @@ func (n *Node) HasMenuCurrent(menuID string, inme *MenuEntry) bool {

func (n *Node) IsMenuCurrent(menuID string, inme *MenuEntry) bool {

me := MenuEntry{Name: n.Title, URL: n.Site.createNodeMenuEntryURL(n.URL)}
me := MenuEntry{Name: n.Title, URL: n.Site.createNodeMenuEntryURL(n.URL())}

if !me.IsSameResource(inme) {
return false
Expand Down Expand Up @@ -138,17 +162,98 @@ func (n *Node) RelRef(ref string) (string, error) {
return n.Site.RelRef(ref, nil)
}

// TODO(bep) multilingo some of these are now hidden. Consider unexport.
type URLPath struct {
URL string
Permalink string
Slug string
Section string
}

func (n *Node) URL() string {
return n.addMultilingualWebPrefix(n.URLPath.URL)
}

func (n *Node) Permalink() string {
return permalink(n.URL())
}

// Scratch returns the writable context associated with this Node.
func (n *Node) Scratch() *Scratch {
if n.scratch == nil {
n.scratch = newScratch()
}
return n.scratch
}

// TODO(bep) multilingo consolidate. See Page.
func (n *Node) Language() *Language {
return n.language
}

func (n *Node) Lang() string {
if n.Language() != nil {
return n.Language().Lang
}
return n.lang
}

// 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
}

func (n *Node) initTranslations() {
n.translationsInit.Do(func() {
if n.translations != nil {
return
}
n.translations = make(Nodes, 0)
for _, l := range n.Site.Languages {
if l == n.language {
n.translations = append(n.translations, n)
continue
}

translation := *n
translation.language = l
translation.translations = n.translations
n.translations = append(n.translations, &translation)
}

sort.Sort(n.translations)
})
}

func (n *Node) addMultilingualWebPrefix(outfile string) string {
lang := n.Lang()
if lang == "" || !n.Site.Multilingual {
return outfile
}
return "/" + path.Join(lang, outfile)
}

func (n *Node) addMultilingualFilesystemPrefix(outfile string) string {
lang := n.Lang()
if lang == "" || !n.Site.Multilingual {
return outfile
}
return string(filepath.Separator) + filepath.Join(lang, outfile)
}
32 changes: 4 additions & 28 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ type Page struct {
translations Pages
extension string
contentType string
lang string
language *Language
renderable bool
Layout string
layoutsCalculated []string
Expand Down Expand Up @@ -431,7 +429,7 @@ func (p *Page) permalink() (*url.URL, error) {
baseURL := string(p.Site.BaseURL)
dir := strings.TrimSpace(helpers.MakePath(filepath.ToSlash(strings.ToLower(p.Source.Dir()))))
pSlug := strings.TrimSpace(helpers.URLize(p.Slug))
pURL := strings.TrimSpace(helpers.URLize(p.URL))
pURL := strings.TrimSpace(helpers.URLize(p.URLPath.URL))
var permalink string
var err error

Expand Down Expand Up @@ -467,14 +465,6 @@ func (p *Page) Extension() string {
return viper.GetString("DefaultExtension")
}

// TODO(bep) multilingo consolidate
func (p *Page) Language() *Language {
return p.language
}
func (p *Page) Lang() string {
return p.lang
}

// AllTranslations returns all translations, including the current Page.
func (p *Page) AllTranslations() Pages {
return p.translations
Expand Down Expand Up @@ -591,7 +581,7 @@ func (p *Page) update(f interface{}) error {
if url := cast.ToString(v); strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
return fmt.Errorf("Only relative URLs are supported, %v provided", url)
}
p.URL = cast.ToString(v)
p.URLPath.URL = cast.ToString(v)
case "type":
p.contentType = cast.ToString(v)
case "extension", "ext":
Expand Down Expand Up @@ -1008,8 +998,8 @@ func (p *Page) FullFilePath() string {

func (p *Page) TargetPath() (outfile string) {
// Always use URL if it's specified
if len(strings.TrimSpace(p.URL)) > 2 {
outfile = strings.TrimSpace(p.URL)
if len(strings.TrimSpace(p.URLPath.URL)) > 2 {
outfile = strings.TrimSpace(p.URLPath.URL)

if strings.HasSuffix(outfile, "/") {
outfile = outfile + "index.html"
Expand Down Expand Up @@ -1042,17 +1032,3 @@ func (p *Page) TargetPath() (outfile string) {

return p.addMultilingualFilesystemPrefix(filepath.Join(strings.ToLower(helpers.MakePath(p.Source.Dir())), strings.TrimSpace(outfile)))
}

func (p *Page) addMultilingualWebPrefix(outfile string) string {
if p.Lang() == "" {
return outfile
}
return "/" + path.Join(p.Lang(), outfile)
}

func (p *Page) addMultilingualFilesystemPrefix(outfile string) string {
if p.Lang() == "" {
return outfile
}
return string(filepath.Separator) + filepath.Join(p.Lang(), outfile)
}
11 changes: 6 additions & 5 deletions hugolib/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ package hugolib
import (
"errors"
"fmt"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
"github.com/spf13/viper"
"html/template"
"math"
"path"
"reflect"
"strings"

"github.com/spf13/cast"
"github.com/spf13/hugo/helpers"
"github.com/spf13/viper"
)

// Pager represents one of the elements in a paginator.
Expand Down Expand Up @@ -274,7 +275,7 @@ func (n *Node) Paginator(options ...interface{}) (*Pager, error) {
return
}

pagers, err := paginatePages(n.Data["Pages"], pagerSize, n.URL)
pagers, err := paginatePages(n.Data["Pages"], pagerSize, n.URL())

if err != nil {
initError = err
Expand Down Expand Up @@ -324,7 +325,7 @@ func (n *Node) Paginate(seq interface{}, options ...interface{}) (*Pager, error)
if n.paginator != nil {
return
}
pagers, err := paginatePages(seq, pagerSize, n.URL)
pagers, err := paginatePages(seq, pagerSize, n.URL())

if err != nil {
initError = err
Expand Down
Loading

0 comments on commit 1489121

Please sign in to comment.