diff options
-rw-r--r-- | hugolib/embedded_shortcodes_test.go | 4 | ||||
-rw-r--r-- | hugolib/gitinfo.go | 5 | ||||
-rw-r--r-- | hugolib/hugo_sites.go | 35 | ||||
-rw-r--r-- | hugolib/hugo_sites_build.go | 8 | ||||
-rw-r--r-- | hugolib/hugo_sites_build_test.go | 58 | ||||
-rw-r--r-- | hugolib/menu.go | 2 | ||||
-rw-r--r-- | hugolib/menu_test.go | 66 | ||||
-rw-r--r-- | hugolib/node.go | 334 | ||||
-rw-r--r-- | hugolib/node_as_page_test.go | 6 | ||||
-rw-r--r-- | hugolib/page.go | 429 | ||||
-rw-r--r-- | hugolib/pageSort_test.go | 11 | ||||
-rw-r--r-- | hugolib/page_collections.go | 5 | ||||
-rw-r--r-- | hugolib/page_permalink_test.go | 10 | ||||
-rw-r--r-- | hugolib/page_test.go | 30 | ||||
-rw-r--r-- | hugolib/pagination.go | 28 | ||||
-rw-r--r-- | hugolib/pagination_test.go | 10 | ||||
-rw-r--r-- | hugolib/shortcode_test.go | 4 | ||||
-rw-r--r-- | hugolib/site.go | 4 | ||||
-rw-r--r-- | hugolib/siteJSONEncode_test.go | 2 | ||||
-rw-r--r-- | hugolib/site_render.go | 3 | ||||
-rw-r--r-- | hugolib/site_test.go | 42 | ||||
-rw-r--r-- | hugolib/taxonomy.go | 9 |
22 files changed, 538 insertions, 567 deletions
diff --git a/hugolib/embedded_shortcodes_test.go b/hugolib/embedded_shortcodes_test.go index 160e36fd5..f61ed2383 100644 --- a/hugolib/embedded_shortcodes_test.go +++ b/hugolib/embedded_shortcodes_test.go @@ -66,9 +66,9 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) { require.NoError(t, err) require.NoError(t, sites.Build(BuildCfg{})) - require.Len(t, sites.Sites[0].regularPages, 1) + require.Len(t, sites.Sites[0].RegularPages, 1) - output := string(sites.Sites[0].regularPages[0].Content) + output := string(sites.Sites[0].RegularPages[0].Content) if !strings.Contains(output, expected) { t.Errorf("Got\n%q\nExpected\n%q", output, expected) diff --git a/hugolib/gitinfo.go b/hugolib/gitinfo.go index 520b7e5d0..b2119048e 100644 --- a/hugolib/gitinfo.go +++ b/hugolib/gitinfo.go @@ -52,7 +52,10 @@ func (h *HugoSites) assembleGitInfo() { s := h.Sites[0] for _, p := range s.AllPages { - // TODO(bep) np consider other nodes + if p.Path() == "" { + // Home page etc. with no content file. + continue + } // Git normalizes file paths on this form: filename := path.Join(contentRoot, contentDir, filepath.ToSlash(p.Path())) g, ok := gitMap[filename] diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go index 78137f6c1..674a0645a 100644 --- a/hugolib/hugo_sites.go +++ b/hugolib/hugo_sites.go @@ -211,8 +211,6 @@ func (h *HugoSites) assignMissingTranslations() error { // createMissingPages creates home page, taxonomies etc. that isnt't created as an // effect of having a content file. func (h *HugoSites) createMissingPages() error { - // TODO(bep) np check node title etc. - var newPages Pages for _, s := range h.Sites { @@ -306,12 +304,11 @@ func (h *HugoSites) createMissingPages() error { // Move the new* methods after cleanup in site.go func (s *Site) newNodePage(typ string) *Page { return &Page{ - Kind: typ, - Node: Node{ - Data: make(map[string]interface{}), - Site: &s.Info, - language: s.Language, - }, site: s} + Kind: typ, + Data: make(map[string]interface{}), + Site: &s.Info, + language: s.Language, + site: s} } func (s *Site) newHomePage() *Page { @@ -321,8 +318,6 @@ func (s *Site) newHomePage() *Page { p.Data["Pages"] = pages p.Pages = pages s.setPageURLs(p, "/") - // TODO(bep) np check Data pages - // TODO(bep) np check setURLs return p } @@ -426,23 +421,7 @@ func (h *HugoSites) setupTranslations() { } } -// preRender performs build tasks that need to be done as late as possible. -// Shortcode handling is the main task in here. -// TODO(bep) We need to look at the whole handler-chain construct with he below in mind. -// TODO(bep) np clean -func (h *HugoSites) preRender(cfg BuildCfg, changed whatChanged) error { - - for _, s := range h.Sites { - if err := s.setCurrentLanguageConfig(); err != nil { - return err - } - s.preparePagesForRender(cfg, changed) - } - - return nil -} - -func (s *Site) preparePagesForRender(cfg BuildCfg, changed whatChanged) { +func (s *Site) preparePagesForRender(cfg *BuildCfg) { pageChan := make(chan *Page) wg := &sync.WaitGroup{} @@ -452,7 +431,7 @@ func (s *Site) preparePagesForRender(cfg BuildCfg, changed whatChanged) { defer wg.Done() for p := range pages { - if !changed.other && p.rendered { + if !cfg.whatChanged.other && p.rendered { // No need to process it again. continue } diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go index 32185dd4f..4f9ad4e04 100644 --- a/hugolib/hugo_sites_build.go +++ b/hugolib/hugo_sites_build.go @@ -169,11 +169,15 @@ func (h *HugoSites) assemble(config *BuildCfg) error { return err } - if err := h.preRender(*config, whatChanged{source: true, other: true}); err != nil { - return err + for _, s := range h.Sites { + if err := s.setCurrentLanguageConfig(); err != nil { + return err + } + s.preparePagesForRender(config) } return nil + } func (h *HugoSites) render(config *BuildCfg) error { diff --git a/hugolib/hugo_sites_build_test.go b/hugolib/hugo_sites_build_test.go index 7ef59bc26..3498de67f 100644 --- a/hugolib/hugo_sites_build_test.go +++ b/hugolib/hugo_sites_build_test.go @@ -83,8 +83,8 @@ func doTestMultiSitesMainLangInRoot(t *testing.T, defaultInSubDir bool) { require.Equal(t, "/blog/en/foo", enSite.Info.pathSpec.RelURL("foo", true)) - doc1en := enSite.regularPages[0] - doc1fr := frSite.regularPages[0] + doc1en := enSite.RegularPages[0] + doc1fr := frSite.RegularPages[0] enPerm, _ := doc1en.Permalink() enRelPerm, _ := doc1en.RelPermalink() @@ -216,24 +216,24 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { assert.Equal(t, "en", enSite.Language.Lang) - if len(enSite.regularPages) != 4 { + if len(enSite.RegularPages) != 4 { t.Fatal("Expected 4 english pages") } assert.Len(t, enSite.Source.Files(), 14, "should have 13 source files") assert.Len(t, enSite.AllPages, 28, "should have 28 total pages (including translations and index types)") - doc1en := enSite.regularPages[0] + doc1en := enSite.RegularPages[0] permalink, err := doc1en.Permalink() assert.NoError(t, err, "permalink call failed") assert.Equal(t, "http://example.com/blog/en/sect/doc1-slug/", permalink, "invalid doc1.en permalink") assert.Len(t, doc1en.Translations(), 1, "doc1-en should have one translation, excluding itself") - doc2 := enSite.regularPages[1] + doc2 := enSite.RegularPages[1] permalink, err = doc2.Permalink() assert.NoError(t, err, "permalink call failed") assert.Equal(t, "http://example.com/blog/en/sect/doc2/", permalink, "invalid doc2 permalink") - doc3 := enSite.regularPages[2] + doc3 := enSite.RegularPages[2] permalink, err = doc3.Permalink() assert.NoError(t, err, "permalink call failed") // Note that /superbob is a custom URL set in frontmatter. @@ -276,10 +276,10 @@ func doTestMultiSitesBuild(t *testing.T, configTemplate, configSuffix string) { frSite := sites.Sites[1] assert.Equal(t, "fr", frSite.Language.Lang) - assert.Len(t, frSite.regularPages, 3, "should have 3 pages") + assert.Len(t, frSite.RegularPages, 3, "should have 3 pages") assert.Len(t, frSite.AllPages, 28, "should have 28 total pages (including translations and nodes)") - for _, frenchPage := range frSite.regularPages { + for _, frenchPage := range frSite.RegularPages { assert.Equal(t, "fr", frenchPage.Lang()) } @@ -386,8 +386,8 @@ func TestMultiSitesRebuild(t *testing.T) { enSite := sites.Sites[0] frSite := sites.Sites[1] - require.Len(t, enSite.regularPages, 4) - require.Len(t, frSite.regularPages, 3) + require.Len(t, enSite.RegularPages, 4) + require.Len(t, frSite.RegularPages, 3) // Verify translations assertFileContent(t, "public/en/sect/doc1-slug/index.html", true, "Hello") @@ -413,7 +413,7 @@ func TestMultiSitesRebuild(t *testing.T) { nil, []fsnotify.Event{{Name: "content/sect/doc2.en.md", Op: fsnotify.Remove}}, func(t *testing.T) { - require.Len(t, enSite.regularPages, 3, "1 en removed") + require.Len(t, enSite.RegularPages, 3, "1 en removed") // Check build stats require.Equal(t, 1, enSite.draftCount, "Draft") @@ -436,12 +436,12 @@ func TestMultiSitesRebuild(t *testing.T) { {Name: "content/new1.fr.md", Op: fsnotify.Create}, }, func(t *testing.T) { - require.Len(t, enSite.regularPages, 5) + require.Len(t, enSite.RegularPages, 5) require.Len(t, enSite.AllPages, 30) - require.Len(t, frSite.regularPages, 4) - require.Equal(t, "new_fr_1", frSite.regularPages[3].Title) - require.Equal(t, "new_en_2", enSite.regularPages[0].Title) - require.Equal(t, "new_en_1", enSite.regularPages[1].Title) + require.Len(t, frSite.RegularPages, 4) + require.Equal(t, "new_fr_1", frSite.RegularPages[3].Title) + require.Equal(t, "new_en_2", enSite.RegularPages[0].Title) + require.Equal(t, "new_en_1", enSite.RegularPages[1].Title) rendered := readDestination(t, "public/en/new1/index.html") require.True(t, strings.Contains(rendered, "new_en_1"), rendered) @@ -456,7 +456,7 @@ func TestMultiSitesRebuild(t *testing.T) { }, []fsnotify.Event{{Name: "content/sect/doc1.en.md", Op: fsnotify.Write}}, func(t *testing.T) { - require.Len(t, enSite.regularPages, 5) + require.Len(t, enSite.RegularPages, 5) doc1 := readDestination(t, "public/en/sect/doc1-slug/index.html") require.True(t, strings.Contains(doc1, "CHANGED"), doc1) @@ -474,8 +474,8 @@ func TestMultiSitesRebuild(t *testing.T) { {Name: "content/new1.en.md", Op: fsnotify.Rename}, }, func(t *testing.T) { - require.Len(t, enSite.regularPages, 5, "Rename") - require.Equal(t, "new_en_1", enSite.regularPages[1].Title) + require.Len(t, enSite.RegularPages, 5, "Rename") + require.Equal(t, "new_en_1", enSite.RegularPages[1].Title) rendered := readDestination(t, "public/en/new1renamed/index.html") require.True(t, strings.Contains(rendered, "new_en_1"), rendered) }}, @@ -489,9 +489,9 @@ func TestMultiSitesRebuild(t *testing.T) { }, []fsnotify.Event{{Name: "layouts/_default/single.html", Op: fsnotify.Write}}, func(t *testing.T) { - require.Len(t, enSite.regularPages, 5) + require.Len(t, enSite.RegularPages, 5) require.Len(t, enSite.AllPages, 30) - require.Len(t, frSite.regularPages, 4) + require.Len(t, frSite.RegularPages, 4) doc1 := readDestination(t, "public/en/sect/doc1-slug/index.html") require.True(t, strings.Contains(doc1, "Template Changed"), doc1) }, @@ -506,9 +506,9 @@ func TestMultiSitesRebuild(t *testing.T) { }, []fsnotify.Event{{Name: "i18n/fr.yaml", Op: fsnotify.Write}}, func(t *testing.T) { - require.Len(t, enSite.regularPages, 5) + require.Len(t, enSite.RegularPages, 5) require.Len(t, enSite.AllPages, 30) - require.Len(t, frSite.regularPages, 4) + require.Len(t, frSite.RegularPages, 4) docEn := readDestination(t, "public/en/sect/doc1-slug/index.html") require.True(t, strings.Contains(docEn, "Hello"), "No Hello") docFr := readDestination(t, "public/fr/sect/doc1/index.html") @@ -530,9 +530,9 @@ func TestMultiSitesRebuild(t *testing.T) { {Name: "layouts/shortcodes/shortcode.html", Op: fsnotify.Write}, }, func(t *testing.T) { - require.Len(t, enSite.regularPages, 5) + require.Len(t, enSite.RegularPages, 5) require.Len(t, enSite.AllPages, 30) - require.Len(t, frSite.regularPages, 4) + require.Len(t, frSite.RegularPages, 4) assertFileContent(t, "public/fr/sect/doc1/index.html", true, "Single", "Modified Shortcode: Salut") assertFileContent(t, "public/en/sect/doc1-slug/index.html", true, "Single", "Modified Shortcode: Hello") }, @@ -626,12 +626,12 @@ title = "Svenska" require.Len(t, homeEn.Translations(), 4) require.Equal(t, "sv", homeEn.Translations()[0].Lang()) - require.Len(t, enSite.regularPages, 4) - require.Len(t, frSite.regularPages, 3) + require.Len(t, enSite.RegularPages, 4) + require.Len(t, frSite.RegularPages, 3) // Veriy Swedish site - require.Len(t, svSite.regularPages, 1) - svPage := svSite.regularPages[0] + require.Len(t, svSite.RegularPages, 1) + svPage := svSite.RegularPages[0] require.Equal(t, "Swedish Contentfile", svPage.Title) require.Equal(t, "sv", svPage.Lang()) require.Len(t, svPage.Translations(), 2) diff --git a/hugolib/menu.go b/hugolib/menu.go index 35991b1c7..116545a9a 100644 --- a/hugolib/menu.go +++ b/hugolib/menu.go @@ -21,8 +21,6 @@ import ( "github.com/spf13/cast" ) -// TODO(bep) np menu entries in section content etc.? - // MenuEntry represents a menu item defined in either Page front matter // or in the site config. type MenuEntry struct { diff --git a/hugolib/menu_test.go b/hugolib/menu_test.go index 447273f0b..460440fa8 100644 --- a/hugolib/menu_test.go +++ b/hugolib/menu_test.go @@ -208,7 +208,7 @@ func doTestPageMenuWithIdentifier(t *testing.T, menuPageSources []source.ByteSou s := setupMenuTests(t, menuPageSources) - assert.Equal(t, 3, len(s.regularPages), "Not enough pages") + assert.Equal(t, 3, len(s.RegularPages), "Not enough pages") me1 := findTestMenuEntryByID(s, "m1", "i1") me2 := findTestMenuEntryByID(s, "m1", "i2") @@ -246,7 +246,7 @@ func doTestPageMenuWithDuplicateName(t *testing.T, menuPageSources []source.Byte s := setupMenuTests(t, menuPageSources) - assert.Equal(t, 3, len(s.regularPages), "Not enough pages") + assert.Equal(t, 3, len(s.RegularPages), "Not enough pages") me1 := findTestMenuEntryByName(s, "m1", "n1") me2 := findTestMenuEntryByName(s, "m1", "n2") @@ -264,13 +264,13 @@ func TestPageMenu(t *testing.T) { s := setupMenuTests(t, menuPageSources) - if len(s.regularPages) != 3 { - t.Fatalf("Posts not created, expected 3 got %d", len(s.regularPages)) + if len(s.RegularPages) != 3 { + t.Fatalf("Posts not created, expected 3 got %d", len(s.RegularPages)) } - first := s.regularPages[0] - second := s.regularPages[1] - third := s.regularPages[2] + first := s.RegularPages[0] + second := s.RegularPages[1] + third := s.RegularPages[2] pOne := findTestMenuEntryByName(s, "p_one", "One") pTwo := findTestMenuEntryByID(s, "p_two", "Two") @@ -290,6 +290,10 @@ func TestPageMenu(t *testing.T) { {"p_one", third, pTwo, false, false}, } { + if i != 4 { + continue + } + isMenuCurrent := this.page.IsMenuCurrent(this.menu, this.menuItem) hasMenuCurrent := this.page.HasMenuCurrent(this.menu, this.menuItem) @@ -358,9 +362,9 @@ Yaml Front Matter with Menu Pages`) {Name: filepath.FromSlash("sect/yaml1.md"), Content: ps1}, {Name: filepath.FromSlash("sect/yaml2.md"), Content: ps2}}) - p1 := s.regularPages[0] + p1 := s.RegularPages[0] assert.Len(t, p1.Menus(), 2, "List YAML") - p2 := s.regularPages[1] + p2 := s.RegularPages[1] assert.Len(t, p2.Menus(), 2, "Map YAML") } @@ -406,14 +410,14 @@ func doTestSectionPagesMenu(canonifyURLs bool, t *testing.T) { viper.Set("canonifyURLs", canonifyURLs) s := setupMenuTests(t, menuPageSectionsSources) - assert.Equal(t, 3, len(s.Sections)) + require.Equal(t, 3, len(s.Sections)) firstSectionPages := s.Sections["first"] - assert.Equal(t, 2, len(firstSectionPages)) + require.Equal(t, 2, len(firstSectionPages)) secondSectionPages := s.Sections["second-section"] - assert.Equal(t, 1, len(secondSectionPages)) + require.Equal(t, 1, len(secondSectionPages)) fishySectionPages := s.Sections["fish-and-chips"] - assert.Equal(t, 1, len(fishySectionPages)) + require.Equal(t, 1, len(fishySectionPages)) nodeFirst := s.getPage(KindSection, "first") require.NotNil(t, nodeFirst) @@ -426,33 +430,33 @@ func doTestSectionPagesMenu(canonifyURLs bool, t *testing.T) { secondSectionMenuEntry := findTestMenuEntryByID(s, "spm", "second-section") fishySectionMenuEntry := findTestMenuEntryByID(s, "spm", "Fish and Chips") - assert.NotNil(t, firstSectionMenuEntry) - assert.NotNil(t, secondSectionMenuEntry) - assert.NotNil(t, nodeFirst) - assert.NotNil(t, nodeSecond) - assert.NotNil(t, fishySectionMenuEntry) - assert.NotNil(t, nodeFishy) + require.NotNil(t, firstSectionMenuEntry) + require.NotNil(t, secondSectionMenuEntry) + require.NotNil(t, nodeFirst) + require.NotNil(t, nodeSecond) + require.NotNil(t, fishySectionMenuEntry) + require.NotNil(t, nodeFishy) - assert.True(t, nodeFirst.IsMenuCurrent("spm", firstSectionMenuEntry)) - assert.False(t, nodeFirst.IsMenuCurrent("spm", secondSectionMenuEntry)) - assert.False(t, nodeFirst.IsMenuCurrent("spm", fishySectionMenuEntry)) - assert.True(t, nodeFishy.IsMenuCurrent("spm", fishySectionMenuEntry)) - assert.Equal(t, "Fish and Chips", fishySectionMenuEntry.Name) + require.True(t, nodeFirst.IsMenuCurrent("spm", firstSectionMenuEntry)) + require.False(t, nodeFirst.IsMenuCurrent("spm", secondSectionMenuEntry)) + require.False(t, nodeFirst.IsMenuCurrent("spm", fishySectionMenuEntry)) + require.True(t, nodeFishy.IsMenuCurrent("spm", fishySectionMenuEntry)) + require.Equal(t, "Fish and Chips", fishySectionMenuEntry.Name) for _, p := range firstSectionPages { - assert.True(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry)) - assert.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry)) + require.True(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry)) + require.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry)) } for _, p := range secondSectionPages { - assert.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry)) - assert.True(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry)) + require.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry)) + require.True(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry)) } for _, p := range fishySectionPages { - assert.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry)) - assert.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry)) - assert.True(t, p.Page.HasMenuCurrent("spm", fishySectionMenuEntry)) + require.False(t, p.Page.HasMenuCurrent("spm", firstSectionMenuEntry)) + require.False(t, p.Page.HasMenuCurrent("spm", secondSectionMenuEntry)) + require.True(t, p.Page.HasMenuCurrent("spm", fishySectionMenuEntry)) } } diff --git a/hugolib/node.go b/hugolib/node.go deleted file mode 100644 index 21bdbba5b..000000000 --- a/hugolib/node.go +++ /dev/null @@ -1,334 +0,0 @@ -// Copyright 2015 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 - -import ( - "fmt" - "html/template" - "path" - "path/filepath" - "strings" - "sync" - "time" - - jww "github.com/spf13/jwalterweatherman" - - "github.com/spf13/hugo/helpers" -) - -// TODO(bep) np clean up node vs page - -type Node struct { - RSSLink template.HTML - Site *SiteInfo `json:"-"` - // layout string - Data map[string]interface{} - Title string - Description string - Keywords []string - Params map[string]interface{} - Date time.Time - Lastmod time.Time - Sitemap Sitemap - URLPath - paginator *Pager - paginatorInit sync.Once - scratch *Scratch - - language *helpers.Language - languageInit sync.Once - lang string -} - -func (n *Node) Now() time.Time { - return time.Now() -} - -func (n *Node) HasMenuCurrent(menuID string, inme *MenuEntry) bool { - if inme.HasChildren() { - me := MenuEntry{Name: n.Title, URL: n.URL()} - - for _, child := range inme.Children { - if me.IsSameResource(child) { - return true - } - if n.HasMenuCurrent(menuID, child) { - return true - } - } - } - - return false -} - -func (n *Node) IsMenuCurrent(menuID string, inme *MenuEntry) bool { - - me := MenuEntry{Name: n.Title, URL: n.Site.createNodeMenuEntryURL(n.URL())} - - 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 - if menu, ok := (*n.Site.Menus)[menuID]; ok { - for _, menuEntry := range *menu { - if menuEntry.IsSameResource(inme) { - return true - } - - descendantFound := n.isSameAsDescendantMenu(inme, menuEntry) - if descendantFound { - return descendantFound - } - - } - } - - return false -} - -// Param is a convenience method to do lookups in Site's Params map. -// -// This method is also implemented on Page and SiteInfo. -func (n *Node) Param(key interface{}) (interface{}, error) { - return n.Site.Param(key) -} - -func (n *Node) Hugo() *HugoInfo { - return hugoInfo -} - -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 - } - } - } - return false -} - -func (n *Node) RSSlink() template.HTML { - return n.RSSLink -} - -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) -} - -type URLPath struct { - URL string - Permalink string - Slug string - Section string -} - -func (n *Node) URL() string { - return n.addLangPathPrefix(n.URLPath.URL) -} - -func (n *Node) Permalink() string { - return n.Site.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 -} - -func (n *Node) Language() *helpers.Language { - n.initLanguage() - return n.language -} - -func (n *Node) Lang() string { - // 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. - if n.Language() != nil { - return n.Language().Lang - } - return n.lang -} - -func (p *Page) isTranslation(candidate *Page) bool { - if p == candidate || p.Kind != candidate.Kind { - return false - } - - if p.lang != candidate.lang || p.language != p.language { - return false - } - - if p.Kind == KindPage || p.Kind == kindUnknown { - panic("Node type not currently supported for this op") - } - - // At this point, we know that this is a traditional Node (hoe page, section, taxonomy) - // It represents the same node, but different language, if the sections is the same. - if len(p.sections) != len(candidate.sections) { - return false - } - - for i := 0; i < len(p.sections); i++ { - if p.sections[i] != candidate.sections[i] { - return false - } - } - - return true - -} - -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 -} - -func (n *Node) initLanguage() { - n.languageInit.Do(func() { - if n.language != nil { - return - } - 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 { - // It can be a file named stefano.chiodino.md. - jww.WARN.Printf("Page language (if it is that) not found in multilang setup: %s.", pageLang) - language = ml.DefaultLang - } - - n.language = language - }) -} - -func (n *Node) LanguagePrefix() string { - return n.Site.LanguagePrefix -} - -func (n *Node) addLangPathPrefix(outfile string) string { - return n.addLangPathPrefixIfFlagSet(outfile, n.shouldAddLanguagePrefix()) -} - -func (n *Node) addLangPathPrefixIfFlagSet(outfile string, should bool) string { - if helpers.IsAbsURL(outfile) { - return outfile - } - - if !should { - return outfile - } - - hadSlashSuffix := strings.HasSuffix(outfile, "/") - - outfile = "/" + path.Join(n.Lang(), outfile) - if hadSlashSuffix { - outfile += "/" - } - return outfile -} - -func (n *Node) addLangFilepathPrefix(outfile string) string { - if outfile == "" { - outfile = helpers.FilePathSeparator - } - if !n.shouldAddLanguagePrefix() { - return outfile - } - return helpers.FilePathSeparator + filepath.Join(n.Lang(), outfile) -} - -func sectionsFromFilename(filename string) []string { - dir, _ := filepath.Split(filename) - dir = strings.TrimSuffix(dir, helpers.FilePathSeparator) - sections := strings.Split(dir, helpers.FilePathSeparator) - return sections -} - -// TODO(bep) np node identificator -func kindFromFilename(filename string) string { - if !strings.Contains(filename, "_index") { - return KindPage - } - |