shithub: hugo

Download patch

ref: 480e01eb153b6c6c4c11a7569d9802274772a568
parent: b4bcc591e45e9526b7eb9fe77f355da5ead8e627
author: spf13 <[email protected]>
date: Wed Aug 14 04:57:14 EDT 2013

Further work on path/section stuff. Tests passing now.

--- a/hugolib/node.go
+++ b/hugolib/node.go
@@ -39,10 +39,5 @@
 }
 
 func (n *Node) GetSection() string {
-	s := ""
-	if n.Section != "" {
-		s = n.Section
-	}
-
-	return s
+	return n.Section
 }
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -94,62 +94,23 @@
 }
 
 func (page *Page) Initalize() error {
-	err := page.setUrlPath()
+	err := page.buildPageFromFile()
 	if err != nil {
 		return err
 	}
-	err = page.buildPageFromFile()
-	if err != nil {
-		return err
-	}
 	page.analyzePage()
 	return nil
 }
 
-func (p *Page) setUrlPath() error {
-	y := strings.TrimPrefix(p.FileName, p.Site.Config.GetAbsPath(p.Site.Config.ContentDir))
-	x := strings.Split(y, string(os.PathSeparator))
-
-	if len(x) <= 1 {
-		return errors.New("Zero length page name")
-	}
-
-	p.Section = strings.Trim(x[1], "/\\")
-	p.Path = strings.Trim(strings.Join(x[:len(x)-1], string(os.PathSeparator)), "/\\")
-	return nil
-}
-
-// If Url is provided it is assumed to be the complete relative path
-// and will override everything
-// Otherwise path + slug is used if provided
-// Lastly path + filename is used if provided
-func (p *Page) setOutFile() {
-	// Always use Url if it's specified
-	if len(strings.TrimSpace(p.Url)) > 2 {
-		p.OutFile = strings.TrimSpace(p.Url)
-		return
-	}
-
-	var outfile string
-	if len(strings.TrimSpace(p.Slug)) > 0 {
-		// Use Slug if provided
-		if p.Site.Config.UglyUrls {
-			outfile = p.Slug + "." + p.Extension
-		} else {
-			outfile = p.Slug + slash + "index." + p.Extension
+func (p *Page) guessSection() {
+	if p.Section == "" {
+		x := strings.Split(p.FileName, string(os.PathSeparator))
+		if len(x) > 1 {
+			if section := x[len(x)-2]; section != "content" {
+				p.Section = section
+			}
 		}
-	} else {
-		// Fall back to filename
-		_, t := filepath.Split(p.FileName)
-		if p.Site.Config.UglyUrls {
-			outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
-		} else {
-			file, _ := fileExt(strings.TrimSpace(t))
-			outfile = file + slash + "index." + p.Extension
-		}
 	}
-
-	p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
 }
 
 func (page *Page) Type() string {
@@ -156,7 +117,7 @@
 	if page.contentType != "" {
 		return page.contentType
 	}
-	page.setUrlPath()
+	page.guessSection()
 	if x := page.GetSection(); x != "" {
 		return x
 	}
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -32,7 +32,7 @@
 `
 
 var INVALID_FRONT_MATTER_LEADING_WS = `
- 
+
  ---
 title: Leading WS
 ---
@@ -52,7 +52,7 @@
 "slug": "spf13-vim-3-0-release-and-new-website"
 }
 
-Content of the file goes Here 
+Content of the file goes Here
 `
 
 var SIMPLE_PAGE_JSON_MULTIPLE = `
--- a/hugolib/path_seperators_test.go
+++ b/hugolib/path_seperators_test.go
@@ -23,10 +23,10 @@
 }
 
 func TestNewPageWithFilePath(t *testing.T) {
-	toCheck := []struct{
-		input string
+	toCheck := []struct {
+		input   string
 		section string
-		layout string
+		layout  string
 	}{
 		{filepath.Join("sub", "foobar.html"), "sub", "sub/single.html"},
 		{filepath.Join("content", "sub", "foobar.html"), "sub", "sub/single.html"},
@@ -35,6 +35,7 @@
 
 	for _, el := range toCheck {
 		p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_YAML), el.input)
+		p.guessSection()
 		if err != nil {
 			t.Fatalf("Reading from SIMPLE_PAGE_YAML resulted in an error: %s", err)
 		}
@@ -47,5 +48,3 @@
 		}
 	}
 }
-
-
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -16,6 +16,7 @@
 import (
 	"bitbucket.org/pkg/inflect"
 	"bytes"
+	"errors"
 	"fmt"
 	"github.com/spf13/nitro"
 	"html/template"
@@ -263,8 +264,9 @@
 		page := NewPage(fileName)
 		page.Site = s.Info
 		page.Tmpl = s.Tmpl
+		_ = s.setUrlPath(page)
 		page.Initalize()
-		page.setOutFile()
+		s.setOutFile(page)
 		if s.Config.BuildDrafts || !page.Draft {
 			s.Pages = append(s.Pages, page)
 		}
@@ -283,6 +285,52 @@
 			s.Pages[i].Prev = s.Pages[i-1]
 		}
 	}
+}
+
+func (s *Site) setUrlPath(p *Page) error {
+	y := strings.TrimPrefix(p.FileName, s.Config.GetAbsPath(s.Config.ContentDir))
+	x := strings.Split(y, string(os.PathSeparator))
+
+	if len(x) <= 1 {
+		return errors.New("Zero length page name")
+	}
+
+	p.Section = strings.Trim(x[1], "/\\")
+	p.Path = strings.Trim(strings.Join(x[:len(x)-1], string(os.PathSeparator)), "/\\")
+	return nil
+}
+
+// If Url is provided it is assumed to be the complete relative path
+// and will override everything
+// Otherwise path + slug is used if provided
+// Lastly path + filename is used if provided
+func (s *Site) setOutFile(p *Page) {
+	// Always use Url if it's specified
+	if len(strings.TrimSpace(p.Url)) > 2 {
+		p.OutFile = strings.TrimSpace(p.Url)
+		return
+	}
+
+	var outfile string
+	if len(strings.TrimSpace(p.Slug)) > 0 {
+		// Use Slug if provided
+		if s.Config.UglyUrls {
+			outfile = p.Slug + "." + p.Extension
+		} else {
+			outfile = p.Slug + slash + "index." + p.Extension
+		}
+	} else {
+		// Fall back to filename
+		_, t := filepath.Split(p.FileName)
+		if s.Config.UglyUrls {
+			outfile = replaceExtension(strings.TrimSpace(t), p.Extension)
+		} else {
+			file, _ := fileExt(strings.TrimSpace(t))
+			outfile = file + slash + "index." + p.Extension
+		}
+	}
+
+	p.OutFile = p.Path + string(os.PathSeparator) + strings.TrimSpace(outfile)
 }
 
 func (s *Site) BuildSiteMeta() (err error) {